Skip to content
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

feat(eslint-plugin): add extension rule [no-invalid-this] too support arrow function class properties #1794

Closed
wants to merge 3 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Mar 25, 2020

Fixes #1571

But does not fix #491

This PR clones the no-invalid-this rule from eslint and modifies it to allow ArrowFunctionExpressions within a Class to reference this. I originally attempted to extend the base rule, but found I needed to be able modify what was being pushed to the stack variable which was inaccessible.

I realize this does not cover cases like:

class A {
    private foo = 0;
    constructor(number) {
        this.foo = number;
    }
    function increment() {
        this.foo += 1;
    }
}

but I'm not really sure how to navigate the AST to find what variables have been initialized in the constructor or on the class body. This is my first time working with ASTs, linters or parsers, so some guidance would really be appreciated!

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @sean-allen!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented Mar 25, 2020

Codecov Report

Merging #1794 into master will decrease coverage by 0.02%.
The diff coverage is 88.46%.

@@            Coverage Diff             @@
##           master    #1794      +/-   ##
==========================================
- Coverage   94.48%   94.46%   -0.03%     
==========================================
  Files         162      163       +1     
  Lines        7490     7516      +26     
  Branches     2148     2153       +5     
==========================================
+ Hits         7077     7100      +23     
  Misses        178      178              
- Partials      235      238       +3     
Flag Coverage Δ
#unittest 94.46% <88.46%> (-0.03%) ⬇️
Impacted Files Coverage Δ
...ackages/eslint-plugin/src/rules/no-invalid-this.ts 88.46% <88.46%> (ø)

@bradzacher
Copy link
Member

OOC, How come you even want to use this rule? Typescript does this check in various forms already.

It seems pretty redundant to use this rule.

@bradzacher bradzacher added the enhancement: new base rule extension New base rule extension required to handle a TS specific case label Mar 25, 2020
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

Logic LGTM.
A few comments.
Please add documentation as per comment.

There were some lint changes recently that are firing on your tests, please address them.

Thanks for your work!

description:
'Disallow `this` keywords outside of classes or class-like objects',
category: 'Best Practices',
recommended: false,
Copy link
Member

Choose a reason for hiding this comment

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

Mark the rule as an extension rule.
You'll have to regen the configs and docs after this change.

Suggested change
recommended: false,
recommended: false,
extendsBaseRule: true,

@@ -0,0 +1,159 @@
import baseRule from 'eslint/lib/rules/no-invalid-this';
Copy link
Member

Choose a reason for hiding this comment

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

it looks like you're not actually using baseRule inside this rule because you had to fork the logic.
In this case we should locally define the types and the schema.

We don't want to reference the parent rule's schema here because if they add a new option, we won't have handling for it until we add it to our fork.

Locally define the Options/MessageIds to match as well - we don't need the separate module type declarations.

Comment on lines +48 to +49
const stack: CheckingContext[] = [],
sourceCode = context.getSourceCode();
Copy link
Member

Choose a reason for hiding this comment

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

nit

Suggested change
const stack: CheckingContext[] = [],
sourceCode = context.getSourceCode();
const stack: CheckingContext[] = [];
const sourceCode = context.getSourceCode();

@@ -0,0 +1 @@
# Disallow `this` keywords outside of classes or class-like objects (`no-invalid-this`)
Copy link
Member

Choose a reason for hiding this comment

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

Example of an extension rule doc:

https://github.com/typescript-eslint/typescript-eslint/blob/1fd86befa6a940a0354c619dd2da08a5c5d69fb4/packages/eslint-plugin/docs/rules/comma-spacing.md

Key points:

  • explain what we've added
  • show how to turn it on
  • link to the base rule docs.

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Apr 12, 2020
@bradzacher bradzacher changed the title feat(eslint-plugin): [no-invalid-this] Add rule and allow ArrowFE methods feat(eslint-plugin): add rule [no-invalid-this] too support arrow function class properties Apr 12, 2020
@bradzacher bradzacher changed the title feat(eslint-plugin): add rule [no-invalid-this] too support arrow function class properties feat(eslint-plugin): add extension rule [no-invalid-this] too support arrow function class properties Apr 12, 2020
@bradzacher
Copy link
Member

closing in favour of #1823

@bradzacher bradzacher closed this May 4, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party enhancement: new base rule extension New base rule extension required to handle a TS specific case
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[no-invalid-this] Add improved no-invalid-this rule that allow this access in class field arrow functions
1 participant