Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check exports exist before access to it #2990

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/rules/no-unused-modules.js
Expand Up @@ -534,15 +534,15 @@ module.exports = {
}

// special case: export * from
const exportAll = exports.get(EXPORT_ALL_DECLARATION);
const exportAll = exports && exports.get(EXPORT_ALL_DECLARATION);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need to report from line 567 to be shown:

context.report(
          node,
          `exported declaration '${value}' not used within other modules`,
        );

At the end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error console says the file has no exports then why it should report any export error? Or that just means an internal bug?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scenario is following:

  1. create new file and define export it in
  2. see eslint error that export is not used anywhere.

Without this PR we see eslint crash and no longer provide other checks and autofixes.
I assume this is internal bug mentioned in console warning, file has export, so this should be fixed somehow. till then this fix works as well

if (typeof exportAll !== 'undefined' && exportedValue !== IMPORT_DEFAULT_SPECIFIER) {
if (exportAll.whereUsed.size > 0) {
return;
}
}

// special case: namespace import
const namespaceImports = exports.get(IMPORT_NAMESPACE_SPECIFIER);
const namespaceImports = exports && exports.get(IMPORT_NAMESPACE_SPECIFIER);
if (typeof namespaceImports !== 'undefined') {
if (namespaceImports.whereUsed.size > 0) {
return;
Expand All @@ -552,7 +552,7 @@ module.exports = {
// exportsList will always map any imported value of 'default' to 'ImportDefaultSpecifier'
const exportsKey = exportedValue === DEFAULT ? IMPORT_DEFAULT_SPECIFIER : exportedValue;

const exportStatement = exports.get(exportsKey);
const exportStatement = exports && exports.get(exportsKey);

const value = exportsKey === IMPORT_DEFAULT_SPECIFIER ? DEFAULT : exportsKey;

Expand Down