Repository navigation
JS: Fix jcorn parsing of super/new.target in class static blocks and RegExp literals after for-of - #23266
Open
stevenjoezhang wants to merge 2 commits into
Open
JS: Fix jcorn parsing of super/new.target in class static blocks and RegExp literals after for-of#23266stevenjoezhang wants to merge 2 commits into
stevenjoezhang wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Regression tests are missing for restoring the enclosing parser context and rejecting await, await using, and for await directly in static blocks.
2 open issues
What changed in this PR
Fixes two parsing failures in the JavaScript extractor’s jcorn parser: super and new.target in class static blocks, and regular-expression literals immediately after of in loops.
Changes:
- Isolates static-block parser state while preserving synchronous syntax restrictions.
- Enables regular-expression tokenization after the contextual
ofkeyword. - Adds regression tests for static blocks, invalid control flow, and loop expressions.
| File | Description |
|---|---|
javascript/​extractor/​test/​com/​semmle/​js/​extractor/​test/​RobustnessTests.java |
Adds static-block and regular-expression loop tests. |
javascript/​extractor/​src/​com/​semmle/​jcorn/​Parser.java |
Tracks static-block context and fixes tokenization after of. |
javascript/​extractor/​src/​com/​semmle/​jcorn/​ESNextParser.java |
Prevents static blocks from permitting top-level for await syntax. |
Review effort: Max (adaptive)
| static { | ||
| this.schema = super.schema.openapi({}); | ||
| } | ||
| }; |
|
|
||
| @Test | ||
| public void staticBlockBoundaryErrorsTest() { | ||
| try { |
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.

Summary
This PR fixes two JavaScript parsing bugs in the
jcornextractor (Parser.java/ESNextParser.java) encountered on valid real-world production bundles (such as n8n and Tabby):superandnew.targetinside ES2022 classstatic { ... }blocks (4ab7715)ofinfor-ofloops (46bbbc3)1.
superandnew.targetin ES2022 class static blocksProblem
Valid top-level ES2022 class static initialization blocks that reference
superornew.targetfail with a fatal parse error ('super' outside of function or class), causing the extractor to drop the AST for the entire file:Previously,
Parser.parseClassMemberparsedstatic { ... }by callingparseBlock(false)directly without updating parser state flags, whileparseExprAtomandparseNewrequiredthis.inFunctionto betrue.Fix
protected boolean inStaticBlockinParser.java.Parser.parseClassMember, when entering astatic { ... }block, save and restoreinFunction,inGenerator,inAsync,inStaticBlock,labels,yieldPos, andawaitPos, settinginStaticBlock = trueand resettinginFunction = inGenerator = inAsync = falsewith a fresh label stack (matching ECMAScriptClassStaticBlocksemantics).super(parseExprAtom) andnew.target(parseNew) wheninFunction || inStaticBlock.await(parseMaybeUnary),await using(parseStatement), andfor await(ESNextParser.parseForStatement) with!this.inFunction && !this.inStaticBlockso synchronous static blocks are not treated as top-level async contexts.returndirectly inside a static block (both at top-level and inside a function),breakescaping a static block, andsuperoutside classes/functions.2. RegExp literals immediately after contextual
ofinfor-ofProblem
Minified
for-of/for-await-ofloops where a RegExp literal immediately followsof(with or without whitespace) fail withUnexpected token:Because
ofis lexed as a contextual identifier (TokenType.name, which setsexprAllowed = false),this.next()inparseForInlexed the following/as a division operator instead of starting a RegExp literal.Fix
this.exprAllowed = true;immediately beforethis.next();inParser.parseForIn.Testing
javascript/extractor/test/com/semmle/js/extractor/test/RobustnessTests.java:staticBlockSuperTest: top-level class declarations/expressions usingsuperandnew.targetinstatic { ... }, arrow functions capturingsuper, nestedfunction/async function, and loopbreak.staticBlockBoundaryErrorsTest: negative tests ensuring top-levelsuper,extends super.foo,returninsidestatic { ... }(top-level and inside a function), andbreakescaping a static block remain rejected.forOfRegexpTest:for/for awaitwithof/regex/(with and without whitespace),for (of of/a/.exec(s)), and division expressions likefor (let x = of / 2; ...).RobustnessTestsand thejavascript/extractorJUnit test suites pass (112/112parser tests and293/293JSTrapTests).codeql database createon the real-world bundles that originally triggered both errors (n8n's 9 MB main bundle and Tabby's Next.js chunk): both now extract cleanly with 0 parse errors.