Skip to content

Bug: Violation of consistent-this rule due to global and function scope mismatch in variable assignment #19244

Closed
@amareshsm

Description

@amareshsm

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.
Image

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

https://eslint.org/play/#eyJ0ZXh0IjoiLyplc2xpbnQgY29uc2lzdGVudC10aGlzOiBbXCJlcnJvclwiLCBcInRoYXRcIl0qL1xuXG52YXIgdGhhdDtcbmZ1bmN0aW9uIGYoKSB7XG4gICAgdGhhdCA9IHRoaXM7XG59Iiwib3B0aW9ucyI6eyJydWxlcyI6eyJjb25zaXN0ZW50LXRoaXMiOlsiZXJyb3IiXX0sImxhbmd1YWdlT3B0aW9ucyI6eyJzb3VyY2VUeXBlIjoibW9kdWxlIiwicGFyc2VyT3B0aW9ucyI6eyJlY21hRmVhdHVyZXMiOnt9fX19fQ==

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionbugESLint is working incorrectlyrepro:yesIssues with a reproducible exampleruleRelates to ESLint's core rules

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy