import-sort icon indicating copy to clipboard operation
import-sort copied to clipboard

`import-sort-style-module` treats absolute first-party imports as third-party

Open linvain opened this issue 5 years ago • 9 comments

Tilda is a common alias for project root. For example, Parcel uses it by default.

import { Main } from "~/components/Main"

linvain avatar May 31 '19 17:05 linvain

We could add tilda check here

@renke are you willing to make such change?

linvain avatar May 31 '19 18:05 linvain

According to validate-npm-package-name:

package name should not contain any of the following characters: ~)('!*

So it's safe to do such change

linvain avatar May 31 '19 18:05 linvain

Hi @linvain

I am happy to accept a PR for this. Extending may also be more reasonable here, because I am not sure if people like to treat those exports as relative or absolute. Something like isRootModule or even isTildeModule.

renke avatar Sep 21 '19 15:09 renke

@renke @linvain I think a PR enabling only tilde (~) imports is too narrow. Some projects do root-relative imports by setting an alias for the root.

For example, you can configure both Webpack and TypeScript to allow absolute path imports:

import Foo from '/root/path/to/foo';

izolate avatar Oct 21 '19 08:10 izolate

We can let user define function isAbsolute(path) or regex absolutePathRegex

We can use jsconfig.json syntax:

    "paths": {
      "~/*" : ["./src/*"]
    }

Or even implement something like eslint-import-resolver-webpack

linvain avatar Oct 23 '19 23:10 linvain

+1 on this, would be very useful to parse jsconfig.json for import path aliasing. It's the one thing stopping me from using this on a few projects as they are quite large and use aliasing to avoid ../../../ etc.

One thing to note about a lot of aliasing setups is there is no prefix character, like / or ~ for example, some people just use import x from "components/y/z" so parsing jsconfig for a set of prefixes would be best.

Southclaws avatar Nov 12 '19 11:11 Southclaws

Hi @linvain any plans to do this? I also use ~ syntax like ~components/ ~consts/ for root and my local files goes to the top of the file - above third party libs. For me it could be easy - simple field inside package.json with paths. But jsconfig probably would be the most universal.

dkoprowski avatar Jun 03 '20 15:06 dkoprowski

I agree with all of this. In general it would be really good if it dealt with aliases (js and ts).

For the moment I have found this issue: https://github.com/renke/import-sort/issues/26

And a style that allows you to do it manually: https://github.com/fsmaia/import-sort-style-module-alias

kekel87 avatar Jun 05 '20 10:06 kekel87

@kekel87 Thanks, it works :)

dkoprowski avatar Jun 05 '20 11:06 dkoprowski