Skip to content

Commit

Permalink
Preserve module marker in es2015+ module emit for tool compatability (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham authored May 21, 2020
1 parent 62675a5 commit 0018b8f
Show file tree
Hide file tree
Showing 36 changed files with 67 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/compiler/transformers/module/esnextAnd2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Node> {
switch (node.kind) {
case SyntaxKind.ImportEqualsDeclaration:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ var M;
async function f1() { }
M.f1 = f1;
})(M || (M = {}));
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/asyncAwaitIsolatedModules_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,4 @@ var M;
}
M.f1 = f1;
})(M || (M = {}));
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/computedPropertyName.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ class F {
}
class G {
}
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ let Decorated = /** @class */ (() => {
})();
export default Decorated;
//// [undecorated.js]
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/es6ExportAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export = a;

//// [es6ExportAssignment.js]
var a = 10;
export {};
2 changes: 2 additions & 0 deletions tests/baselines/reference/es6ExportAssignment2.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ import * as a from "a";

//// [a.js]
var a = 10;
export {};
//// [b.js]
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/es6ExportAssignment3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import * as a from "a";


//// [b.js]
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/es6ExportAssignment4.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ import * as a from "a";


//// [b.js]
export {};
2 changes: 2 additions & 0 deletions tests/baselines/reference/es6ImportEqualsDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ import a = require("server");

//// [server.js]
var a = 10;
export {};
//// [client.js]
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/es6ImportEqualsDeclaration2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ import {a} from "server";


//// [client.js]
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var a = /** @class */ (function () {
}
return a;
}());
export {};
//// [main.js]
import * as a from "./a";
a;
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface i {
import "es6ImportWithoutFromClauseNonInstantiatedModule_0";

//// [es6ImportWithoutFromClauseNonInstantiatedModule_0.js]
export {};
//// [es6ImportWithoutFromClauseNonInstantiatedModule_1.js]
import "es6ImportWithoutFromClauseNonInstantiatedModule_0";

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/es6modulekindWithES5Target10.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ namespace N {
export = N; // Error

//// [es6modulekindWithES5Target10.js]
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ namespace N {
export = N; // Error

//// [esnextmodulekindWithES5Target10.js]
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ import bar from './bar';

//// [bar.js]
function bar() { }
export {};
//// [foo.js]
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ module m {
export default m;

//// [exportDefaultForNonInstantiatedModule.js]
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ var x = import { foo } from './0';
export function foo() { return "foo"; }
//// [1.js]
var x = ;
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/importNonExportedMember6.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ var Foo = /** @class */ (function () {
}
return Foo;
}());
export {};
//// [b.js]
export {};
2 changes: 2 additions & 0 deletions tests/baselines/reference/importNonExportedMember7.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ var Foo = /** @class */ (function () {
}
return Foo;
}());
export {};
//// [b.js]
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/instantiateContextualTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,4 @@ let obj = {
foo(bar) { }
};
assignPartial(obj, { foo(...args) { } }); // args has type [string]
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export * from "./a";


//// [a.js]
export {};
//// [b.js]
export * from "./a";
1 change: 1 addition & 0 deletions tests/baselines/reference/potentiallyUncalledDecorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,4 @@ let G = /** @class */ (() => {
], G);
return G;
})();
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import * as b from './b.json';
"b": "hello"
}
//// [out/file1.js]
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import * as b from './b.json';
"b": "hello"
}
//// [out/file1.js]
export {};
2 changes: 2 additions & 0 deletions tests/baselines/reference/scannerUnicodeEscapeInKeyword2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ import public from "./1"

//// [strictModeWordInImportDeclaration.js]
"use strict";
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ class SomeDerived extends SomeBase {
super. `hello world`;
}
}
export {};
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ const Foo = (<h1></h1>);

//// [foo.jsx]
var Foo = (<h1></h1>);
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/umdGlobalAugmentationNoCrash.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ React.foo;

//// [some_module.js]
React.foo;
export {};
//// [emits.js]
"use strict";
console.log("hello");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ React.createRef;

//// [some_module.js]
React.createRef;
export {};
//// [emits.js]
"use strict";
console.log("hello");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ let x2 = foo2(sa); // unknown
let y2 = foo2(sx); // { extra: number }
withRouter(MyComponent);
let z = foo(ab); // [AB<string>, string]
export {};
1 change: 1 addition & 0 deletions tests/baselines/reference/unionTypeInference.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ async function fun(deepPromised) {
}
}
baz(xx);
export {};

0 comments on commit 0018b8f

Please sign in to comment.