Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17266,12 +17266,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;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/reference/thisTypeSyntacticContext.js
Original file line number Diff line number Diff line change
@@ -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 changes: 52 additions & 0 deletions tests/baselines/reference/thisTypeSyntacticContext.symbols
Original file line number Diff line number Diff line change
@@ -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 changes: 69 additions & 0 deletions tests/baselines/reference/thisTypeSyntacticContext.types
Original file line number Diff line number Diff line change
@@ -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


13 changes: 13 additions & 0 deletions tests/cases/conformance/types/thisType/thisTypeSyntacticContext.ts
Original file line number Diff line number Diff line change
@@ -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)();