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:
Will Nayes
2022-07-05 11:08:43 -05:00
committed by GitHub
parent 3dbe62e3f0
commit 501e442ffc
6 changed files with 49 additions and 4 deletions

View File

@@ -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);

View File

@@ -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 } }
}
);
});
}

View File

@@ -0,0 +1,3 @@
"use strict";
exports.__esModule = true;
//# sourceMappingURL=file.js.map

View File

@@ -0,0 +1,3 @@
"use strict";
exports.__esModule = true;
//# sourceMappingURL=file.js.map

View File

@@ -0,0 +1,3 @@
"use strict";
exports.__esModule = true;
//# sourceMappingURL=file.js.map

View File

@@ -0,0 +1,3 @@
"use strict";
exports.__esModule = true;
//# sourceMappingURL=file.js.map