-
Notifications
You must be signed in to change notification settings - Fork 22
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
Inconsistent formatting of nested ternary expressions #41
Comments
This comment has been minimized.
This comment has been minimized.
Another example what it is currently: search.premium
? somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething &&
somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething
: something
? somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething &&
somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething
: null And what it probably should be: search.premium
? somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething &&
somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething
: something
? somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething &&
somethingsomethingsomethingsomethingsomethingsomethingsomethingsomething
: null |
Referencing standard/standard#1451 |
Interesting. I think this would have to be optional (keep same formatting as Prettier by default). I generally try to use the same option name as eslint whenever possible. I will probably need some time to look at this. A contribution would be very welcome for consideration. Comments from the user community would also be very welcome. |
@sheerun I think this issue can be closed; search.premium
? commission => {
return suggestions.hits.map(hit => something(hit))
}
: () => onOpen() Note that using My comments about the multi-level ternary examples coming next. |
Your examples with multi-level or nested ternaries seem to touch upon 2 things:
|
@brodybits This issue is indeed more about nested ternaries.
I'm interested in enabling just 2. and leave alignment expressions inside ternary untouched |
Thanks @sheerun. The fact that this issue and sheerun/prettier-standard#76 are about nested ternary expressions was not clear to me until now. Can you please do me and others a favor and clarify as follows:
I do think the solution should be pretty straightforward (actually got a quick solution working in my workarea), challenge would be to determine what and how should be configured. |
@brodybits Sure, done |
I think extra option |
btw. don't blame yourself that you didn't get it, because I didn't as well at the time :P It took me few months to fully realise what's the actual issue |
I see you fixed it in both places, thanks!
Sounds nice. I will ask you to review when I raise the PR, where we can discuss further if needed.
Bullseye, a major benefit of GitHub issues and using GitHub itself:) I did try a quick fix which then broke something else. I will test each of the existing cases of using And thanks for giving credit in sheerun/prettier-standard@b8687b1, even better if it would really be consistent:) |
If we fix it the way we discussed, I am now thinking to name it From the number of issues and discussions related to this one and prettier/prettier#5814, I am now thinking of an alternate solution that I proposed in #71. |
I think what we are looking for is (more) balanced indentation of nested ternary expressions, which would be more like Prettier was before prettier/prettier#5039 was merged. I also discovered prettier/prettier#5257, which I am now tracking in #73. I will probably need some more time to figure this one out. I tried changing a few things, nothing seems to work 100% right for me right now. We have seen things go wrong with ternary formatting in both this fork and upstream Prettier, would ideally like to get it right with more thorough testing this time. The more verbose ternary formatting solution I proposed in #71 would probably be easier and quicker, though likely to be affected even worse by #73. Any contribution would be welcome for consideration. As I said before, I would be especially picky about the option name. |
I think I figured out how to get the balanced nested ternary indentation working, may need some more time to test and investigate before fixing. |
@sheerun I have updated the title and pinned this issue as a conflict with standard. Unfortunately I have found a case from brody4hire/react-native-module-init#85 where the formatting of a nested ternary by standard 16 does not look right: const mockCallSnapshot = []
jest.mock('execa', () => (cmd, args, opts) => {
mockCallSnapshot.push({ execa: [cmd, args, opts] })
return cmd === 'git'
? args[1] === 'user.email'
? Promise.resolve({ stdout: 'alice@example.com' })
: Promise.resolve({ stdout: 'Alice' })
: Promise.resolve()
}) Here is how I think it should be formatted: const mockCallSnapshot = []
jest.mock('execa', () => (cmd, args, opts) => {
mockCallSnapshot.push({ execa: [cmd, args, opts] })
return cmd === 'git'
? args[1] === 'user.email'
? Promise.resolve({ stdout: 'alice@example.com' })
: Promise.resolve({ stdout: 'Alice' })
: Promise.resolve()
}) I hope we can get this fixed on standard somehow. Yes I may be possible for me to make prettierx format nested ternaries more consistently with standard 16 but think this would be extra work in the wrong direction. |
check for consistent formatting as discussed in: - eslint/eslint#13971 - standard/standard#1624 - #41
check for consistent formatting on prettierx side as discussed in: - eslint/eslint#13971 - standard/standard#1624 - #41
check for consistent formatting on prettierx side as discussed in: - eslint/eslint#13971 - standard/standard#1624 - #41
Issues that I discovered still lurking in eslint & "Standard JS":
I hope to get these resolved in the near future. |
in tests/standard & docs as reported in #40 (this should have been part of the proposal in prettier/prettier#5723) Note that this inconsistency is resolved by #41 which is to be included in an upcoming merge. Co-authored-by: Christopher J. Brody <chris.brody@gmail.com> Co-authored-by: Mohit Singh <mohit@mohitsingh.in> Co-authored-by: Adam Stankiewicz <sheerun@sher.pl>
to resolve a conflict with standard as reported in #40 with test updates in: - tests/standard - tests/ternaries resolves #40 Co-authored-by: Christopher J. Brody <chris.brody@gmail.com> Co-authored-by: Mohit Singh <mohit@mohitsingh.in> Co-authored-by: Ika <ikatyang@gmail.com> Co-authored-by: Lucas Azzola <derflatulator@gmail.com>
This relates to sheerun/prettier-standard#76
Here's example formatting of prettierx with indents on nested ternaries enabled:
The core of this issue is that it's possible to configure prettierx to add extra indents to nested ternary expressions, but it also enables another extra formatting rule described below:
function is normally formatted with indentation:
but when ternary expression is used (with
--align-ternary-lines=false
option that enables indents on nested ternaries) it doesn't have it:Command used:
prettierx --generator-star-spacing --space-before-function-paren --single-quote --jsx-single-quote --no-semi --yield-star-spacing --align-ternary-lines=false
The real issue with this formatting is that's what standard --fix actually does! So technically is nothing wrong with prettierx, but for me it seems indeed this is wrong decision and the indentation should be at the level of expression after
?
, not the?
itself (this doesn't prevent expressions being indented for each level of?:
):Unfortunately this also means formatting would be divergent from what standard.js does.
In the end the correct code, with just indents on nested ternaries enabled, is:
The text was updated successfully, but these errors were encountered: