Skip to content

Prettier adding 2 spaces when using ternary operator and plain object definition #4979

@DanielHoffmann

Description

@DanielHoffmann

Prettier 1.14.2
https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAzArlMMCW0AE8AzjABQCU+wAOlPvfgE5wzqN0yPpy0N-4B+Kr36j6xGAEYk+AOQTJsgDQix-CQCYZ8uCQ2zVYgL6G+MmnTXrdU7QuWmrmuzf2O+RgNy0TUWpCgiCAAbOAA6YIgAc1IJCnJvKBAlEAgAB1xoImRQAENGRggAdwAFfIRslFyANwgcABNkkAAjRlywAGsWAGU09pwoKORObhSBojhGGBK2qIBbXORUXOCJlIArIgAPACE2zp7cubgAGQG4JZW1kD7GCcZkFtzmgE9IpJS0xgGYAHUGmAAC2QAA4AAyfQoTX5tNKPL66SbVC4pZgAR3QOGYM1y80WSGWqzgKQmcxwwy4xJARAGUVCAEV0BB4BTRiAYM9-vUgcgACwpTi5HDBWkAYQgcwWjyg0BRIHQEwAKs9KoSJkYjEA

Happens with any set of options but easier to see with --tab-width 4

--tab-width 4 --single-quote --trailing-comma none

Input:

function test() {
    return true
        ? {
              test1: 'test1',
              test2: 'test2'
          }
        : {
              test1: 'test1',
              test2: 'test2'
          };
}

console.log(test());

Output:

function test() {
    return true
        ? {
              test1: 'test1',
              test2: 'test2'
          }
        : {
              test1: 'test1',
              test2: 'test2'
          };
}

console.log(test());

Expected behavior:

function test() {
    return true
        ? {
            test1: 'test1',
            test2: 'test2'
        }
        : {
            test1: 'test1',
            test2: 'test2'
        };
}

console.log(test());

2 extra spaces are added on lines subsequent to a ternary operator. This breaks eslint indent rule.

I set up a project showing this bug and how it breaks eslint:
clone https://github.com/DanielHoffmann/prettier-test
npm install
npm run precommit

The precommit script will run prettier with --tab-width 4 and after that will run eslint with rules: { indent: ["error", 4] }, this will always result in an error because of the 2 extra spaces mentioned before

This seems related to #4976

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:javascriptIssues affecting JSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.type:questionQuestions and support requests. Please use Stack Overflow for them, not the issue tracker.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions