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

no-invalid-this: false positive for method syntax #4034

Merged

Conversation

amacleay
Copy link
Contributor

@amacleay amacleay commented Jul 12, 2018

PR checklist

Overview of change:

Previously, the no-invalid-this rule only had shallow tracking of context. this should be allowed if any only iff the closest parent non-arrow function is either a) a class method, or b) a function with a this type parameter.

There were several drawbacks of not tracking context directly:

Is there anything you'd like reviewers to focus on?

CHANGELOG.md entry:

[bugfix] no-invalid-this rule fixes false positives on method-like syntax and false negatives on nested functions

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @amacleay! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@amacleay amacleay force-pushed the no-invalid-this-method-declaration branch from 8167440 to 65e2dfd Compare July 12, 2018 16:47
@amacleay
Copy link
Contributor Author

cc @aervin - I changed a line you added which I believe to be a mistake (https://github.com/palantir/tslint/pull/4034/files#diff-7062f9042aeae109e75e6f78d7430418R33) so my changes deserve extra scrutiny because I'm pretty often wrong!

@amacleay
Copy link
Contributor Author

amacleay commented Aug 8, 2018

Hi all! Feedback would be much appreciated if someone has an opportunity to provide it. I think this should be ready to go as-is if the changes seem appropriate to fix the bug (or, what I believe to be a bug).

Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Some stylistic comments, but probably not blocking IMO.

@@ -59,39 +59,64 @@ export class Rule extends Lint.Rules.AbstractRule {

function walk(ctx: Lint.WalkContext<boolean>): void {
const { sourceFile, options: checkFuncInMethod } = ctx;

const enum parentType {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems semantically odd to recreate this each time walk is called. We don't need that, so this should be moved outside of the walk function.

UnboundRegularFunction,
}

function thisIsAllowed(parent: parentType): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: instead of this three-line function, how about const thisAllowedTypes = new Set([ParentType.ClassMethod, ParentType.BoundRegularFunction]);, and use its .has?

@amacleay
Copy link
Contributor Author

amacleay commented Nov 7, 2018

hjpblbmep2ajo

* 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
Copy link
Contributor Author

amacleay commented Nov 7, 2018

Pushing a rebase to hopefully resolve the failure

@amacleay amacleay force-pushed the no-invalid-this-method-declaration branch from cad840a to 17229fc Compare November 7, 2018 14:29
@ericanderson ericanderson merged commit b9fba26 into palantir:master Dec 12, 2018
Copy link
Member

@ericanderson ericanderson left a comment

Choose a reason for hiding this comment

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

Can you please do a followup for the style nit?

@@ -61,41 +61,63 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

const enum parentType {
Copy link
Member

Choose a reason for hiding this comment

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

nit, enums should be CamelCase: ParentType.

Suggested change
const enum parentType {
const enum ParentType {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You got it: #4376

@amacleay amacleay deleted the no-invalid-this-method-declaration branch December 12, 2018 16:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants