Skip to content

Commit 114f68c

Browse files
authored
fix(44725): handle this parameter in tagged template call (microsoft#44734)
1 parent fdc31ba commit 114f68c

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28724,8 +28724,10 @@ namespace ts {
2872428724
* Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise.
2872528725
*/
2872628726
function getThisArgumentOfCall(node: CallLikeExpression): LeftHandSideExpression | undefined {
28727-
if (node.kind === SyntaxKind.CallExpression) {
28728-
const callee = skipOuterExpressions(node.expression);
28727+
const expression = node.kind === SyntaxKind.CallExpression ? node.expression :
28728+
node.kind === SyntaxKind.TaggedTemplateExpression ? node.tag : undefined;
28729+
if (expression) {
28730+
const callee = skipOuterExpressions(expression);
2872928731
if (isAccessExpression(callee)) {
2873028732
return callee.expression;
2873128733
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [thisTypeInTaggedTemplateCall.ts]
2+
class Foo {
3+
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
4+
return new this()
5+
}
6+
}
7+
8+
Foo.m`test`;
9+
(Foo.m)`test`;
10+
11+
12+
//// [thisTypeInTaggedTemplateCall.js]
13+
class Foo {
14+
static m(strings) {
15+
return new this();
16+
}
17+
}
18+
Foo.m `test`;
19+
(Foo.m) `test`;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts ===
2+
class Foo {
3+
>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0))
4+
5+
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
6+
>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
7+
>T : Symbol(T, Decl(thisTypeInTaggedTemplateCall.ts, 1, 13))
8+
>this : Symbol(this, Decl(thisTypeInTaggedTemplateCall.ts, 1, 16))
9+
>T : Symbol(T, Decl(thisTypeInTaggedTemplateCall.ts, 1, 13))
10+
>strings : Symbol(strings, Decl(thisTypeInTaggedTemplateCall.ts, 1, 34))
11+
>TemplateStringsArray : Symbol(TemplateStringsArray, Decl(lib.es5.d.ts, --, --))
12+
13+
return new this()
14+
>this : Symbol(this, Decl(thisTypeInTaggedTemplateCall.ts, 1, 16))
15+
}
16+
}
17+
18+
Foo.m`test`;
19+
>Foo.m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
20+
>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0))
21+
>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
22+
23+
(Foo.m)`test`;
24+
>Foo.m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
25+
>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0))
26+
>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
27+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts ===
2+
class Foo {
3+
>Foo : Foo
4+
5+
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
6+
>m : <T>(this: new () => T, strings: TemplateStringsArray | string) => T
7+
>this : new () => T
8+
>strings : string | TemplateStringsArray
9+
10+
return new this()
11+
>new this() : T
12+
>this : new () => T
13+
}
14+
}
15+
16+
Foo.m`test`;
17+
>Foo.m`test` : Foo
18+
>Foo.m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
19+
>Foo : typeof Foo
20+
>m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
21+
>`test` : "test"
22+
23+
(Foo.m)`test`;
24+
>(Foo.m)`test` : Foo
25+
>(Foo.m) : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
26+
>Foo.m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
27+
>Foo : typeof Foo
28+
>m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
29+
>`test` : "test"
30+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: esnext
2+
3+
class Foo {
4+
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
5+
return new this()
6+
}
7+
}
8+
9+
Foo.m`test`;
10+
(Foo.m)`test`;

0 commit comments

Comments
 (0)