Skip to content

Change Request: Add isGlobalReference() to ScopeManager #649

@nzakas

Description

@nzakas

Which packages would you like to change?

  • espree
  • eslint-scope
  • eslint-visitor-keys

What problem do you want to solve?

There are a lot of ESLint rules that need to determine if a given identifier refers to a global or not. As a result, we have multiple copies of functions that try to determine if an identifier refers to a global:
https://github.com/search?q=repo%3Aeslint%2Feslint%20isglobal&type=code

That also means plugin rules need to implement their own.

What do you think is the correct solution?

I think this is common enough that we should add a isGlobalReference() method to ScopeManager to encapsulate this functionality:

const isGlobal = scopeManager.isGlobalScope(node);

I think the method should accept a node and then determine from that node if it contains a reference to a global variable. Basically, we can copy the code from here:

https://github.com/eslint/eslint/blob/e62e26761561e1d78c6466a2d74dbf946012fddc/lib/rules/prefer-regex-literals.js#L175-L184

Then maybe we could expose the method on SourceCode to make it easier for rules to access.

Participation

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

Additional comments

Looking for a volunteer to take this on.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Needs Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions