diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index f9abe8be995..a7427e58134 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3696,10 +3696,6 @@ namespace ts { // var foo3 = require("subfolder // import * as foo1 from "module-from-node -> we want this import to be a statement rather than import call expression const importCall = parseImportCallExpression(); - if (importCall.specifier.kind === SyntaxKind.StringLiteral) { - (sourceFile.imports || (sourceFile.imports = [])).push(importCall.specifier as StringLiteral); - } - sourceFile.containsDynamicImport = true; return importCall; } const expression = token() === SyntaxKind.SuperKeyword ? parseSuperExpression() : parseMemberExpressionOrHigher(); diff --git a/src/compiler/program.ts b/src/compiler/program.ts index ec263831cd4..2057bf017dd 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1183,7 +1183,7 @@ namespace ts { const isDtsFile = isDeclarationFile(file); // file.imports may not be undefined if there exists dynamic import - let imports = file.imports; + let imports: LiteralExpression[]; let moduleAugmentations: LiteralExpression[]; let ambientModules: string[]; @@ -1202,9 +1202,7 @@ namespace ts { for (const node of file.statements) { collectModuleReferences(node, /*inAmbientModule*/ false); - if (isJavaScriptFile) { - collectRequireCalls(node); - } + collectImportOrRequireCalls(node); } file.imports = imports || emptyArray; @@ -1266,12 +1264,15 @@ namespace ts { } } - function collectRequireCalls(node: Node): void { - if (isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) { + function collectImportOrRequireCalls(node: Node): void { + if (isJavaScriptFile && isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) { (imports || (imports = [])).push((node).arguments[0]); } + else if (node.kind === SyntaxKind.ImportCallExpression && (node).specifier.kind === SyntaxKind.StringLiteral) { + (imports || (imports = [])).push((node).specifier); + } else { - forEachChild(node, collectRequireCalls); + forEachChild(node, collectImportOrRequireCalls); } } } diff --git a/src/harness/unittests/commandLineParsing.ts b/src/harness/unittests/commandLineParsing.ts index 61b96d89553..6aa105e2c03 100644 --- a/src/harness/unittests/commandLineParsing.ts +++ b/src/harness/unittests/commandLineParsing.ts @@ -113,7 +113,7 @@ namespace ts { start: undefined, length: undefined, }, { - messageText: "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015'.", + messageText: "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'es2018'.", category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,