Skip to content

Change Request: Add isGlobalReference() to SourceCode #19651

@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 SourceCode to encapsulate this functionality:

const isGlobal = sourceCode.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:

function isGlobalReference(node) {
const scope = sourceCode.getScope(node);
const variable = findVariable(scope, node);
return (
variable !== null &&
variable.scope.type === "global" &&
variable.defs.length === 0
);
}

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

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusioncoreRelates to ESLint's core APIs and featuresenhancementThis change enhances an existing feature of ESLintevaluatingThe team will evaluate this issue to decide whether it meets the criteria for inclusion

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions