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
Conversation
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 Report
@@ 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
|
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. |
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.
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, |
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.
Mark the rule as an extension rule.
You'll have to regen the configs and docs after this change.
recommended: false, | |
recommended: false, | |
extendsBaseRule: true, |
@@ -0,0 +1,159 @@ | |||
import baseRule from 'eslint/lib/rules/no-invalid-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.
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.
const stack: CheckingContext[] = [], | ||
sourceCode = context.getSourceCode(); |
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.
nit
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`) |
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.
Example of an extension rule doc:
Key points:
- explain what we've added
- show how to turn it on
- link to the base rule docs.
closing in favour of #1823 |
Fixes #1571
But does not
fix
#491This PR clones the
no-invalid-this
rule fromeslint
and modifies it to allowArrowFunctionExpression
s within aClass
to referencethis
. I originally attempted to extend the base rule, but found I needed to be able modify what was being pushed to thestack
variable which was inaccessible.I realize this does not cover cases like:
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!