Skip to content
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

Support // sort-imports-ignore #230

Closed
shalev-kaveh opened this issue May 16, 2023 · 10 comments
Closed

Support // sort-imports-ignore #230

shalev-kaveh opened this issue May 16, 2023 · 10 comments
Labels
feature request A new feature request

Comments

@shalev-kaveh
Copy link

shalev-kaveh commented May 16, 2023

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: #26 due to the large demand by the community

@shalev-kaveh shalev-kaveh added the feature request A new feature request label May 16, 2023
@ElForastero
Copy link

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
Copy link
Contributor

Sorry @ayusharma or @byara for the ping, but do you think you'll have a chance to review this PR anytime soon ? #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
Copy link

kiliman commented Jun 30, 2023

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
Copy link

bdombro commented Oct 23, 2023

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

@kiliman
Copy link

kiliman commented Oct 23, 2023

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
Copy link
Author

Resolved at #237
Thanks!

@bdombro
Copy link

bdombro commented Nov 9, 2023

No new release yet though... :-/

@byara
Copy link
Collaborator

byara commented Nov 10, 2023

Released in v4.3.0

@bdombro
Copy link

bdombro commented Nov 10, 2023

TY!

@volosovich
Copy link

TY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A new feature request
Projects
None yet
Development

No branches or pull requests

7 participants