Skip to content

Bug: Predefined globals are left in GlobalScope#implicit #19766

@mdjermanovic

Description

@mdjermanovic

Environment

Node version: v22.14.0
npm version: 10.9.2
Local ESLint version: v9.27.0
Global ESLint version: no
Operating System: windows

What parser are you using?

Default (Espree)

What did you do?

Configuration
function formatReference(reference) {
  const { identifier } = reference;
  const position = identifier.loc.start;

  return `${identifier.name}(${position.line}:${position.column + 1})`;
}

export default [
  {
    plugins: {
      'test-plugin': {
        rules: {
          'test-rule': {
            create(context) {
              return {
                Program(node) {
                  const globalScope = context.sourceCode.getScope(node);

                  console.log(
                    'Array variable is in the global scope?',
                    globalScope.set.has('Array') &&
                      globalScope.variables.some(({ name }) => name === 'Array')
                  );

                  console.log(
                    'Array variable references in the global scope:',
                    globalScope.set
                      .get('Array')
                      .references.map(formatReference)
                      .join(', ')
                  );

                  console.log(
                    'Array variable is in GlobalScope#implicit?',
                    globalScope.implicit.set.has('Array') &&
                      globalScope.implicit.variables.some(
                        ({ name }) => name === 'Array'
                      )
                  );

                  console.log(
                    'GlobalScope#implicit.left:',
                    globalScope.implicit.left.map(formatReference).join(', ')
                  );
                },
              };
            },
          },
        },
      },
    },
    rules: {
      'test-plugin/test-rule': 2,
    },
    languageOptions: {
      sourceType: 'script',
    },
  },
];
Array = 1;

Array;

What did you expect to happen?

I'd expect the Array variable not to be in GlobalScope#implicit since ESLint adds it as a predefined global variable.

What actually happened?

Array variable is in the global scope? true
Array variable references in the global scope: Array(1:1), Array(3:1)
Array variable is in GlobalScope#implicit? true
GlobalScope#implicit.left: Array(1:1), Array(3:1)

Link to Minimal Reproducible Example

https://stackblitz.com/edit/stackblitz-starters-2uhwupap?file=index.js

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 incorrectlycoreRelates to ESLint's core APIs and featuresrepro:yesIssues with a reproducible example

Type

No type

Projects

Status

Complete

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions