prettier-plugin-sort-imports icon indicating copy to clipboard operation
prettier-plugin-sort-imports copied to clipboard

Order local files by relative path

Open nunocasteleira opened this issue 3 years ago • 9 comments

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

Originally posted by nunocasteleira February 4, 2022 With "importOrder": ["^react$", "^react", "<THIRD_PARTY_MODULES>", "^@.*", "^[./]"] I get the following:

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:

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!

nunocasteleira avatar Feb 09 '22 14:02 nunocasteleira

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?).

fbartho avatar Feb 10 '22 22:02 fbartho

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

nunocasteleira avatar Feb 14 '22 10:02 nunocasteleira

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

tanmayairbase avatar Feb 15 '22 12:02 tanmayairbase

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.

nunocasteleira avatar Feb 16 '22 16:02 nunocasteleira

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

"importOrder": ["^react$", "^react", "<THIRD_PARTY_MODULES>", "^@.*", "^./(.*)", "^../(.*)"]

condemil avatar May 13 '22 06:05 condemil

@nunocasteleira did you find a solution that works?

jgarciamccausland avatar May 25 '22 14:05 jgarciamccausland

@nunocasteleira did you find a solution that works?

no, I didn't :/

nunocasteleira avatar Jun 07 '22 13:06 nunocasteleira

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.

jgarciamccausland avatar Jun 07 '22 14:06 jgarciamccausland

Still no mitigations yet?

skyf0l avatar Feb 06 '24 08:02 skyf0l