trivago / prettier-plugin-sort-imports

A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order.
Apache License 2.0
3.26k stars 128 forks source link

Order local files by relative path #127

Open nunocasteleira opened 2 years ago

nunocasteleira commented 2 years ago

Discussed in https://github.com/trivago/prettier-plugin-sort-imports/discussions/126

Originally posted by **nunocasteleira** February 4, 2022 With `"importOrder": ["^react$", "^react", "", "^@.*", "^[./]"]` I get the following: ```js import React from 'react'; import { useHistory } from 'react-router-dom'; import classNames from 'classnames'; import { Container, Link, Typography } from '@material-ui/core'; import { ReactComponent as Illustration} from '../../../../assets/svg/illustration.svg'; import { RoutePaths } from '../../../App/Router/RouterConstants'; import Button from '../../../Shared/Button/Button'; import useStyles from './Component.style'; ``` I would like to get the following: ```js import React from 'react'; import { useHistory } from 'react-router-dom'; import classNames from 'classnames'; import { Container, Link, Typography } from '@material-ui/core'; import useStyles from './Component.style'; import { RoutePaths } from '../../../App/Router/RouterConstants'; import Button from '../../../Shared/Button/Button'; import { ReactComponent as Illustration} from '../../../../assets/svg/illustration.svg'; ``` What can I do? Thanks!
fbartho commented 2 years ago

I too would expect relative-paths to have a consistent ordering.

I think there's a secondary ask above where @nunocasteleira may also want control over how the relative-path specifiers compare to each other (./Foo should it be above or below ../Bar?).

nunocasteleira commented 2 years ago

@fbartho is absolutely right. I would like ./Foo to come before ../Bar, and then come ../../Bar and ../../Foo.

tanmayairbase commented 2 years ago

I guess we could do "^[.]\\/", "^\.\.\\/", instead of (or before) "^[./]"?

nunocasteleira commented 2 years ago

Thanks for your comment, @tanmayairbase, but it doesn't do what I'm looking for. "^\.\.\\/" this escaped string throws an error and that approach relies on specifying every relative path ad infinity.

condemil commented 2 years ago

You can separate imports that starts with ./ and ../ into two separate groups:

"importOrder": ["^react$", "^react", "<THIRD_PARTY_MODULES>", "^@.*", "^./(.*)", "^../(.*)"]
jgarciamccausland commented 2 years ago

@nunocasteleira did you find a solution that works?

nunocasteleira commented 2 years ago

@nunocasteleira did you find a solution that works?

no, I didn't :/

jgarciamccausland commented 2 years ago

So we ended up using somewhat of a workaround with: "importOrder": ["^react$", "^react", "<THIRD_PARTY_MODULES>", "^@.*$", "^./(.*)", "^[./]"],

It will have ./ imports before ../ but still have nested ../../ imports backward. So something like the following:

./Bar
./Foo
../../../Bar
../../../Foo
../../Bar
../../Foo
../Bar
../Foo

Not ideal but we decided to roll with it for now.

skyf0l commented 7 months ago

Still no mitigations yet?