prettier: Inconsistency breaking assignments
For example,
aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
anotherVeryLongNameForIllustrativePurposes;
aParticularlyLongAndObnoxiousNameForIllustrativePurposes = anotherVeryLongNameForIllustrativePurposes;
Similarly there is no break after the =
in
aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
'a very long string for illustrative purposes'.length;
or
aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
anotherVeryLongNameForIllustrativePurposes();
although there is in
aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
anotherVeryLongNameForIllustrativePurposes.length;
and
aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
anotherVeryLongNameForIllustrativePurposes + 1;
I’d expect every one of these to break after the =
, since otherwise line length exceeds 80 characters. (Though I thought it used to… is this an intentional, or a regression?)
Here’s a link containing all of these cases.
This also applies to variable declarations.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 25
- Comments: 42 (15 by maintainers)
Commits related to this issue
- Use instead of prettier — committed to oakserver/oak by kitsonk 4 years ago
- collect tests from #2482 (older than Jule 2017) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than Oct 2017) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than March 2018) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than March 2018) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than March 2018) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than March 2018) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than March 2018) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than March 2018) — committed to thorn0/prettier by thorn0 3 years ago
- collect tests from #2482 (older than March 2018) — committed to thorn0/prettier by thorn0 3 years ago
In general, I think any linebreak after an
=
or=>
should be a last resort - and that any other formatting, if possible, would be preferred.@bradennapier if you have a proposal for a consistent way of fixing it, I think everyone would be interested!
sooooo is this broken forever? lol it makes projects unusable with prettier. i have print width to 80 and eslint max-len is at 100 and its not breaking a row that is 150 characters long…
Here is another case imo:
Prettier 1.13.7 Playground link
Input:
Output:
Expected behavior:
2.3
Personally for
a = b
, I think you should almost never breaka = b
unless you can also breakb
… Though it may depend on the length ofa
.for example, I find this to be a pretty useless change:
=>
The shortness of
a
makes puttingb
on the next line look awkward and imbalanced. And as said earlier, this happens all the time in object property initialization and json files as well.Towards actually fixing this - I think something like
fill
might be the right thing to do? (Assuming I am remembering howfill
works.) As in,fill(group(left, '='), right)
.The desired behavior would be:
=
, break after the=
before breaking the RHS.=
.=
does not fit on one line, break the LHS if possible, and otherwise just allow the line length to be exceeded. Do not break immediately before the=
under any circumstances.I think this handles most of the cases in this thread correctly, except the OP’s case in #2184.
Possibly there are some cases where you wouldn’t want to use
fill
and instead just allow breaking after the=
, likex = a.b
.I found at least 3 cases which still having issues, I hope these can be also fixed. Appreciate for your help.
Sample code below can be found in the playground.
@lydell I am sorry, I don’t mean any disrespect. Prettier is an amazing tool and this is the first issue I had with it. I am maintaining couple of OSS libraries myself so I know well how ungrateful “job” it can be. Even if I had some time to spare for helping out here, I would have no idea where to start as I am unfamiliar with how the whole thing works.
Based on the comment above it felt like it has a fairly straightforward solution. Apologies, if it’s not the case.
@FredyC Hi!
Why do you say this? Have we said something like that?
This issue is still open because it’s a difficult problem and nobody has done the work trying to fix it yet (other than posting suggestions in this thread). Prettier is run by volunteers, so people work on whatever they feel like.
A PR that improves the situation would be very much appreciated, even if it does not cover all cases.
It also happens in a jsx arrow function, something like this where i’d instead expect:
@RenaldasK We recommend using tslint-config-prettier to turn off all rules that conflict with Prettier.
@pbarbiero I think this error is the same as #1893