-
Notifications
You must be signed in to change notification settings - Fork 387
feat(nextjs): add 7 new Next.js best-practice rules from vercel-labs skill #680
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
Open
devin-ai-integration
wants to merge
9
commits into
main
Choose a base branch
from
devin/1780571023-nextjs-best-practice-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+443
−10
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7cb2ba4
feat(nextjs): add 4 new Next.js best-practice rules
devin-ai-integration[bot] d77b116
fix: replace em dashes with periods in rule recommendations/messages
devin-ai-integration[bot] 253417e
fix: narrow GA regex to exclude GTM false positives
devin-ai-integration[bot] 197c11b
feat(nextjs): add 3 more rules - no-script-in-head, no-vercel-og-impo…
devin-ai-integration[bot] 92b337b
style: apply formatter to new rule files
devin-ai-integration[bot] 9967e6e
refactor: thermo-nuclear quality pass on new rules
devin-ai-integration[bot] 78f0f66
refactor: descriptive variable names per AGENTS.md
devin-ai-integration[bot] aeec7ff
fix: OG_IMAGE_FILE_PATTERN match numeric suffixes (opengraph-image2.tsx)
devin-ai-integration[bot] 161a6c5
fix: detect `export { x as default }` in route handler rule
devin-ai-integration[bot] 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
31 changes: 31 additions & 0 deletions
31
...t-plugin-react-doctor/src/plugin/rules/nextjs/nextjs-error-boundary-missing-use-client.ts
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,31 @@ | ||
| import { APP_DIRECTORY_PATTERN, ERROR_BOUNDARY_FILE_PATTERN } from "../../constants/nextjs.js"; | ||
| import { defineRule } from "../../utils/define-rule.js"; | ||
| import { hasDirective } from "../../utils/has-directive.js"; | ||
| import { normalizeFilename } from "../../utils/normalize-filename.js"; | ||
| import type { Rule } from "../../utils/rule.js"; | ||
| import type { RuleContext } from "../../utils/rule-context.js"; | ||
| import type { EsTreeNodeOfType } from "../../utils/es-tree-node-of-type.js"; | ||
|
|
||
| export const nextjsErrorBoundaryMissingUseClient = defineRule<Rule>({ | ||
| id: "nextjs-error-boundary-missing-use-client", | ||
| title: "Error boundary missing 'use client'", | ||
| tags: ["test-noise"], | ||
| requires: ["nextjs"], | ||
| severity: "error", | ||
| recommendation: | ||
| "Add `'use client'` at the top of this file. Error boundaries must be Client Components to catch and render fallback UI", | ||
| create: (context: RuleContext) => ({ | ||
| Program(programNode: EsTreeNodeOfType<"Program">) { | ||
| const filename = normalizeFilename(context.filename ?? ""); | ||
| if (!APP_DIRECTORY_PATTERN.test(filename)) return; | ||
| if (!ERROR_BOUNDARY_FILE_PATTERN.test(filename)) return; | ||
| if (hasDirective(programNode, "use client")) return; | ||
|
|
||
| context.report({ | ||
| node: programNode, | ||
| message: | ||
| "This error boundary silently does nothing without 'use client'. Next.js requires error.tsx to be a Client Component.", | ||
| }); | ||
| }, | ||
| }), | ||
| }); |
38 changes: 38 additions & 0 deletions
38
...lint-plugin-react-doctor/src/plugin/rules/nextjs/nextjs-global-error-missing-html-body.ts
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,38 @@ | ||
| import { APP_DIRECTORY_PATTERN, GLOBAL_ERROR_FILE_PATTERN } from "../../constants/nextjs.js"; | ||
| import { defineRule } from "../../utils/define-rule.js"; | ||
| import { fileContainsJsxElements } from "../../utils/file-contains-jsx-elements.js"; | ||
| import { normalizeFilename } from "../../utils/normalize-filename.js"; | ||
| import type { Rule } from "../../utils/rule.js"; | ||
| import type { RuleContext } from "../../utils/rule-context.js"; | ||
| import type { EsTreeNodeOfType } from "../../utils/es-tree-node-of-type.js"; | ||
|
|
||
| const REQUIRED_TAGS = ["html", "body"] as const; | ||
|
|
||
| export const nextjsGlobalErrorMissingHtmlBody = defineRule<Rule>({ | ||
| id: "nextjs-global-error-missing-html-body", | ||
| title: "global-error.tsx missing <html>/<body>", | ||
| tags: ["test-noise"], | ||
| requires: ["nextjs"], | ||
| severity: "error", | ||
| recommendation: | ||
| "Wrap your error UI in `<html><body>...</body></html>`. The root layout is unmounted when global-error renders", | ||
| create: (context: RuleContext) => ({ | ||
| Program(programNode: EsTreeNodeOfType<"Program">) { | ||
| const filename = normalizeFilename(context.filename ?? ""); | ||
| if (!APP_DIRECTORY_PATTERN.test(filename)) return; | ||
| if (!GLOBAL_ERROR_FILE_PATTERN.test(filename)) return; | ||
|
|
||
| const foundTags = fileContainsJsxElements(programNode, REQUIRED_TAGS); | ||
| const missingTags = REQUIRED_TAGS.filter((tag) => !foundTags.has(tag)).map( | ||
| (tag) => `<${tag}>`, | ||
| ); | ||
|
|
||
| if (missingTags.length > 0) { | ||
| context.report({ | ||
| node: programNode, | ||
| message: `global-error.tsx is missing ${missingTags.join(" and ")}. The root layout unmounts on error, so this page renders broken HTML.`, | ||
| }); | ||
| } | ||
| }, | ||
| }), | ||
| }); |
78 changes: 78 additions & 0 deletions
78
...-plugin-react-doctor/src/plugin/rules/nextjs/nextjs-no-default-export-in-route-handler.ts
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,78 @@ | ||
| import { | ||
| APP_DIRECTORY_PATTERN, | ||
| ROUTE_HANDLER_FILE_PATTERN, | ||
| ROUTE_HANDLER_HTTP_METHODS, | ||
| } from "../../constants/nextjs.js"; | ||
| import { defineRule } from "../../utils/define-rule.js"; | ||
| import { normalizeFilename } from "../../utils/normalize-filename.js"; | ||
| import type { Rule } from "../../utils/rule.js"; | ||
| import type { RuleContext } from "../../utils/rule-context.js"; | ||
| import { isNodeOfType } from "../../utils/is-node-of-type.js"; | ||
| import type { EsTreeNodeOfType } from "../../utils/es-tree-node-of-type.js"; | ||
|
|
||
| const programHasNamedHttpMethodExport = (programNode: EsTreeNodeOfType<"Program">): boolean => { | ||
| for (const statement of programNode.body ?? []) { | ||
| if (!isNodeOfType(statement, "ExportNamedDeclaration")) continue; | ||
| const declaration = statement.declaration; | ||
| if ( | ||
| isNodeOfType(declaration, "FunctionDeclaration") && | ||
| declaration.id?.name && | ||
| ROUTE_HANDLER_HTTP_METHODS.has(declaration.id.name) | ||
| ) { | ||
| return true; | ||
| } | ||
| if (isNodeOfType(declaration, "VariableDeclaration")) { | ||
| for (const declarator of declaration.declarations ?? []) { | ||
| if ( | ||
| isNodeOfType(declarator.id, "Identifier") && | ||
| ROUTE_HANDLER_HTTP_METHODS.has(declarator.id.name) | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| for (const specifier of statement.specifiers ?? []) { | ||
| if ( | ||
| isNodeOfType(specifier, "ExportSpecifier") && | ||
| isNodeOfType(specifier.exported, "Identifier") && | ||
| ROUTE_HANDLER_HTTP_METHODS.has(specifier.exported.name) | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| export const nextjsNoDefaultExportInRouteHandler = defineRule<Rule>({ | ||
| id: "nextjs-no-default-export-in-route-handler", | ||
| title: "Default export in route handler", | ||
| tags: ["test-noise"], | ||
| requires: ["nextjs"], | ||
| severity: "error", | ||
| recommendation: | ||
| "Replace `export default` with named HTTP method exports: `export async function GET(request) { … }`", | ||
| create: (context: RuleContext) => { | ||
| let isAppRouteHandler = false; | ||
| let programNode: EsTreeNodeOfType<"Program"> | null = null; | ||
|
|
||
| return { | ||
| Program(node: EsTreeNodeOfType<"Program">) { | ||
| const filename = normalizeFilename(context.filename ?? ""); | ||
| isAppRouteHandler = | ||
| APP_DIRECTORY_PATTERN.test(filename) && ROUTE_HANDLER_FILE_PATTERN.test(filename); | ||
| programNode = node; | ||
| }, | ||
| ExportDefaultDeclaration(node: EsTreeNodeOfType<"ExportDefaultDeclaration">) { | ||
| if (!isAppRouteHandler || !programNode) return; | ||
| if (programHasNamedHttpMethodExport(programNode)) return; | ||
|
|
||
| context.report({ | ||
| node, | ||
| message: | ||
| "Default exports in route.ts are silently ignored. Next.js only recognizes named HTTP method exports (GET, POST, etc.).", | ||
| }); | ||
| }, | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }; | ||
| }, | ||
| }); | ||
49 changes: 49 additions & 0 deletions
49
packages/oxlint-plugin-react-doctor/src/plugin/rules/nextjs/nextjs-no-edge-og-runtime.ts
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,49 @@ | ||
| import { OG_IMAGE_FILE_PATTERN } from "../../constants/nextjs.js"; | ||
| import { defineRule } from "../../utils/define-rule.js"; | ||
| import { normalizeFilename } from "../../utils/normalize-filename.js"; | ||
| import { isNodeOfType } from "../../utils/is-node-of-type.js"; | ||
| import type { Rule } from "../../utils/rule.js"; | ||
| import type { RuleContext } from "../../utils/rule-context.js"; | ||
| import type { EsTreeNodeOfType } from "../../utils/es-tree-node-of-type.js"; | ||
|
|
||
| export const nextjsNoEdgeOgRuntime = defineRule<Rule>({ | ||
| id: "nextjs-no-edge-og-runtime", | ||
| title: "Edge runtime in OG image route", | ||
| tags: ["test-noise"], | ||
| requires: ["nextjs"], | ||
| severity: "warn", | ||
| recommendation: | ||
| "Remove `export const runtime = 'edge'` from OG image files. The default Node.js runtime supports more fonts and APIs", | ||
| create: (context: RuleContext) => { | ||
| let isOgImageFile = false; | ||
|
|
||
| return { | ||
| Program() { | ||
| const filename = normalizeFilename(context.filename ?? ""); | ||
| isOgImageFile = OG_IMAGE_FILE_PATTERN.test(filename); | ||
| }, | ||
| ExportNamedDeclaration(node: EsTreeNodeOfType<"ExportNamedDeclaration">) { | ||
| if (!isOgImageFile) return; | ||
|
|
||
| const declaration = node.declaration; | ||
| if (!isNodeOfType(declaration, "VariableDeclaration")) return; | ||
|
|
||
| for (const declarator of declaration.declarations ?? []) { | ||
| if (!isNodeOfType(declarator, "VariableDeclarator")) continue; | ||
| if (!isNodeOfType(declarator.id, "Identifier")) continue; | ||
| if (declarator.id.name !== "runtime") continue; | ||
|
|
||
| const initValue = isNodeOfType(declarator.init, "Literal") ? declarator.init.value : null; | ||
|
|
||
| if (initValue === "edge") { | ||
| context.report({ | ||
| node, | ||
| message: | ||
| "Edge runtime limits OG image generation. Node.js runtime supports more fonts, filesystem access, and larger response sizes.", | ||
| }); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); |
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.