Skip to content

Commit

Permalink
Fix a namespace import not causing the default export to be considere…
Browse files Browse the repository at this point in the history
…d used.
  • Loading branch information
jaydenseric committed Aug 8, 2024
1 parent fef1722 commit b9323ff
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 7 additions & 13 deletions findUnusedExports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions findUnusedExports.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit b9323ff

Please sign in to comment.