diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index bf0c7dbf7b9..6141f52f5f4 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -514,14 +514,14 @@ namespace ts { function visitImportCallExpression(node: ImportCall): Expression { switch (compilerOptions.module) { - case ModuleKind.CommonJS: - return transformImportCallExpressionCommonJS(node); case ModuleKind.AMD: return transformImportCallExpressionAMD(node); case ModuleKind.UMD: return transformImportCallExpressionUMD(node); + case ModuleKind.CommonJS: + default: + return transformImportCallExpressionCommonJS(node); } - Debug.fail("All supported module kind in this transformation step should have been handled"); } function transformImportCallExpressionUMD(node: ImportCall): Expression { diff --git a/tests/baselines/reference/importCallExpressionNoModuleKindSpecified.errors.txt b/tests/baselines/reference/importCallExpressionNoModuleKindSpecified.errors.txt new file mode 100644 index 00000000000..cf665dda049 --- /dev/null +++ b/tests/baselines/reference/importCallExpressionNoModuleKindSpecified.errors.txt @@ -0,0 +1,37 @@ +error TS2468: Cannot find global value 'Promise'. +tests/cases/conformance/dynamicImport/2.ts(3,24): error TS2712: A dynamic import call in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option. +tests/cases/conformance/dynamicImport/2.ts(7,12): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option. +tests/cases/conformance/dynamicImport/2.ts(9,29): error TS2712: A dynamic import call in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option. + + +!!! error TS2468: Cannot find global value 'Promise'. +==== tests/cases/conformance/dynamicImport/0.ts (0 errors) ==== + export class B { + print() { return "I am B"} + } + + export function foo() { return "foo" } + +==== tests/cases/conformance/dynamicImport/1.ts (0 errors) ==== + export function backup() { return "backup"; } + +==== tests/cases/conformance/dynamicImport/2.ts (3 errors) ==== + declare var console: any; + class C { + private myModule = import("./0"); + ~~~~~~~~~~~~~ +!!! error TS2712: A dynamic import call in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option. + method() { + this.myModule.then(Zero => { + console.log(Zero.foo()); + }, async err => { + ~~~~~~~~~~~~~~ +!!! error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option. + console.log(err); + let one = await import("./1"); + ~~~~~~~~~~~~~ +!!! error TS2712: A dynamic import call in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option. + console.log(one.backup()); + }); + } + } \ No newline at end of file diff --git a/tests/baselines/reference/importCallExpressionNoModuleKindSpecified.js b/tests/baselines/reference/importCallExpressionNoModuleKindSpecified.js new file mode 100644 index 00000000000..487d4e03a6c --- /dev/null +++ b/tests/baselines/reference/importCallExpressionNoModuleKindSpecified.js @@ -0,0 +1,105 @@ +//// [tests/cases/conformance/dynamicImport/importCallExpressionNoModuleKindSpecified.ts] //// + +//// [0.ts] +export class B { + print() { return "I am B"} +} + +export function foo() { return "foo" } + +//// [1.ts] +export function backup() { return "backup"; } + +//// [2.ts] +declare var console: any; +class C { + private myModule = import("./0"); + method() { + this.myModule.then(Zero => { + console.log(Zero.foo()); + }, async err => { + console.log(err); + let one = await import("./1"); + console.log(one.backup()); + }); + } +} + +//// [0.js] +"use strict"; +exports.__esModule = true; +var B = (function () { + function B() { + } + B.prototype.print = function () { return "I am B"; }; + return B; +}()); +exports.B = B; +function foo() { return "foo"; } +exports.foo = foo; +//// [1.js] +"use strict"; +exports.__esModule = true; +function backup() { return "backup"; } +exports.backup = backup; +//// [2.js] +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [0, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var C = (function () { + function C() { + this.myModule = Promise.resolve().then(function () { return require("./0"); }); + } + C.prototype.method = function () { + var _this = this; + this.myModule.then(function (Zero) { + console.log(Zero.foo()); + }, function (err) { return __awaiter(_this, void 0, void 0, function () { + var one; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + console.log(err); + return [4 /*yield*/, Promise.resolve().then(function () { return require("./1"); })]; + case 1: + one = _a.sent(); + console.log(one.backup()); + return [2 /*return*/]; + } + }); + }); }); + }; + return C; +}()); diff --git a/tests/cases/conformance/dynamicImport/importCallExpressionNoModuleKindSpecified.ts b/tests/cases/conformance/dynamicImport/importCallExpressionNoModuleKindSpecified.ts new file mode 100644 index 00000000000..2d2f54e00b1 --- /dev/null +++ b/tests/cases/conformance/dynamicImport/importCallExpressionNoModuleKindSpecified.ts @@ -0,0 +1,24 @@ +// @filename: 0.ts +export class B { + print() { return "I am B"} +} + +export function foo() { return "foo" } + +// @filename: 1.ts +export function backup() { return "backup"; } + +// @filename: 2.ts +declare var console: any; +class C { + private myModule = import("./0"); + method() { + this.myModule.then(Zero => { + console.log(Zero.foo()); + }, async err => { + console.log(err); + let one = await import("./1"); + console.log(one.backup()); + }); + } +} \ No newline at end of file