-
Notifications
You must be signed in to change notification settings - Fork 0
feat: adds nextjs lint #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import traverse from '@babel/traverse'; | ||
| import type { File } from '@babel/types'; | ||
| import type { LintResult } from '../types'; | ||
|
|
||
| const RULE_NAME = 'no-server-import-in-client'; | ||
|
|
||
| const SERVER_ONLY_MODULES = ['server-only', 'next/headers']; | ||
|
|
||
| function isServerOnlyModule(source: string): boolean { | ||
| return SERVER_ONLY_MODULES.some((mod) => source === mod); | ||
| } | ||
|
|
||
| export function noServerImportInClient(ast: File, _code: string): LintResult[] { | ||
| const hasUseClient = ast.program.directives.some((d) => d.value.value === 'use client'); | ||
|
|
||
| if (!hasUseClient) { | ||
| return []; | ||
| } | ||
|
|
||
| const results: LintResult[] = []; | ||
|
|
||
| traverse(ast, { | ||
| ImportDeclaration(path) { | ||
| // Skip type-only imports (erased at compile time) | ||
| if (path.node.importKind === 'type') return; | ||
|
|
||
| const source = path.node.source.value; | ||
| const { loc } = path.node; | ||
|
|
||
| if (isServerOnlyModule(source)) { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `'${source}' is a server-only module and cannot be imported in a "use client" file.`, | ||
| line: loc?.start.line ?? 0, | ||
| column: loc?.start.column ?? 0, | ||
| severity: 'error', | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| ExportNamedDeclaration(path) { | ||
| const { source, loc } = path.node; | ||
| if (!source) return; | ||
|
|
||
| if (path.node.exportKind === 'type') return; | ||
|
|
||
| if (isServerOnlyModule(source.value)) { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `'${source.value}' is a server-only module and cannot be re-exported from a "use client" file.`, | ||
| line: loc?.start.line ?? 0, | ||
| column: loc?.start.column ?? 0, | ||
| severity: 'error', | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| ExportAllDeclaration(path) { | ||
| const source = path.node.source.value; | ||
| const { loc } = path.node; | ||
|
|
||
| if (path.node.exportKind === 'type') return; | ||
|
|
||
| if (isServerOnlyModule(source)) { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `'${source}' is a server-only module and cannot be re-exported from a "use client" file.`, | ||
| line: loc?.start.line ?? 0, | ||
| column: loc?.start.column ?? 0, | ||
| severity: 'error', | ||
| }); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| return results; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import traverse from '@babel/traverse'; | ||
| import type { File } from '@babel/types'; | ||
| import type { LintResult } from '../types'; | ||
|
|
||
| const RULE_NAME = 'require-use-client'; | ||
|
|
||
| export function requireUseClient(ast: File, _code: string): LintResult[] { | ||
| const hasDirective = ast.program.directives.some( | ||
| (d) => d.value.value === 'use client' || d.value.value === 'use server', | ||
| ); | ||
|
|
||
| if (hasDirective) { | ||
| return []; | ||
| } | ||
|
|
||
| const results: LintResult[] = []; | ||
|
|
||
| traverse(ast, { | ||
| CallExpression(path) { | ||
| const { callee, loc } = path.node; | ||
|
|
||
| // React hooks: useState(), useEffect(), useRef(), etc. | ||
| if (callee.type === 'Identifier' && /^use[A-Z]/.test(callee.name)) { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `'${callee.name}()' is a client-only hook. Add "use client" at the top of this file.`, | ||
| line: loc?.start.line ?? 0, | ||
| column: loc?.start.column ?? 0, | ||
| severity: 'error', | ||
| }); | ||
| } | ||
|
|
||
| // createContext() | ||
| if (callee.type === 'Identifier' && callee.name === 'createContext') { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `'createContext()' is client-only. Add "use client" at the top of this file.`, | ||
| line: loc?.start.line ?? 0, | ||
| column: loc?.start.column ?? 0, | ||
| severity: 'error', | ||
| }); | ||
| } | ||
|
|
||
| // React.createContext() | ||
| if ( | ||
| callee.type === 'MemberExpression' && | ||
| callee.object.type === 'Identifier' && | ||
| callee.object.name === 'React' && | ||
| callee.property.type === 'Identifier' && | ||
| callee.property.name === 'createContext' | ||
| ) { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `'React.createContext()' is client-only. Add "use client" at the top of this file.`, | ||
| line: loc?.start.line ?? 0, | ||
| column: loc?.start.column ?? 0, | ||
| severity: 'error', | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| JSXAttribute(path) { | ||
| const { name, loc } = path.node; | ||
|
|
||
| // Event handler props: onClick, onChange, onSubmit, etc. | ||
| if (name.type === 'JSXIdentifier' && /^on[A-Z]/.test(name.name)) { | ||
| results.push({ | ||
| rule: RULE_NAME, | ||
| message: `'${name.name}' is a client-only event handler. Add "use client" at the top of this file.`, | ||
| line: loc?.start.line ?? 0, | ||
| column: loc?.start.column ?? 0, | ||
| severity: 'error', | ||
| }); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| return results; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { lintJsxCode } from '../src'; | ||
|
|
||
| const config = { rules: ['no-server-import-in-client'] }; | ||
|
|
||
| describe('no-server-import-in-client rule', () => { | ||
| it('should flag server-only import in "use client" file', () => { | ||
| const code = ` | ||
| "use client"; | ||
| import 'server-only'; | ||
| export function Component() { | ||
| return <div />; | ||
| } | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| expect(results[0].rule).toBe('no-server-import-in-client'); | ||
| expect(results[0].message).toContain('server-only'); | ||
| expect(results[0].severity).toBe('error'); | ||
| }); | ||
|
|
||
| it('should flag next/headers import in "use client" file', () => { | ||
| const code = ` | ||
| "use client"; | ||
| import { headers } from 'next/headers'; | ||
| export function Component() { | ||
| return <div />; | ||
| } | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| expect(results[0].message).toContain('next/headers'); | ||
| }); | ||
|
|
||
| it('should flag multiple server-only imports', () => { | ||
| const code = ` | ||
| "use client"; | ||
| import 'server-only'; | ||
danielchen0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { headers } from 'next/headers'; | ||
| export function Component() { | ||
| return <div />; | ||
| } | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(2); | ||
danielchen0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it('should flag re-exports from server-only modules', () => { | ||
| const code = ` | ||
| "use client"; | ||
danielchen0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export { headers } from 'next/headers'; | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(1); | ||
| expect(results[0].message).toContain('re-exported'); | ||
| }); | ||
|
|
||
| it('should allow server-only imports without "use client" directive', () => { | ||
| const code = ` | ||
| import 'server-only'; | ||
| import { headers } from 'next/headers'; | ||
| export function ServerComponent() { | ||
| return <div />; | ||
| } | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should allow server-only imports in "use server" files', () => { | ||
| const code = ` | ||
| "use server"; | ||
| import { headers } from 'next/headers'; | ||
| export async function getHeaders() { | ||
| return headers(); | ||
| } | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should allow type-only imports from server modules', () => { | ||
| const code = ` | ||
| "use client"; | ||
| import type { HeadersFunction } from 'next/headers'; | ||
| export function Component() { | ||
| return <div />; | ||
| } | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should allow normal imports in "use client" files', () => { | ||
| const code = ` | ||
| "use client"; | ||
| import { useState } from 'react'; | ||
| import Link from 'next/link'; | ||
| export function Component() { | ||
| return <Link href="/">Home</Link>; | ||
| } | ||
| `; | ||
| const results = lintJsxCode(code, config); | ||
| expect(results).toHaveLength(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.