Skip to content

Additional formatting options for multi-line ternary expressions, e.g. always enclose in parentheses #746

@pineapplemachine

Description

@pineapplemachine

I consider these to be acceptable options for writing a ternary expression:

// Single line (for shorter expressions)
let x = condition ? trueOperand : falseOperand;

// Multiple line (first acceptable option, use either 1 or 2 but use the same one consistently)
let x = (condition ?
    trueOperand : 
    falseOperand
);

// Multiple line (second acceptable option, use either 1 or 2 but use the same one consistently)
let x = (
    condition ?
    trueOperand : 
    falseOperand
);

// Imitate a match expression (nice to have)
let x = (
    firstCondition ? firstResult : 
    secondCondition ? secondResult : 
    thirdCondition ? thirdResult :  
    defaultResult
);

But currently dprint seems insistent on formatting ternary expressions like this below. This violates the consistent principle of indented blocks always being enclosed in parens, brackets, or braces, and I think that inconsistency harms readability. I strongly dislike this formatting style (with "operatorPosition": "sameLine"):

let x = condition ?
    trueOperand : 
    falseOperand;

I would like to have an option to not enforce this formatting, and ideally to enforce formatting like the above examples instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions