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.41k stars 135 forks source link

Support // sort-imports-ignore #230

Closed shalev-kaveh closed 1 year ago

shalev-kaveh commented 1 year ago

Is your feature request related to a problem? Yes, I can't ignore this plugin only on certain files. To ignore import sort, we need to ignore it via .prettierignore, therefore ignoring prettifying the whole file.

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered I suggest a simple fixup, similar to other sort imports plugins provide: https://github.com/simonhaenisch/prettier-plugin-organize-imports/blob/d5ac2f4f2dba549a5a55bf05b3f2f0360e2358e4/index.js#L14

Files containing the substring // sort-imports-ignore or // tslint:disable:sort-imports will be skipped.

So files with the sort imports ignore comment won't be reordered, e.g.

// sort-imports-ignore
import './polyfills';

import foo from 'foo'

Additional context A follow-up on: https://github.com/trivago/prettier-plugin-sort-imports/issues/26 due to the large demand by the community

ElForastero commented 1 year ago

I would like to provide an additional use case for such a rule. I have a singleton configuration object that needs to be imported on the very first line.

// This import MUST be placed before any other app code
import './config';
import { registerRootComponent } from 'expo';
import App from './App';

// registerRootComponent calls AppRegistry.registerComponent('main', () => App);
// It also ensures that whether you load the app in Expo Go or in a native build,
// the environment is set up appropriately
registerRootComponent(App);
stephdotnet commented 1 year ago

Sorry @ayusharma or @byara for the ping, but do you think you'll have a chance to review this PR anytime soon ? https://github.com/trivago/prettier-plugin-sort-imports/pull/237 This PR solves this issue in the simplest way (Sorting will be ignored if we detect this comment : // sort-imports-ignore)

I'll be happy to help if you need to chance something.

Thanks for your work and your time 🙏

kiliman commented 1 year ago

EDIT: Here's a workaround until this feature is implemented.

You can configure the sorting rules, so if you want, you can have the local imports first. Perhaps add a prefix to ensure those are always first, like './!setup-env-vars.ts'

// sort ./!* files first, then normal modules, followed by local files
importOrder: ['^[.]\\/!', '<THIRD_PARTY_MODULES>', '^[.]\\/'],

Although I prefer to avoid relying on side-effects, at least with this naming convention, it will signal to the developer that this is a special file.

image
bdombro commented 1 year ago

I would love to see an ignore rule. The suggested solution, sorting relative first, is absolutely not what I want.

kiliman commented 1 year ago

Look, I was just trying to offer a workaround since the plugin doesn't currently support this use case. Not sure why I got the downvotes. I never said we don't need this.

Also, it's not all relative files first, just ones that have the special prefix. Everything else is sorted at the end.

shalev-kaveh commented 1 year ago

Resolved at https://github.com/trivago/prettier-plugin-sort-imports/pull/237 Thanks!

bdombro commented 1 year ago

No new release yet though... :-/

byara commented 1 year ago

Released in v4.3.0

bdombro commented 1 year ago

TY!

volosovich commented 8 months ago

TY