-
Notifications
You must be signed in to change notification settings - Fork 421
feat(cfg): control-flow graph engine, formal-verification stack, and CFG-backed rules #892
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
aidenybai
wants to merge
13
commits into
main
Choose a base branch
from
cfg-beefup-and-rule-fixes
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.
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0b7a7dd
feat(oxlint-plugin): beef up the CFG and fix the false positives it e…
aidenybai 9d55d87
feat(oxlint-plugin): lower expression-level control flow into the CFG…
aidenybai 7e40c19
test(nextjs-no-redirect-in-try-catch): lock in nested-finally swallow…
aidenybai 20064a3
fix(effect-cleanup-not-on-every-path): drop em dashes from recommenda…
aidenybai 125cb98
refactor(cfg): extract control-flow graph into @react-doctor/cfg + po…
aidenybai 1fe5e7b
fix(no-unreleased-resource): shorten title under the 60-char cap
aidenybai fe234b4
fix(no-dead-assignment): eliminate try/catch and loop-carried CFG fal…
aidenybai 346a9f0
fix(language-server): raise hook timeout for windows stdio integratio…
aidenybai 30cc452
perf(cfg): share the analysis layer across rules; de-quadratic the gr…
rayhanadev 7f3c8f7
fix(no-stale-closure-capture): flag reassignments on early-return bra…
rayhanadev 03da338
test(cfg): add adversarial control-flow corpus for the CFG-backed rules
rayhanadev d1b6230
fix(no-use-before-define): flag self-referential initializer TDZ reads
rayhanadev df5a163
fix(cfg): correct unconditional-set in infinite loops and self-refere…
rayhanadev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "oxlint-plugin-react-doctor": patch | ||
| --- | ||
|
|
||
| Beef up the control-flow graph and fix the false positives it exposed. | ||
|
|
||
| The internal CFG now exposes reachability, dominance, post-dominance, loop-membership, and unreachable-code primitives, models loop back-edges and infinite loops, and gives `try`/`catch`/`finally` proper finalize/join semantics (a `finally` body stays reachable even when the `try` returns; code after the try is unreachable when no path completes normally). Several rules adopt it: | ||
|
|
||
| - `nextjs-no-redirect-in-try-catch` no longer mis-flags `redirect()` / `notFound()` in a `catch` block, in a `finally` block, or in a `try` that has only a `finally` (no `catch`) — none of those swallow the navigation control-flow error. | ||
| - `no-mutating-reducer-state` no longer reports a loop that mutates and then `return`s a fresh object (`for (…) { state.items.push(x); return { ...state } }`) when a trailing `return state` only runs on the no-match path. | ||
| - `js-hoist-regexp`, `js-index-maps`, and `js-set-map-lookups` no longer mis-flag work inside a callback that merely escapes a loop (the loop-aware check now uses real CFG loop membership instead of lexical nesting depth). |
8 changes: 0 additions & 8 deletions
8
packages/oxlint-plugin-react-doctor/src/plugin/constants/js.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
34 changes: 34 additions & 0 deletions
34
packages/oxlint-plugin-react-doctor/src/plugin/rules/js-performance/js-hoist-regexp.test.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,34 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import { runRule } from "../../../test-utils/run-rule.js"; | ||
| import { jsHoistRegexp } from "./js-hoist-regexp.js"; | ||
|
|
||
| describe("js-hoist-regexp", () => { | ||
| it("flags `new RegExp(...)` built inside a loop body", () => { | ||
| const result = runRule( | ||
| jsHoistRegexp, | ||
| `function fn(rows) { for (const row of rows) { const re = new RegExp(row.pattern); test(re); } }`, | ||
| ); | ||
| expect(result.parseErrors).toEqual([]); | ||
| expect(result.diagnostics).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("does not flag a `new RegExp(...)` outside any loop", () => { | ||
| const result = runRule(jsHoistRegexp, `const re = new RegExp(pattern);`); | ||
| expect(result.diagnostics).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("does not flag a `new RegExp(...)` inside a callback that merely escapes the loop", () => { | ||
| // Regression: the regexp is built per-click, not per-iteration — | ||
| // the click handler is a separate function with its own acyclic | ||
| // CFG, so cycle-based loop membership must report nothing. | ||
| const result = runRule( | ||
| jsHoistRegexp, | ||
| `function fn(rows) { | ||
| for (const row of rows) { | ||
| row.element.onclick = () => { const re = new RegExp(row.pattern); test(re); }; | ||
| } | ||
| }`, | ||
| ); | ||
| expect(result.diagnostics).toHaveLength(0); | ||
| }); | ||
| }); |
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
75 changes: 75 additions & 0 deletions
75
...xlint-plugin-react-doctor/src/plugin/rules/nextjs/nextjs-no-redirect-in-try-catch.test.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,75 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import { runRule } from "../../../test-utils/run-rule.js"; | ||
| import { nextjsNoRedirectInTryCatch } from "./nextjs-no-redirect-in-try-catch.js"; | ||
|
|
||
| const CASES: ReadonlyArray<{ name: string; code: string; expectedDiagnosticCount: number }> = [ | ||
| { | ||
| name: "flags redirect() in a try body that has a catch handler", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = () => { try { redirect("/x"); } catch (e) {} };`, | ||
| expectedDiagnosticCount: 1, | ||
| }, | ||
| { | ||
| name: "ignores redirect() in the catch handler", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = () => { try {} catch { redirect("/x"); } };`, | ||
| expectedDiagnosticCount: 0, | ||
| }, | ||
| { | ||
| name: "ignores redirect() in the finally block", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = () => { try {} finally { redirect("/x"); } };`, | ||
| expectedDiagnosticCount: 0, | ||
| }, | ||
| { | ||
| name: "ignores redirect() in a try with only a finally (no catch)", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = () => { try { redirect("/x"); } finally {} };`, | ||
| expectedDiagnosticCount: 0, | ||
| }, | ||
| { | ||
| name: "flags at the inner try when the nested try is the one that catches", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = () => { try { try { redirect("/x"); } catch {} } catch {} };`, | ||
| expectedDiagnosticCount: 1, | ||
| }, | ||
| { | ||
| name: "flags an inner-catch redirect that the outer try body swallows", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = () => { try { try {} catch { redirect("/x"); } } catch {} };`, | ||
| expectedDiagnosticCount: 1, | ||
| }, | ||
| { | ||
| name: "ignores redirect() outside any try", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = () => { redirect("/x"); };`, | ||
| expectedDiagnosticCount: 0, | ||
| }, | ||
| { | ||
| name: "still flags redirect() nested in an if inside the try body", | ||
| code: `import { redirect } from "next/navigation"; | ||
| export const action = (x) => { try { if (x) redirect("/x"); } catch (e) {} };`, | ||
| expectedDiagnosticCount: 1, | ||
| }, | ||
| { | ||
| name: "flags notFound() in a try body that has a catch handler", | ||
| code: `import { notFound } from "next/navigation"; | ||
| export const action = () => { try { notFound(); } catch (e) {} };`, | ||
| expectedDiagnosticCount: 1, | ||
| }, | ||
| { | ||
| name: "flags permanentRedirect() in a try body that has a catch handler", | ||
| code: `import { permanentRedirect } from "next/navigation"; | ||
| export const action = () => { try { permanentRedirect("/x"); } catch (e) {} };`, | ||
| expectedDiagnosticCount: 1, | ||
| }, | ||
| ]; | ||
|
|
||
| describe("nextjs-no-redirect-in-try-catch", () => { | ||
| for (const testCase of CASES) { | ||
| it(testCase.name, () => { | ||
| const result = runRule(nextjsNoRedirectInTryCatch, testCase.code); | ||
| expect(result.diagnostics).toHaveLength(testCase.expectedDiagnosticCount); | ||
| }); | ||
| } | ||
| }); |
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
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.