Skip to content

Fix namespace import/export helper usage under '--esModuleInterop' #39490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35287,6 +35287,10 @@ namespace ts {
if (importClause.namedBindings) {
if (importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
checkImportBinding(importClause.namedBindings);
if (moduleKind !== ModuleKind.System && moduleKind < ModuleKind.ES2015 && compilerOptions.esModuleInterop) {
// import * as ns from "foo";
checkExternalEmitHelpers(node, ExternalEmitHelpers.ImportStar);
}
}
else {
const moduleExisted = resolveExternalModuleName(node, node.moduleSpecifier);
Expand All @@ -35297,6 +35301,7 @@ namespace ts {
}
}
}

}

function checkImportEqualsDeclaration(node: ImportEqualsDeclaration) {
Expand Down Expand Up @@ -35364,6 +35369,7 @@ namespace ts {
}
else {
// export * from "foo"
// export * as ns from "foo";
const moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier!);
if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) {
error(node.moduleSpecifier, Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol));
Expand All @@ -35372,7 +35378,18 @@ namespace ts {
checkAliasSymbol(node.exportClause);
}
if (moduleKind !== ModuleKind.System && moduleKind < ModuleKind.ES2015) {
checkExternalEmitHelpers(node, ExternalEmitHelpers.ExportStar);
if (node.exportClause) {
// export * as ns from "foo";
// For ES2015 modules, we emit it as a pair of `import * as a_1 ...; export { a_1 as ns }` and don't need the helper.
// We only use the helper here when in esModuleInterop
if (compilerOptions.esModuleInterop) {
checkExternalEmitHelpers(node, ExternalEmitHelpers.ImportStar);
}
}
else {
// export * from "foo"
checkExternalEmitHelpers(node, ExternalEmitHelpers.ExportStar);
}
}
}
}
Expand Down Expand Up @@ -37628,6 +37645,7 @@ namespace ts {
case ExternalEmitHelpers.AsyncDelegator: return "__asyncDelegator";
case ExternalEmitHelpers.AsyncValues: return "__asyncValues";
case ExternalEmitHelpers.ExportStar: return "__exportStar";
case ExternalEmitHelpers.ImportStar: return "__importStar";
case ExternalEmitHelpers.MakeTemplateObject: return "__makeTemplateObject";
case ExternalEmitHelpers.ClassPrivateFieldGet: return "__classPrivateFieldGet";
case ExternalEmitHelpers.ClassPrivateFieldSet: return "__classPrivateFieldSet";
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,9 @@ namespace ts {
factory.createExpressionStatement(
createExportExpression(
factory.cloneNode(node.exportClause.name),
moduleKind !== ModuleKind.AMD ?
getHelperExpressionForExport(node, createRequireCall(node)) :
factory.createIdentifier(idText(node.exportClause.name))
getHelperExpressionForExport(node, moduleKind !== ModuleKind.AMD ?
createRequireCall(node) :
factory.createIdentifier(idText(node.exportClause.name)))
)
),
node
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/transformers/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ namespace ts {
uniqueExports.set(idText(name), true);
exportedNames = append(exportedNames, name);
}
// we use the same helpers for `export * as ns` as we do for `import * as ns`
hasImportStar = true;
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6414,10 +6414,11 @@ namespace ts {
AsyncDelegator = 1 << 14, // __asyncDelegator (used by ES2017 async generator yield* transformation)
AsyncValues = 1 << 15, // __asyncValues (used by ES2017 for..await..of transformation)
ExportStar = 1 << 16, // __exportStar (used by CommonJS/AMD/UMD module transformation)
MakeTemplateObject = 1 << 17, // __makeTemplateObject (used for constructing template string array objects)
ClassPrivateFieldGet = 1 << 18, // __classPrivateFieldGet (used by the class private field transformation)
ClassPrivateFieldSet = 1 << 19, // __classPrivateFieldSet (used by the class private field transformation)
CreateBinding = 1 << 20, // __createBinding (use by the module transform for (re)exports and namespace imports)
ImportStar = 1 << 17, // __importStar (used by CommonJS/AMD/UMD module transformation)
MakeTemplateObject = 1 << 18, // __makeTemplateObject (used for constructing template string array objects)
ClassPrivateFieldGet = 1 << 19, // __classPrivateFieldGet (used by the class private field transformation)
ClassPrivateFieldSet = 1 << 20, // __classPrivateFieldSet (used by the class private field transformation)
CreateBinding = 1 << 21, // __createBinding (use by the module transform for (re)exports and namespace imports)
FirstEmitHelper = Extends,
LastEmitHelper = CreateBinding,

Expand Down
3 changes: 1 addition & 2 deletions src/testRunner/compilerRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,12 @@ namespace Harness {
let configuredName = "";
const keys = Object
.keys(configurationOverrides)
.map(k => k.toLowerCase())
.sort();
for (const key of keys) {
if (configuredName) {
configuredName += ",";
}
configuredName += `${key}=${configurationOverrides[key].toLowerCase()}`;
configuredName += `${key.toLowerCase()}=${configurationOverrides[key].toLowerCase()}`;
}
if (configuredName) {
const extname = vpath.extname(this.justName);
Expand Down
23 changes: 23 additions & 0 deletions tests/baselines/reference/esModuleInteropTslibHelpers.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
tests/cases/compiler/file2.ts(1,1): error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.


==== tests/cases/compiler/refs.d.ts (0 errors) ====
declare module "path";
==== tests/cases/compiler/file.ts (0 errors) ====
import path from "path";
path.resolve("", "../");
export class Foo { }
==== tests/cases/compiler/file2.ts (1 errors) ====
import * as path from "path";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
path.resolve("", "../");
export class Foo2 { }
==== tests/cases/compiler/file3.ts (0 errors) ====
import {default as resolve} from "path";
resolve("", "../");
export class Foo3 { }
==== tests/cases/compiler/file4.ts (0 errors) ====
import {Bar, default as resolve} from "path";
resolve("", "../");
export { Bar }
21 changes: 20 additions & 1 deletion tests/baselines/reference/exportAsNamespace2(module=amd).js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,30 @@ define(["require", "exports"], function (require, exports) {
exports.b = 2;
});
//// [1.js]
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
define(["require", "exports", "./0"], function (require, exports, ns) {
"use strict";
exports.__esModule = true;
exports.ns = void 0;
exports.ns = ns;
exports.ns = __importStar(ns);
ns.a;
ns.b;
});
Expand Down
21 changes: 20 additions & 1 deletion tests/baselines/reference/exportAsNamespace3(module=amd).js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,30 @@ define(["require", "exports"], function (require, exports) {
exports.b = 2;
});
//// [1.js]
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
define(["require", "exports", "./0"], function (require, exports, ns) {
"use strict";
exports.__esModule = true;
exports.ns = void 0;
exports.ns = ns;
exports.ns = __importStar(ns);
ns.a;
ns.b;
var ns = { a: 1, b: 2 };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/importHelpersWithExportStarAs.ts] ////

//// [a.ts]
export class A { }

//// [b.ts]
export * as a from "./a";

//// [tslib.d.ts]
declare module "tslib" {
function __importStar(m: any): void;
}

//// [a.js]
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.A = void 0;
class A {
}
exports.A = A;
});
//// [b.js]
define(["require", "exports", "./a"], function (require, exports, a) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = void 0;
exports.a = a;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
export class A { }
>A : Symbol(A, Decl(a.ts, 0, 0))

=== tests/cases/compiler/b.ts ===
export * as a from "./a";
>a : Symbol(a, Decl(b.ts, 0, 6))

=== tests/cases/compiler/tslib.d.ts ===
declare module "tslib" {
>"tslib" : Symbol("tslib", Decl(tslib.d.ts, --, --))

function __importStar(m: any): void;
>__importStar : Symbol(__importStar, Decl(tslib.d.ts, --, --))
>m : Symbol(m, Decl(tslib.d.ts, --, --))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
export class A { }
>A : A

=== tests/cases/compiler/b.ts ===
export * as a from "./a";
>a : typeof a

=== tests/cases/compiler/tslib.d.ts ===
declare module "tslib" {
>"tslib" : typeof import("tslib")

function __importStar(m: any): void;
>__importStar : (m: any) => void
>m : any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/importHelpersWithExportStarAs.ts] ////

//// [a.ts]
export class A { }

//// [b.ts]
export * as a from "./a";

//// [tslib.d.ts]
declare module "tslib" {
function __importStar(m: any): void;
}

//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.A = void 0;
class A {
}
exports.A = A;
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = void 0;
exports.a = require("./a");
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
export class A { }
>A : Symbol(A, Decl(a.ts, 0, 0))

=== tests/cases/compiler/b.ts ===
export * as a from "./a";
>a : Symbol(a, Decl(b.ts, 0, 6))

=== tests/cases/compiler/tslib.d.ts ===
declare module "tslib" {
>"tslib" : Symbol("tslib", Decl(tslib.d.ts, --, --))

function __importStar(m: any): void;
>__importStar : Symbol(__importStar, Decl(tslib.d.ts, --, --))
>m : Symbol(m, Decl(tslib.d.ts, --, --))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
export class A { }
>A : A

=== tests/cases/compiler/b.ts ===
export * as a from "./a";
>a : typeof a

=== tests/cases/compiler/tslib.d.ts ===
declare module "tslib" {
>"tslib" : typeof import("tslib")

function __importStar(m: any): void;
>__importStar : (m: any) => void
>m : any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/importHelpersWithExportStarAs.ts] ////

//// [a.ts]
export class A { }

//// [b.ts]
export * as a from "./a";

//// [tslib.d.ts]
declare module "tslib" {
function __importStar(m: any): void;
}

//// [a.js]
export class A {
}
//// [b.js]
import * as a_1 from "./a";
export { a_1 as a };
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
export class A { }
>A : Symbol(A, Decl(a.ts, 0, 0))

=== tests/cases/compiler/b.ts ===
export * as a from "./a";
>a : Symbol(a, Decl(b.ts, 0, 6))

=== tests/cases/compiler/tslib.d.ts ===
declare module "tslib" {
>"tslib" : Symbol("tslib", Decl(tslib.d.ts, --, --))

function __importStar(m: any): void;
>__importStar : Symbol(__importStar, Decl(tslib.d.ts, --, --))
>m : Symbol(m, Decl(tslib.d.ts, --, --))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/a.ts ===
export class A { }
>A : A

=== tests/cases/compiler/b.ts ===
export * as a from "./a";
>a : typeof a

=== tests/cases/compiler/tslib.d.ts ===
declare module "tslib" {
>"tslib" : typeof import("tslib")

function __importStar(m: any): void;
>__importStar : (m: any) => void
>m : any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/importHelpersWithExportStarAs.ts] ////

//// [a.ts]
export class A { }

//// [b.ts]
export * as a from "./a";

//// [tslib.d.ts]
declare module "tslib" {
function __importStar(m: any): void;
}

//// [a.js]
export class A {
}
//// [b.js]
export * as a from "./a";
Loading