-
Notifications
You must be signed in to change notification settings - Fork 885
[bug-fix] no-invalid-this / ignore functions w "this" param #3267
Conversation
src/rules/noInvalidThisRule.ts
Outdated
@@ -76,6 +78,9 @@ function walk(ctx: Lint.WalkContext<boolean>): void { | |||
|
|||
case ts.SyntaxKind.FunctionDeclaration: | |||
case ts.SyntaxKind.FunctionExpression: | |||
if (hasContextualThis(node as ts.FunctionDeclaration)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ts.FunctionLikeDeclaration
for correctness
src/rules/noInvalidThisRule.ts
Outdated
@@ -96,3 +101,7 @@ function walk(ctx: Lint.WalkContext<boolean>): void { | |||
ts.forEachChild(node, cb); | |||
}); | |||
} | |||
|
|||
function hasContextualThis(node: ts.FunctionDeclaration): boolean { | |||
return node.parameters.some((param) => param.name.getText() === "this"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return node.parameters.some(isThisParameter);
@@ -1,3 +1,21 @@ | |||
export function f<T>(this: Observable<T>): Observable<T> { | |||
const nestedFunction = (this) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arrow functions cannot have a this
parameter
We don't need to worry about it, because TypeScript shows an error. But we shouldn't use it in any test
src/rules/noInvalidThisRule.ts
Outdated
@@ -96,3 +101,7 @@ function walk(ctx: Lint.WalkContext<boolean>): void { | |||
ts.forEachChild(node, cb); | |||
}); | |||
} | |||
|
|||
function hasContextualThis(node: ts.FunctionDeclaration): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please rename to hasThisParameter
(or completely inline it)
This has nothing to do with contextual this
. It's about explicitly declared this
parameters.
Contextual this
looks as follows:
declare function callWithThis(fn: (this: Array<any>) => void): void;
callWithThis(function() {
console.log(this.length); // this is allowed, because `this` is contextually typed to be an array
});
To detect that you would need the TypeChecker. I don't think that's worth it, because TypeScript provides the option --noImplicitThis
which makes this rule obsolete.
… arrow function w this param from test
Thanks @aervin |
* Keep track of evaluation context: look to the closest parent function and decide if it is allowed to refer to `this` * Fix a false negative in the tests from palantir#3267
* Keep track of evaluation context: look to the closest parent function and decide if it is allowed to refer to `this` * Fix a false negative in the tests from palantir#3267
* Keep track of evaluation context: look to the closest parent function and decide if it is allowed to refer to `this` * Fix a false negative in the tests from palantir#3267
* no-invalid-this: false positive for method syntax * Keep track of evaluation context: look to the closest parent function and decide if it is allowed to refer to `this` * Fix a false negative in the tests from #3267 * no-invalid-this: stylistic cleanup
PR checklist
Overview of change:
Functions with contextual "this" will be ignored.
Is there anything you'd like reviewers to focus on?
Are additional test cases needed?