From 36c4c6b5e600ad81c0874187df0b0d7cdfc44379 Mon Sep 17 00:00:00 2001 From: zanminkian Date: Wed, 25 Dec 2024 12:26:28 +0000 Subject: [PATCH] fix(eslint-plugin-esm): report on `./..` and `./` --- .changeset/young-dogs-heal.md | 5 +++++ .../eslint-plugin-esm/doc/rules/nearest-relative-path.md | 6 ++++++ .../src/rules/nearest-relative-path.spec.ts | 8 ++++++++ .../eslint-plugin-esm/src/rules/nearest-relative-path.ts | 5 ++++- 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 .changeset/young-dogs-heal.md diff --git a/.changeset/young-dogs-heal.md b/.changeset/young-dogs-heal.md new file mode 100644 index 00000000..e533572b --- /dev/null +++ b/.changeset/young-dogs-heal.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-esm": patch +--- + +fix(eslint-plugin-esm): report on `./..` and `./` diff --git a/packages/eslint-plugin-esm/doc/rules/nearest-relative-path.md b/packages/eslint-plugin-esm/doc/rules/nearest-relative-path.md index 1d7328d3..8b69220b 100644 --- a/packages/eslint-plugin-esm/doc/rules/nearest-relative-path.md +++ b/packages/eslint-plugin-esm/doc/rules/nearest-relative-path.md @@ -27,6 +27,10 @@ import "./../foo" // filename: /a/b/c/d/e.js import("./../foo") // filename: /a/b/c/d/e.js export * from "./../foo" // filename: /a/b/c/d/e.js export {a} from "./../foo" // filename: /a/b/c/d/e.js +import foo from "./" // filename: /a/b/c/d/e.js +import foo from '../' // filename: /a/b/c/d/e.js +import foo from '../../' // filename: /a/b/c/d/e.js +import foo from './..' // filename: /a/b/c/d/e.js ``` ### Pass @@ -44,5 +48,7 @@ export * from "./a" // filename: /a/b/c/d/e.js export {a} from "a" // filename: /a/b/c/d/e.js export {a} from "./a" // filename: /a/b/c/d/e.js import foo from "." // filename: /a/b/c/d/e.js +import foo from '..' // filename: /a/b/c/d/e.js +import foo from '../..' // filename: /a/b/c/d/e.js ``` diff --git a/packages/eslint-plugin-esm/src/rules/nearest-relative-path.spec.ts b/packages/eslint-plugin-esm/src/rules/nearest-relative-path.spec.ts index d7acc998..7cf90dc2 100644 --- a/packages/eslint-plugin-esm/src/rules/nearest-relative-path.spec.ts +++ b/packages/eslint-plugin-esm/src/rules/nearest-relative-path.spec.ts @@ -13,7 +13,10 @@ const valid = [ 'export * from "./a"', 'export {a} from "a"', 'export {a} from "./a"', + 'import foo from "."', + "import foo from '..'", + "import foo from '../..'", ].map((code) => ({ code, filename: "/a/b/c/d/e.js" })); const invalid = [ @@ -39,6 +42,11 @@ const invalid = [ 'import("./../foo")', 'export * from "./../foo"', 'export {a} from "./../foo"', + + 'import foo from "./"', + "import foo from '../'", + "import foo from '../../'", + "import foo from './..'", ].map((code) => ({ code, filename: "/a/b/c/d/e.js" })); test({ valid, invalid, ...nearestRelativePath }); diff --git a/packages/eslint-plugin-esm/src/rules/nearest-relative-path.ts b/packages/eslint-plugin-esm/src/rules/nearest-relative-path.ts index b53c3e3e..6dc320f5 100644 --- a/packages/eslint-plugin-esm/src/rules/nearest-relative-path.ts +++ b/packages/eslint-plugin-esm/src/rules/nearest-relative-path.ts @@ -15,13 +15,16 @@ function check(filename: string, source: string) { ) { return false; } + if (source.endsWith("/")) { + return true; + } const currentPath = path.dirname(filename); const absoluteSource = path.resolve(currentPath, source); // compatible with windows let resultPath = path .relative(currentPath, absoluteSource) .replaceAll("\\", "/"); - if (!resultPath.startsWith("./") && !resultPath.startsWith("../")) { + if (!resultPath.startsWith("./") && !resultPath.startsWith("..")) { resultPath = `./${resultPath}`; } return resultPath !== source;