-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Prettier removing a piece of code when formatting multi-line conditions #532
Comments
👋 @philcatterall! I’m unable to reproduce this in Playground: Prettier 2.8.1 --parser babel Input: const values = {
myProperty1: true,
myProperty2: false,
myProperty3: true,
myProperty4: false,
myProperty5: true,
myProperty6: false,
srvsys_sc_files_otherorgsdata_to_nfd_notingroup: true,
srvsys_sc_search_owncustomers_against_nfd: true,
srvsys_sc_search_othercustomers_nfd_ingroup: false,
srvsys_sc_search_otherorgcustomers_nfd_notingroup: true
};
setValue(!values.myProperty1
&& values.myProperty2
&& !values.myProperty3
&& !values.myProperty4
&& values.myProperty5
&& !values.myProperty6
);
function setValue(newValue: boolean) {
//do something
console.log(newValue);
}
//Prettier suggested fix would remove the 1st &&
//Replace `&&·values.myProperty2` with `··values.myProperty2·&&`eslintprettier/prettier Output: const values = {
myProperty1: true,
myProperty2: false,
myProperty3: true,
myProperty4: false,
myProperty5: true,
myProperty6: false,
srvsys_sc_files_otherorgsdata_to_nfd_notingroup: true,
srvsys_sc_search_owncustomers_against_nfd: true,
srvsys_sc_search_othercustomers_nfd_ingroup: false,
srvsys_sc_search_otherorgcustomers_nfd_notingroup: true,
};
setValue(
!values.myProperty1 &&
values.myProperty2 &&
!values.myProperty3 &&
!values.myProperty4 &&
values.myProperty5 &&
!values.myProperty6
);
function setValue(newValue: boolean) {
//do something
console.log(newValue);
}
//Prettier suggested fix would remove the 1st &&
//Replace `&&·values.myProperty2` with `··values.myProperty2·&&`eslintprettier/prettier The code might be removed by some other tooling. If that’s the case, this issue is out of this repo’s scope. If you think otherwise, it’d be helpful to see reproduction steps. |
@kachkaev in Playground, prettier (correctly) reformats the code. Cheers, |
babel vs typescript parser ?? |
No, parser is
.eslintrc.json
|
Looking at your |
We can transfer this issue to https://github.com/prettier/eslint-plugin-prettier. I don’t have access to that repo so let’s wait for someone else to do this. Alternatively, you can close this issue and recreate it in another repo yourself. In general, I would recommend using Prettier without |
Well no worries, just thought you should be aware that prettier changed and broke the code by removing a needed "&& |
Yep, exactly! 💯 🙂 I also prefer I’ll keep this issue open for other moderators to transfer or close it. |
OK I'll give that a go. Interesting suggestion, all the .eslintrc examples I have seen so far (including the examples from Microsoft) suggest |
Transfered to eslint-plugin-prettier. |
To fix this issue, we need find out what's the conflictting rule and extend it's fix range. |
@kachkaev Hey Alex, out of curiosity (even though I've now abided by Prettier's "opinion" :-), I was fiddling with the .eslintrc settings, bearing in mind your feedback, playing with different settings. After several hours of googling, and experimentation, I think I'm now more confused than ever, given the high number of combinations of extends, parseroptions, settings, plugins and rules !!!. So I hope you don't mind me asking, but could you post for me your recommended settings ? I'm using vsCode and mostly coding in Typescript, with a bit of Javascript and some JSON files. Also for some projects using React. Any guidance you could give me would be very welcome. Many thanks, Phil |
Yeah there are a few ways to integrate ESLint and Prettier for historical reasons, which can be confusing indeed: You can find some thoughts on this topic in this answer: prettier/prettier#13925 (comment). As of project examples, I can’t recall anything that would be easy to copy-paste. My This is a bit offtopic, so please feel free to create a discussion and tag me there if you need more help. |
Hi Alex, many thanks, and yes sorry, it is a bit off-topic, but your reply is sincerely appreciated. I will follow the rules from now and continue in discussions if I need, (embracing fully Prettier's ethos !! Cheers |
Maybe this isn't a bug, but it's a scenario where prettier is changing code, and in your documentation, you said if prettier changes code, it shouldn't, so it should be reported.
Hence I am reporting.
If I have several "Anded" expressions, split across multiple lines (in my real example its quite long), prettier suggests a fix of "Replace
&&·values.myProperty2
with··values.myProperty2·&&
eslintprettier/prettier"However this fix removes a required "&&".
Yes, it turns out I should have my "&&"s on the same line (though I prefer them at the beginning, perhaps that is a different debate). But should prettier remove a required one ?
Hope this helps.
Prettier 2.8.1
Playground link
Input:
Output:
Expected behavior:
The text was updated successfully, but these errors were encountered: