Fix CJS local binding emit for ES decorators (#53725)

This commit is contained in:
Ron Buckton
2023-04-10 17:05:38 -04:00
committed by GitHub
parent 926c6f1ddb
commit 3e76fb5ca2
11 changed files with 268 additions and 5 deletions

View File

@@ -46,6 +46,7 @@ import {
getAllDecoratorsOfClassElement,
getCommentRange,
getEffectiveBaseTypeNode,
getEmitScriptTarget,
getFirstConstructorWithBody,
getHeritageClause,
getNonAssignmentOperatorForCompoundAssignment,
@@ -279,6 +280,9 @@ export function transformESDecorators(context: TransformationContext): (x: Sourc
hoistVariableDeclaration,
} = context;
const compilerOptions = context.getCompilerOptions();
const languageVersion = getEmitScriptTarget(compilerOptions);
let top: LexicalEnvironmentStackEntry | undefined;
let classInfo: ClassInfo | undefined;
let classThis: Identifier | undefined;
@@ -1003,7 +1007,12 @@ export function transformESDecorators(context: TransformationContext): (x: Sourc
Debug.assertIsDefined(node.name, "A class declaration that is not a default export must have a name.");
const iife = transformClassLike(node, factory.createStringLiteralFromNode(node.name));
const modifiers = visitNodes(node.modifiers, modifierVisitor, isModifier);
const varDecl = factory.createVariableDeclaration(node.name, /*exclamationToken*/ undefined, /*type*/ undefined, iife);
// When we transform to ES5/3 this will be moved inside an IIFE and should reference the name
// without any block-scoped variable collision handling
const declName = languageVersion <= ScriptTarget.ES2015 ?
factory.getInternalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true) :
factory.getLocalName(node, /*allowComments*/ false, /*allowSourceMaps*/ true);
const varDecl = factory.createVariableDeclaration(declName, /*exclamationToken*/ undefined, /*type*/ undefined, iife);
const varDecls = factory.createVariableDeclarationList([varDecl], NodeFlags.Let);
const statement = factory.createVariableStatement(modifiers, varDecls);
setOriginalNode(statement, node);