Skip to content

Commit f0ae577

Browse files
committed
Add a reusable '0' literal for the module transform
1 parent 3b06ef1 commit f0ae577

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/compiler/factory/emitNode.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ namespace ts {
22
/**
33
* Associates a node with the current transformation, initializing
44
* various transient transformation properties.
5+
*
6+
* This function should only be called when writing to an emit node.
57
* @internal
68
*/
79
export function getOrCreateEmitNode(node: Node): EmitNode {
@@ -20,6 +22,9 @@ namespace ts {
2022

2123
node.emitNode = {} as EmitNode;
2224
}
25+
else {
26+
Debug.assert(!(node.emitNode?.flags! & EmitFlags.Reusable), "Invalid attempt to modify a reusable node.");
27+
}
2328

2429
return node.emitNode;
2530
}

src/compiler/factory/utilities.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace ts {
1818
: factory.createElementAccessExpression(target, memberName),
1919
memberName
2020
);
21-
getOrCreateEmitNode(expression).flags |= EmitFlags.NoNestedSourceMaps;
21+
addEmitFlags(expression, EmitFlags.NoNestedSourceMaps);
2222
return expression;
2323
}
2424
}
@@ -479,8 +479,7 @@ namespace ts {
479479

480480
if (create) {
481481
const parseNode = getOriginalNode(node, isSourceFile);
482-
const emitNode = getOrCreateEmitNode(parseNode);
483-
return emitNode.externalHelpersModuleName || (emitNode.externalHelpersModuleName = factory.createUniqueName(externalHelpersModuleNameText));
482+
return parseNode?.emitNode?.externalHelpersModuleName || (getOrCreateEmitNode(parseNode).externalHelpersModuleName = factory.createUniqueName(externalHelpersModuleNameText));
484483
}
485484
}
486485
}

src/compiler/transformers/module/module.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace ts {
4242
context.enableSubstitution(SyntaxKind.ShorthandPropertyAssignment); // Substitutes shorthand property assignments for imported/exported symbols.
4343
context.enableEmitNotification(SyntaxKind.SourceFile); // Restore state when substituting nodes in a file.
4444

45+
let zeroLiteral: NumericLiteral | undefined;
4546
const moduleInfoMap: ExternalModuleInfo[] = []; // The ExternalModuleInfo for each file.
4647
const deferredExports: (Statement[] | undefined)[] = []; // Exports to defer until an EndOfDeclarationMarker is found.
4748

@@ -1811,16 +1812,22 @@ namespace ts {
18111812
return result;
18121813
}
18131814

1815+
function getZeroLiteral() {
1816+
zeroLiteral ||= factory.createNumericLiteral(0);
1817+
setEmitFlags(zeroLiteral, EmitFlags.Reusable);
1818+
return zeroLiteral;
1819+
}
1820+
18141821
function substituteCallExpression(node: CallExpression) {
18151822
if (isIdentifier(node.expression) && getImportOrExportBindingReference(node.expression, /*removeEntry*/ false)) {
18161823
return isCallChain(node) ?
18171824
factory.updateCallChain(node,
1818-
setTextRange(factory.createComma(factory.createNumericLiteral(0), node.expression), node.expression),
1825+
setTextRange(factory.createComma(getZeroLiteral(), node.expression), node.expression),
18191826
node.questionDotToken,
18201827
/*typeArguments*/ undefined,
18211828
node.arguments) :
18221829
factory.updateCallExpression(node,
1823-
setTextRange(factory.createComma(factory.createNumericLiteral(0), node.expression), node.expression),
1830+
setTextRange(factory.createComma(getZeroLiteral(), node.expression), node.expression),
18241831
/*typeArguments*/ undefined,
18251832
node.arguments);
18261833
}
@@ -1831,7 +1838,7 @@ namespace ts {
18311838
if (isIdentifier(node.tag) && getImportOrExportBindingReference(node.tag, /*removeEntry*/ false)) {
18321839
return factory.updateTaggedTemplateExpression(
18331840
node,
1834-
setTextRange(factory.createComma(factory.createNumericLiteral(0), node.tag), node.tag),
1841+
setTextRange(factory.createComma(getZeroLiteral(), node.tag), node.tag),
18351842
/*typeArguments*/ undefined,
18361843
node.template);
18371844
}

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6624,6 +6624,7 @@ namespace ts {
66246624
/*@internal*/ TypeScriptClassWrapper = 1 << 25, // The node is an IIFE class wrapper created by the ts transform.
66256625
/*@internal*/ NeverApplyImportHelper = 1 << 26, // Indicates the node should never be wrapped with an import star helper (because, for example, it imports tslib itself)
66266626
/*@internal*/ IgnoreSourceNewlines = 1 << 27, // Overrides `printerOptions.preserveSourceNewlines` to print this node (and all descendants) with default whitespace.
6627+
/*@internal*/ Reusable = 1 << 28, // Marks this node as reusable across multiple transforms. Once marked reusable, no other emit flags or emit node values may be modified.
66276628
}
66286629

66296630
export interface EmitHelperBase {

0 commit comments

Comments
 (0)