Skip to content

Commit d5bf689

Browse files
committed
Fix ASI for 'as' operator
1 parent 1e6bd1c commit d5bf689

File tree

5 files changed

+93
-2
lines changed

5 files changed

+93
-2
lines changed

src/compiler/parser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,8 +2822,12 @@ module ts {
28222822
}
28232823

28242824
if (token === SyntaxKind.AsKeyword) {
2825-
parseTokenNode();
2826-
leftOperand = makeAsExpression(leftOperand, parseType());
2825+
if (canParseSemicolon()) {
2826+
break;
2827+
} else {
2828+
parseTokenNode();
2829+
leftOperand = makeAsExpression(leftOperand, parseType());
2830+
}
28272831
} else {
28282832
leftOperand = makeBinaryExpression(leftOperand, parseTokenNode(), parseBinaryExpressionOrHigher(newPrecedence));
28292833
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [asOperatorASI.ts]
2+
class Foo { }
3+
declare function as(...args: any[]);
4+
5+
// Example 1
6+
var x = 10
7+
as `Hello world`; // should not error
8+
9+
// Example 2
10+
var y = 20
11+
as(Foo); // should emit
12+
13+
14+
//// [asOperatorASI.js]
15+
var Foo = (function () {
16+
function Foo() {
17+
}
18+
return Foo;
19+
})();
20+
// Example 1
21+
var x = 10;
22+
(_a = ["Hello world"], _a.raw = ["Hello world"], as(_a)); // should not error
23+
// Example 2
24+
var y = 20;
25+
as(Foo); // should emit
26+
var _a;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/conformance/expressions/asOperator/asOperatorASI.ts ===
2+
class Foo { }
3+
>Foo : Symbol(Foo, Decl(asOperatorASI.ts, 0, 0))
4+
5+
declare function as(...args: any[]);
6+
>as : Symbol(as, Decl(asOperatorASI.ts, 0, 13))
7+
>args : Symbol(args, Decl(asOperatorASI.ts, 1, 20))
8+
9+
// Example 1
10+
var x = 10
11+
>x : Symbol(x, Decl(asOperatorASI.ts, 4, 3))
12+
13+
as `Hello world`; // should not error
14+
>as : Symbol(as, Decl(asOperatorASI.ts, 0, 13))
15+
16+
// Example 2
17+
var y = 20
18+
>y : Symbol(y, Decl(asOperatorASI.ts, 8, 3))
19+
20+
as(Foo); // should emit
21+
>as : Symbol(as, Decl(asOperatorASI.ts, 0, 13))
22+
>Foo : Symbol(Foo, Decl(asOperatorASI.ts, 0, 0))
23+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/conformance/expressions/asOperator/asOperatorASI.ts ===
2+
class Foo { }
3+
>Foo : Foo
4+
5+
declare function as(...args: any[]);
6+
>as : (...args: any[]) => any
7+
>args : any[]
8+
9+
// Example 1
10+
var x = 10
11+
>x : number
12+
>10 : number
13+
14+
as `Hello world`; // should not error
15+
>as `Hello world` : any
16+
>as : (...args: any[]) => any
17+
>`Hello world` : string
18+
19+
// Example 2
20+
var y = 20
21+
>y : number
22+
>20 : number
23+
24+
as(Foo); // should emit
25+
>as(Foo) : any
26+
>as : (...args: any[]) => any
27+
>Foo : typeof Foo
28+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Foo { }
2+
declare function as(...args: any[]);
3+
4+
// Example 1
5+
var x = 10
6+
as `Hello world`; // should not error
7+
8+
// Example 2
9+
var y = 20
10+
as(Foo); // should emit

0 commit comments

Comments
 (0)