Skip to content
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

Open
JsonFreeman opened this issue Jun 29, 2015 · 6 comments · May be fixed by #61430
Open

Indentation is too aggressive for ternary operator #3676

JsonFreeman opened this issue Jun 29, 2015 · 6 comments · May be fixed by #61430
Labels
Bug A bug in TypeScript Domain: Formatter The issue relates to the built-in formatter Domain: Smart Indentation Automatic indenting after a newline (not quite formatter/formatting) Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Milestone

Comments

@JsonFreeman
Copy link
Contributor

Actual:

var v =
    0 ? 1 :
        2 ? 3 :
            4;

Expected:

var v =
    0 ? 1 :
    2 ? 3 :
    4;

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.

@JsonFreeman JsonFreeman added the Bug A bug in TypeScript label Jun 29, 2015
@mhegazy mhegazy added this to the Community milestone Jun 29, 2015
@mhegazy mhegazy added the Help Wanted You can do this label Jun 29, 2015
@aozgaa
Copy link
Contributor

aozgaa commented Jun 29, 2015

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. ?

@DanielRosenwasser
Copy link
Member

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.

@JsonFreeman
Copy link
Contributor Author

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.

@olmobrutall
Copy link

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.

@saschanaz
Copy link
Contributor

Does the team still want to fix this? In other words, will the team merge my PR if I revive #5905?

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@ltlombardi
Copy link

ltlombardi commented May 31, 2019

I have I different , but similar problem, with Conditional Typing. It's syntax uses ternary algo. Here is an example:
Expected

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?

@DanielRosenwasser DanielRosenwasser added Domain: Formatter The issue relates to the built-in formatter Domain: Smart Indentation Automatic indenting after a newline (not quite formatter/formatting) labels Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Formatter The issue relates to the built-in formatter Domain: Smart Indentation Automatic indenting after a newline (not quite formatter/formatting) Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants