Skip to content

Commit 4768286

Browse files
authored
Merge pull request #9541 from Microsoft/Fix9531
Fix #9531: account for `async` as an contextual keyword when parsing export assignments
2 parents 230c9cf + bf240d2 commit 4768286

File tree

5 files changed

+200
-1
lines changed

5 files changed

+200
-1
lines changed

Diff for: src/compiler/parser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,8 @@ namespace ts {
11841184

11851185
function nextTokenIsClassOrFunctionOrAsync(): boolean {
11861186
nextToken();
1187-
return token === SyntaxKind.ClassKeyword || token === SyntaxKind.FunctionKeyword || token === SyntaxKind.AsyncKeyword;
1187+
return token === SyntaxKind.ClassKeyword || token === SyntaxKind.FunctionKeyword ||
1188+
(token === SyntaxKind.AsyncKeyword && lookAhead(nextTokenIsFunctionKeywordOnSameLine));
11881189
}
11891190

11901191
// True if positioned at the start of a list element
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//// [tests/cases/compiler/exportDefaultAsyncFunction2.ts] ////
2+
3+
//// [asyncawait.ts]
4+
5+
export function async<T>(...args: any[]): any { }
6+
export function await(...args: any[]): any { }
7+
8+
//// [a.ts]
9+
import { async, await } from 'asyncawait';
10+
export default async(() => await(Promise.resolve(1)));
11+
12+
//// [b.ts]
13+
export default async () => { return 0; };
14+
15+
//// [c.ts]
16+
import { async, await } from 'asyncawait';
17+
export default async<number>();
18+
19+
//// [d.ts]
20+
import { async, await } from 'asyncawait';
21+
22+
export default async;
23+
24+
//// [e.ts]
25+
import { async, await } from 'asyncawait';
26+
27+
export default async
28+
29+
export function foo() { }
30+
31+
//// [asyncawait.js]
32+
export function async(...args) { }
33+
export function await(...args) { }
34+
//// [a.js]
35+
import { async, await } from 'asyncawait';
36+
export default async(() => await(Promise.resolve(1)));
37+
//// [b.js]
38+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
39+
return new (P || (P = Promise))(function (resolve, reject) {
40+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
41+
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
42+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
43+
step((generator = generator.apply(thisArg, _arguments)).next());
44+
});
45+
};
46+
export default () => __awaiter(this, void 0, void 0, function* () { return 0; });
47+
//// [c.js]
48+
import { async } from 'asyncawait';
49+
export default async();
50+
//// [d.js]
51+
import { async } from 'asyncawait';
52+
export default async;
53+
//// [e.js]
54+
import { async } from 'asyncawait';
55+
export default async;
56+
export function foo() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=== tests/cases/compiler/asyncawait.ts ===
2+
3+
export function async<T>(...args: any[]): any { }
4+
>async : Symbol(async, Decl(asyncawait.ts, 0, 0))
5+
>T : Symbol(T, Decl(asyncawait.ts, 1, 22))
6+
>args : Symbol(args, Decl(asyncawait.ts, 1, 25))
7+
8+
export function await(...args: any[]): any { }
9+
>await : Symbol(await, Decl(asyncawait.ts, 1, 49))
10+
>args : Symbol(args, Decl(asyncawait.ts, 2, 22))
11+
12+
=== tests/cases/compiler/a.ts ===
13+
import { async, await } from 'asyncawait';
14+
>async : Symbol(async, Decl(a.ts, 0, 8))
15+
>await : Symbol(await, Decl(a.ts, 0, 15))
16+
17+
export default async(() => await(Promise.resolve(1)));
18+
>async : Symbol(async, Decl(a.ts, 0, 8))
19+
>await : Symbol(await, Decl(a.ts, 0, 15))
20+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
21+
>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, --, --))
22+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
23+
24+
=== tests/cases/compiler/b.ts ===
25+
export default async () => { return 0; };
26+
No type information for this code.
27+
No type information for this code.=== tests/cases/compiler/c.ts ===
28+
import { async, await } from 'asyncawait';
29+
>async : Symbol(async, Decl(c.ts, 0, 8))
30+
>await : Symbol(await, Decl(c.ts, 0, 15))
31+
32+
export default async<number>();
33+
>async : Symbol(async, Decl(c.ts, 0, 8))
34+
35+
=== tests/cases/compiler/d.ts ===
36+
import { async, await } from 'asyncawait';
37+
>async : Symbol(async, Decl(d.ts, 0, 8))
38+
>await : Symbol(await, Decl(d.ts, 0, 15))
39+
40+
export default async;
41+
>async : Symbol(async, Decl(d.ts, 0, 8))
42+
43+
=== tests/cases/compiler/e.ts ===
44+
import { async, await } from 'asyncawait';
45+
>async : Symbol(async, Decl(e.ts, 0, 8))
46+
>await : Symbol(await, Decl(e.ts, 0, 15))
47+
48+
export default async
49+
>async : Symbol(async, Decl(e.ts, 0, 8))
50+
51+
export function foo() { }
52+
>foo : Symbol(foo, Decl(e.ts, 2, 20))
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
=== tests/cases/compiler/asyncawait.ts ===
2+
3+
export function async<T>(...args: any[]): any { }
4+
>async : <T>(...args: any[]) => any
5+
>T : T
6+
>args : any[]
7+
8+
export function await(...args: any[]): any { }
9+
>await : (...args: any[]) => any
10+
>args : any[]
11+
12+
=== tests/cases/compiler/a.ts ===
13+
import { async, await } from 'asyncawait';
14+
>async : <T>(...args: any[]) => any
15+
>await : (...args: any[]) => any
16+
17+
export default async(() => await(Promise.resolve(1)));
18+
>async(() => await(Promise.resolve(1))) : any
19+
>async : <T>(...args: any[]) => any
20+
>() => await(Promise.resolve(1)) : () => any
21+
>await(Promise.resolve(1)) : any
22+
>await : (...args: any[]) => any
23+
>Promise.resolve(1) : Promise<number>
24+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
25+
>Promise : PromiseConstructor
26+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
27+
>1 : number
28+
29+
=== tests/cases/compiler/b.ts ===
30+
export default async () => { return 0; };
31+
>async () => { return 0; } : () => Promise<number>
32+
>0 : number
33+
34+
=== tests/cases/compiler/c.ts ===
35+
import { async, await } from 'asyncawait';
36+
>async : <T>(...args: any[]) => any
37+
>await : (...args: any[]) => any
38+
39+
export default async<number>();
40+
>async<number>() : any
41+
>async : <T>(...args: any[]) => any
42+
43+
=== tests/cases/compiler/d.ts ===
44+
import { async, await } from 'asyncawait';
45+
>async : <T>(...args: any[]) => any
46+
>await : (...args: any[]) => any
47+
48+
export default async;
49+
>async : <T>(...args: any[]) => any
50+
51+
=== tests/cases/compiler/e.ts ===
52+
import { async, await } from 'asyncawait';
53+
>async : <T>(...args: any[]) => any
54+
>await : (...args: any[]) => any
55+
56+
export default async
57+
>async : <T>(...args: any[]) => any
58+
59+
export function foo() { }
60+
>foo : () => void
61+

Diff for: tests/cases/compiler/exportDefaultAsyncFunction2.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @target: es6
2+
3+
// @filename: asyncawait.ts
4+
export function async<T>(...args: any[]): any { }
5+
export function await(...args: any[]): any { }
6+
7+
// @filename: a.ts
8+
import { async, await } from 'asyncawait';
9+
export default async(() => await(Promise.resolve(1)));
10+
11+
// @filename: b.ts
12+
export default async () => { return 0; };
13+
14+
// @filename: c.ts
15+
import { async, await } from 'asyncawait';
16+
export default async<number>();
17+
18+
// @filename: d.ts
19+
import { async, await } from 'asyncawait';
20+
21+
export default async;
22+
23+
// @filename: e.ts
24+
import { async, await } from 'asyncawait';
25+
26+
export default async
27+
28+
export function foo() { }

0 commit comments

Comments
 (0)