prettier/prettier

Too much indentation in method body selected by range if first line is comment or incompletely selected

cmcaine opened this issue · 7 comments

Prettier 1.14.0
Playground link

--parser babylon
--range-end 304
--range-start 101

Can reproduce with just range 100 - 101 (the newline character after the brace on line 4), so that seems to be the culprit.

If I replace line 4 with an expression (e.g. let x) then the method is formatted correctly more often so long as my selection covers the second character of the expression, but you can break it again by moving the start of the selection over the brace on line 3.

Input:

class Foo {
    /** Does this key match a given MinimalKey extending object? */
    match(keyevent) {
        // 'in' doesn't include prototypes, so it's safe for this object.
        for (let attr in this) {
            if (this[attr] !== keyevent[attr]) return false
        }
        return true
    }
}

Output:

class Foo {
    /** Does this key match a given MinimalKey extending object? */
    match(keyevent) {
                      // 'in' doesn't include prototypes, so it's safe for this object.
                      for (let attr in this) {
                        if (this[attr] !== keyevent[attr]) return false;
                      }
                      return true;
                    }
}

Expected behavior:

Don't indent so much. Don't be so sensitive to range choice.

Ref: nrwl/precise-commits#15

j-f1 commented

I’m starting to wonder if we should change range formatting to format the whole file, but with two “cursors,” then replace what’s in the range with the text between the cursors.

Sounds sensible to me. In my project we just ended up running prettier on everything so that we could use whole file prettier.

This makes range formatting unusable. Prettier randomly indents half of codebase

btw. to make it worse range formatting is very slow because of API prettier is exposing (you can provide just one range, so if you need to format n ranges you need to call format n times)

@sheerun Kind of related: #5807 (comment)

Here's another even simpler example:

test.js

function test() {
  'foo';
  'bar';
}

command:

cat test.js | prettier --parser babel --range-start 25 --range-end 25
function test() {
                  ("foo");
                  ("bar");
                }

See one more manifestation of this bug in #7082.