question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling of function on multiple lines with 1.19

See original GitHub issue

Prettier 1.19.1 Playground link

--parser babel
--print-width 115
--tab-width 4

Input:

import { Component } from 'react';
  
class Test extends Component {
    updateProp(
        prop,
        { target: { value } },
    ) { 
        let nextValue = Math.floor(+value);
        if (nextValue < 1) {
            nextValue = 1;
        }
        this.setState({ [prop]: nextValue }, () => this.props.onChange(this.state));
    }
    
    toggleProp(prop) {
        this.setState( prevState => ({ [prop]: !prevState[prop] }), () => this.props.onChange(this.state));
    }   
} 

Output:

import { Component } from "react";

class Test extends Component {
    updateProp(prop, { target: { value } }) {
        let nextValue = Math.floor(+value);
        if (nextValue < 1) {
            nextValue = 1;
        }
        this.setState({ [prop]: nextValue }, () => this.props.onChange(this.state));
    }

    toggleProp(prop) {
        this.setState(
            prevState => ({ [prop]: !prevState[prop] }),
            () => this.props.onChange(this.state)
        );
    }
}

Expected behavior: The change to updateProp() is expected, but I didn’t expect the calls to this.setState() to wrap to multiple lines. Is there a change that I didn’t see in the release notes or is this a bug?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
thorn0commented, Jan 14, 2020

the same weird behavior of multi-line with a single line on the last line

As I already said, it’s a different heuristic in play here. “setTimeout-like calls”. Unlike the one we discussed before (“function composition”), this one seems to have more room for improvement.

1reaction
thorn0commented, Jan 14, 2020

As for join, I stand corrected again! So the discussed rule is: if a call has more than one argument and there are at least two function literals or at least one nested call with function literal arguments among the arguments, then each argument gets its own line.

As for map, you forgot to put parens around its body: value => { word: value } vs value => ({ word: value }), so Prettier thinks word: value is a labelled statement and uses its another heuristic/pattern/rule, namely “a setTimeout-like call”, to format this call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I step into a function when its call is over multiple lines?
According to pdb's documentation, I can step into a function by typing s . Pressing s at the first line does not have...
Read more >
Calls to functions or methods named “compose” are always ...
In the code I'm working on I changed the function name to “meld” so this is mostly just bikeshedding for me now, but...
Read more >
Introducing Arrow Functions | Advanced JavaScript
Snippet 1.19: Single line expression broken into multiple lines. If we have a single line arrow function returning an object literal, we will...
Read more >
D3.js - Multiple Lines Chart w/ Line-by-Line Code Explanations
Line 19–25: Draw the lines by appending “path” . The attr(“d”) defines the path to be drawn, within it, we call the d3.line()...
Read more >
Go Doc Comments - The Go Programming Language
The introduction of doc comment reformatting in Go 1.19's gofmt makes mistakes like these more visible by adding blank lines around the code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found