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

feat: added support for sort-imports-ignore #237

Merged
merged 6 commits into from Oct 24, 2023
Merged

feat: added support for sort-imports-ignore #237

merged 6 commits into from Oct 24, 2023

Conversation

stephdotnet
Copy link
Contributor

Hi,

Thank you for this amazing package. I have been using it on some of my projects. However, recently I needed to ignore the import order for certain specific files.

I encountered this issue: #230 and decided to contribute to the solution.

I chose to follow the basic idea of ignoring the file if a leading comment // sort-imports-ignore is detected (as you mentioned in a previous issue, it is tricky to ignore specific lines, and we will probably need an RFC before diving into it: #26).

I attempted to follow your approach for parsing and formatting the code, which was very interesting by the way, and added the necessary tests.

Please feel free to ask for more if needed, and I will make the appropriate changes.

@ayusharma ayusharma requested review from byara and ayusharma and removed request for byara June 26, 2023 09:41
@d0whc3r
Copy link

d0whc3r commented Jun 27, 2023

maybe the string to search for ignore could be in the "constants.ts" file

@stephdotnet
Copy link
Contributor Author

maybe the string to search for ignore could be in the "constants.ts" file

I actually didn't notice the constants.ts file and i'll add that right the way. Nice catch @d0whc3r

@codemers
Copy link

Would really like this feature to! Thanks @stephdotnet

@bdombro
Copy link

bdombro commented Oct 23, 2023

@ayusharma could you please review this? Seems to be a popular feature request.

@ayusharma ayusharma requested a review from byara October 23, 2023 18:48
Copy link
Collaborator

@ayusharma ayusharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me ❤️ @byara can you please check 🙏

@byara byara merged commit c8bb4fa into trivago:main Oct 24, 2023
5 checks passed
@byara
Copy link
Collaborator

byara commented Nov 10, 2023

Released in v4.3.0

@danielR2001
Copy link

danielR2001 commented Nov 18, 2023

@byara I did add // sort-imports-ignore but it still sorts the imports on save (I have prettier on save) 😞
Do I need something else?
this is my prettierrc

{
  "plugins": [
    "@trivago/prettier-plugin-sort-imports"
  ],
  "importOrder": [
    "^@(?!library1|library2)",
    "^[./]"
  ],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "importOrderParserPlugins": [
    "typescript",
    "decorators-legacy"
  ]
}

@TheMikeyRoss
Copy link

@byara I did add // sort-imports-ignore but it still sorts the imports on save (I have prettier on save) 😞 Do I need something else? this is my prettierrc

{
  "plugins": [
    "@trivago/prettier-plugin-sort-imports"
  ],
  "importOrder": [
    "^@(?!library1|library2)",
    "^[./]"
  ],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "importOrderParserPlugins": [
    "typescript",
    "decorators-legacy"
  ]
}

Same for me as well,

@danielR2001 Did you manage to resolve it?

@zaaakher
Copy link

zaaakher commented Dec 9, 2023

@TheMikeyRoss @danielR2001 Make sure // sort-imports-ignore is at the very top of the file

@TheMikeyRoss
Copy link

@TheMikeyRoss Make sure // sort-imports-ignore is at the very top of the file

Thanks @zaaakher that fixed it for me

@danielR2001
Copy link

@TheMikeyRoss @danielR2001 Make sure // sort-imports-ignore is at the very top of the file

Thanks @zaaakher , that fixed for me too!

@qiutian00
Copy link

@TheMikeyRoss @danielR2001 Make sure // sort-imports-ignore is at the very top of the file

This works for me.

@farzadmf
Copy link

farzadmf commented May 2, 2024

Hey everyone. Is this also supported in jsx files? I have:

{
  "endOfLine": "auto",
  "importOrder": ["^@/(.*)$", "^[./]"],
  "importOrderParserPlugins": ["typescript", "jsx", "decorators-legacy"],
  "importOrderSeparation": true,
  "importOrderSortSpecifiers": true,
  "plugins": ["@trivago/prettier-plugin-sort-imports"],
  "printWidth": 120,
  "proseWrap": "always",
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "all"
}

But // sort-imports-ignore doesn't stop anything (even when placed at the top of the file)

EDIT: technically, it's a .tsx file that I'm running prettier on

@farzadmf
Copy link

farzadmf commented May 2, 2024

To add to what I wrote above, my "interesting lines" that I don't want to move are for jest:

const myMock = jest.fn();

jest.mock('src/dependency/imported/by/MyModule', () => { /* ... */ });

// import other stuff
// import MyModule (jest.mock call would capture the dpendency here)

// My tests

Now, when imports are sorted, obviously, jest.mock will have no effect. Is there any solution/workaround for this? (I don't want to add the test files to .prettierignore because then I lose all the formatting functionality)

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

Successfully merging this pull request may close these issues.

None yet