fix(44725): handle this parameter in tagged template call (#44734)

This commit is contained in:
Oleksandr T 2021-06-30 00:11:35 +03:00 committed by GitHub
parent fdc31baffe
commit 114f68cd3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 2 deletions

View File

@ -28724,8 +28724,10 @@ namespace ts {
* Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise.
*/
function getThisArgumentOfCall(node: CallLikeExpression): LeftHandSideExpression | undefined {
if (node.kind === SyntaxKind.CallExpression) {
const callee = skipOuterExpressions(node.expression);
const expression = node.kind === SyntaxKind.CallExpression ? node.expression :
node.kind === SyntaxKind.TaggedTemplateExpression ? node.tag : undefined;
if (expression) {
const callee = skipOuterExpressions(expression);
if (isAccessExpression(callee)) {
return callee.expression;
}

View File

@ -0,0 +1,19 @@
//// [thisTypeInTaggedTemplateCall.ts]
class Foo {
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
return new this()
}
}
Foo.m`test`;
(Foo.m)`test`;
//// [thisTypeInTaggedTemplateCall.js]
class Foo {
static m(strings) {
return new this();
}
}
Foo.m `test`;
(Foo.m) `test`;

View File

@ -0,0 +1,27 @@
=== tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts ===
class Foo {
>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0))
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
>T : Symbol(T, Decl(thisTypeInTaggedTemplateCall.ts, 1, 13))
>this : Symbol(this, Decl(thisTypeInTaggedTemplateCall.ts, 1, 16))
>T : Symbol(T, Decl(thisTypeInTaggedTemplateCall.ts, 1, 13))
>strings : Symbol(strings, Decl(thisTypeInTaggedTemplateCall.ts, 1, 34))
>TemplateStringsArray : Symbol(TemplateStringsArray, Decl(lib.es5.d.ts, --, --))
return new this()
>this : Symbol(this, Decl(thisTypeInTaggedTemplateCall.ts, 1, 16))
}
}
Foo.m`test`;
>Foo.m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0))
>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
(Foo.m)`test`;
>Foo.m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))
>Foo : Symbol(Foo, Decl(thisTypeInTaggedTemplateCall.ts, 0, 0))
>m : Symbol(Foo.m, Decl(thisTypeInTaggedTemplateCall.ts, 0, 11))

View File

@ -0,0 +1,30 @@
=== tests/cases/conformance/types/thisType/thisTypeInTaggedTemplateCall.ts ===
class Foo {
>Foo : Foo
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
>m : <T>(this: new () => T, strings: TemplateStringsArray | string) => T
>this : new () => T
>strings : string | TemplateStringsArray
return new this()
>new this() : T
>this : new () => T
}
}
Foo.m`test`;
>Foo.m`test` : Foo
>Foo.m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
>Foo : typeof Foo
>m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
>`test` : "test"
(Foo.m)`test`;
>(Foo.m)`test` : Foo
>(Foo.m) : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
>Foo.m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
>Foo : typeof Foo
>m : <T>(this: new () => T, strings: string | TemplateStringsArray) => T
>`test` : "test"

View File

@ -0,0 +1,10 @@
// @target: esnext
class Foo {
static m<T>(this: new () => T, strings: TemplateStringsArray | string) {
return new this()
}
}
Foo.m`test`;
(Foo.m)`test`;