Skip to content

Commit

Permalink
Merge pull request #210 from arethetypeswrong/bug/208
Browse files Browse the repository at this point in the history
Process all re-exports in Named Exports rule
  • Loading branch information
andrewbranch authored Sep 17, 2024
2 parents d1272e8 + 3ca2866 commit f5cb8cb
Show file tree
Hide file tree
Showing 7 changed files with 1,479 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-rings-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@arethetypeswrong/core": patch
---

Fix bug in Named Exports rule that could cause false positives for re-exported values
1 change: 1 addition & 0 deletions packages/cli/test/snapshots.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const tests = [
],

["eslint-module-utils@2.8.1.tgz", "--entrypoints-legacy --ignore-rules=cjs-only-exports-default"],
["@cerbos__core@0.18.1.tgz"],
];

const defaultOpts = "-f table-flipped";
Expand Down
21 changes: 21 additions & 0 deletions packages/cli/test/snapshots/@cerbos__core@0.18.1.tgz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# @cerbos__core@0.18.1.tgz

```
$ attw @cerbos__core@0.18.1.tgz -f table-flipped
@cerbos/core v0.18.1
No problems found 🌟
┌────────────────┬────────┬───────────────────┬───────────────────┬─────────┐
│ │ node10 │ node16 (from CJS) │ node16 (from ESM) │ bundler │
├────────────────┼────────┼───────────────────┼───────────────────┼─────────┤
│ "@cerbos/core" │ 🟢 │ 🟢 (CJS) │ 🟢 (CJS) │ 🟢 │
└────────────────┴────────┴───────────────────┴───────────────────┴─────────┘
```

Exit code: 0
33 changes: 15 additions & 18 deletions packages/core/src/internal/esm/cjsNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ import { Package } from "../../createPackage.js";
import { getCjsModuleBindings } from "./cjsBindings.js";
import { cjsResolve } from "./cjsResolve.js";

export function getCjsModuleNamespace(fs: Package, file: URL, seen = new Set<string>()) {
export function getCjsModuleNamespace(fs: Package, file: URL, seen = new Set<string>()): Set<string> {
seen.add(file.pathname);
const { exports, reexports } = getCjsModuleBindings(fs.readFile(file.pathname));
const exports = new Set<string>();
const bindings = getCjsModuleBindings(fs.readFile(file.pathname));
bindings.exports.forEach((name) => exports.add(name));

// CJS always exports `default`
if (!exports.includes("default")) {
exports.push("default");
if (!exports.has("default")) {
exports.add("default");
}

// Additionally, resolve facade reexports
const lastResolvableReexport = (() => {
for (const source of reexports.reverse()) {
try {
return cjsResolve(fs, source, file);
} catch {}
}
})();
if (
lastResolvableReexport &&
lastResolvableReexport.format === "commonjs" &&
!seen.has(lastResolvableReexport.resolved.pathname)
) {
const extra = getCjsModuleNamespace(fs, lastResolvableReexport.resolved, seen);
exports.push(...extra.filter((name) => !exports.includes(name)));

for (const source of bindings.reexports.reverse()) {
try {
const { format, resolved } = cjsResolve(fs, source, file);
if (format === "commonjs" && !seen.has(resolved.pathname)) {
const reexported = getCjsModuleNamespace(fs, resolved, seen);
reexported.forEach((name) => exports.add(name));
}
} catch {}
}

return exports;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/internal/esm/esmNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function getEsmModuleNamespace(
seen.add(resolved.pathname);

if (format === "commonjs") {
return getCjsModuleNamespace(fs, resolved);
return [...getCjsModuleNamespace(fs, resolved)];
}

// Parse module bindings
Expand Down
Binary file not shown.
Loading

0 comments on commit f5cb8cb

Please sign in to comment.