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
Don’t multi-line imports (let them take less space) / Conflict with VSCode’s organizeImports #5995
Comments
Hi! Thanks for the well written issue and the video! VSCode underlining in the wrong place sounds like a bug in VSCode and/or the Prettier VSCode plugin to me – please report this to either/both of those! That being said, I do think it would be interesting to discuss not breaking
It happens quite often to me that files start with more than a window-full of just imports, that I have to scroll past every time I open them. To me it has started to feel more and more that imports are just cruft at the top of the file that I rarely look at and want to spend as little time on as possible. I want the computer to add new imports and remove unused ones, sort them and format them, and otherwise mostly keep them out of the way. I think not breaking imports onto multiple lines could help with this. It could at least be worth exploring. |
Exactly. Noone reads them, and even during code navigation (on Cmd+Click on some identifier), VSCode jumps us into the file which contains the imported symbol definition, not to the import statement line. Many IDEs for other languages (e.g. for Java) even collapse all import statements by default. |
Just to play "devil's advocate": too many imported declarations could be a sign of a module that could be split into smaller module (ideally smaller modules that are functional:) |
That'd make me sad. Minified code will tend to have all of its imports on the same line, even though that isn't how people normally write code, and it would be a shame for prettier to leave it in this unusual state.
This does not match my experience. |
Just to be clear, noone proposes to have all imports on the same line. The proposal is to (optionally) have symbols related to every single “import” statement on the same line, i.e. one line per each “import” statement. |
Oh! Forgive me, I misunderstood. That seems fine to me. (Doing it unconditionally, anyway; I don't think it would be worth adding an option.) |
If we do it unconditionally, many people (the ones who got used to the current behavior) will likely be upset... |
Sorry for being unclear. I meant that this: import huffman from "n-ary-huffman";
import { repr } from "tiny-decoders";
import iconsChecksum from "../icons/checksum";
import type {
ElementReport,
ElementTypes,
ElementWithHint,
ExtendedElementReport,
HintMeasurements,
HintUpdate,
} from "../shared/hints";
import {
type HintsMode,
type KeyboardAction,
type NormalizedKeypress,
} from "../shared/keyboard";
import {
Resets,
addListener,
bind,
isMixedCase,
log,
makeRandomToken,
partition,
splitEnteredText,
unreachable,
} from "../shared/main";
import type {
FromBackground,
FromOptions,
FromPopup,
FromRenderer,
FromWorker,
ToBackground,
ToOptions,
ToPopup,
ToRenderer,
ToWorker,
} from "../shared/messages";
import {
type OptionsData,
type PartialOptions,
flattenOptions,
getDefaults,
makeOptionsDecoder,
unflattenOptions,
} from "../shared/options";
import { type Durations, type Perf, TimeTracker } from "../shared/perf"; Would be printed as: import huffman from "n-ary-huffman";
import { repr } from "tiny-decoders";
import iconsChecksum from "../icons/checksum";
import type { ElementReport, ElementTypes, ElementWithHint, ExtendedElementReport, HintMeasurements, HintUpdate } from "../shared/hints";
import { type HintsMode, type KeyboardAction, type NormalizedKeypress } from "../shared/keyboard";
import { Resets, addListener, bind, isMixedCase, log, makeRandomToken, partition, splitEnteredText, unreachable } from "../shared/main";
import type { FromBackground, FromOptions, FromPopup, FromRenderer, FromWorker, ToBackground, ToOptions, ToPopup, ToRenderer, ToWorker } from "../shared/messages";
import { type OptionsData, type PartialOptions, flattenOptions, getDefaults, makeOptionsDecoder, unflattenOptions } from "../shared/options";
import { type Durations, type Perf, TimeTracker } from "../shared/perf"; And most likely behind an option, yes. |
Hi, There are a couple of reasons for this:
What you should be able to write is: //@ts-ignore
import { a, b } from 'MyNonTypedModule' But it's converted to: //@ts-ignore
import {
a,
b
} from 'MyNonTypedModule' And this fails. We can fix this by writing: import {
a,
b
//@ts-ignore
} from 'MyNonTypedModule' But now auto import stop working. import {
a,
b
//@ts-ignore
} from 'MyNonTypedModule'
import { c } from 'MyNonTypedModule' I hope we all can agree that this is a real bug, not just a personal opinion on formatting code? (And maybe I should have written it is its own issue, but: ) I have written an option 'ForceSingleLineImports' with full tests, Im new to opensource projects on github, so I dont know how to get the code up. Can anyone help me with the permission? |
We probably won’t add an option for this, but I recommend:
I’d also encourage you to use your forked version — we’re perfectly fine with that if you can’t use the official version of Prettier. Finally, have you considered authoring types for the non-typed modules you have? It would fix your whole problem. |
Hi @j-f1 |
Yes. We've had requests for doing the opposite, i.e. always break named imports in multiple lines. And also had #1954 |
Playing Devil's advocate: The rational thing to do for people like me, who think the imports take too much space, would be to put all imports at the end of the file. |
That’s actually a good idea @lydell! |
Any updated about this? |
@j-f1 What's the reasoning behind not supporting the single-line format for imports? |
That's a shame. Not keeping imports on one line to me is a blocker from ever using Prettier (and not a fork). I mostly use Typescript, and it often has additional imports (for typing) that makes multi-line imports occur more often. Adding 5-20 lines for a large percentage of files is just more noise, as I don't feel like imports need to be viewed very often (linting warns for unused imports, editors will often auto import functions/classes). This feels like the return of code folding for imports, where e.g. Java in Webstorm automatically hides imports to keep code readable. |
Hi I have written it, because it's for me, is important that the imports are sorted and single line, |
How about formatting them like this: import {
Lorem, ipsum, dolor, sit, amet, consectetur, adipiscing, elit,
Maecenas, vitae, venenatis, arcu, Sed, ultrices,
} from "lorem-ipsum" |
Anything new here? Why a new option can't be created? This behavior is very noisy sometimes, especially in TypeScript files, which requires a lot of type importing (which usually have long names). Because of that I get this output: import { compare } from 'bcryptjs';
import { sign, verify } from 'jsonwebtoken';
import { getManager } from 'typeorm';
import {
AUTH_REFRESH_TOKEN_KEY as REFRESH_TOKEN_KEY,
AUTH_ACCESS_TOKEN_KEY as ACCESS_TOKEN_KEY
} from '../constants/env';
import {
AUTH_INVALID_CREDENTIALS as INVALID_CREDENTIALS,
AUTH_INVALID_TOKEN as INVALID_TOKEN
} from '../constants/error-codes';
import User from '../entities/User';
import { RefreshJwtToken, AccessJwtToken } from '../types/Tokens';
import AuthorizationError from '../utils/errors/AuthorizationError'; A good portion of the JavaScript (and TypeScript) community also consider this format uglier, and not prettier. I know that a new option can't be created for every single code-style aspect, but I really think that this one is very important for some use cases, such as this one. Please consider adding this option! :) |
Please see our Option Philosophy. |
Please consider a way around this... it really is frustrating when you cant see the top line of your component when you open a file because of it being multi-lined. I want this functionality everywhere except my imports :( import {
AfterViewInit,
Component,
ComponentFactoryResolver,
EventEmitter,
Injector,
Input,
Output,
Type,
ViewChildren,
ViewContainerRef
} from '@angular/core'; |
I also find this very uncomfortable to break imports to multiple lines, the imports part of the file gets too large and I need to scroll over it every time I open the file, plus it looks not consistent when one imports are written in one line while another it multiple, for me it's much simpler to scroll horizontally (on MacOs) and keep it in long line, I rarely need to look what's in my imports. This one of the reasons why I'm considering not using prettier. |
I would find it very valuable to match the behavior of typescript's organize imports command. Right now they're kinda fighting each other. Both typescript and prettier are very widespread amongst the projects I've been working with. |
2023 +1 ? |
Is there a prettier plugin that can fix this problem ? (I was not able to find one) |
Putting |
yes, use eslint |
The horror on my face seeing there is STILL no option for this crap... |
Lets create a "democraticPrettier", release it to the public and kill prettier! |
It feels like you were never forced to work on a large TypeScript project, requiring the entire first view of the file to be a mass bulk of imports 90% of which are types. Makes us rethink using Prettier as a whole. This thread and answers from your team is outrages. |
'prettiest'? |
Where is the option to configure this already? |
2023 +1 |
We need this option so badly. the imports look so messy!!!! Each import statements take 15 lines. eww. |
Take a look at dprint - https://github.com/dprint/dprint. You could probably get it configured to function mostly like prettier. There is an option for single-line imports. |
Please, implement this option!! |
2023, prettier 3.0, nothing changed for this? |
since it doesn't seem like there's much traction on this issue. the bare minimum i would recommend is using the |
Our org was trying to incorporate prettier, but this import hell is just a nonsense. This decision is a very questionable (at least) and response is lacking any meaningful reasons to support the decision, and rather looks quite personal. This is just a disappointment. |
4 years... no change |
**Note**: Yes, the imports being on multiple lines is really annoying. No, unfortunately there is no way around this currently :( prettier/prettier#5995
**Note**: Yes, the imports being on multiple lines is really annoying. No, unfortunately there is no way around this currently :( prettier/prettier#5995
**Note**: Yes, the imports being on multiple lines is really annoying. No, unfortunately there is no way around this currently :( prettier/prettier#5995
Try out ESLint Stylistic |
Are Prettier devs seriously ignoring the demand for this feature for more than 4 years?? Wow. Really makes me want to look into alternatives. 😒 |
In all fairness, this is an open-source project, so we can't expect much. But we migrated to ESLint and loved it so far. |
Shame on you guys... |
Prettier devs - this is inexcusable, causing a lot of headaches and costing a lot of man hours only due to your stubbornness! |
I switched to Biome.js due to this a few months back. Been very impressed. While there will never be an opinionated formatter that everyone can agree 100% with, and don't agree with all of the decisions made by Biome.js, they have done a much better job of articulating the technical reasons behind the choices, so it never feels arbitrary or just down to a personal preference. |
Still no solution |
I wrote a VSCode extension that overrides Prettier's default import formatting, irregardless of any printWidth setting: Run commands as code actions You will need to use it together with this extension JS/TS Import/Export Sorter The following configuration will enforce single-line imports yet allow Prettier to format everything else. "editor.formatOnSave": false,
"editor.codeActionsOnSave": [
"source.organizeImports", // Optional
"source.runCommandsAsCodeActions.formatAndSave"
],
"run-commands-as-code-actions.formatAndSave": [{
"command": "prettier.forceFormatDocument",
"delay": 0
}, {
"command": "tsImportSorter.command.sortImports",
"delay": 400 // Shorter might also work, but this is safe
}, {
"command": "workbench.action.files.saveWithoutFormatting",
"delay": 400 // Shorter might also work, but this is safe
}],
"tsImportSorter.configuration.emptyLinesBetweenGroups": 0,
"tsImportSorter.configuration.excludeGlob": ["**/node_modules/**"], // Adjust to your liking
"tsImportSorter.configuration.maxLineLength": 0,
"tsImportSorter.configuration.wrappingStyle": {
"ignoreComments": false, // Optional
"maxBindingNamesPerLine": 0,
"maxDefaultAndBindingNamesPerLine": 0,
"maxExportNamesPerLine": 0,
"maxNamesPerWrappedLine": 0
}, |
@John-Dennehy are you willing to share your biome config that does this (non multi line imports)? Also running into this issue at work and the biome docs don't really explain it well. |
Hi.
There is a bug somewhere when builtin VSCode's source.organizeImports feature is used together with Prettier.
People sometimes prefer to use VSCode's built-in source.organizeImports feature ON SAVE which, among other things, places all imports on the same line. And this conflicts with Prettier when it formats imports at multiple lines: if there is an error in the code, the underlining of this error shifts towards beginning of the file.
I recorded a video which shows the effect, pretty easy to reproduce:
https://www.dropbox.com/s/apj5znzlqy8pub6/vscode-prettier.mp4?dl=0
There are two possible ways I imagine:
The text was updated successfully, but these errors were encountered: