Bug: Violation of consistent-this
rule due to global and function scope mismatch in variable assignment #19244
Description
Environment
Node version: 20.16.0
npm version: 9.8.1
Local ESLint version: 9.10
Global ESLint version: 9.10
Operating System: Mac OS
What parser are you using?
Default (Espree)
What did you do?
I wrote the following code, which declares a variable (that) without initializing it immediately and then assigns this to it inside a function:
var that; // Declaration of 'that' (but no initialization)
function f() {
that = this; // Assignment of 'this' to 'that'
}
The above code is the same example mentioned in the docs as incorrect.
What did you expect to happen?
I expected ESLint to show an error for the consistent-this rule because the rule mandates that:
According to the docs:
If a variable (like that) is declared to capture this, it must be either initialized with this immediately in the same scope or assigned this within the same scope.
The variable is declared globally but assigned inside a function, which should violate the rule.
What actually happened?
ESLint did not show an error. The rule appears to be not enforcing the condition that requires the assignment of this to occur in the same scope as the declaration. The code is allowed to pass without any warnings or errors, even though it seems to violate the intended behavior of the consistent-this rule.
Link to Minimal Reproducible Example
Participation
- I am willing to submit a pull request for this issue.
Additional comments
No response
Metadata
Assignees
Labels
Type
Projects
Status
Ready to Implement
Activity