prettier / prettier/prettier

Unexpected new line in function body with chained expression

Open
#8,322 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

area:arrow functions area:member chains lang:javascript status:needs discussion
Dominant language
JavaScript
Stars
52.3k
Forks
5k
Avg merge
19h 2m
Merged PRs (30d)
117

Description

Prettier 2.0.5
Playground link

--parser babel
--tab-width 4

Input:

// Small example.
transform((test) => test
    .method1("test", "in", "blah")
    .method1("another", "word")
    .method2()
    .method2());

// More realistic case.
const getQuery = builder("supernova")
    .select("name")
    .select("magnitude")
    .filter("and", (filter) => filter
        .condition("eventtype", "eq", 1)
        .filter("or", (filter) => filter
            .condition("date", "gt", "1998-01-01T00:00:00.000Z")
            .condition("magnitude", "lt", 2.72)
        )
    );

Output:

// Small example.
transform((test) =>
    test
        .method1("test", "in", "blah")
        .method1("another", "word")
        .method2()
        .method2()
);

// More realistic case.
const getQuery = builder("supernova")
    .select("name")
    .select("magnitude")
    .filter("and", (filter) =>
        filter
            .condition("eventtype", "eq", 1)
            .filter("or", (filter) =>
                filter
                    .condition("date", "gt", "1998-01-01T00:00:00.000Z")
                    .condition("magnitude", "lt", 2.72)
            )
    );

Expected behavior:

I'd prefer it look like the input. The line break in the arrow functions doubles the nesting of the inner expressions and can often lead to more unnecessary line breaks.

#5733 looks similar to my issue, but I don't know if the inner expressions are "fat" or not.

Possible conflicts

If this is changed outside of being used in a function argument, there may be other cases to consider and then I can certainly see arguments for not changing this behavior. For example, it can make it harder to differentiate function arguments from the body itself:

Input

const codeBlock = (loadLibraries) => (loadExternalData) => loadLibraries()
    .then(library => {
        ...
    })

Output

const codeBlock = (loadLibraries) => (loadExternalData) =>
    loadLibraries().then((library) => {
        ...
    });

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Prettier Playground reproduction using the babel parser, comparing the input and output for chained expressions inside arrow-function arguments. Review the related #5733 issue for context, then confirm that the formatter preserves the requested nesting without regressing the possible conflict example.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.