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.

Prettier always expand multiline import statements

See original GitHub issue

Force prettier to always expand import statements regardless of line width like this:

import {
  package
} from 'path';

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kachkaevcommented, Nov 19, 2018

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:

  1. Original (formatted by you or Prettier):

    import { package } from 'path';
    
  2. Your change:

    import { package //
    } from 'path';
    
  3. Prettier’s reaction to this:

    import {
      package, //
    } from "./path";
    

    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.

1reaction
lupu60commented, Nov 19, 2018

ok, thank you for your time. I will investigate this on ESLint 😄 .

Read more comments on GitHub >

github_iconTop 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 >

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