Skip to content

Commit 282c38d

Browse files
a-tarasyukKingwl
authored andcommitted
fix(26325): use a unique name for reserved words in 'constructor like' function name (microsoft#39684)
1 parent 80326ba commit 282c38d

11 files changed

+3760
-25
lines changed

src/compiler/transformers/es2015.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -814,18 +814,19 @@ namespace ts {
814814
*/
815815
function transformClassBody(node: ClassExpression | ClassDeclaration, extendsClauseElement: ExpressionWithTypeArguments | undefined): Block {
816816
const statements: Statement[] = [];
817+
const name = factory.getInternalName(node);
818+
const constructorLikeName = isIdentifierANonContextualKeyword(name) ? factory.getGeneratedNameForNode(name) : name;
817819
startLexicalEnvironment();
818820
addExtendsHelperIfNeeded(statements, node, extendsClauseElement);
819-
addConstructor(statements, node, extendsClauseElement);
821+
addConstructor(statements, node, constructorLikeName, extendsClauseElement);
820822
addClassMembers(statements, node);
821823

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

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

@@ -868,7 +869,7 @@ namespace ts {
868869
* @param node The ClassExpression or ClassDeclaration node.
869870
* @param extendsClauseElement The expression for the class `extends` clause.
870871
*/
871-
function addConstructor(statements: Statement[], node: ClassExpression | ClassDeclaration, extendsClauseElement: ExpressionWithTypeArguments | undefined): void {
872+
function addConstructor(statements: Statement[], node: ClassExpression | ClassDeclaration, name: Identifier, extendsClauseElement: ExpressionWithTypeArguments | undefined): void {
872873
const savedConvertedLoopState = convertedLoopState;
873874
convertedLoopState = undefined;
874875
const ancestorFacts = enterSubtree(HierarchyFacts.ConstructorExcludes, HierarchyFacts.ConstructorIncludes);
@@ -878,7 +879,7 @@ namespace ts {
878879
/*decorators*/ undefined,
879880
/*modifiers*/ undefined,
880881
/*asteriskToken*/ undefined,
881-
factory.getInternalName(node),
882+
name,
882883
/*typeParameters*/ undefined,
883884
transformConstructorParameters(constructor, hasSynthesizedSuper),
884885
/*type*/ undefined,

0 commit comments

Comments
 (0)