diff --git a/src/compiler/transformers/module/esnextAnd2015.ts b/src/compiler/transformers/module/esnextAnd2015.ts index d7329856c064c..f208d7a8d46c1 100644 --- a/src/compiler/transformers/module/esnextAnd2015.ts +++ b/src/compiler/transformers/module/esnextAnd2015.ts @@ -18,25 +18,36 @@ namespace ts { } if (isExternalModule(node) || compilerOptions.isolatedModules) { - const externalHelpersImportDeclaration = createExternalHelpersImportDeclarationIfNeeded(node, compilerOptions); - if (externalHelpersImportDeclaration) { - const statements: Statement[] = []; - const statementOffset = addPrologue(statements, node.statements); - append(statements, externalHelpersImportDeclaration); - - addRange(statements, visitNodes(node.statements, visitor, isStatement, statementOffset)); - return updateSourceFileNode( - node, - setTextRange(createNodeArray(statements), node.statements)); - } - else { - return visitEachChild(node, visitor, context); + const result = updateExternalModule(node); + if (!isExternalModule(node) || some(result.statements, isExternalModuleIndicator)) { + return result; } + return updateSourceFileNode( + result, + setTextRange(createNodeArray([...result.statements, createEmptyExports()]), result.statements), + ); } return node; } + function updateExternalModule(node: SourceFile) { + const externalHelpersImportDeclaration = createExternalHelpersImportDeclarationIfNeeded(node, compilerOptions); + if (externalHelpersImportDeclaration) { + const statements: Statement[] = []; + const statementOffset = addPrologue(statements, node.statements); + append(statements, externalHelpersImportDeclaration); + + addRange(statements, visitNodes(node.statements, visitor, isStatement, statementOffset)); + return updateSourceFileNode( + node, + setTextRange(createNodeArray(statements), node.statements)); + } + else { + return visitEachChild(node, visitor, context); + } + } + function visitor(node: Node): VisitResult { switch (node.kind) { case SyntaxKind.ImportEqualsDeclaration: diff --git a/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js b/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js index 065aa675b6e57..520d659c3f740 100644 --- a/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js +++ b/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js @@ -71,3 +71,4 @@ var M; async function f1() { } M.f1 = f1; })(M || (M = {})); +export {}; diff --git a/tests/baselines/reference/asyncAwaitIsolatedModules_es6.js b/tests/baselines/reference/asyncAwaitIsolatedModules_es6.js index 015992805bcbd..81eb137f28bae 100644 --- a/tests/baselines/reference/asyncAwaitIsolatedModules_es6.js +++ b/tests/baselines/reference/asyncAwaitIsolatedModules_es6.js @@ -112,3 +112,4 @@ var M; } M.f1 = f1; })(M || (M = {})); +export {}; diff --git a/tests/baselines/reference/computedPropertyName.js b/tests/baselines/reference/computedPropertyName.js index a3378327f282d..27adbb6113886 100644 --- a/tests/baselines/reference/computedPropertyName.js +++ b/tests/baselines/reference/computedPropertyName.js @@ -65,3 +65,4 @@ class F { } class G { } +export {}; diff --git a/tests/baselines/reference/decoratedClassFromExternalModule.js b/tests/baselines/reference/decoratedClassFromExternalModule.js index a4ae38133b89a..fc81b9c73beaf 100644 --- a/tests/baselines/reference/decoratedClassFromExternalModule.js +++ b/tests/baselines/reference/decoratedClassFromExternalModule.js @@ -27,3 +27,4 @@ let Decorated = /** @class */ (() => { })(); export default Decorated; //// [undecorated.js] +export {}; diff --git a/tests/baselines/reference/es6ExportAssignment.js b/tests/baselines/reference/es6ExportAssignment.js index 3d0198901ef3c..b7354ee43985d 100644 --- a/tests/baselines/reference/es6ExportAssignment.js +++ b/tests/baselines/reference/es6ExportAssignment.js @@ -4,3 +4,4 @@ export = a; //// [es6ExportAssignment.js] var a = 10; +export {}; diff --git a/tests/baselines/reference/es6ExportAssignment2.js b/tests/baselines/reference/es6ExportAssignment2.js index 531c9e5a5ba3d..3278af118c0c9 100644 --- a/tests/baselines/reference/es6ExportAssignment2.js +++ b/tests/baselines/reference/es6ExportAssignment2.js @@ -10,4 +10,6 @@ import * as a from "a"; //// [a.js] var a = 10; +export {}; //// [b.js] +export {}; diff --git a/tests/baselines/reference/es6ExportAssignment3.js b/tests/baselines/reference/es6ExportAssignment3.js index 5a2cd208a5a7e..c5de73f75a2e9 100644 --- a/tests/baselines/reference/es6ExportAssignment3.js +++ b/tests/baselines/reference/es6ExportAssignment3.js @@ -9,3 +9,4 @@ import * as a from "a"; //// [b.js] +export {}; diff --git a/tests/baselines/reference/es6ExportAssignment4.js b/tests/baselines/reference/es6ExportAssignment4.js index 656dcc0729f77..aa0ce82196986 100644 --- a/tests/baselines/reference/es6ExportAssignment4.js +++ b/tests/baselines/reference/es6ExportAssignment4.js @@ -11,3 +11,4 @@ import * as a from "a"; //// [b.js] +export {}; diff --git a/tests/baselines/reference/es6ImportEqualsDeclaration.js b/tests/baselines/reference/es6ImportEqualsDeclaration.js index 1ca0e94cf8aa9..506c22a351e93 100644 --- a/tests/baselines/reference/es6ImportEqualsDeclaration.js +++ b/tests/baselines/reference/es6ImportEqualsDeclaration.js @@ -9,4 +9,6 @@ import a = require("server"); //// [server.js] var a = 10; +export {}; //// [client.js] +export {}; diff --git a/tests/baselines/reference/es6ImportEqualsDeclaration2.js b/tests/baselines/reference/es6ImportEqualsDeclaration2.js index cc6d33a9afa4c..5458cf6b67307 100644 --- a/tests/baselines/reference/es6ImportEqualsDeclaration2.js +++ b/tests/baselines/reference/es6ImportEqualsDeclaration2.js @@ -20,3 +20,4 @@ import {a} from "server"; //// [client.js] +export {}; diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.js b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.js index 603f158946e94..02ce556a4773b 100644 --- a/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.js +++ b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.js @@ -17,6 +17,7 @@ var a = /** @class */ (function () { } return a; }()); +export {}; //// [main.js] import * as a from "./a"; a; diff --git a/tests/baselines/reference/es6ImportNamedImportIdentifiersParsing.js b/tests/baselines/reference/es6ImportNamedImportIdentifiersParsing.js index 5e22dd98bd442..8780d28d8264a 100644 --- a/tests/baselines/reference/es6ImportNamedImportIdentifiersParsing.js +++ b/tests/baselines/reference/es6ImportNamedImportIdentifiersParsing.js @@ -6,3 +6,4 @@ import { default as yield } from "somemodule"; // no error import { default as default } from "somemodule"; // default as is ok, error of default binding name //// [es6ImportNamedImportIdentifiersParsing.js] +export {}; diff --git a/tests/baselines/reference/es6ImportWithoutFromClauseNonInstantiatedModule.js b/tests/baselines/reference/es6ImportWithoutFromClauseNonInstantiatedModule.js index 8fa9de0a98dd4..91991b089f24a 100644 --- a/tests/baselines/reference/es6ImportWithoutFromClauseNonInstantiatedModule.js +++ b/tests/baselines/reference/es6ImportWithoutFromClauseNonInstantiatedModule.js @@ -8,6 +8,7 @@ export interface i { import "es6ImportWithoutFromClauseNonInstantiatedModule_0"; //// [es6ImportWithoutFromClauseNonInstantiatedModule_0.js] +export {}; //// [es6ImportWithoutFromClauseNonInstantiatedModule_1.js] import "es6ImportWithoutFromClauseNonInstantiatedModule_0"; diff --git a/tests/baselines/reference/es6modulekindWithES5Target10.js b/tests/baselines/reference/es6modulekindWithES5Target10.js index 037ff12a0b086..751ec232d784f 100644 --- a/tests/baselines/reference/es6modulekindWithES5Target10.js +++ b/tests/baselines/reference/es6modulekindWithES5Target10.js @@ -7,3 +7,4 @@ namespace N { export = N; // Error //// [es6modulekindWithES5Target10.js] +export {}; diff --git a/tests/baselines/reference/esnextmodulekindWithES5Target10.js b/tests/baselines/reference/esnextmodulekindWithES5Target10.js index e68e1d72b982b..fb174782d2d50 100644 --- a/tests/baselines/reference/esnextmodulekindWithES5Target10.js +++ b/tests/baselines/reference/esnextmodulekindWithES5Target10.js @@ -7,3 +7,4 @@ namespace N { export = N; // Error //// [esnextmodulekindWithES5Target10.js] +export {}; diff --git a/tests/baselines/reference/exportAssignmentWithoutAllowSyntheticDefaultImportsError.js b/tests/baselines/reference/exportAssignmentWithoutAllowSyntheticDefaultImportsError.js index 5d433cba79b71..4611641fe7ab7 100644 --- a/tests/baselines/reference/exportAssignmentWithoutAllowSyntheticDefaultImportsError.js +++ b/tests/baselines/reference/exportAssignmentWithoutAllowSyntheticDefaultImportsError.js @@ -9,4 +9,6 @@ import bar from './bar'; //// [bar.js] function bar() { } +export {}; //// [foo.js] +export {}; diff --git a/tests/baselines/reference/exportDefaultForNonInstantiatedModule.js b/tests/baselines/reference/exportDefaultForNonInstantiatedModule.js index c76a7b0806db5..855e0de07f98f 100644 --- a/tests/baselines/reference/exportDefaultForNonInstantiatedModule.js +++ b/tests/baselines/reference/exportDefaultForNonInstantiatedModule.js @@ -7,3 +7,4 @@ module m { export default m; //// [exportDefaultForNonInstantiatedModule.js] +export {}; diff --git a/tests/baselines/reference/importCallExpressionIncorrect2.js b/tests/baselines/reference/importCallExpressionIncorrect2.js index 4f0fa0314870b..f27acf1ac7b0c 100644 --- a/tests/baselines/reference/importCallExpressionIncorrect2.js +++ b/tests/baselines/reference/importCallExpressionIncorrect2.js @@ -11,3 +11,4 @@ var x = import { foo } from './0'; export function foo() { return "foo"; } //// [1.js] var x = ; +export {}; diff --git a/tests/baselines/reference/importMeta(module=esnext,target=es5).js b/tests/baselines/reference/importMeta(module=esnext,target=es5).js index 522b0f016615d..724ad5b3aca1b 100644 --- a/tests/baselines/reference/importMeta(module=esnext,target=es5).js +++ b/tests/baselines/reference/importMeta(module=esnext,target=es5).js @@ -95,6 +95,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) { } }); }); })(); +export {}; //// [moduleLookingFile01.js] export var x = import.meta; export var y = import.metal; @@ -103,6 +104,7 @@ export var z = import.import.import.malkovich; var globalA = import.meta; var globalB = import.metal; var globalC = import.import.import.malkovich; +export {}; //// [assignmentTargets.js] export var foo = import.meta.blah = import.meta.blue = import.meta; import.meta = foo; diff --git a/tests/baselines/reference/importMeta(module=esnext,target=esnext).js b/tests/baselines/reference/importMeta(module=esnext,target=esnext).js index 4b6fa5436f567..26c4c3e5cc203 100644 --- a/tests/baselines/reference/importMeta(module=esnext,target=esnext).js +++ b/tests/baselines/reference/importMeta(module=esnext,target=esnext).js @@ -49,6 +49,7 @@ const { a, b, c } = import.meta.wellKnownProperty; image.width = image.height = size; document.body.appendChild(image); })(); +export {}; //// [moduleLookingFile01.js] export let x = import.meta; export let y = import.metal; @@ -57,6 +58,7 @@ export let z = import.import.import.malkovich; let globalA = import.meta; let globalB = import.metal; let globalC = import.import.import.malkovich; +export {}; //// [assignmentTargets.js] export const foo = import.meta.blah = import.meta.blue = import.meta; import.meta = foo; diff --git a/tests/baselines/reference/importNonExportedMember6.js b/tests/baselines/reference/importNonExportedMember6.js index 9866fbd54b00e..c173228e9926e 100644 --- a/tests/baselines/reference/importNonExportedMember6.js +++ b/tests/baselines/reference/importNonExportedMember6.js @@ -13,4 +13,6 @@ var Foo = /** @class */ (function () { } return Foo; }()); +export {}; //// [b.js] +export {}; diff --git a/tests/baselines/reference/importNonExportedMember7.js b/tests/baselines/reference/importNonExportedMember7.js index 24beab599900c..b376ed3de7a04 100644 --- a/tests/baselines/reference/importNonExportedMember7.js +++ b/tests/baselines/reference/importNonExportedMember7.js @@ -13,4 +13,6 @@ var Foo = /** @class */ (function () { } return Foo; }()); +export {}; //// [b.js] +export {}; diff --git a/tests/baselines/reference/instantiateContextualTypes.js b/tests/baselines/reference/instantiateContextualTypes.js index 46d87cb11538b..e5be49b98fd83 100644 --- a/tests/baselines/reference/instantiateContextualTypes.js +++ b/tests/baselines/reference/instantiateContextualTypes.js @@ -236,3 +236,4 @@ let obj = { foo(bar) { } }; assignPartial(obj, { foo(...args) { } }); // args has type [string] +export {}; diff --git a/tests/baselines/reference/isolatedModulesDontElideReExportStar.js b/tests/baselines/reference/isolatedModulesDontElideReExportStar.js index 4d546135f749f..d689594fdcb92 100644 --- a/tests/baselines/reference/isolatedModulesDontElideReExportStar.js +++ b/tests/baselines/reference/isolatedModulesDontElideReExportStar.js @@ -8,5 +8,6 @@ export * from "./a"; //// [a.js] +export {}; //// [b.js] export * from "./a"; diff --git a/tests/baselines/reference/potentiallyUncalledDecorators.js b/tests/baselines/reference/potentiallyUncalledDecorators.js index fe46a41c41ba6..29c4199cd858e 100644 --- a/tests/baselines/reference/potentiallyUncalledDecorators.js +++ b/tests/baselines/reference/potentiallyUncalledDecorators.js @@ -217,3 +217,4 @@ let G = /** @class */ (() => { ], G); return G; })(); +export {}; diff --git a/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEs2015.js b/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEs2015.js index 12d02801bb242..60bb84c2b3d5c 100644 --- a/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEs2015.js +++ b/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEs2015.js @@ -15,3 +15,4 @@ import * as b from './b.json'; "b": "hello" } //// [out/file1.js] +export {}; diff --git a/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEsNext.js b/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEsNext.js index f3bcbc546fee1..2f37693eed3b5 100644 --- a/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEsNext.js +++ b/tests/baselines/reference/requireOfJsonFileWithModuleNodeResolutionEmitEsNext.js @@ -15,3 +15,4 @@ import * as b from './b.json'; "b": "hello" } //// [out/file1.js] +export {}; diff --git a/tests/baselines/reference/scannerUnicodeEscapeInKeyword2.js b/tests/baselines/reference/scannerUnicodeEscapeInKeyword2.js index d93ba71d1d312..eda3976c557f1 100644 --- a/tests/baselines/reference/scannerUnicodeEscapeInKeyword2.js +++ b/tests/baselines/reference/scannerUnicodeEscapeInKeyword2.js @@ -48,6 +48,7 @@ var \u0079ield = 12; // ok function* gen() { yield 12; //not ok } +export {}; //// [file2.js] var x = "hello"; // not ok var \u{0061}wait = 12; // ok @@ -59,4 +60,5 @@ function* gen() { yield 12; //not ok } const a = { def\u0061ult: 12 }; // OK, `default` not in keyword position +export {}; // chrome and jsc may still error on this, ref https://bugs.chromium.org/p/chromium/issues/detail?id=993000 and https://bugs.webkit.org/show_bug.cgi?id=200638 diff --git a/tests/baselines/reference/strictModeWordInImportDeclaration.js b/tests/baselines/reference/strictModeWordInImportDeclaration.js index 4a335d8a744d9..6ba7aa85c02fb 100644 --- a/tests/baselines/reference/strictModeWordInImportDeclaration.js +++ b/tests/baselines/reference/strictModeWordInImportDeclaration.js @@ -6,3 +6,4 @@ import public from "./1" //// [strictModeWordInImportDeclaration.js] "use strict"; +export {}; diff --git a/tests/baselines/reference/taggedTemplatesWithTypeArguments2.js b/tests/baselines/reference/taggedTemplatesWithTypeArguments2.js index bc170467fa8be..6ab9b7afa1d9f 100644 --- a/tests/baselines/reference/taggedTemplatesWithTypeArguments2.js +++ b/tests/baselines/reference/taggedTemplatesWithTypeArguments2.js @@ -59,3 +59,4 @@ class SomeDerived extends SomeBase { super. `hello world`; } } +export {}; diff --git a/tests/baselines/reference/tsxResolveExternalModuleExportsTypes.js b/tests/baselines/reference/tsxResolveExternalModuleExportsTypes.js index e9aa4de50a199..97ec3b5aa6f46 100644 --- a/tests/baselines/reference/tsxResolveExternalModuleExportsTypes.js +++ b/tests/baselines/reference/tsxResolveExternalModuleExportsTypes.js @@ -24,3 +24,4 @@ const Foo = (

); //// [foo.jsx] var Foo = (

); +export {}; diff --git a/tests/baselines/reference/umdGlobalAugmentationNoCrash.js b/tests/baselines/reference/umdGlobalAugmentationNoCrash.js index 23c2344a33a30..28b68ee250683 100644 --- a/tests/baselines/reference/umdGlobalAugmentationNoCrash.js +++ b/tests/baselines/reference/umdGlobalAugmentationNoCrash.js @@ -21,6 +21,7 @@ React.foo; //// [some_module.js] React.foo; +export {}; //// [emits.js] "use strict"; console.log("hello"); diff --git a/tests/baselines/reference/umdNamespaceMergedWithGlobalAugmentationIsNotCircular.js b/tests/baselines/reference/umdNamespaceMergedWithGlobalAugmentationIsNotCircular.js index 377082fa5d53d..ac165ac0a8017 100644 --- a/tests/baselines/reference/umdNamespaceMergedWithGlobalAugmentationIsNotCircular.js +++ b/tests/baselines/reference/umdNamespaceMergedWithGlobalAugmentationIsNotCircular.js @@ -25,6 +25,7 @@ React.createRef; //// [some_module.js] React.createRef; +export {}; //// [emits.js] "use strict"; console.log("hello"); diff --git a/tests/baselines/reference/unionAndIntersectionInference3.js b/tests/baselines/reference/unionAndIntersectionInference3.js index 12ce5a9a19bdf..c39914f090209 100644 --- a/tests/baselines/reference/unionAndIntersectionInference3.js +++ b/tests/baselines/reference/unionAndIntersectionInference3.js @@ -105,3 +105,4 @@ let x2 = foo2(sa); // unknown let y2 = foo2(sx); // { extra: number } withRouter(MyComponent); let z = foo(ab); // [AB, string] +export {}; diff --git a/tests/baselines/reference/unionTypeInference.js b/tests/baselines/reference/unionTypeInference.js index c5157165eae32..d79e0b0df8203 100644 --- a/tests/baselines/reference/unionTypeInference.js +++ b/tests/baselines/reference/unionTypeInference.js @@ -111,3 +111,4 @@ async function fun(deepPromised) { } } baz(xx); +export {};