fix(parser): reject 'new super()' syntax with error TS2824#63464
Closed
SkyCoderAakash wants to merge 1 commit into
Closed
fix(parser): reject 'new super()' syntax with error TS2824#63464SkyCoderAakash wants to merge 1 commit into
SkyCoderAakash wants to merge 1 commit into
Conversation
Contributor
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. |
Comment on lines
+6805
to
+6812
| // 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); |
Comment on lines
+6804
to
6815
|
|
||
| // 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)"); |
Member
|
This doesn't meet the bar for a 6.0 patch. See #62963 |
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.
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