Repository navigation
feat(js): parsec-js couldn't tell which declaration a name refers to - #193
Open
jamesconwaydev wants to merge 2 commits into
Open
jamesconwaydev wants to merge 2 commits into
jamesconwaydev wants to merge 2 commits into
Conversation
The parser gives a tree, but a tool that renames a variable, finds a global or bundles modules needs to know what each name means, and test262's 190 redeclaration tests still parsed, since only the scopes can tell that `let a; var a;` declares a name twice. parsec_js::scope adds Scopes::analyze, one pass over the tree with its own stack: - scopes for the global or module level, functions (with a separate body scope when a parameter has a default or computed key), blocks, switches, for heads with let or const, catch clauses, with bodies, classes, class fields' initializers and static blocks; - bindings with var and function declarations hoisted, Annex B's function-in-block var for sloppy code where a var of the name would be no error and it isn't a parameter, B.3.4's var in a catch block, a function expression's and a class's own names, imports, and an implicit arguments in each non-arrow function that uses it; - every IdentifierReference resolved or listed in free_names, with its access (read, write or both) and a TDZ flag when it comes before its let, const or class in the same function; - dynamic on the scopes a direct eval reaches and on with bodies; - lookups by node: scope_of, reference, declaration, and lookup by name; - diagnostics for duplicate-declaration and lexical-var-conflict, with the first declaration labelled. test262 now runs every file through the analysis too: the 190 redeclaration negatives fail through it, and none of the 48,408 files that should parse gets a diagnostic, so the scope fragments leave the early_error list. The parse fuzz target checks the analysis's references and bindings, and parsec-html-bench js-scope times it on the bench corpus and lists every free name.
This was referenced Nov 21, 2026
Brings the branch onto its new base after the pull request below it merged.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #158.
Builds on #192, which this is based on; once the stack below it merges, this is rebased onto its base.
Problem. Since #187 parsec-js gives a tree, but nothing says what a name in it means. A tool that renames a variable, lists a script's globals or bundles modules has to know that the
xin{ let x; x }is the block's and the one after the block isn't, thatf()beforefunction f() {}calls it, and thatargumentsin an arrow belongs to the function around it. The same gap left 190 of test262's parse negatives accepted:let a; var a;andtry {} catch (e) { let e; }parse fine, and only the scopes show the name declared twice.Change. A new module,
parsec_js::scope. Nothing runs.Scopes::analyze(&ast, src) -> Scopes, one walk over the tree on its own stack, so a 200,000-deep member chain analyzes on a 256 KB thread.ScopeKind):Global,Module,Function,FunctionBody(a function's body when a parameter has a default or computed key, sofunction f(a = x) { var x }reads the outerx, as the spec's separate environment has it),Block(blocks,switchcases,forheads withletorconst, and the block B.3.3 puts aroundif (a) function f() {}),Catch,With,Class,ClassField(each field initializer's implicit function) andStaticBlock. Each has itsnode,parent,strict, anddynamic.BindingKind):varand function declarations hoisted to their function;let,const,classin their block; parameters; catch parameters; imports; a function expression's and a class's own name inside it; andarguments, made in a non-arrow function only when something refers to it.varbinding in its function (B.3.2), unless avarof its name would be an early error there or it's a parameter's name; references outside the block resolve to that. Avarin acatchblock may share a simple parameter's name (B.3.4),for (var e of …)included, as test262 now has it.binding(Nonewhen free),access(Read,Write,ReadWrite), andtdzwhen it comes before itslet,constorclassin the same function (let x = x;,class C extends C {},for (let x of x)). Property names, labels,new.target, private names and re-exported names aren't references.dynamic: a directeval(…)(or(eval)(…), but noteval?.()or(0, eval)()) marks its scope and every scope around it; awithbody is a dynamic scope of its own.scope_of(node),reference(node),declaration(node),lookup(scope, name),free_names()(each once, in order of first use), andscopes(),bindings(),references().diagnostics():duplicate-declarationfor a lexical name declared twice in a scope, or as a parameter or catch parameter too, andlexical-var-conflictwhen avarand a lexical name meet, with a label on the first declaration. Sloppy blocks may declare a plain function twice; generators and async functions may not, and nor may strict code or a module's top level.tests/test262.rsruns every file through the analysis as well as the parser. The redeclaration path fragments leave itsearly_errorlist; the regular expression ones and the two undeclared-export ones stay until the next PRs in this stack.parsefuzz target now also checks each reference is an identifier whose binding has its name, or a free name, and each diagnostic's span.parsec-html-bench js-scopetimes the analysis on the bench corpus and prints every source's free names.Tests.
tests/scope.rs, 22 tests. A helper writes each reference asname#n, the nth declaration of that name it resolved to, so a test reads asscript("let x; { let x; x } x", &["x#2", "x#1"]).references_resolve_to_the_nearest_declaration,var_and_function_declarations_hoist_to_their_function,let_const_and_class_are_block_scoped,parameters_and_the_body(with and without the separate body scope),arguments_is_implicit_in_functions_but_not_arrows,a_function_expression_sees_its_own_name,catch_parameters_are_scoped_to_the_clause,class_fields_and_static_blocks_get_scopes,modules_bind_imports_and_refer_to_exports,escaped_names_are_the_same_name,names_that_are_not_references_are_left_alone.the_temporal_dead_zone_is_flagged: 13 cases, including the ones a nested function or a static field makes safe.annex_b_hoists_sloppy_functions_in_blocks: 12 cases, each of the conditions that stop the hoisting.writes_are_told_from_reads,eval_and_with_make_scopes_dynamic,lookups_by_node,free_names_are_listed_once_in_order,deep_trees_are_analyzed_without_recursion.lexical_names_declared_twice(21 sources),lexical_and_var_names_that_meet(15),redeclarations_the_spec_allows(19, which fail if the analysis is too eager),a_diagnostic_points_at_the_second_and_labels_the_first.test262 goes from 3,824 to 4,014 of the 4,374 parse negatives caught, 360 still skipped (358 regular expression patterns, 2 undeclared exports), 0 failures. None of the 48,408 files that should parse gets a diagnostic, which is the check on false positives.
On the bench corpus, the analysis runs at 239 MB/s on typescript.js (45,147 scopes, 245,302 references), 273 MB/s on react-dom, 364 MB/s on three.js. The free names it lists are the globals each bundle uses; typescript.js's 63 include
BreakpointResolverand the like, which its esbuildmodule.exportsannotation names and never declares. The analysis is opt-in and the parser is unchanged.cargo +nightly fuzz run parse -- -max_total_time=120 -max_len=4096: 1,331,398 runs, no crash.cargo test --workspace,cargo clippy --workspace --all-targets -- -D warningsand the bench's,cargo fmt --checkfor both,cargo docfor each crate with-D warnings,cargo deny check licenses bans sourcesfor both, the fuzz manifests'cargo metadataandscripts/check-headers.shpass locally.