Skip to content

[Feature request]: Rewrite aliases to other aliases #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
matthew-dean opened this issue Dec 1, 2023 · 2 comments · May be fixed by #176
Open

[Feature request]: Rewrite aliases to other aliases #150

matthew-dean opened this issue Dec 1, 2023 · 2 comments · May be fixed by #176

Comments

@matthew-dean
Copy link

I'm using Nuxt, and Nuxt maps the "root" directory to ~~. Because of this mapping, VSCode will often autocomplete paths to, say, a script file, to something like:

import { Foo } from '~~/legacy/scripts/Foo'

However, to reduce verbosity in commonly-used paths, I have aliases like ~js to refer to ~~/legacy/scripts.

What I would like is the ability to rewrite not just relative paths to alias paths, but alias paths to other alias paths.

So, on save (or eslint --fix), I would like the above re-written to:

import { Foo } from '~js/Foo'

Is it possible to include more customization into what is re-written? This seems somewhat related to #133, in that some customization / filters would be nice.

@dword-design
Copy link
Owner

dword-design commented Aug 17, 2024

Any proposal on how to solve this? I had the case that I have multiple aliases and one of them is a subpath of another one and the plugin should always select the inner most one but not sure if that totally solves the issue.

@alythobani
Copy link

Great plugin @dword-design :)

+1 to this feature too although it's not a big deal for me at the moment.

Currently it seems like the plugin looks for the first match found in the alias config?

E.g. if this gets automatically imported by VSCode:

import { TextInputWithLabel } from "../../../ui/inputs/TextInput";

And this is my config:

"@dword-design/import-alias/prefer-alias": [
  "error",
  {
    alias: {
      "@": "./src",
      "@ui": "./src/components/ui",
    },
  },
],

Then @dword-design/import-alias/prefer-alias will convert the import into this:

import { TextInputWithLabel } from "@/components/ui/inputs/TextInput";

Versus if this is my config:

"@dword-design/import-alias/prefer-alias": [
  "error",
  {
    alias: {
      "@ui": "./src/components/ui",
      "@": "./src",
    },
  },
],

Then the relative import will be converted into this:

import { TextInputWithLabel } from "@ui/inputs/TextInput";

(Although if the import is already @/components/ui/inputs/TextInput, the plugin won't complain even with this second config)

So one approach could be to just explicitly make this the rule - precedence is determined by order of aliases in the config - and the plugin could check alias imports to make sure there's not an earlier alias in the list that matches the import path.

@dword-design dword-design linked a pull request Apr 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants