mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-09 07:55:10 -05:00
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:
committed by
GitHub
parent
a2c11bb7a0
commit
11eabc0946
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
27
tests/baselines/reference/thisTypeSyntacticContext.js
Normal file
27
tests/baselines/reference/thisTypeSyntacticContext.js
Normal 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)();
|
||||
52
tests/baselines/reference/thisTypeSyntacticContext.symbols
Normal file
52
tests/baselines/reference/thisTypeSyntacticContext.symbols
Normal 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))
|
||||
|
||||
|
||||
69
tests/baselines/reference/thisTypeSyntacticContext.types
Normal file
69
tests/baselines/reference/thisTypeSyntacticContext.types
Normal 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
|
||||
|
||||
|
||||
@@ -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)();
|
||||
|
||||
Reference in New Issue
Block a user