Bug: no-invalid-this: false positive with this in class field
See original GitHub issueEnvironment
Node version: v16.13.1 npm version: 8.3.0 Local ESLint version: 8.6.0 Global ESLint version: N/A Operating System: Ubuntu 20.04.2 LTS
What parser are you using?
Default (Espree)
What did you do?
Configuration
{
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"no-invalid-this": 2
}
}
class Foo {
bar = this;
}
What did you expect to happen?
No error reported.
What actually happened?
$ npx eslint index.js
/home/regseb/testcase/index.js
2:11 error Unexpected 'this' no-invalid-this
✖ 1 problem (1 error, 0 warnings)
Participation
- I am willing to submit a pull request for this issue.
Additional comments
No error reported with this configuration (without "sourceType": "module"
):
{
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
"no-invalid-this": 2
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
False positive "(ESLint) no-invalid-this" in class property method
Class fields only recently gained stage 4, so eslint doesn't support them yet (see #14343). (Separately, arrow functions in a class field are ......
Read more >ESLint v8.7.0 released - ESLint - Pluggable JavaScript Linter
This release adds some new features and fixes several bugs found in the ... a8db9a5 fix: no-invalid-this false positive in class field ...
Read more >Documentation bug for S4035 - Sonar Community
The code shown behaves correctly and is no example for the issue. To make it illustrate the problem, class B must inherit from...
Read more >Code Issues - Embold Help Center
Similarly, any defect or bug found in the code leads to code issues. ... DisposeFieldsProperly, This class has a disposable field and is...
Read more >node_modules/@typescript-eslint/eslint-plugin/CHANGELOG ...
eslint-plugin: [no-unnecessary-condition] false positive when array ... eslint-plugin: [no-invalid-this] allow "this" in class property ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
To clarify, this rule only flags any use of
this
where the value ofthis
isundefined
(for example, at the top level of modules), which isn’t the case in the provided example. Ifthis
is an object, the rule doesn’t make assumptions about whether a particular usage makes sense or not.This bug is reproducible in v8.0.0 as well. It happens only when
PropertyDefinition#value
isThisExpression
. Due to a higher specificity of"PropertyDefinition > *.value"
,ThisExpression
gets called first and is therefore mistakenly treated as if it were in an outer context.I’ll work on the fix.
ah, i misunderstood the purpose of the rule then.