Skip to content

Commit cbc2059

Browse files
authored
Fixed a crash when parsing invalid decorator on await expression (#62659)
1 parent af3a377 commit cbc2059

File tree

11 files changed

+160
-11
lines changed

11 files changed

+160
-11
lines changed

src/compiler/parser.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7505,7 +7505,11 @@ namespace Parser {
75057505
case SyntaxKind.LetKeyword:
75067506
case SyntaxKind.ConstKeyword:
75077507
case SyntaxKind.UsingKeyword:
7508+
return parseVariableStatement(pos, hasJSDoc, modifiersIn);
75087509
case SyntaxKind.AwaitKeyword:
7510+
if (!isAwaitUsingDeclaration()) {
7511+
break;
7512+
}
75097513
return parseVariableStatement(pos, hasJSDoc, modifiersIn);
75107514
case SyntaxKind.FunctionKeyword:
75117515
return parseFunctionDeclaration(pos, hasJSDoc, modifiersIn);
@@ -7534,17 +7538,16 @@ namespace Parser {
75347538
default:
75357539
return parseExportDeclaration(pos, hasJSDoc, modifiersIn);
75367540
}
7537-
default:
7538-
if (modifiersIn) {
7539-
// We reached this point because we encountered decorators and/or modifiers and assumed a declaration
7540-
// would follow. For recovery and error reporting purposes, return an incomplete declaration.
7541-
const missing = createMissingNode<MissingDeclaration>(SyntaxKind.MissingDeclaration, /*reportAtCurrentPosition*/ true, Diagnostics.Declaration_expected);
7542-
setTextRangePos(missing, pos);
7543-
(missing as Mutable<MissingDeclaration>).modifiers = modifiersIn;
7544-
return missing;
7545-
}
7546-
return undefined!; // TODO: GH#18217
75477541
}
7542+
if (modifiersIn) {
7543+
// We reached this point because we encountered decorators and/or modifiers and assumed a declaration
7544+
// would follow. For recovery and error reporting purposes, return an incomplete declaration.
7545+
const missing = createMissingNode<MissingDeclaration>(SyntaxKind.MissingDeclaration, /*reportAtCurrentPosition*/ true, Diagnostics.Declaration_expected);
7546+
setTextRangePos(missing, pos);
7547+
(missing as Mutable<MissingDeclaration>).modifiers = modifiersIn;
7548+
return missing;
7549+
}
7550+
return undefined!; // TODO: GH#18217
75487551
}
75497552

75507553
function nextTokenIsStringLiteral() {
@@ -7677,7 +7680,9 @@ namespace Parser {
76777680
flags |= NodeFlags.Using;
76787681
break;
76797682
case SyntaxKind.AwaitKeyword:
7680-
Debug.assert(isAwaitUsingDeclaration());
7683+
if (!isAwaitUsingDeclaration()) {
7684+
break;
7685+
}
76817686
flags |= NodeFlags.AwaitUsing;
76827687
nextToken();
76837688
break;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
decoratorOnAwait.ts(3,5): error TS1146: Declaration expected.
2+
3+
4+
==== decoratorOnAwait.ts (1 errors) ====
5+
declare function dec<T>(target: T): T;
6+
7+
@dec
8+
9+
!!! error TS1146: Declaration expected.
10+
await 1
11+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/conformance/decorators/invalid/decoratorOnAwait.ts] ////
2+
3+
//// [decoratorOnAwait.ts]
4+
declare function dec<T>(target: T): T;
5+
6+
@dec
7+
await 1
8+
9+
10+
//// [decoratorOnAwait.js]
11+
await 1;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/decorators/invalid/decoratorOnAwait.ts] ////
2+
3+
=== decoratorOnAwait.ts ===
4+
declare function dec<T>(target: T): T;
5+
>dec : Symbol(dec, Decl(decoratorOnAwait.ts, 0, 0))
6+
>T : Symbol(T, Decl(decoratorOnAwait.ts, 0, 21))
7+
>target : Symbol(target, Decl(decoratorOnAwait.ts, 0, 24))
8+
>T : Symbol(T, Decl(decoratorOnAwait.ts, 0, 21))
9+
>T : Symbol(T, Decl(decoratorOnAwait.ts, 0, 21))
10+
11+
@dec
12+
>dec : Symbol(dec, Decl(decoratorOnAwait.ts, 0, 0))
13+
14+
await 1
15+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/conformance/decorators/invalid/decoratorOnAwait.ts] ////
2+
3+
=== decoratorOnAwait.ts ===
4+
declare function dec<T>(target: T): T;
5+
>dec : <T>(target: T) => T
6+
> : ^ ^^ ^^ ^^^^^
7+
>target : T
8+
> : ^
9+
10+
@dec
11+
>dec : <T>(target: T) => T
12+
> : ^ ^^ ^^ ^^^^^
13+
14+
await 1
15+
>await 1 : 1
16+
> : ^
17+
>1 : 1
18+
> : ^
19+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
decoratorOnUsing.ts(4,7): error TS1134: Variable declaration expected.
2+
3+
4+
==== decoratorOnUsing.ts (1 errors) ====
5+
declare function dec<T>(target: T): T;
6+
7+
@dec
8+
using 1
9+
~
10+
!!! error TS1134: Variable declaration expected.
11+
12+
@dec
13+
using x
14+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/conformance/decorators/invalid/decoratorOnUsing.ts] ////
2+
3+
//// [decoratorOnUsing.ts]
4+
declare function dec<T>(target: T): T;
5+
6+
@dec
7+
using 1
8+
9+
@dec
10+
using x
11+
12+
13+
//// [decoratorOnUsing.js]
14+
using ;
15+
1;
16+
using x;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/conformance/decorators/invalid/decoratorOnUsing.ts] ////
2+
3+
=== decoratorOnUsing.ts ===
4+
declare function dec<T>(target: T): T;
5+
>dec : Symbol(dec, Decl(decoratorOnUsing.ts, 0, 0))
6+
>T : Symbol(T, Decl(decoratorOnUsing.ts, 0, 21))
7+
>target : Symbol(target, Decl(decoratorOnUsing.ts, 0, 24))
8+
>T : Symbol(T, Decl(decoratorOnUsing.ts, 0, 21))
9+
>T : Symbol(T, Decl(decoratorOnUsing.ts, 0, 21))
10+
11+
@dec
12+
>dec : Symbol(dec, Decl(decoratorOnUsing.ts, 0, 0))
13+
14+
using 1
15+
16+
@dec
17+
>dec : Symbol(dec, Decl(decoratorOnUsing.ts, 0, 0))
18+
19+
using x
20+
>x : Symbol(x, Decl(decoratorOnUsing.ts, 6, 5))
21+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/conformance/decorators/invalid/decoratorOnUsing.ts] ////
2+
3+
=== decoratorOnUsing.ts ===
4+
declare function dec<T>(target: T): T;
5+
>dec : <T>(target: T) => T
6+
> : ^ ^^ ^^ ^^^^^
7+
>target : T
8+
> : ^
9+
10+
@dec
11+
>dec : <T>(target: T) => T
12+
> : ^ ^^ ^^ ^^^^^
13+
14+
using 1
15+
>1 : 1
16+
> : ^
17+
18+
@dec
19+
>dec : <T>(target: T) => T
20+
> : ^ ^^ ^^ ^^^^^
21+
22+
using x
23+
>x : any
24+
> : ^^^
25+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare function dec<T>(target: T): T;
2+
3+
@dec
4+
await 1

0 commit comments

Comments
 (0)