Skip parens and non-null assertions when looking for this-context (#23097)

* Skip parens and ! for getting this-context of call

* Add test and improve code a bit

* Use skipOuterExpressions instead
This commit is contained in:
Nathan Shively-Sanders
2018-04-03 06:23:59 -07:00
committed by GitHub
parent a2c11bb7a0
commit 11eabc0946
6 changed files with 165 additions and 7 deletions

View File

@@ -17269,12 +17269,9 @@ namespace ts {
*/
function getThisArgumentOfCall(node: CallLikeExpression): LeftHandSideExpression {
if (node.kind === SyntaxKind.CallExpression) {
const callee = node.expression;
if (callee.kind === SyntaxKind.PropertyAccessExpression) {
return (callee as PropertyAccessExpression).expression;
}
else if (callee.kind === SyntaxKind.ElementAccessExpression) {
return (callee as ElementAccessExpression).expression;
const callee = skipOuterExpressions(node.expression);
if (callee.kind === SyntaxKind.PropertyAccessExpression || callee.kind === SyntaxKind.ElementAccessExpression) {
return (callee as PropertyAccessExpression | ElementAccessExpression).expression;
}
}
}

View File

@@ -2022,7 +2022,7 @@ namespace ts {
export function skipParentheses(node: Node): Node;
export function skipParentheses(node: Node): Node {
while (node.kind === SyntaxKind.ParenthesizedExpression) {
node = (<ParenthesizedExpression>node).expression;
node = (node as ParenthesizedExpression).expression;
}
return node;

View File

@@ -0,0 +1,27 @@
//// [thisTypeSyntacticContext.ts]
function f(this: { n: number }) {
}
const o: { n: number, test?: (this: { n: number }) => void } = { n: 1 }
o.test = f
o.test();
o!.test();
o.test!();
o.test!!!();
(o.test!)();
(o.test)();
//// [thisTypeSyntacticContext.js]
function f() {
}
var o = { n: 1 };
o.test = f;
o.test();
o.test();
o.test();
o.test();
(o.test)();
(o.test)();

View File

@@ -0,0 +1,52 @@
=== tests/cases/conformance/types/thisType/thisTypeSyntacticContext.ts ===
function f(this: { n: number }) {
>f : Symbol(f, Decl(thisTypeSyntacticContext.ts, 0, 0))
>this : Symbol(this, Decl(thisTypeSyntacticContext.ts, 0, 11))
>n : Symbol(n, Decl(thisTypeSyntacticContext.ts, 0, 18))
}
const o: { n: number, test?: (this: { n: number }) => void } = { n: 1 }
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>n : Symbol(n, Decl(thisTypeSyntacticContext.ts, 3, 10))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>this : Symbol(this, Decl(thisTypeSyntacticContext.ts, 3, 30))
>n : Symbol(n, Decl(thisTypeSyntacticContext.ts, 3, 37))
>n : Symbol(n, Decl(thisTypeSyntacticContext.ts, 3, 64))
o.test = f
>o.test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>f : Symbol(f, Decl(thisTypeSyntacticContext.ts, 0, 0))
o.test();
>o.test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
o!.test();
>o!.test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
o.test!();
>o.test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
o.test!!!();
>o.test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
(o.test!)();
>o.test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
(o.test)();
>o.test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))
>o : Symbol(o, Decl(thisTypeSyntacticContext.ts, 3, 5))
>test : Symbol(test, Decl(thisTypeSyntacticContext.ts, 3, 21))

View File

@@ -0,0 +1,69 @@
=== tests/cases/conformance/types/thisType/thisTypeSyntacticContext.ts ===
function f(this: { n: number }) {
>f : (this: { n: number; }) => void
>this : { n: number; }
>n : number
}
const o: { n: number, test?: (this: { n: number }) => void } = { n: 1 }
>o : { n: number; test?: (this: { n: number; }) => void; }
>n : number
>test : (this: { n: number; }) => void
>this : { n: number; }
>n : number
>{ n: 1 } : { n: number; }
>n : number
>1 : 1
o.test = f
>o.test = f : (this: { n: number; }) => void
>o.test : (this: { n: number; }) => void
>o : { n: number; test?: (this: { n: number; }) => void; }
>test : (this: { n: number; }) => void
>f : (this: { n: number; }) => void
o.test();
>o.test() : void
>o.test : (this: { n: number; }) => void
>o : { n: number; test?: (this: { n: number; }) => void; }
>test : (this: { n: number; }) => void
o!.test();
>o!.test() : void
>o!.test : (this: { n: number; }) => void
>o! : { n: number; test?: (this: { n: number; }) => void; }
>o : { n: number; test?: (this: { n: number; }) => void; }
>test : (this: { n: number; }) => void
o.test!();
>o.test!() : void
>o.test! : (this: { n: number; }) => void
>o.test : (this: { n: number; }) => void
>o : { n: number; test?: (this: { n: number; }) => void; }
>test : (this: { n: number; }) => void
o.test!!!();
>o.test!!!() : void
>o.test!!! : (this: { n: number; }) => void
>o.test!! : (this: { n: number; }) => void
>o.test! : (this: { n: number; }) => void
>o.test : (this: { n: number; }) => void
>o : { n: number; test?: (this: { n: number; }) => void; }
>test : (this: { n: number; }) => void
(o.test!)();
>(o.test!)() : void
>(o.test!) : (this: { n: number; }) => void
>o.test! : (this: { n: number; }) => void
>o.test : (this: { n: number; }) => void
>o : { n: number; test?: (this: { n: number; }) => void; }
>test : (this: { n: number; }) => void
(o.test)();
>(o.test)() : void
>(o.test) : (this: { n: number; }) => void
>o.test : (this: { n: number; }) => void
>o : { n: number; test?: (this: { n: number; }) => void; }
>test : (this: { n: number; }) => void

View File

@@ -0,0 +1,13 @@
function f(this: { n: number }) {
}
const o: { n: number, test?: (this: { n: number }) => void } = { n: 1 }
o.test = f
o.test();
o!.test();
o.test!();
o.test!!!();
(o.test!)();
(o.test)();