Skip to content

Commit 9a3ff07

Browse files
justin808claude
andcommitted
Fix ESLint rule edge cases and improve code quality
- Use backreference in regex to ensure matching quotes (prevents 'use client" false positives) - Update PR reference URL from #1896 to #1919 - Simplify newline handling by letting regex capture it consistently 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 4e301bf commit 9a3ff07

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

eslint-rules/no-use-client-in-server-files.cjs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
description: "Prevent 'use client' directive in .server.tsx files",
2121
category: 'Best Practices',
2222
recommended: true,
23-
url: 'https://github.com/shakacode/react_on_rails/pull/1896',
23+
url: 'https://github.com/shakacode/react_on_rails/pull/1919',
2424
},
2525
messages: {
2626
useClientInServerFile: `Files with '.server.tsx' extension should not have 'use client' directive. Server files are for React Server Components and should not use client-only APIs. If this component needs client-side features, rename it to .client.tsx or .tsx instead.`,
@@ -42,10 +42,10 @@ module.exports = {
4242
const sourceCode = context.sourceCode || context.getSourceCode();
4343
const text = sourceCode.getText();
4444

45-
// Check for 'use client' directive at the start of the file (not multiline)
46-
// Handle both single and double quotes, with or without semicolon
47-
// Only matches at the very beginning of the file (not anywhere on its own line)
48-
const useClientPattern = /^\s*['"]use client['"];?\s*\n?/;
45+
// Check for 'use client' directive at the start of the file
46+
// Uses backreference (\1) to ensure matching quotes (both single or both double)
47+
// Only matches at the very beginning of the file
48+
const useClientPattern = /^\s*(['"])use client\1;?\s*\n?/;
4949
const match = text.match(useClientPattern);
5050

5151
if (match) {
@@ -56,16 +56,8 @@ module.exports = {
5656
node,
5757
messageId: 'useClientInServerFile',
5858
fix(fixer) {
59-
// Remove the 'use client' directive and any trailing newlines
60-
const start = directiveIndex;
61-
let end = directiveIndex + match[0].length;
62-
63-
// Also remove the newline after the directive if present
64-
if (text[end] === '\n') {
65-
end += 1;
66-
}
67-
68-
return fixer.removeRange([start, end]);
59+
// Remove the 'use client' directive (regex already captures trailing newline)
60+
return fixer.removeRange([directiveIndex, directiveIndex + match[0].length]);
6961
},
7062
});
7163
}

0 commit comments

Comments
 (0)