Skip to content

Commit c39ddeb

Browse files
committed
fix(scripts): match boundary directives that carry a trailing comment
A directive keeps its meaning when a note follows it on the same line, so strip a trailing '//' or block comment before matching. Shared by the 'use client' and 'use server' detectors.
1 parent 1250943 commit c39ddeb

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

scripts/check-client-boundary-imports.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ async function listFiles(dir: string): Promise<string[]> {
9090
return out
9191
}
9292

93+
/**
94+
* Drops a trailing `//` or `/* *\/` comment from an already-trimmed line. A
95+
* directive keeps its meaning when a note follows it on the same line, so the
96+
* comment has to come off before the directive is matched.
97+
*/
98+
function stripTrailingComment(line: string): string {
99+
return line.replace(/(?:\/\/.*|\/\*.*?\*\/)\s*$/, '').trim()
100+
}
101+
102+
/** A lone directive statement, e.g. `'use server'` or `"use client";`. */
103+
const DIRECTIVE_STATEMENT = /^(['"])(use [a-z-]+)\1\s*;?$/
104+
93105
/**
94106
* Returns the module's leading directive prologue string, if any. A directive
95107
* must be the first statement; comments and blank lines may precede it.
@@ -100,7 +112,7 @@ function leadingDirective(content: string): string | null {
100112
if (line === '' || line.startsWith('//') || line.startsWith('/*') || line.startsWith('*')) {
101113
continue
102114
}
103-
const match = /^(['"])(use [a-z-]+)\1;?$/.exec(line)
115+
const match = DIRECTIVE_STATEMENT.exec(stripTrailingComment(line))
104116
return match ? match[2] : null
105117
}
106118
return null
@@ -129,7 +141,8 @@ async function findUseServerDirectives(): Promise<string[]> {
129141
for (const absFile of await listFiles(dir)) {
130142
const lines = (await readFile(absFile, 'utf8')).split('\n')
131143
for (let i = 0; i < lines.length; i++) {
132-
if (/^(['"])use server\1;?$/.test(lines[i].trim())) {
144+
const match = DIRECTIVE_STATEMENT.exec(stripTrailingComment(lines[i].trim()))
145+
if (match?.[2] === 'use server') {
133146
found.push(`${path.relative(ROOT, absFile)}:${i + 1}`)
134147
}
135148
}

0 commit comments

Comments
 (0)