Skip to content

fix(26325): tsc emits invalid code for class expression assigned to a reserved word property #39684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,18 +814,19 @@ namespace ts {
*/
function transformClassBody(node: ClassExpression | ClassDeclaration, extendsClauseElement: ExpressionWithTypeArguments | undefined): Block {
const statements: Statement[] = [];
const name = factory.getInternalName(node);
const constructorLikeName = isIdentifierANonContextualKeyword(name) ? factory.getGeneratedNameForNode(name) : name;
startLexicalEnvironment();
addExtendsHelperIfNeeded(statements, node, extendsClauseElement);
addConstructor(statements, node, extendsClauseElement);
addConstructor(statements, node, constructorLikeName, extendsClauseElement);
addClassMembers(statements, node);

// Create a synthetic text range for the return statement.
const closingBraceLocation = createTokenRange(skipTrivia(currentText, node.members.end), SyntaxKind.CloseBraceToken);
const localName = factory.getInternalName(node);

// The following partially-emitted expression exists purely to align our sourcemap
// emit with the original emitter.
const outer = factory.createPartiallyEmittedExpression(localName);
const outer = factory.createPartiallyEmittedExpression(constructorLikeName);
setTextRangeEnd(outer, closingBraceLocation.end);
setEmitFlags(outer, EmitFlags.NoComments);

Expand Down Expand Up @@ -868,7 +869,7 @@ namespace ts {
* @param node The ClassExpression or ClassDeclaration node.
* @param extendsClauseElement The expression for the class `extends` clause.
*/
function addConstructor(statements: Statement[], node: ClassExpression | ClassDeclaration, extendsClauseElement: ExpressionWithTypeArguments | undefined): void {
function addConstructor(statements: Statement[], node: ClassExpression | ClassDeclaration, name: Identifier, extendsClauseElement: ExpressionWithTypeArguments | undefined): void {
const savedConvertedLoopState = convertedLoopState;
convertedLoopState = undefined;
const ancestorFacts = enterSubtree(HierarchyFacts.ConstructorExcludes, HierarchyFacts.ConstructorIncludes);
Expand All @@ -878,7 +879,7 @@ namespace ts {
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*asteriskToken*/ undefined,
factory.getInternalName(node),
name,
/*typeParameters*/ undefined,
transformConstructorParameters(constructor, hasSynthesizedSuper),
/*type*/ undefined,
Expand Down
Loading