Skip to content

Commit

Permalink
fix(26325): use a unique name for reserved words in 'constructor like…
Browse files Browse the repository at this point in the history
…' function name (microsoft#39684)
  • Loading branch information
a-tarasyuk authored and Kingwl committed Aug 24, 2020
1 parent 6a75377 commit 85501e6
Show file tree
Hide file tree
Showing 11 changed files with 3,760 additions and 25 deletions.
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

0 comments on commit 85501e6

Please sign in to comment.