From 38b4c9a1e55da09cbfb5d4bfe00f46fb64e2d91f Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Tue, 26 Mar 2024 21:04:43 -0500 Subject: [PATCH] fix(imports): Always add relative imports to the `other` sort group Fixes #62 --- src/__tests__/exports.spec.ts | 61 +++++++++++++++++++++++++++++++++-- src/__tests__/imports.spec.ts | 57 ++++++++++++++++++++++++++++++++ src/resolver.ts | 6 ++++ src/test-utils.ts | 5 ++- 4 files changed, 126 insertions(+), 3 deletions(-) diff --git a/src/__tests__/exports.spec.ts b/src/__tests__/exports.spec.ts index 9d595a7..b4f8c70 100644 --- a/src/__tests__/exports.spec.ts +++ b/src/__tests__/exports.spec.ts @@ -98,6 +98,29 @@ createRuleTester().run("sort/exports", rule, { }, ], }, + { + name: "Ignores relative paths when detecting depedencies", + code: dedent` + const mark = '' + + export default React + export { depA } from 'dependency-a' + export { depB } from 'dependency-b' + export { relB } from '../../relative-from-node-modules.js' + export { relA } from './a' + export { mark } + `.trim(), + options: [ + { + groups: [ + { type: "default", order: 10 }, + { type: "sourceless", order: 50 }, + { type: "dependency", order: 30 }, + { type: "other", order: 40 }, + ], + }, + ], + }, ], invalid: [ { @@ -291,7 +314,7 @@ createRuleTester().run("sort/exports", rule, { export * from 'a' export { b } from 'b' export { mark } - `.trim(), + `.trim(), options: [ { groups: [ @@ -305,6 +328,40 @@ createRuleTester().run("sort/exports", rule, { ], errors: [{ messageId: "unsorted" }], }, + { + name: "Ignores relative paths when detecting depedencies", + code: dedent` + const mark = '' + + export { depB } from 'dependency-b' + export { mark } + export default React + export { relB } from '../../relative-from-node-modules.js' + export { relA } from './a' + export { depA } from 'dependency-a' + `.trim(), + output: dedent` + const mark = '' + + export default React + export { depA } from 'dependency-a' + export { depB } from 'dependency-b' + export { relB } from '../../relative-from-node-modules.js' + export { relA } from './a' + export { mark } + `.trim(), + options: [ + { + groups: [ + { type: "default", order: 10 }, + { type: "sourceless", order: 50 }, + { type: "dependency", order: 30 }, + { type: "other", order: 40 }, + ], + }, + ], + errors: [{ messageId: "unsorted" }], + }, ], }) @@ -456,7 +513,7 @@ createRuleTester({ export type { A } from 'dependency-a' export { mark } export default React - `.trim(), + `.trim(), options: [ { groups: [ diff --git a/src/__tests__/imports.spec.ts b/src/__tests__/imports.spec.ts index ecc5c87..6f791f9 100644 --- a/src/__tests__/imports.spec.ts +++ b/src/__tests__/imports.spec.ts @@ -106,6 +106,29 @@ createRuleTester().run("sort/imports", rule, { }, ], }, + { + name: "Ignores relative paths when detecting depedencies", + code: dedent` + import 'index.css' + import 'side-effect' + import a from "dependency-b" + import b from "dependency-c" + import d from "b.jpg" + import c from "../../relative-from-node-modules.js" + import h from "../b" + import i from "./b" + `, + options: [ + { + groups: [ + { type: "side-effect", order: 10 }, + { regex: "\\.(png|jpg)$", order: 30 }, + { type: "dependency", order: 20 }, + { type: "other", order: 40 }, + ], + }, + ], + }, // Separator { @@ -1111,5 +1134,39 @@ createRuleTester({ }, ], }, + { + name: "Ignores relative paths when detecting depedencies", + code: dedent` + import 'index.css' + import c from "../../relative-from-node-modules.js" + import b from "dependency-c" + import d from "b.jpg" + import 'side-effect' + import h from "../b" + import a from "dependency-b" + import i from "./b" + `, + errors: [{ messageId: "unsorted" }], + output: dedent` + import 'index.css' + import 'side-effect' + import a from "dependency-b" + import b from "dependency-c" + import d from "b.jpg" + import c from "../../relative-from-node-modules.js" + import h from "../b" + import i from "./b" + `, + options: [ + { + groups: [ + { type: "side-effect", order: 10 }, + { regex: "\\.(png|jpg)$", order: 30 }, + { type: "dependency", order: 20 }, + { type: "other", order: 40 }, + ], + }, + ], + }, ], }) diff --git a/src/resolver.ts b/src/resolver.ts index fa7488a..ef3c1e7 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -3,6 +3,12 @@ import resolve from "isomorphic-resolve" const resolveCache = new Map() export function isResolved(source: string) { + // Ignore relative imports to prevent false positives where a path is + // resolvable from the node_modules directory and elsewhere in the project. + if (source.startsWith(".")) { + return false + } + if (globalThis.resolver) { return globalThis.resolver(source) } diff --git a/src/test-utils.ts b/src/test-utils.ts index 992fad1..2ed9cae 100644 --- a/src/test-utils.ts +++ b/src/test-utils.ts @@ -3,7 +3,10 @@ import { RuleTester } from "eslint" import { it, describe } from "vitest" globalThis.resolver = (source) => { - return source.startsWith("dependency-") + return ( + source.startsWith("dependency-") || + source === "../../relative-from-node-modules.js" + ) } export function createValidCodeVariants(