Skip to content

Commit

Permalink
fix(isValidNodeImport): detect invalid cjs modules (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 25, 2023
1 parent b7adabf commit cfa04fb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ export async function isValidNodeImport(
(await fsp.readFile(resolvedPath, "utf8").catch(() => {})) ||
"";

return hasCJSSyntax(code) || !hasESMSyntax(code);
return !hasESMSyntax(code);
}
2 changes: 2 additions & 0 deletions test/fixture/imports/js-cjs/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// eslint-disable-next-line unicorn/prefer-module
module.exports = "js-cjs";

console.log(import("node:fs"));
12 changes: 12 additions & 0 deletions test/fixture/imports/mixed/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable unicorn/prefer-module */
export const isCrtAvailable = () => {
try {
if (
typeof require === "function" &&
typeof module !== "undefined" &&
require("aws-crt")
) {
return ["md/crt-avail"];
}
} catch {}
};
5 changes: 5 additions & 0 deletions test/fixture/imports/mixed/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "mixed",
"version": "1.0.0",
"main": "index.cjs"
}
1 change: 1 addition & 0 deletions test/syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const nodeImportTests = {
[join(import.meta.url, "../fixture/imports/js-esm")]: false,
[join(import.meta.url, "../fixture/imports/js-esm/es/index.mjs")]: true,
[join(import.meta.url, "../fixture/imports/js-esm/es/index.js")]: false,
[join(import.meta.url, "../fixture/imports/mixed")]: false,
};

describe("isValidNodeImport", () => {
Expand Down

0 comments on commit cfa04fb

Please sign in to comment.