Prettier always expand multiline import statements
See original GitHub issueForce prettier to always expand import statements regardless of line width like this:
import {
package
} from 'path';
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Rationale
A consequence of this is that long singleline objects are automatically expanded, but short multiline objects are never collapsed.
Read more >Import statement automatically converted to multi-line in ...
I am programming in Angular 7 typescript and following extension is installed on it: Angular Essentials; Prettier; TSlint. Please, help me to ...
Read more >Google JavaScript Style Guide
Imports are done with goog.require and goog.requireType statements. The names imported by a goog.require statement may be used both in code and in...
Read more >prettier-plugin-organize-imports
Zero config. No more weird diffs or annoying merge conflicts in PRs caused by import statements. If your editor supports auto-imports ...
Read more >CoffeeScript
Here are some notable exceptions: JSX always requires transpilation. Splats, a.k.a. object rest/spread syntax, for objects are supported by Node 8.6+. The ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Prettier will never merge or reorder imports, because such changes can have side effects and break your code. This can happen if the modules you import are sensitive to things like the presence of global variables set by other modules.
As of single line / multiline formatting, Prettier already does it, but chooses to break lines only when all imports don’t fit your max line length (80 characterss by default).
If you do want a short statement like
import { package } from 'path';
to occupy three lines instead of one for some reason, you can do this by adding a comment:Original (formatted by you or Prettier):
Your change:
Prettier’s reaction to this:
Removing
//
will squash things to one line again, because this looks prettier 🙂I totally agree with you that having two statements that import things from one module is an issue, Prettier is just not a tool to solve it. I’m personally dealing with such things using TSLint given that my code is usually written in TypeScript. After configuring TSLint for the project and installing TSLint extension for VSCode, I’ve set
"tslint.autoFixOnSave": true,
and my import statements have begun to sort themselves automatically. Possible duplicates are vividly marked as a TSLint error and I fix it straight away. If you are writing in JavaScript instead of TypeScript, a similar thing can be achieved with ESLint I guess.ok, thank you for your time. I will investigate this on ESLint 😄 .