Skip to content

fix(parser): reject 'new super()' syntax with error TS2824#63464

Closed
SkyCoderAakash wants to merge 1 commit into
microsoft:mainfrom
SkyCoderAakash:fix/new-super-parse-error
Closed

fix(parser): reject 'new super()' syntax with error TS2824#63464
SkyCoderAakash wants to merge 1 commit into
microsoft:mainfrom
SkyCoderAakash:fix/new-super-parse-error

Conversation

@SkyCoderAakash
Copy link
Copy Markdown
Contributor

Overview

Fixes #63451 - TypeScript incorrectly allows new super() in static contexts, which is invalid JavaScript syntax and causes runtime errors.

Changes

  • Added validation in parseNewExpressionOrNewDotTarget to detect new followed by super keyword
  • Added diagnostic message TS2824: 'super' cannot be used with 'new' keyword
  • Added test case tests/cases/compiler/newSuperError.ts

Files Changed

File Change
src/compiler/parser.ts Added validation check
src/compiler/diagnosticMessages.json Added error message with code 2824
tests/cases/compiler/newSuperError.ts Added test case

Copilot AI review requested due to automatic review settings May 9, 2026 18:14
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog May 9, 2026
@typescript-bot typescript-bot added For Milestone Bug PRs that fix a bug with a specific milestone labels May 9, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 parseNewExpressionOrNewDotTarget to diagnose new 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 thread src/compiler/parser.ts
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 thread src/compiler/parser.ts
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)");
@RyanCavanaugh
Copy link
Copy Markdown
Member

This doesn't meet the bar for a 6.0 patch. See #62963

@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

new super() in static method does not cause error

4 participants