Skip to content

Commit 4a6ae58

Browse files
authored
Merge pull request #16370 from Microsoft/release-2.4_manuallyPortTheFlagToIncrementalSourceFile
[Release 2.4] Manually port the flag to incremental source file
2 parents 4ca158b + b04c8f3 commit 4a6ae58

File tree

6 files changed

+191
-4
lines changed

6 files changed

+191
-4
lines changed

src/compiler/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,11 @@ namespace ts {
481481
// becoming detached from any SourceFile). It is recommended that this SourceFile not
482482
// be used once 'update' is called on it.
483483
export function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile {
484-
return IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks);
484+
const newSourceFile = IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks);
485+
// Because new source file node is created, it may not have the flag PossiblyContainDynamicImport. This is the case if there is no new edit to add dynamic import.
486+
// We will manually port the flag to the new source file.
487+
newSourceFile.flags |= (sourceFile.flags & NodeFlags.PossiblyContainDynamicImport);
488+
return newSourceFile;
485489
}
486490

487491
/* @internal */

src/compiler/transformers/module/module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,14 @@ namespace ts {
514514

515515
function visitImportCallExpression(node: ImportCall): Expression {
516516
switch (compilerOptions.module) {
517-
case ModuleKind.CommonJS:
518-
return transformImportCallExpressionCommonJS(node);
519517
case ModuleKind.AMD:
520518
return transformImportCallExpressionAMD(node);
521519
case ModuleKind.UMD:
522520
return transformImportCallExpressionUMD(node);
521+
case ModuleKind.CommonJS:
522+
default:
523+
return transformImportCallExpressionCommonJS(node);
523524
}
524-
Debug.fail("All supported module kind in this transformation step should have been handled");
525525
}
526526

527527
function transformImportCallExpressionUMD(node: ImportCall): Expression {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error TS2468: Cannot find global value 'Promise'.
2+
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.
3+
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.
4+
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.
5+
6+
7+
!!! error TS2468: Cannot find global value 'Promise'.
8+
==== tests/cases/conformance/dynamicImport/0.ts (0 errors) ====
9+
export class B {
10+
print() { return "I am B"}
11+
}
12+
13+
export function foo() { return "foo" }
14+
15+
==== tests/cases/conformance/dynamicImport/1.ts (0 errors) ====
16+
export function backup() { return "backup"; }
17+
18+
==== tests/cases/conformance/dynamicImport/2.ts (3 errors) ====
19+
declare var console: any;
20+
class C {
21+
private myModule = import("./0");
22+
~~~~~~~~~~~~~
23+
!!! 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.
24+
method() {
25+
this.myModule.then(Zero => {
26+
console.log(Zero.foo());
27+
}, async err => {
28+
~~~~~~~~~~~~~~
29+
!!! 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.
30+
console.log(err);
31+
let one = await import("./1");
32+
~~~~~~~~~~~~~
33+
!!! 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.
34+
console.log(one.backup());
35+
});
36+
}
37+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//// [tests/cases/conformance/dynamicImport/importCallExpressionNoModuleKindSpecified.ts] ////
2+
3+
//// [0.ts]
4+
export class B {
5+
print() { return "I am B"}
6+
}
7+
8+
export function foo() { return "foo" }
9+
10+
//// [1.ts]
11+
export function backup() { return "backup"; }
12+
13+
//// [2.ts]
14+
declare var console: any;
15+
class C {
16+
private myModule = import("./0");
17+
method() {
18+
this.myModule.then(Zero => {
19+
console.log(Zero.foo());
20+
}, async err => {
21+
console.log(err);
22+
let one = await import("./1");
23+
console.log(one.backup());
24+
});
25+
}
26+
}
27+
28+
//// [0.js]
29+
"use strict";
30+
exports.__esModule = true;
31+
var B = (function () {
32+
function B() {
33+
}
34+
B.prototype.print = function () { return "I am B"; };
35+
return B;
36+
}());
37+
exports.B = B;
38+
function foo() { return "foo"; }
39+
exports.foo = foo;
40+
//// [1.js]
41+
"use strict";
42+
exports.__esModule = true;
43+
function backup() { return "backup"; }
44+
exports.backup = backup;
45+
//// [2.js]
46+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
47+
return new (P || (P = Promise))(function (resolve, reject) {
48+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
49+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
50+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
51+
step((generator = generator.apply(thisArg, _arguments || [])).next());
52+
});
53+
};
54+
var __generator = (this && this.__generator) || function (thisArg, body) {
55+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
56+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
57+
function verb(n) { return function (v) { return step([n, v]); }; }
58+
function step(op) {
59+
if (f) throw new TypeError("Generator is already executing.");
60+
while (_) try {
61+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
62+
if (y = 0, t) op = [0, t.value];
63+
switch (op[0]) {
64+
case 0: case 1: t = op; break;
65+
case 4: _.label++; return { value: op[1], done: false };
66+
case 5: _.label++; y = op[1]; op = [0]; continue;
67+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
68+
default:
69+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
70+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
71+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
72+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
73+
if (t[2]) _.ops.pop();
74+
_.trys.pop(); continue;
75+
}
76+
op = body.call(thisArg, _);
77+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
78+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
79+
}
80+
};
81+
var C = (function () {
82+
function C() {
83+
this.myModule = Promise.resolve().then(function () { return require("./0"); });
84+
}
85+
C.prototype.method = function () {
86+
var _this = this;
87+
this.myModule.then(function (Zero) {
88+
console.log(Zero.foo());
89+
}, function (err) { return __awaiter(_this, void 0, void 0, function () {
90+
var one;
91+
return __generator(this, function (_a) {
92+
switch (_a.label) {
93+
case 0:
94+
console.log(err);
95+
return [4 /*yield*/, Promise.resolve().then(function () { return require("./1"); })];
96+
case 1:
97+
one = _a.sent();
98+
console.log(one.backup());
99+
return [2 /*return*/];
100+
}
101+
});
102+
}); });
103+
};
104+
return C;
105+
}());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @filename: 0.ts
2+
export class B {
3+
print() { return "I am B"}
4+
}
5+
6+
export function foo() { return "foo" }
7+
8+
// @filename: 1.ts
9+
export function backup() { return "backup"; }
10+
11+
// @filename: 2.ts
12+
declare var console: any;
13+
class C {
14+
private myModule = import("./0");
15+
method() {
16+
this.myModule.then(Zero => {
17+
console.log(Zero.foo());
18+
}, async err => {
19+
console.log(err);
20+
let one = await import("./1");
21+
console.log(one.backup());
22+
});
23+
}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
// @lib: es6
4+
5+
// @Filename: ./foo.ts
6+
//// export function bar() { return 1; }
7+
8+
//// var x1 = import("./foo");
9+
//// x1.then(foo => {
10+
//// var s: string = foo.bar();
11+
//// })
12+
//// /*1*/
13+
14+
verify.numberOfErrorsInCurrentFile(1);
15+
goTo.marker("1");
16+
edit.insert(" ");
17+
verify.numberOfErrorsInCurrentFile(1);

0 commit comments

Comments
 (0)