Skip to content

Commit 9735273

Browse files
committed
better recovery
1 parent b6eb990 commit 9735273

File tree

9 files changed

+103
-15
lines changed

9 files changed

+103
-15
lines changed

src/compiler/parser.ts

Lines changed: 13 additions & 10 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() {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
decoratorOnAwait.ts(4,7): error TS1134: Variable declaration expected.
1+
decoratorOnAwait.ts(3,5): error TS1146: Declaration expected.
22

33

44
==== decoratorOnAwait.ts (1 errors) ====
55
declare function dec<T>(target: T): T;
66

77
@dec
8+
9+
!!! error TS1146: Declaration expected.
810
await 1
9-
~
10-
!!! error TS1134: Variable declaration expected.
1111

tests/baselines/reference/decoratorOnAwait.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ await 1
88

99

1010
//// [decoratorOnAwait.js]
11-
var ;
12-
1;
11+
await 1;

tests/baselines/reference/decoratorOnAwait.types

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ declare function dec<T>(target: T): T;
1212
> : ^ ^^ ^^ ^^^^^
1313

1414
await 1
15+
>await 1 : 1
16+
> : ^
1517
>1 : 1
1618
> : ^
1719

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @target: esnext
2+
declare function dec<T>(target: T): T;
3+
4+
@dec
5+
using 1
6+
7+
@dec
8+
using x

0 commit comments

Comments
 (0)