Repository navigation
fix(javascript): correct lexical and dynamic scope analysis - #538
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c46e5f1ac0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| : ["reads-property", "writes-property"], | ||
| family, | ||
| relation_kinds: [...relationKinds], | ||
| reason: frontier.kind, |
There was a problem hiding this comment.
Accept dynamic-scope in the graph unknown schema
For any analyzed file containing with or direct eval, this passes "dynamic-scope" to createJavaScriptSemanticGraphUnknown, but JAVASCRIPT_SEMANTIC_UNKNOWN_REASONS in javascriptSemanticGraphSchemas.ts does not include that value. Its Zod parse therefore throws during buildJavaScriptSemanticGraph instead of returning the newly added unknown; add the reason to the schema or map it to an existing supported reason.
Useful? React with 👍 / 👎.
| const reason = t.isWithStatement(node) | ||
| ? "`with` introduces a runtime binding environment; names inside are not statically resolvable." |
There was a problem hiding this comment.
Do not resolve names through a with environment
When a with body references an outer binding, such as let x; with (obj) { use(x); }, recording a frontier alone leaves the scope state unchanged, so collectSemanticReferences still marks x as resolved and the graph emits a resolved read even though obj.x may intercept it at runtime. The with body must block or downgrade lexical resolution rather than merely adding an unrelated unknown frontier.
AGENTS.md reference: AGENTS.md:L9-L9
Useful? React with 👍 / 👎.
| : t.isCallExpression(node) && | ||
| t.isIdentifier(node.callee) && | ||
| node.callee.name === "eval" |
There was a problem hiding this comment.
Gate eval frontiers on an unshadowed global
For valid sloppy-mode code such as function run(eval) { eval("text"); }, the callee is a parameter and cannot introduce bindings through JavaScript's direct-eval semantics, yet this name-only check reports a dynamic-scope unknown. Resolve the callee binding and honor blocked resolution, as the new CommonJS and existing require checks do, so ordinary functions named eval are not misclassified.
AGENTS.md reference: AGENTS.md:L9-L9
Useful? React with 👍 / 👎.
| // A switch body is one lexical scope shared by all its cases, so `let` in | ||
| // two cases must resolve to one binding rather than collide or leak. | ||
| if (t.isSwitchStatement(node)) return "block"; |
There was a problem hiding this comment.
Keep the switch discriminant outside the case scope
Creating the block scope on the whole SwitchStatement also assigns the discriminant AST to that scope. In let mode = 1; switch (mode) { case 1: let mode = 2; }, resolution therefore binds the discriminant to the case-local mode, although JavaScript evaluates the discriminant before establishing the case block's lexical environment and uses the outer binding. Scope only the case-block descendants while leaving the discriminant in the enclosing scope.
Useful? React with 👍 / 👎.
c46e5f1 to
ec91661
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Static JavaScript analysis could invent CommonJS exports from shadowed names, resolve switch discriminants against case-local declarations, and fail graph construction when encountering
withor directeval.This change checks lexical and dynamic environments before recognizing CommonJS globals, isolates static-block declarations, shares one lexical scope across switch cases while evaluating the discriminant in its outer scope, and exposes dynamic-scope unknowns through the graph schema.
withblocks outer-name resolution while retaining lexical locals; shadowed, optional, and indirectevalcalls do not receive a direct-eval frontier. Computed assignments on unrelated or shadowed objects no longer invent wildcard exports.Rebased onto current main, preserving the existing destructuring-write and compound-assignment read/write fixes. This is static analysis; it does not restore runtime replay or execute analyzed source.
Validation:
npm run check:pr: passed; 349 test files, 1,971 tests passed and one skipped.npm run check:fast: passed.npm run docs:generate: passed; generated catalog and evidence metadata refreshed.