Skip to content

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
github:mainfrom
stevenjoezhang:fix/jcorn-es2022-static-block-super
Open

stevenjoezhang wants to merge 2 commits into
github:mainfrom
stevenjoezhang:fix/jcorn-es2022-static-block-super

Conversation

@stevenjoezhang

Copy link
Copy Markdown

Summary

This PR fixes two JavaScript parsing bugs in the jcorn extractor (Parser.java / ESNextParser.java) encountered on valid real-world production bundles (such as n8n and Tabby):

  1. Allow super and new.target inside ES2022 class static { ... } blocks (4ab7715)
  2. Allow RegExp literals immediately following the contextual keyword of in for-of loops (46bbbc3)

1. super and new.target in ES2022 class static blocks

Problem

Valid top-level ES2022 class static initialization blocks that reference super or new.target fail with a fatal parse error ('super' outside of function or class), causing the extractor to drop the AST for the entire file:

class Base {
  static schema = { openapi: (x) => x };
}
class Child extends Base {
  static {
    this.schema = super.schema.openapi({});
  }
}

Previously, Parser.parseClassMember parsed static { ... } by calling parseBlock(false) directly without updating parser state flags, while parseExprAtom and parseNew required this.inFunction to be true.

Fix

  • Added protected boolean inStaticBlock in Parser.java.
  • In Parser.parseClassMember, when entering a static { ... } block, save and restore inFunction, inGenerator, inAsync, inStaticBlock, labels, yieldPos, and awaitPos, setting inStaticBlock = true and resetting inFunction = inGenerator = inAsync = false with a fresh label stack (matching ECMAScript ClassStaticBlock semantics).
  • Allowed super (parseExprAtom) and new.target (parseNew) when inFunction || inStaticBlock.
  • Guarded top-level await (parseMaybeUnary), await using (parseStatement), and for await (ESNextParser.parseForStatement) with !this.inFunction && !this.inStaticBlock so synchronous static blocks are not treated as top-level async contexts.
  • Preserves existing rejections for return directly inside a static block (both at top-level and inside a function), break escaping a static block, and super outside classes/functions.

2. RegExp literals immediately after contextual of in for-of

Problem

Minified for-of / for-await-of loops where a RegExp literal immediately follows of (with or without whitespace) fail with Unexpected token:

for await (var s of/multipart\/mixed/i.test(u) ? a : b) {}

Because of is lexed as a contextual identifier (TokenType.name, which sets exprAllowed = false), this.next() in parseForIn lexed the following / as a division operator instead of starting a RegExp literal.

Fix

  • Set this.exprAllowed = true; immediately before this.next(); in Parser.parseForIn.

Testing

  • Added unit tests in javascript/extractor/test/com/semmle/js/extractor/test/RobustnessTests.java:
    • staticBlockSuperTest: top-level class declarations/expressions using super and new.target in static { ... }, arrow functions capturing super, nested function/async function, and loop break.
    • staticBlockBoundaryErrorsTest: negative tests ensuring top-level super, extends super.foo, return inside static { ... } (top-level and inside a function), and break escaping a static block remain rejected.
    • forOfRegexpTest: for/for await with of/regex/ (with and without whitespace), for (of of/a/.exec(s)), and division expressions like for (let x = of / 2; ...).
  • Verified RobustnessTests and the javascript/extractor JUnit test suites pass (112/112 parser tests and 293/293 JS TrapTests).
  • End-to-end verified codeql database create on 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.

@stevenjoezhang
stevenjoezhang requested review from a team as code owners November 26, 2026 16:55
Copilot AI adaptive review requested due to automatic review settings November 26, 2026 16:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 of keyword.
  • 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 {
@github-actions github-actions Bot added the JS label Nov 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants