Skip to content

Error while loading rule 'import/no-unused-modules': Cannot read properties of undefined (reading 'get') #2388

@tomdglenn91

Description

@tomdglenn91

So this one may be difficult to reproduce as I cannot replicate in a sandbox, but in my big codebase it breaks.

I have a mixture of javascript and typescript as we migrate over to ts, and have just switched on this rule. I have it configured as such:

'import/no-unused-modules': [
          'warn',
          {
            unusedExports: true,
            src: [path.join(__dirname, 'src', 'main')],
            ignoreExports: ['**/white/**'],
          },
        ],

This is turned on for both javascript and the typescript overrides. I basically do not want to use this rule in anything that exists in a folder called white. This is due to some module hacking we are doing to overwrite code at build time.

I get the following stack trace when it errors:

❯ yarn run eslint --ext .js --ext .jsx --ext .ts --ext .tsx src/main/constants
yarn run v1.22.17
$ /Users/tom/code/projects/frontend/node_modules/.bin/eslint --ext .js --ext .jsx --ext .ts --ext .tsx src/main/constants

Oops! Something went wrong! :(

ESLint: 7.32.0

TypeError: Error while loading rule 'import/no-unused-modules': Cannot read properties of undefined (reading 'get')
Occurred while linting /Users/tom/code/projects/frontend/src/main/constants/access-permissions.js
    at /Users/tom/code/projects/frontend/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:277:44
    at Set.forEach (<anonymous>)
    at /Users/tom/code/projects/frontend/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:274:11
    at Map.forEach (<anonymous>)
    at prepareImportsAndExports (/Users/tom/code/projects/frontend/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:273:13)
    at doPreparation (/Users/tom/code/projects/frontend/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:344:3)
    at Object.create (/Users/tom/code/projects/frontend/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:484:9)
    at createRuleListeners (/Users/tom/code/projects/frontend/node_modules/eslint/lib/linter/linter.js:765:21)
    at /Users/tom/code/projects/frontend/node_modules/eslint/lib/linter/linter.js:937:31
    at Array.forEach (<anonymous>)
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I can get around this by going into the code itself in node_modules and changing:

var currentExports = exportList.get(val);
var currentExport = currentExports.get(EXPORT_ALL_DECLARATION);
currentExport.whereUsed.add(key);

to:

var currentExports = exportList.get(val);
if (currentExports) {
  var currentExport = currentExports.get(EXPORT_ALL_DECLARATION);
  currentExport.whereUsed.add(key);
}

I can raise a PR for this if necessary, however before doing so I wanted to check if this seems like a valid solution as I wasn't sure if it could be something my side (I also couldn't figure out your testing pattern in the 10 minutes i looked!)

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions