8000 Fix module resolution for import call emit (#41390) · typescript-bot/TypeScript@6fac3dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6fac3dd

Browse files
authored
Fix module resolution for import call emit (microsoft#41390)
1 parent b9ac2f5 commit 6fac3dd

14 files changed

+63
-14
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38654,7 +38654,10 @@ namespace ts {
3865438654
isOptionalParameter,
3865538655
moduleExportsSomeValue,
3865638656
isArgumentsLocalBinding,
38657-
getExternalModuleFileFromDeclaration,
38657+
getExternalModuleFileFromDeclaration: nodeIn => {
38658+
const node = getParseTreeNode(nodeIn, hasPossibleExternalModuleReference);
38659+
return node && getExternalModuleFileFromDeclaration(node);
38660+
},
3865838661
getTypeReferenceDirectivesForEntityName,
3865938662
getTypeReferenceDirectivesForSymbol,
3866038663
isLiteralConstDeclaration,
@@ -38815,7 +38818,7 @@ namespace ts {
3881538818
}
3881638819
}
3881738820

38818-
function getExternalModuleFileFromDeclaration(declaration: AnyImportOrReExport | ModuleDeclaration | ImportTypeNode): SourceFile | undefined {
38821+
function getExternalModuleFileFromDeclaration(declaration: AnyImportOrReExport | ModuleDeclaration | ImportTypeNode | ImportCall): SourceFile | undefined {
3881938822
const specifier = declaration.kind === SyntaxKind.ModuleDeclaration ? tryCast(declaration.name, isStringLiteral) : getExternalModuleName(declaration);
3882038823
const moduleSymbol = resolveExternalModuleNameWorker(specifier!, specifier!, /*moduleNotFoundError*/ undefined); // TODO: GH#18217
3882138824
if (!moduleSymbol) {

src/compiler/factory/utilities.ts

Lines changed: 7 additions & 7 deletions
A3E2
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,12 @@ namespace ts {
511511
* 3- The containing SourceFile has an entry in renamedDependencies for the import as requested by some module loaders (e.g. System).
512512
* Otherwise, a new StringLiteral node representing the module name will be returned.
513513
*/
514-
export function getExternalModuleNameLiteral(factory: NodeFactory, importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration, sourceFile: SourceFile, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) {
515-
const moduleName = getExternalModuleName(importNode)!; // TODO: GH#18217
516-
if (moduleName.kind === SyntaxKind.StringLiteral) {
514+
export function getExternalModuleNameLiteral(factory: NodeFactory, importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration 67E6 | ImportCall, sourceFile: SourceFile, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) {
515+
const moduleName = getExternalModuleName(importNode);
516+
if (moduleName && isStringLiteral(moduleName)) {
517517
return tryGetModuleNameFromDeclaration(importNode, host, factory, resolver, compilerOptions)
518-
|| tryRenameExternalModule(factory, <StringLiteral>moduleName, sourceFile)
519-
|| factory.cloneNode(<StringLiteral>moduleName);
518+
|| tryRenameExternalModule(factory, moduleName, sourceFile)
519+
|| factory.cloneNode(moduleName);
520520
}
521521

522522
return undefined;
@@ -528,7 +528,7 @@ namespace ts {
528528
*/
529529
function tryRenameExternalModule(factory: NodeFactory, moduleName: LiteralExpression, sourceFile: SourceFile) {
530530
const rename = sourceFile.renamedDependencies && sourceFile.renamedDependencies.get(moduleName.text);
531-
return rename && factory.createStringLiteral(rename);
531+
return rename ? factory.createStringLiteral(rename) : undefined;
532532
}
533533

534534
/**
@@ -551,7 +551,7 @@ namespace ts {
551551
return undefined;
552552
}
553553

554-
function tryGetModuleNameFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration, host: EmitHost, factory: NodeFactory, resolver: EmitResolver, compilerOptions: CompilerOptions) {
554+
function tryGetModuleNameFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ImportCall, host: EmitHost, factory: NodeFactory, resolver: EmitResolver, compilerOptions: CompilerOptions) {
555555
return tryGetModuleNameFromFile(factory, resolver.getExternalModuleFileFromDeclaration(declaration), host, compilerOptions);
556556
}
557557

src/compiler/transformers/module/module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,10 @@ namespace ts {
609609
}
610610

611611
function visitImportCallExpression(node: ImportCall): Expression {
612-
const argument = visitNode(firstOrUndefined(node.arguments), moduleExpressionElementVisitor);
612+
const externalModuleName = getExternalModuleNameLiteral(factory, node, currentSourceFile, host, resolver, compilerOptions);
613+
const firstArgument = visitNode(firstOrUndefined(node.arguments), moduleExpressionElementVisitor);
614+
// Only use the external module name if it differs from the first argument. This allows us to preserve the quote style of the argument on output.
615+
const argument = externalModuleName && (!firstArgument || !isStringLiteral(firstArgument) || firstArgument.text !== externalModuleName.text) ? externalModuleName : firstArgument;
613616
const containsLexicalThis = !!(node.transformFlags & TransformFlags.ContainsLexicalThis);
614617
switch (compilerOptions.module) {
615618
case ModuleKind.AMD:

src/compiler/transformers/module/system.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,13 +1495,17 @@ namespace ts {
14951495
// }
14961496
// };
14971497
// });
1498+
const externalModuleName = getExternalModuleNameLiteral(factory, node, currentSourceFile, host, resolver, compilerOptions);
1499+
const firstArgument = visitNode(firstOrUndefined(node.arguments), destructuringAndImportCallVisitor);
1500+
// Only use the external module name if it differs from the first argument. This allows us to preserve the quote style of the argument on output.
1501+
const argument = externalModuleName && (!firstArgument || !isStringLiteral(firstArgument) || firstArgument.text !== externalModuleName.text) ? externalModuleName : firstArgument;
14981502
return factory.createCallExpression(
14991503
factory.createPropertyAccessExpression(
15001504
contextObject,
15011505
factory.createIdentifier("import")
15021506
),
15031507
/*typeArguments*/ undefined,
1504-
some(node.arguments) ? [visitNode(node.arguments[0], destructuringAndImportCallVisitor)] : []
1508+
argument ? [argument] : []
15051509
);
15061510
}
15071511

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4607,7 +4607,7 @@ namespace ts {
46074607
isOptionalParameter(node: ParameterDeclaration): boolean;
46084608
moduleExportsSomeValue(moduleReferenceExpression: Expression): boolean;
46094609
isArgumentsLocalBinding(node: Identifier): boolean;
4610-
getExternalModuleFileFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration | ImportTypeNode): SourceFile | undefined;
4610+
getExternalModuleFileFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration | ImportTypeNode | ImportCall): SourceFile | undefined;
46114611
getTypeReferenceDirectivesForEntityName(name: EntityNameOrEntityNameExpression): string[] | undefined;
46124612
getTypeReferenceDirectivesForSymbol(symbol: Symbol, meaning?: SymbolFlags): string[] | undefined;
46134613
isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature 10000 | ParameterDeclaration): boolean;

src/compiler/utilities.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,10 @@ namespace ts {
907907
}
908908
}
909909

910+
export function hasPossibleExternalModuleReference(node: Node): node is AnyImportOrReExport | ModuleDeclaration | ImportTypeNode | ImportCall {
911+
return isAnyImportOrReExport(node) || isModuleDeclaration(node) || isImportTypeNode(node) || isImportCall(node);
912+
}
913+
910914
export function isAnyImportOrReExport(node: Node): node is AnyImportOrReExport {
911915
return isAnyImportSyntax(node) || isExportDeclaration(node);
912916
}
@@ -2426,7 +2430,7 @@ namespace ts {
24262430
}
24272431
}
24282432

2429-
export function getExternalModuleName(node: AnyImportOrReExport | ImportTypeNode): Expression | undefined {
2433+
export function getExternalModuleName(node: AnyImportOrReExport | ImportTypeNode | ImportCall): Expression | undefined {
24302434
switch (node.kind) {
24312435
case SyntaxKind.ImportDeclaration:
24322436
case SyntaxKind.ExportDeclaration:
@@ -2435,6 +2439,8 @@ namespace ts {
24352439
return node.moduleReference.kind === SyntaxKind.ExternalModuleReference ? node.moduleReference.expression : undefined;
24362440
case SyntaxKind.ImportType:
24372441
return isLiteralImportTypeNode(node) ? node.argument.literal : undefined;
2442+
case SyntaxKind.CallExpression:
2443+
return node.arguments[0];
24382444
default:
24392445
return Debug.assertNever(node);
24402446
}

tests/baselines/reference/outFilerootDirModuleNamesAmd.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ foo();
88
//// [b.ts]
99
import Foo from "./a";
1010
export default function foo() { new Foo(); }
11-
11+
12+
// https://github.com/microsoft/TypeScript/issues/37429
13+
import("./a");
1214

1315
//// [output.js]
1416
define("b", ["require", "exports", "a"], function (require, exports, a_1) {
1517
"use strict";
1618
Object.defineProperty(exports, "__esModule", { value: true });
1719
function foo() { new a_1.default(); }
1820
exports.default = foo;
21+
// https://github.com/microsoft/TypeScript/issues/37429
22+
new Promise((resolve_1, reject_1) => { require(["a"], resolve_1, reject_1); });
1923
});
2024
define("a", ["require", "exports", "b"], function (require, exports, b_1) {
2125
"use strict";

tests/baselines/reference/outFilerootDirModuleNamesAmd.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ export default function foo() { new Foo(); }
1616
>foo : Symbol(foo, Decl(b.ts, 0, 22))
1717
>Foo : Symbol(Foo, Decl(b.ts, 0, 6))
1818

19+
// https://github.com/microsoft/TypeScript/issues/37429
20+
import("./a");
21+
>"./a" : Symbol("tests/cases/conformance/es6/moduleExportsAmd/src/a", Decl(a.ts, 0, 0))
22+

tests/baselines/reference/outFilerootDirModuleNamesAmd.types

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ export default function foo() { new Foo(); }
1818
>new Foo() : Foo
1919
>Foo : typeof Foo
2020

21+
// https://github.com/microsoft/TypeScript/issues/37429
22+
import("./a");
23+
>import("./a") : Promise<typeof import("tests/cases/conformance/es6/moduleExportsAmd/src/a")>
24+
>"./a" : "./a"
25+

tests/baselines/reference/outFilerootDirModuleNamesSystem.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ foo();
88
//// [b.ts]
99
import Foo from "./a";
1010
export default function foo() { new Foo(); }
11+
12+
// https://github.com/microsoft/TypeScript/issues/37429
13+
import("./a");
1114

1215

1316
//// [output.js]
@@ -24,6 +27,8 @@ System.register("b", ["a"], function (exports_1, context_1) {
2427
}
2528
],
2629
execute: function () {
30+
// https://github.com/microsoft/TypeScript/issues/37429
31+
context_1.import("a");
2732
}
2833
};
2934
});

tests/baselines/reference/outFilerootDirModuleNamesSystem.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ export default function foo() { new Foo(); }
1616
>foo : Symbol(foo, Decl(b.ts, 0, 22))
1717
>Foo : Symbol(Foo, Decl(b.ts, 0, 6))
1818

19+
// https://github.com/microsoft/TypeScript/issues/37429
20+
import("./a");
21+
>"./a" : Symbol("tests/cases/conformance/es6/moduleExportsSystem/src/a", Decl(a.ts, 0, 0))
22+

tests/baselines/reference/outFilerootDirModuleNamesSystem.types

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ export default function foo() { new Foo(); }
1818
>new Foo() : Foo
1919
>Foo : typeof Foo
2020

21+
// https://github.com/microsoft/TypeScript/issues/37429
22+
import("./a");
23+
>import("./a") : Promise<typeof import("tests/cases/conformance/es6/moduleExportsSystem/src/a")>
24+
>"./a" : "./a"
25+

tests/cases/conformance/es6/moduleExportsAmd/outFilerootDirModuleNamesAmd.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ foo();
1010
// @filename: src/b.ts
1111
import Foo from "./a";
1212
export default function foo() { new Foo(); }
13+
14+
// https://github.com/microsoft/TypeScript/issues/37429
15+
import("./a");

tests/cases/conformance/es6/moduleExportsSystem/outFilerootDirModuleNamesSystem.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ foo();
1010
// @filename: src/b.ts
1111
import Foo from "./a";
1212
export default function foo() { new Foo(); }
13+
14+
// https://github.com/microsoft/TypeScript/issues/37429
15+
import("./a");

0 commit comments

Comments
 (0)
0