Skip to content

Commit 501e442

Browse files
authored
Elide import equals in transpileModule if referenced only by export type (#49664)
* Elide import equals in transpileModule if referenced only by export type. * Revise approach to avoid marking alias in export type as referenced. * Handle type only export specifier.
1 parent 3dbe62e commit 501e442

6 files changed

+49
-4
lines changed

src/compiler/checker.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25783,9 +25783,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
2578325783
return getTypeOfSymbol(symbol);
2578425784
}
2578525785

25786-
// We should only mark aliases as referenced if there isn't a local value declaration
25787-
// for the symbol. Also, don't mark any property access expression LHS - checkPropertyAccessExpression will handle that
25788-
if (!(node.parent && isPropertyAccessExpression(node.parent) && node.parent.expression === node)) {
25786+
if (shouldMarkIdentifierAliasReferenced(node)) {
2578925787
markAliasReferenced(symbol, node);
2579025788
}
2579125789

@@ -25933,6 +25931,25 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
2593325931
return assignmentKind ? getBaseTypeOfLiteralType(flowType) : flowType;
2593425932
}
2593525933

25934+
function shouldMarkIdentifierAliasReferenced(node: Identifier): boolean {
25935+
const parent = node.parent;
25936+
if (parent) {
25937+
// A property access expression LHS? checkPropertyAccessExpression will handle that.
25938+
if (isPropertyAccessExpression(parent) && parent.expression === node) {
25939+
return false;
25940+
}
25941+
// Next two check for an identifier inside a type only export.
25942+
if (isExportSpecifier(parent) && parent.isTypeOnly) {
25943+
return false;
25944+
}
25945+
const greatGrandparent = parent.parent?.parent;
25946+
if (greatGrandparent && isExportDeclaration(greatGrandparent) && greatGrandparent.isTypeOnly) {
25947+
return false;
25948+
}
25949+
}
25950+
return true;
25951+
}
25952+
2593625953
function isInsideFunctionOrInstancePropertyInitializer(node: Node, threshold: Node): boolean {
2593725954
return !!findAncestor(node, n => n === threshold ? "quit" : isFunctionLike(n) || (
2593825955
n.parent && isPropertyDeclaration(n.parent) && !hasStaticModifier(n.parent) && n.parent.initializer === n
@@ -41307,7 +41324,9 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
4130741324
error(exportedName, Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, idText(exportedName));
4130841325
}
4130941326
else {
41310-
markExportAsReferenced(node);
41327+
if (!node.isTypeOnly && !node.parent.parent.isTypeOnly) {
41328+
markExportAsReferenced(node);
41329+
}
4131141330
const target = symbol && (symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol);
4131241331
if (!target || target === unknownSymbol || target.flags & SymbolFlags.Value) {
4131341332
checkExpressionCached(node.propertyName || node.name);

src/testRunner/unittests/services/transpile.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,5 +485,19 @@ export { a as alias };
485485
export * as alias from './file';`, {
486486
noSetFileName: true
487487
});
488+
489+
transpilesCorrectly("Elides import equals referenced only by export type",
490+
`import IFoo = Namespace.IFoo;` +
491+
`export type { IFoo };`, {
492+
options: { compilerOptions: { module: ModuleKind.CommonJS } }
493+
}
494+
);
495+
496+
transpilesCorrectly("Elides import equals referenced only by type only export specifier",
497+
`import IFoo = Namespace.IFoo;` +
498+
`export { type IFoo };`, {
499+
options: { compilerOptions: { module: ModuleKind.CommonJS } }
500+
}
501+
);
488502
});
489503
}

tests/baselines/reference/transpile/Elides import equals referenced only by export type.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/transpile/Elides import equals referenced only by export type.oldTranspile.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/transpile/Elides import equals referenced only by type only export specifier.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/transpile/Elides import equals referenced only by type only export specifier.oldTranspile.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)