Skip to content

Commit 4e301bf

Browse files
justin808claude
andcommitted
Integrate ESLint rule and improve implementation
- Integrate no-use-client-in-server-files rule into eslint.config.ts - Fix regex to only match at file start (remove multiline flag) - Use template literal for cleaner error message - Remove console.log from test file for cleaner CI output 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a709c96 commit 4e301bf

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ module.exports = {
2323
url: 'https://github.com/shakacode/react_on_rails/pull/1896',
2424
},
2525
messages: {
26-
useClientInServerFile:
27-
"Files with '.server.tsx' extension should not have 'use client' directive. " +
28-
'Server files are for React Server Components and should not use client-only APIs. ' +
29-
'If this component needs client-side features, rename it to .client.tsx or .tsx instead.',
26+
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.`,
3027
},
3128
schema: [],
3229
fixable: 'code',
@@ -45,9 +42,10 @@ module.exports = {
4542
const sourceCode = context.sourceCode || context.getSourceCode();
4643
const text = sourceCode.getText();
4744

48-
// Check for 'use client' directive at the start of the file
45+
// Check for 'use client' directive at the start of the file (not multiline)
4946
// Handle both single and double quotes, with or without semicolon
50-
const useClientPattern = /^\s*['"]use client['"];?\s*$/m;
47+
// Only matches at the very beginning of the file (not anywhere on its own line)
48+
const useClientPattern = /^\s*['"]use client['"];?\s*\n?/;
5149
const match = text.match(useClientPattern);
5250

5351
if (match) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,3 @@ import React from 'react';
157157
},
158158
],
159159
});
160-
161-
console.log('All tests passed!');

eslint.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import tsEslint from 'typescript-eslint';
88
import { includeIgnoreFile } from '@eslint/compat';
99
import js from '@eslint/js';
1010
import { FlatCompat } from '@eslint/eslintrc';
11+
import noUseClientInServerFiles from './eslint-rules/no-use-client-in-server-files.cjs';
1112

1213
const compat = new FlatCompat({
1314
baseDirectory: __dirname,
@@ -165,6 +166,19 @@ const config = tsEslint.config([
165166
'import/named': 'off',
166167
},
167168
},
169+
{
170+
files: ['**/*.server.ts', '**/*.server.tsx'],
171+
plugins: {
172+
'react-on-rails': {
173+
rules: {
174+
'no-use-client-in-server-files': noUseClientInServerFiles,
175+
},
176+
},
177+
},
178+
rules: {
179+
'react-on-rails/no-use-client-in-server-files': 'error',
180+
},
181+
},
168182
{
169183
files: ['lib/generators/react_on_rails/templates/**/*'],
170184
rules: {

0 commit comments

Comments
 (0)