From ed5a7fb2f9acc781c026d8f9672ba927bfea785e Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 15 Mar 2026 02:12:56 +0000 Subject: [PATCH] fix(parser): report syntax error for `new super()` (#20384) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - Report a syntax error for `new super()` per the ECMAScript spec grammar - `MemberExpression` includes `SuperProperty` (`super.x`, `super[x]`) but NOT bare `super`, so `new super()` is invalid - `new super.method()` and `new super[x]()` remain valid Closes #20306 ๐Ÿค– Generated with [Claude Code](https://claude.com/claude-code) --- crates/oxc_parser/src/diagnostics.rs | 5 +++++ crates/oxc_parser/src/js/expression.rs | 4 ++++ tasks/coverage/snapshots/parser_babel.snap | 8 ++++++++ tasks/coverage/snapshots/parser_typescript.snap | 12 +++++++++--- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/oxc_parser/src/diagnostics.rs b/crates/oxc_parser/src/diagnostics.rs index 069d1764b3ba8..c6b550e078f7d 100644 --- a/crates/oxc_parser/src/diagnostics.rs +++ b/crates/oxc_parser/src/diagnostics.rs @@ -490,6 +490,11 @@ pub fn new_dynamic_import(span: Span) -> OxcDiagnostic { .with_help("Wrap this with parenthesis") } +#[cold] +pub fn new_super(span: Span) -> OxcDiagnostic { + OxcDiagnostic::error("'new super()' is not allowed").with_label(span) +} + #[cold] pub fn private_name_constructor(span: Span) -> OxcDiagnostic { OxcDiagnostic::error("Classes can't have an element named '#constructor'").with_label(span) diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 022bbbdc111e5..3d021ae9642b2 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -996,6 +996,10 @@ impl<'a, C: Config> ParserImpl<'a, C> { self.error(diagnostics::new_dynamic_import(self.end_span(rhs_span))); } + if matches!(callee, Expression::Super(_)) { + self.error(diagnostics::new_super(self.end_span(rhs_span))); + } + let span = self.end_span(span); if optional { diff --git a/tasks/coverage/snapshots/parser_babel.snap b/tasks/coverage/snapshots/parser_babel.snap index 3b8936143d143..dd912ad557438 100644 --- a/tasks/coverage/snapshots/parser_babel.snap +++ b/tasks/coverage/snapshots/parser_babel.snap @@ -9957,6 +9957,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc โ•ฐโ”€โ”€โ”€โ”€ help: Replace with `super()` or `super.prop` or `super[prop]` + ร— 'new super()' is not allowed + โ•ญโ”€[babel/packages/babel-parser/test/fixtures/esprima/es2015-super-property/invalid_super_id/input.js:2:17] + 1 โ”‚ class A { + 2 โ”‚ foo() { new super + 3 } + ยท โ”€โ”€โ”€โ”€โ”€ + 3 โ”‚ } + โ•ฐโ”€โ”€โ”€โ”€ + ร— TS(2337): Super calls are not permitted outside constructors or in nested functions inside constructors. โ•ญโ”€[babel/packages/babel-parser/test/fixtures/esprima/es2015-super-property/invalid_super_id/input.js:2:13] 1 โ”‚ class A { diff --git a/tasks/coverage/snapshots/parser_typescript.snap b/tasks/coverage/snapshots/parser_typescript.snap index fb7d005d7b9e0..c9c0e8973d577 100644 --- a/tasks/coverage/snapshots/parser_typescript.snap +++ b/tasks/coverage/snapshots/parser_typescript.snap @@ -3,7 +3,7 @@ commit: c9e7428b parser_typescript Summary: AST Parsed : 9828/9831 (99.97%) Positive Passed: 9817/9831 (99.86%) -Negative Passed: 1532/2587 (59.22%) +Negative Passed: 1533/2587 (59.26%) Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration3.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration4.ts @@ -882,8 +882,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/superCallIns Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/superInConstructorParam1.ts -Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/superNewCall1.ts - Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/superPropertyAccess2.ts Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/superPropertyAccessInSuperCall01.ts @@ -11824,6 +11822,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/parser/ecmasc 53 โ”‚ }, โ•ฐโ”€โ”€โ”€โ”€ + ร— 'new super()' is not allowed + โ•ญโ”€[typescript/tests/cases/compiler/superNewCall1.ts:9:13] + 8 โ”‚ constructor() { + 9 โ”‚ new super(value => String(value)); + ยท โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ + 10 โ”‚ } + โ•ฐโ”€โ”€โ”€โ”€ + ร— 'super' can only be used with function calls or in property accesses โ•ญโ”€[typescript/tests/cases/compiler/superWithTypeArgument.ts:7:9] 6 โ”‚ constructor() {