Skip to content

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

@cmcaine

Description

@cmcaine

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:rangesIssues about formatting/ignoring/etc segments of fileslang:javascriptIssues affecting JSlocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions