Skip to content

Commit 05faee8

Browse files
committed
fix(44639): Fix emit for optional chain with type assertions
1 parent 9251304 commit 05faee8

8 files changed

+150
-1
lines changed

src/compiler/transformers/es2020.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace ts {
5454
Debug.assertNotNode(chain, isNonNullChain);
5555
links.unshift(chain);
5656
}
57-
return { expression: chain.expression, chain: links };
57+
return { expression: skipPartiallyEmittedExpressions(chain.expression), chain: links };
5858
}
5959

6060
function visitNonOptionalParenthesizedExpression(node: ParenthesizedExpression, captureThisArg: boolean, isDelete: boolean): Expression {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [optionalChainingInTypeAssertions.ts]
2+
class Foo {
3+
m() {}
4+
}
5+
6+
const foo = new Foo();
7+
8+
(foo.m as any)?.();
9+
(<any>foo.m)?.();
10+
11+
12+
//// [optionalChainingInTypeAssertions.js]
13+
var _a, _b;
14+
class Foo {
15+
m() { }
16+
}
17+
const foo = new Foo();
18+
(_a = foo.m) === null || _a === void 0 ? void 0 : _a.call(foo);
19+
(_b = foo.m) === null || _b === void 0 ? void 0 : _b.call(foo);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/conformance/expressions/optionalChaining/optionalChainingInTypeAssertions.ts ===
2+
class Foo {
3+
>Foo : Symbol(Foo, Decl(optionalChainingInTypeAssertions.ts, 0, 0))
4+
5+
m() {}
6+
>m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
7+
}
8+
9+
const foo = new Foo();
10+
>foo : Symbol(foo, Decl(optionalChainingInTypeAssertions.ts, 4, 5))
11+
>Foo : Symbol(Foo, Decl(optionalChainingInTypeAssertions.ts, 0, 0))
12+
13+
(foo.m as any)?.();
14+
>foo.m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
15+
>foo : Symbol(foo, Decl(optionalChainingInTypeAssertions.ts, 4, 5))
16+
>m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
17+
18+
(<any>foo.m)?.();
19+
>foo.m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
20+
>foo : Symbol(foo, Decl(optionalChainingInTypeAssertions.ts, 4, 5))
21+
>m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
22+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/expressions/optionalChaining/optionalChainingInTypeAssertions.ts ===
2+
class Foo {
3+
>Foo : Foo
4+
5+
m() {}
6+
>m : () => void
7+
}
8+
9+
const foo = new Foo();
10+
>foo : Foo
11+
>new Foo() : Foo
12+
>Foo : typeof Foo
13+
14+
(foo.m as any)?.();
15+
>(foo.m as any)?.() : any
16+
>(foo.m as any) : any
17+
>foo.m as any : any
18+
>foo.m : () => void
19+
>foo : Foo
20+
>m : () => void
21+
22+
(<any>foo.m)?.();
23+
>(<any>foo.m)?.() : any
24+
>(<any>foo.m) : any
25+
><any>foo.m : any
26+
>foo.m : () => void
27+
>foo : Foo
28+
>m : () => void
29+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [optionalChainingInTypeAssertions.ts]
2+
class Foo {
3+
m() {}
4+
}
5+
6+
const foo = new Foo();
7+
8+
(foo.m as any)?.();
9+
(<any>foo.m)?.();
10+
11+
12+
//// [optionalChainingInTypeAssertions.js]
13+
class Foo {
14+
m() { }
15+
}
16+
const foo = new Foo();
17+
foo.m?.();
18+
foo.m?.();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/conformance/expressions/optionalChaining/optionalChainingInTypeAssertions.ts ===
2+
class Foo {
3+
>Foo : Symbol(Foo, Decl(optionalChainingInTypeAssertions.ts, 0, 0))
4+
5+
m() {}
6+
>m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
7+
}
8+
9+
const foo = new Foo();
10+
>foo : Symbol(foo, Decl(optionalChainingInTypeAssertions.ts, 4, 5))
11+
>Foo : Symbol(Foo, Decl(optionalChainingInTypeAssertions.ts, 0, 0))
12+
13+
(foo.m as any)?.();
14+
>foo.m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
15+
>foo : Symbol(foo, Decl(optionalChainingInTypeAssertions.ts, 4, 5))
16+
>m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
17+
18+
(<any>foo.m)?.();
19+
>foo.m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
20+
>foo : Symbol(foo, Decl(optionalChainingInTypeAssertions.ts, 4, 5))
21+
>m : Symbol(Foo.m, Decl(optionalChainingInTypeAssertions.ts, 0, 11))
22+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/expressions/optionalChaining/optionalChainingInTypeAssertions.ts ===
2+
class Foo {
3+
>Foo : Foo
4+
5+
m() {}
6+
>m : () => void
7+
}
8+
9+
const foo = new Foo();
10+
>foo : Foo
11+
>new Foo() : Foo
12+
>Foo : typeof Foo
13+
14+
(foo.m as any)?.();
15+
>(foo.m as any)?.() : any
16+
>(foo.m as any) : any
17+
>foo.m as any : any
18+
>foo.m : () => void
19+
>foo : Foo
20+
>m : () => void
21+
22+
(<any>foo.m)?.();
23+
>(<any>foo.m)?.() : any
24+
>(<any>foo.m) : any
25+
><any>foo.m : any
26+
>foo.m : () => void
27+
>foo : Foo
28+
>m : () => void
29+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: es2015, esnext
2+
3+
class Foo {
4+
m() {}
5+
}
6+
7+
const foo = new Foo();
8+
9+
(foo.m as any)?.();
10+
(<any>foo.m)?.();

0 commit comments

Comments
 (0)