Skip to content

Commit 393ee28

Browse files
committed
Move 'use strict' directive out of generator for async function.
1 parent 4a16f65 commit 393ee28

File tree

6 files changed

+76
-8
lines changed

6 files changed

+76
-8
lines changed

src/compiler/transformers/generators.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,12 @@ namespace ts {
572572
operationLocations = undefined;
573573
state = createTempVariable(/*recordTempVariable*/ undefined);
574574

575+
const statementOffset = addPrologueDirectives(statements, body.statements);
576+
575577
// Build the generator
576578
startLexicalEnvironment();
577579

578-
transformAndEmitStatements(body.statements);
580+
transformAndEmitStatements(body.statements, statementOffset);
579581

580582
const buildResult = build();
581583
addNodes(statements, endLexicalEnvironment());
@@ -1117,9 +1119,10 @@ namespace ts {
11171119
return visitEachChild(node, visitor, context);
11181120
}
11191121

1120-
function transformAndEmitStatements(statements: Statement[]) {
1121-
for (const statement of statements) {
1122-
transformAndEmitStatement(statement);
1122+
function transformAndEmitStatements(statements: Statement[], start = 0) {
1123+
const numStatements = statements.length;
1124+
for (let i = start; i < numStatements; i++) {
1125+
transformAndEmitStatement(statements[i]);
11231126
}
11241127
}
11251128

src/compiler/transformers/ts.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,11 +2181,13 @@ namespace ts {
21812181
return transformFunctionBodyWorker(node.body);
21822182
}
21832183

2184-
function transformFunctionBodyWorker(body: Block) {
2184+
function transformFunctionBodyWorker(body: Block, start = 0) {
21852185
const savedCurrentScope = currentScope;
21862186
currentScope = body;
21872187
startLexicalEnvironment();
2188-
const visited = visitEachChild(body, visitor, context);
2188+
2189+
const statements = visitNodes(body.statements, visitor, isStatement, start);
2190+
const visited = updateBlock(body, statements);
21892191
const declarations = endLexicalEnvironment();
21902192
currentScope = savedCurrentScope;
21912193
return mergeFunctionBodyLexicalEnvironment(visited, declarations);
@@ -2233,14 +2235,14 @@ namespace ts {
22332235

22342236
if (!isArrowFunction) {
22352237
const statements: Statement[] = [];
2236-
2238+
const statementOffset = addPrologueDirectives(statements, (<Block>node.body).statements);
22372239
statements.push(
22382240
createReturn(
22392241
createAwaiterHelper(
22402242
currentSourceFileExternalHelpersModuleName,
22412243
hasLexicalArguments,
22422244
promiseConstructor,
2243-
transformFunctionBodyWorker(<Block>node.body)
2245+
transformFunctionBodyWorker(<Block>node.body, statementOffset)
22442246
)
22452247
)
22462248
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [asyncUseStrict_es6.ts]
2+
declare var a: boolean;
3+
declare var p: Promise<boolean>;
4+
async function func(): Promise<void> {
5+
"use strict";
6+
var b = await p || a;
7+
}
8+
9+
//// [asyncUseStrict_es6.js]
10+
function func() {
11+
"use strict";
12+
return __awaiter(this, void 0, void 0, function* () {
13+
var b = (yield p) || a;
14+
});
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/conformance/async/es6/asyncUseStrict_es6.ts ===
2+
declare var a: boolean;
3+
>a : Symbol(a, Decl(asyncUseStrict_es6.ts, 0, 11))
4+
5+
declare var p: Promise<boolean>;
6+
>p : Symbol(p, Decl(asyncUseStrict_es6.ts, 1, 11))
7+
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
8+
9+
async function func(): Promise<void> {
10+
>func : Symbol(func, Decl(asyncUseStrict_es6.ts, 1, 32))
11+
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))
12+
13+
"use strict";
14+
var b = await p || a;
15+
>b : Symbol(b, Decl(asyncUseStrict_es6.ts, 4, 7))
16+
>p : Symbol(p, Decl(asyncUseStrict_es6.ts, 1, 11))
17+
>a : Symbol(a, Decl(asyncUseStrict_es6.ts, 0, 11))
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/conformance/async/es6/asyncUseStrict_es6.ts ===
2+
declare var a: boolean;
3+
>a : boolean
4+
5+
declare var p: Promise<boolean>;
6+
>p : Promise<boolean>
7+
>Promise : Promise<T>
8+
9+
async function func(): Promise<void> {
10+
>func : () => Promise<void>
11+
>Promise : Promise<T>
12+
13+
"use strict";
14+
>"use strict" : string
15+
16+
var b = await p || a;
17+
>b : boolean
18+
>await p || a : boolean
19+
>await p : boolean
20+
>p : Promise<boolean>
21+
>a : boolean
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @target: ES6
2+
// @noEmitHelpers: true
3+
declare var a: boolean;
4+
declare var p: Promise<boolean>;
5+
async function func(): Promise<void> {
6+
"use strict";
7+
var b = await p || a;
8+
}

0 commit comments

Comments
 (0)