Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

[bug-fix] no-invalid-this / ignore functions w "this" param #3267

Merged
merged 4 commits into from Oct 2, 2017

Conversation

aervin
Copy link
Contributor

@aervin aervin commented Oct 1, 2017

PR checklist

  • Addresses an existing issue: #3022
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

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?

@aervin aervin changed the title Bug/no invalid this [bug-fix] no-invalid-this / ignore contextual "this" Oct 1, 2017
@@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts.FunctionLikeDeclaration for correctness

@@ -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");
Copy link
Contributor

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) => {
Copy link
Contributor

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

@@ -96,3 +101,7 @@ function walk(ctx: Lint.WalkContext<boolean>): void {
ts.forEachChild(node, cb);
});
}

function hasContextualThis(node: ts.FunctionDeclaration): boolean {
Copy link
Contributor

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.

@aervin aervin changed the title [bug-fix] no-invalid-this / ignore contextual "this" [bug-fix] no-invalid-this / ignore functions"this" Oct 1, 2017
@aervin aervin changed the title [bug-fix] no-invalid-this / ignore functions"this" [bug-fix] no-invalid-this / ignore functions w "this" param Oct 1, 2017
@ajafff ajafff merged commit b104e5a into palantir:master Oct 2, 2017
@ajafff
Copy link
Contributor

ajafff commented Oct 2, 2017

Thanks @aervin

HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
amacleay added a commit to amacleay/tslint that referenced this pull request Jul 12, 2018
* 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
amacleay added a commit to amacleay/tslint that referenced this pull request Jul 12, 2018
* 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
amacleay added a commit to amacleay/tslint that referenced this pull request Nov 7, 2018
* 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
ericanderson pushed a commit that referenced this pull request Dec 12, 2018
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants