Skip to content

Enforce newlines between operands of ternary expressions if the expression spans multiple lines (multiline-ternary) #1558

@feross

Description

@feross

https://eslint.org/docs/rules/multiline-ternary

Desired config:

/*eslint multiline-ternary: ["error", "always-multiline"]*/

JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.

For example:

var foo = bar > baz ? value1 : value2;

The above can be rewritten as the following to improve readability and more clearly delineate the operands:

var foo = bar > baz ?
    value1 :
    value2;

Rule Details

This rule enforces or disallows newlines between operands of a ternary expression.
Note: The location of the operators is not enforced by this rule. Please see the operator-linebreak rule if you are interested in enforcing the location of the operators themselves.

always-multiline

Examples of incorrect code for this rule with the "always-multiline" option:

/*eslint multiline-ternary: ["error", "always-multiline"]*/

foo > bar ? value1 :
    value2;

foo > bar ?
    value1 : value2;

foo > bar &&
    bar > baz ? value1 : value2;

Examples of correct code for this rule with the "always-multiline" option:

/*eslint multiline-ternary: ["error", "always-multiline"]*/

foo > bar ? value1 : value2;

foo > bar ?
    value1 :
    value2;

foo > bar ?
    (baz > qux ? value1 : value2) :
    value3;

foo > bar ?
    (baz > qux ?
        value1 :
        value2) :
    value3;

foo > bar &&
    bar > baz ?
        value1 :
        value2;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions