From b9323ffcdf08df8550395467560920c903d70ec2 Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Thu, 8 Aug 2024 21:11:53 +1000 Subject: [PATCH] Fix a namespace import not causing the default export to be considered used. --- changelog.md | 1 + findUnusedExports.mjs | 20 +++++++------------- findUnusedExports.test.mjs | 4 +--- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/changelog.md b/changelog.md index 024a0cc..f17531d 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ### Patch +- Fixed a namespace import not causing the default export to be considered used. - Improved the internal module `MODULE_GLOB` tests. ## 6.0.0 diff --git a/findUnusedExports.mjs b/findUnusedExports.mjs index 0ecb0a0..879fdb2 100644 --- a/findUnusedExports.mjs +++ b/findUnusedExports.mjs @@ -189,19 +189,13 @@ export default async function findUnusedExports(options = {}) { ); if (importedModulePath) { - if (moduleImports.has("*")) { - // Delete all the named exports from the unused exports set. - for (const name of possiblyUnusedExports[importedModulePath]) - if (name !== "default") - possiblyUnusedExports[importedModulePath].delete(name); - - // Check if a default import also needs to be deleted from the unused - // exports set. - if (moduleImports.has("default")) - possiblyUnusedExports[importedModulePath].delete("default"); - } else - for (const name of moduleImports) - possiblyUnusedExports[importedModulePath].delete(name); + // If a namespace import (`import * as`) imported all exports of the + // module, delete every export from the unused exports set. Otherwise, + // delete only the imported exports from the unused exports set. + for (const name of moduleImports.has("*") + ? possiblyUnusedExports[importedModulePath] + : moduleImports) + possiblyUnusedExports[importedModulePath].delete(name); // Check if the module still has possibly unused exports. if (!possiblyUnusedExports[importedModulePath].size) diff --git a/findUnusedExports.test.mjs b/findUnusedExports.test.mjs index ce5b942..fa5ff2b 100644 --- a/findUnusedExports.test.mjs +++ b/findUnusedExports.test.mjs @@ -80,9 +80,7 @@ describe("Function `findUnusedExports`.", { concurrency: true }, () => { ), ); - deepStrictEqual(await findUnusedExports({ cwd: fixtureProjectPath }), { - [join(fixtureProjectPath, "a.mjs")]: new Set(["default"]), - }); + deepStrictEqual(await findUnusedExports({ cwd: fixtureProjectPath }), {}); }); it("Bare import specifier.", async () => {