Skip to content

Commit 72d742b

Browse files
committed
Fix prologue order in async function
1 parent f5adadb commit 72d742b

File tree

6 files changed

+156
-13
lines changed

6 files changed

+156
-13
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,6 +1831,7 @@ namespace ts {
18311831
let statementsLocation: TextRange;
18321832
let closeBraceLocation: TextRange;
18331833

1834+
const leadingStatements: Statement[] = [];
18341835
const statements: Statement[] = [];
18351836
const body = node.body;
18361837
let statementOffset: number;
@@ -1839,21 +1840,16 @@ namespace ts {
18391840
if (isBlock(body)) {
18401841
// ensureUseStrict is false because no new prologue-directive should be added.
18411842
// addStandardPrologue will put already-existing directives at the beginning of the target statement-array
1842-
statementOffset = addStandardPrologue(statements, body.statements, /*ensureUseStrict*/ false);
1843+
statementOffset = addStandardPrologue(leadingStatements, body.statements, /*ensureUseStrict*/ false);
18431844
}
18441845

1845-
addCaptureThisForNodeIfNeeded(statements, node);
1846-
addDefaultValueAssignmentsIfNeeded(statements, node);
1847-
addRestParameterIfNeeded(statements, node, /*inConstructorWithSynthesizedSuper*/ false);
1848-
1849-
// If we added any generated statements, this must be a multi-line block.
1850-
if (!multiLine && statements.length > 0) {
1851-
multiLine = true;
1852-
}
1846+
addCaptureThisForNodeIfNeeded(leadingStatements, node);
1847+
addDefaultValueAssignmentsIfNeeded(leadingStatements, node);
1848+
addRestParameterIfNeeded(leadingStatements, node, /*inConstructorWithSynthesizedSuper*/ false);
18531849

18541850
if (isBlock(body)) {
18551851
// addCustomPrologue puts already-existing directives at the beginning of the target statement-array
1856-
statementOffset = addCustomPrologue(statements, body.statements, statementOffset, visitor);
1852+
statementOffset = addCustomPrologue(leadingStatements, body.statements, statementOffset, visitor);
18571853

18581854
statementsLocation = body.statements;
18591855
addRange(statements, visitNodes(body.statements, visitor, isStatement, statementOffset));
@@ -1896,15 +1892,14 @@ namespace ts {
18961892

18971893
const lexicalEnvironment = context.endLexicalEnvironment();
18981894
prependStatements(statements, lexicalEnvironment);
1899-
19001895
prependCaptureNewTargetIfNeeded(statements, node, /*copyOnWrite*/ false);
19011896

19021897
// If we added any final generated statements, this must be a multi-line block
1903-
if (!multiLine && lexicalEnvironment && lexicalEnvironment.length) {
1898+
if (some(leadingStatements) || some(lexicalEnvironment)) {
19041899
multiLine = true;
19051900
}
19061901

1907-
const block = createBlock(setTextRange(createNodeArray(statements), statementsLocation), multiLine);
1902+
const block = createBlock(setTextRange(createNodeArray([...leadingStatements, ...statements]), statementsLocation), multiLine);
19081903
setTextRange(block, node.body);
19091904
if (!multiLine && singleLine) {
19101905
setEmitFlags(block, EmitFlags.SingleLine);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
describe("asyncArrowEvaluation", () => {
2+
// https://github.com/Microsoft/TypeScript/issues/24722
3+
it("this capture (es5)", async () => {
4+
const result = evaluator.evaluateTypeScript(`
5+
export class A {
6+
b = async (...args: any[]) => {
7+
await Promise.resolve();
8+
output.push({ ["a"]: () => this }); // computed property name after 'await' triggers case
9+
};
10+
}
11+
export const output: any[] = [];
12+
export async function main() {
13+
await new A().b();
14+
}`);
15+
await result.main();
16+
assert.instanceOf(result.output[0].a(), result.A);
17+
});
18+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//// [asyncArrowFunction11_es5.ts]
2+
// https://github.com/Microsoft/TypeScript/issues/24722
3+
class A {
4+
b = async (...args: any[]) => {
5+
await Promise.resolve();
6+
const obj = { ["a"]: () => this }; // computed property name after `await` triggers case
7+
};
8+
}
9+
10+
//// [asyncArrowFunction11_es5.js]
11+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12+
return new (P || (P = Promise))(function (resolve, reject) {
13+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
14+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
15+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
16+
step((generator = generator.apply(thisArg, _arguments || [])).next());
17+
});
18+
};
19+
var __generator = (this && this.__generator) || function (thisArg, body) {
20+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
21+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
22+
function verb(n) { return function (v) { return step([n, v]); }; }
23+
function step(op) {
24+
if (f) throw new TypeError("Generator is already executing.");
25+
while (_) try {
26+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
27+
if (y = 0, t) op = [op[0] & 2, t.value];
28+
switch (op[0]) {
29+
case 0: case 1: t = op; break;
30+
case 4: _.label++; return { value: op[1], done: false };
31+
case 5: _.label++; y = op[1]; op = [0]; continue;
32+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
33+
default:
34+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
35+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
36+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
37+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
38+
if (t[2]) _.ops.pop();
39+
_.trys.pop(); continue;
40+
}
41+
op = body.call(thisArg, _);
42+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
43+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
44+
}
45+
};
46+
// https://github.com/Microsoft/TypeScript/issues/24722
47+
var A = /** @class */ (function () {
48+
function A() {
49+
var _this = this;
50+
this.b = function () {
51+
var args = [];
52+
for (var _i = 0; _i < arguments.length; _i++) {
53+
args[_i] = arguments[_i];
54+
}
55+
return __awaiter(_this, void 0, void 0, function () {
56+
var _a, obj;
57+
var _this = this;
58+
return __generator(this, function (_b) {
59+
switch (_b.label) {
60+
case 0: return [4 /*yield*/, Promise.resolve()];
61+
case 1:
62+
_b.sent();
63+
obj = (_a = {}, _a["a"] = function () { return _this; }, _a);
64+
return [2 /*return*/];
65+
}
66+
});
67+
});
68+
};
69+
}
70+
return A;
71+
}());
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction11_es5.ts ===
2+
// https://github.com/Microsoft/TypeScript/issues/24722
3+
class A {
4+
>A : Symbol(A, Decl(asyncArrowFunction11_es5.ts, 0, 0))
5+
6+
b = async (...args: any[]) => {
7+
>b : Symbol(A.b, Decl(asyncArrowFunction11_es5.ts, 1, 9))
8+
>args : Symbol(args, Decl(asyncArrowFunction11_es5.ts, 2, 15))
9+
10+
await Promise.resolve();
11+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
12+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
13+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
14+
15+
const obj = { ["a"]: () => this }; // computed property name after `await` triggers case
16+
>obj : Symbol(obj, Decl(asyncArrowFunction11_es5.ts, 4, 13))
17+
>["a"] : Symbol(["a"], Decl(asyncArrowFunction11_es5.ts, 4, 21))
18+
>"a" : Symbol(["a"], Decl(asyncArrowFunction11_es5.ts, 4, 21))
19+
>this : Symbol(A, Decl(asyncArrowFunction11_es5.ts, 0, 0))
20+
21+
};
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction11_es5.ts ===
2+
// https://github.com/Microsoft/TypeScript/issues/24722
3+
class A {
4+
>A : A
5+
6+
b = async (...args: any[]) => {
7+
>b : (...args: any[]) => Promise<void>
8+
>async (...args: any[]) => { await Promise.resolve(); const obj = { ["a"]: () => this }; // computed property name after `await` triggers case } : (...args: any[]) => Promise<void>
9+
>args : any[]
10+
11+
await Promise.resolve();
12+
>await Promise.resolve() : void
13+
>Promise.resolve() : Promise<void>
14+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
15+
>Promise : PromiseConstructor
16+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
17+
18+
const obj = { ["a"]: () => this }; // computed property name after `await` triggers case
19+
>obj : { ["a"]: () => this; }
20+
>{ ["a"]: () => this } : { ["a"]: () => this; }
21+
>["a"] : () => this
22+
>"a" : "a"
23+
>() => this : () => this
24+
>this : this
25+
26+
};
27+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: es5
2+
// @lib: esnext, dom
3+
// @downlevelIteration: true
4+
// https://github.com/Microsoft/TypeScript/issues/24722
5+
class A {
6+
b = async (...args: any[]) => {
7+
await Promise.resolve();
8+
const obj = { ["a"]: () => this }; // computed property name after `await` triggers case
9+
};
10+
}

0 commit comments

Comments
 (0)