mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-06 15:03:14 -05:00
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.
This commit is contained in:
@@ -25783,9 +25783,7 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
|
||||
return getTypeOfSymbol(symbol);
|
||||
}
|
||||
|
||||
// We should only mark aliases as referenced if there isn't a local value declaration
|
||||
// for the symbol. Also, don't mark any property access expression LHS - checkPropertyAccessExpression will handle that
|
||||
if (!(node.parent && isPropertyAccessExpression(node.parent) && node.parent.expression === node)) {
|
||||
if (shouldMarkIdentifierAliasReferenced(node)) {
|
||||
markAliasReferenced(symbol, node);
|
||||
}
|
||||
|
||||
@@ -25933,6 +25931,25 @@ m2: ${(this.mapper2 as unknown as DebugTypeMapper).__debugToString().split("\n")
|
||||
return assignmentKind ? getBaseTypeOfLiteralType(flowType) : flowType;
|
||||
}
|
||||
|
||||
function shouldMarkIdentifierAliasReferenced(node: Identifier): boolean {
|
||||
const parent = node.parent;
|
||||
if (parent) {
|
||||
// A property access expression LHS? checkPropertyAccessExpression will handle that.
|
||||
if (isPropertyAccessExpression(parent) && parent.expression === node) {
|
||||
return false;
|
||||
}
|
||||
// Next two check for an identifier inside a type only export.
|
||||
if (isExportSpecifier(parent) && parent.isTypeOnly) {
|
||||
return false;
|
||||
}
|
||||
const greatGrandparent = parent.parent?.parent;
|
||||
if (greatGrandparent && isExportDeclaration(greatGrandparent) && greatGrandparent.isTypeOnly) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function isInsideFunctionOrInstancePropertyInitializer(node: Node, threshold: Node): boolean {
|
||||
return !!findAncestor(node, n => n === threshold ? "quit" : isFunctionLike(n) || (
|
||||
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")
|
||||
error(exportedName, Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, idText(exportedName));
|
||||
}
|
||||
else {
|
||||
markExportAsReferenced(node);
|
||||
if (!node.isTypeOnly && !node.parent.parent.isTypeOnly) {
|
||||
markExportAsReferenced(node);
|
||||
}
|
||||
const target = symbol && (symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol);
|
||||
if (!target || target === unknownSymbol || target.flags & SymbolFlags.Value) {
|
||||
checkExpressionCached(node.propertyName || node.name);
|
||||
|
||||
@@ -485,5 +485,19 @@ export { a as alias };
|
||||
export * as alias from './file';`, {
|
||||
noSetFileName: true
|
||||
});
|
||||
|
||||
transpilesCorrectly("Elides import equals referenced only by export type",
|
||||
`import IFoo = Namespace.IFoo;` +
|
||||
`export type { IFoo };`, {
|
||||
options: { compilerOptions: { module: ModuleKind.CommonJS } }
|
||||
}
|
||||
);
|
||||
|
||||
transpilesCorrectly("Elides import equals referenced only by type only export specifier",
|
||||
`import IFoo = Namespace.IFoo;` +
|
||||
`export { type IFoo };`, {
|
||||
options: { compilerOptions: { module: ModuleKind.CommonJS } }
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
//# sourceMappingURL=file.js.map
|
||||
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
//# sourceMappingURL=file.js.map
|
||||
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
//# sourceMappingURL=file.js.map
|
||||
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
//# sourceMappingURL=file.js.map
|
||||
Reference in New Issue
Block a user