this post was submitted on 10 Apr 2024
10 points (100.0% liked)

Cool GitHub Projects

1243 readers
1 users here now

Wormhole

!code_review@programming.dev

Icon base by Caro Asercion under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] FooBarrington@lemmy.world 0 points 7 months ago (4 children)

Cool stuff, I was looking for something like this!

Only thing I'm missing is an option to prefer local imports over alias imports - I like to use alias imports for different modules, but local imports for stuff in the same module (but only downward, never parents). Would such a feature be interesting to you?

[–] vitonsky@programming.dev 1 points 7 months ago (1 children)

Could you elaborate your idea.

If i correct understood you, you want to use relative imports inside alias directory. This is possible, you may check test about it https://github.com/vitonsky/eslint-plugin-paths/blob/d5a307866df9cf460a50301820e12e3653eb1cca/src/rules/alias.test.ts#L36-L40

Is it that you need?

[–] FooBarrington@lemmy.world 1 points 7 months ago (1 children)

It is, but I'd specifically like a rule that enforces this style of import over aliased imports. I.e. when importing a package, the order of importance is:

  1. Relative import (if on same or lower level)
  2. Aliased import (if in ancestor directory or different module)
[–] vitonsky@programming.dev 1 points 7 months ago (1 children)

Current plugin is just about force use aliases instead of relative paths (and back for deprecated aliases).

If you need to sort imports, you may try rule simple-import-sort/imports from https://github.com/lydell/eslint-plugin-simple-import-sort It is very flexible, so you may customize your own groups. For instance, you may group aliases in one group and relative imports to another group. Check their docs

[–] FooBarrington@lemmy.world 1 points 7 months ago* (last edited 7 months ago)

Sorry, maybe I'm not being clear - I'm not talking about sorting. I'm talking about an ESLint rule that flags alias imports that can be expressed as relative subdirectory imports, but flags other imports that could have been alias imports. Does that make sense?

Examples:

// we're in @/lib/foo

// correct
import A from "./foobar/a"
import B from "./b"
import C from "@/lib/c"
import D from "@/d"

// incorrect
import A from "@/lib/foo/foobar/a"
import B from "@/lib/foo/b"
load more comments (2 replies)