Skip to content

Commit

Permalink
Merge pull request #64 from mskelton/fix-relative-imports
Browse files Browse the repository at this point in the history
Fix relative imports
  • Loading branch information
mskelton authored Mar 27, 2024
2 parents fa8de6e + 38b4c9a commit b4b8563
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
61 changes: 59 additions & 2 deletions src/__tests__/exports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -291,7 +314,7 @@ createRuleTester().run("sort/exports", rule, {
export * from 'a'
export { b } from 'b'
export { mark }
`.trim(),
`.trim(),
options: [
{
groups: [
Expand All @@ -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" }],
},
],
})

Expand Down Expand Up @@ -456,7 +513,7 @@ createRuleTester({
export type { A } from 'dependency-a'
export { mark }
export default React
`.trim(),
`.trim(),
options: [
{
groups: [
Expand Down
57 changes: 57 additions & 0 deletions src/__tests__/imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 },
],
},
],
},
],
})
6 changes: 6 additions & 0 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import resolve from "isomorphic-resolve"
const resolveCache = new Map<string, boolean>()

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)
}
Expand Down
5 changes: 4 additions & 1 deletion src/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b4b8563

Please sign in to comment.