Preserve module marker in es2015+ module emit for tool compatability (#38712)

This commit is contained in:
Wesley Wigham
2020-05-21 14:44:23 -07:00
committed by GitHub
parent 62675a58d6
commit 0018b8f988
36 changed files with 67 additions and 13 deletions

View File

@@ -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:

View File

@@ -71,3 +71,4 @@ var M;
async function f1() { }
M.f1 = f1;
})(M || (M = {}));
export {};

View File

@@ -112,3 +112,4 @@ var M;
}
M.f1 = f1;
})(M || (M = {}));
export {};

View File

@@ -65,3 +65,4 @@ class F {
}
class G {
}
export {};

View File

@@ -27,3 +27,4 @@ let Decorated = /** @class */ (() => {
})();
export default Decorated;
//// [undecorated.js]
export {};

View File

@@ -4,3 +4,4 @@ export = a;
//// [es6ExportAssignment.js]
var a = 10;
export {};

View File

@@ -10,4 +10,6 @@ import * as a from "a";
//// [a.js]
var a = 10;
export {};
//// [b.js]
export {};

View File

@@ -9,3 +9,4 @@ import * as a from "a";
//// [b.js]
export {};

View File

@@ -11,3 +11,4 @@ import * as a from "a";
//// [b.js]
export {};

View File

@@ -9,4 +9,6 @@ import a = require("server");
//// [server.js]
var a = 10;
export {};
//// [client.js]
export {};

View File

@@ -20,3 +20,4 @@ import {a} from "server";
//// [client.js]
export {};

View File

@@ -17,6 +17,7 @@ var a = /** @class */ (function () {
}
return a;
}());
export {};
//// [main.js]
import * as a from "./a";
a;

View File

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

View File

@@ -8,6 +8,7 @@ export interface i {
import "es6ImportWithoutFromClauseNonInstantiatedModule_0";
//// [es6ImportWithoutFromClauseNonInstantiatedModule_0.js]
export {};
//// [es6ImportWithoutFromClauseNonInstantiatedModule_1.js]
import "es6ImportWithoutFromClauseNonInstantiatedModule_0";

View File

@@ -7,3 +7,4 @@ namespace N {
export = N; // Error
//// [es6modulekindWithES5Target10.js]
export {};

View File

@@ -7,3 +7,4 @@ namespace N {
export = N; // Error
//// [esnextmodulekindWithES5Target10.js]
export {};

View File

@@ -9,4 +9,6 @@ import bar from './bar';
//// [bar.js]
function bar() { }
export {};
//// [foo.js]
export {};

View File

@@ -7,3 +7,4 @@ module m {
export default m;
//// [exportDefaultForNonInstantiatedModule.js]
export {};

View File

@@ -11,3 +11,4 @@ var x = import { foo } from './0';
export function foo() { return "foo"; }
//// [1.js]
var x = ;
export {};

View File

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

View File

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

View File

@@ -13,4 +13,6 @@ var Foo = /** @class */ (function () {
}
return Foo;
}());
export {};
//// [b.js]
export {};

View File

@@ -13,4 +13,6 @@ var Foo = /** @class */ (function () {
}
return Foo;
}());
export {};
//// [b.js]
export {};

View File

@@ -236,3 +236,4 @@ let obj = {
foo(bar) { }
};
assignPartial(obj, { foo(...args) { } }); // args has type [string]
export {};

View File

@@ -8,5 +8,6 @@ export * from "./a";
//// [a.js]
export {};
//// [b.js]
export * from "./a";

View File

@@ -217,3 +217,4 @@ let G = /** @class */ (() => {
], G);
return G;
})();
export {};

View File

@@ -15,3 +15,4 @@ import * as b from './b.json';
"b": "hello"
}
//// [out/file1.js]
export {};

View File

@@ -15,3 +15,4 @@ import * as b from './b.json';
"b": "hello"
}
//// [out/file1.js]
export {};

View File

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

View File

@@ -6,3 +6,4 @@ import public from "./1"
//// [strictModeWordInImportDeclaration.js]
"use strict";
export {};

View File

@@ -59,3 +59,4 @@ class SomeDerived extends SomeBase {
super. `hello world`;
}
}
export {};

View File

@@ -24,3 +24,4 @@ const Foo = (<h1></h1>);
//// [foo.jsx]
var Foo = (<h1></h1>);
export {};

View File

@@ -21,6 +21,7 @@ React.foo;
//// [some_module.js]
React.foo;
export {};
//// [emits.js]
"use strict";
console.log("hello");

View File

@@ -25,6 +25,7 @@ React.createRef;
//// [some_module.js]
React.createRef;
export {};
//// [emits.js]
"use strict";
console.log("hello");

View File

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

View File

@@ -111,3 +111,4 @@ async function fun(deepPromised) {
}
}
baz(xx);
export {};