Skip to content

Commit

Permalink
fix(eslint-plugin-esm): report on ./.. and ./
Browse files Browse the repository at this point in the history
  • Loading branch information
zanminkian committed Dec 25, 2024
1 parent e6aeea1 commit 36c4c6b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/young-dogs-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-esm": patch
---

fix(eslint-plugin-esm): report on `./..` and `./`
6 changes: 6 additions & 0 deletions packages/eslint-plugin-esm/doc/rules/nearest-relative-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
<!-- prettier-ignore-end -->
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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 });
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 36c4c6b

Please sign in to comment.