fix(parser): reject 'new super()' syntax with error TS2824#63464
fix(parser): reject 'new super()' syntax with error TS2824#63464SkyCoderAakash wants to merge 1 commit intomicrosoft:mainfrom
Conversation
|
@SkyCoderAakash please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Pull request overview
Fixes a parser correctness gap where new super() was accepted (not valid JavaScript syntax), by introducing an early parse-time diagnostic (TS2824) and a compiler test to cover the repro from #63451.
Changes:
- Add a parser check in
parseNewExpressionOrNewDotTargetto diagnosenew super. - Add a new diagnostic message/code (2824) for the invalid syntax.
- Add a compiler test case exercising
new super()in static contexts.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/compiler/parser.ts | Adds a parse-time diagnostic path for new super() and returns a synthesized NewExpression for recovery. |
| src/compiler/diagnosticMessages.json | Introduces TS2824 message text for the new parse error. |
| tests/cases/compiler/newSuperError.ts | Adds a regression test covering new super() in a static block and static method. |
| // Validate that 'new' is not used with 'super' - this is not JavaScript syntax | ||
| if (token() === SyntaxKind.SuperKeyword) { | ||
| parseErrorAtCurrentToken(Diagnostics.super_cannot_be_used_with_new_keyword); | ||
| return finishNode(factoryCreateNewExpression( | ||
| createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ false), | ||
| /*typeArguments*/ undefined, | ||
| /*argumentsArray*/ undefined | ||
| ), pos); |
|
|
||
| // Validate that 'new' is not used with 'super' - this is not JavaScript syntax | ||
| if (token() === SyntaxKind.SuperKeyword) { | ||
| parseErrorAtCurrentToken(Diagnostics.super_cannot_be_used_with_new_keyword); | ||
| return finishNode(factoryCreateNewExpression( | ||
| createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ false), | ||
| /*typeArguments*/ undefined, | ||
| /*argumentsArray*/ undefined | ||
| ), pos); | ||
| } | ||
|
|
||
| if (parseOptional(SyntaxKind.DotToken)) { |
| } | ||
| } | ||
|
|
||
| console.log("If you see this, the test failed (compilation should show error)"); |
Overview
Fixes #63451 - TypeScript incorrectly allows
new super()in static contexts, which is invalid JavaScript syntax and causes runtime errors.Changes
parseNewExpressionOrNewDotTargetto detectnewfollowed bysuperkeyword'super' cannot be used with 'new' keywordtests/cases/compiler/newSuperError.tsFiles Changed
src/compiler/parser.tssrc/compiler/diagnosticMessages.jsontests/cases/compiler/newSuperError.ts