-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Indentation is too aggressive for ternary operator #3676
Comments
I was thinking of lispy indentation (with if-else blocks) earlier, but I don't think that's a very useful point of reference. How does this compare to the indentation of the ternary operator in other languages, such as vanilla JavaScript, C/C++, C#, etc. ? |
I think that's the right question to ask here. Let's try to stay close to other languages service teams. If it turns out there are shortcomings, then they should know too. |
I just tried an experiment. Javascript and C# don't modify the indentation at all. They leave it as the user wrote (in VS at least). I could see this as a solution as well, at least that way we could format things how we want. But my favorite solution is still to be prescriptive, and indent the false branch the same as the true branch. We could also add an option that toggles formatting for ternary operators, since they require specialized behavior anyway. |
I also think that the expected behavior should be: var opacity =
depth == 0 ? 1 :
depth == 1 ? .7 :
depth == 2 ? .5 :
depth == 3 ? .4 : .3; One think to note however, in VS C# when you press enter to separate the conditional in two lines he has a preference for keeping the vertical alignment. var opacity =
depth == 0 ? 1 : /*Press enter here*/depth == 1 ? .7 :
depth == 2 ? .5 :
depth == 3 ? .4 : .3; Then if changed VS will respect your formatting. I think this is the best behavior. |
Does the team still want to fix this? In other words, will the team merge my PR if I revive #5905? |
I have I different , but similar problem, with Conditional Typing. It's syntax uses ternary algo. Here is an example: type Base<T> = {
[P in keyof T]: T[P] extends boolean ? boolean :
T[P] extends string ? string :
any;
}; Result after format type Base<T> = {
[P in keyof T]: T[P] extends boolean ? boolean :
T[P] extends string ? string :
any;
}; Should open a separate issue? |
Actual:
Expected:
I believe the fix is that if the false branch of the ternary operator should have the same indentation as the true branch. In the case where the true branch is on the same line as the condition, it should not be considered indented, and therefore the false branch should not be indented.
The text was updated successfully, but these errors were encountered: