question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sort imports does ignore prettier-ignore statements

See original GitHub issue

Your Environment

  • Prettier version: 2.4.1 / 2.2.1 (both in yarn lock)
  • node version: v17.0.1
  • package manager: yarn@2
  • IDE: Webstorm
  • **prettier-plugin-sort-import: 3.1.1 Describe the bug

To Reproduce

Add the prettier config and add a new js or ts file. There add one import to the top that does not belong to the top usually and try to make it stay at the top via // prettier-ignore (and variants).

Expected behavior When ordering imports I would expect it to be possible to ignore imports that need to be at a certain position (e.g. why-did-you-render wanting to be imported at the top). The prettier-ignore should not be ignored.

Screenshots, code sample, etc

// prettier-ignore-start
import "../../scripts/wdyr"; // must be first import!
// prettier-ignore-end

import { ApolloProvider } from "@apollo/client";
import React from "react";

turns into

// prettier-ignore-start
// must be first import!
// prettier-ignore-end
import { ApolloProvider } from "@apollo/client";
import React from "react";

import "../../scripts/wdyr";

and

// prettier-ignore
import "../../scripts/wdyr"; // must be first import!

import { ApolloProvider } from "@apollo/client";
import React from "react";

turns into

// prettier-ignore
// must be first import!
import { ApolloProvider } from "@apollo/client";
import React from "react";

import "../../scripts/wdyr";

As can be seen, the first import does not stay the first import.

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

module.exports = {
  importOrder: ["^@/(.*)$", "^[./]"],
  importOrderSeparation: true,
};

Error log No log.

Contribute to @trivago/prettier-plugin-sort-imports

  • I’m willing to fix this bug 🥇 (if provided a bit of guidance where to look)

PS: I think I will add an alias @scripts and change the sorting for it to appear at the top. Then maybe importing @scripts/wdyr will stay.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:23
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
Morrizcommented, Jun 2, 2022

yeah, too bad we can’t use this plugin without this fix 😭

5reactions
blutorangecommented, Mar 16, 2022

@denik1981 If it’s only the side-effect imports for which you want to preserve the order, you could try this fork https://www.npmjs.com/package/@ianvs/prettier-plugin-sort-imports which includes the PR I made that was not accepted. (Seeing that many people seem to be affected, any chance that PR can get merged? Possibly by incrementing the major version?)

Also, based on how that PR handles side effects, it would be possible to implement ignore comments by marking imports with such a comment as a “side-effect import”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignoring Code
Use .prettierignore to ignore (i.e. not reformat) certain files and folders completely. Use “prettier-ignore” comments to ignore parts of files.
Read more >
plasmohq/prettier-plugin-sort-imports
Prevent imports from being sorted. This plugin supports standard prettier ignore comments. By default, side-effect imports (like import "core-js ...
Read more >
How to sort imports for a JavaScript Project using Prettier
This post is about how you can sort import statements in your JavaScript projects easily with Prettier in VS Code when you format...
Read more >
sort-imports - ESLint - Pluggable JavaScript Linter
Default is false . ignoreDeclarationSort. Ignores the sorting of import declaration statements. Examples of incorrect code for this rule with the default {...
Read more >
prettier ignore specific rule - You.com | The AI Search ...
This is a prettier plugin that allows organizing import as part of the prettier formatting and has an option to disable organize import...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found