Calling of function on multiple lines with 1.19
See original GitHub issuePrettier 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:
- Created 4 years ago
- Comments:13 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.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 }
vsvalue => ({ word: value })
, so Prettier thinksword: value
is a labelled statement and uses its another heuristic/pattern/rule, namely “asetTimeout
-like call”, to format this call.