Skip to content

Commit

Permalink
Add support for the React useEffect hook (#5608)
Browse files Browse the repository at this point in the history
* Add support for the React `useEffect` hook

* Format

* Format

* [Refactor] remove unnecessary condition

`canHaveTrailingComma` is defined as `I(lastElem && ...)`, which will always be true when `lastElem === null`.

* Use a hacky method to allow the array to break

* Revert "[Refactor] remove unnecessary condition"

This reverts commit 91906ba.

* Add tests for `React.useEffect` form
  • Loading branch information
j-f1 committed Dec 10, 2018
1 parent 98680a4 commit fa7eafa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/language-js/printer-estree.js
Expand Up @@ -3873,6 +3873,23 @@ function printArgumentsList(path, options, print) {
]);
}

// useEffect(() => { ... }, [foo, bar, baz])
if (
args.length === 2 &&
args[0].type === "ArrowFunctionExpression" &&
args[0].body.type === "BlockStatement" &&
args[1].type === "ArrayExpression" &&
!args.find(arg => arg.leadingComments || arg.trailingComments)
) {
return concat([
"(",
path.call(print, "arguments", 0),
", ",
path.call(print, "arguments", 1),
")"
]);
}

let anyArgEmptyLine = false;
let hasEmptyLineFollowingFirstArg = false;
const lastArgIndex = args.length - 1;
Expand Down
71 changes: 71 additions & 0 deletions tests/break-calls/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -116,3 +116,74 @@ runtimeAgent.getProperties(
================================================================================
`;

exports[`react.js 1`] = `
====================================options=====================================
parsers: ["flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
function helloWorld() {
useEffect(() => {
// do something
}, [props.value])
useEffect(() => {
// do something
}, [props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value])
}
function helloWorldWithReact() {
React.useEffect(() => {
// do something
}, [props.value])
React.useEffect(() => {
// do something
}, [props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value])
}
=====================================output=====================================
function helloWorld() {
useEffect(() => {
// do something
}, [props.value]);
useEffect(() => {
// do something
}, [
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value
]);
}
function helloWorldWithReact() {
React.useEffect(() => {
// do something
}, [props.value]);
React.useEffect(() => {
// do something
}, [
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value,
props.value
]);
}
================================================================================
`;
18 changes: 18 additions & 0 deletions tests/break-calls/react.js
@@ -0,0 +1,18 @@
function helloWorld() {
useEffect(() => {
// do something
}, [props.value])
useEffect(() => {
// do something
}, [props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value])
}

function helloWorldWithReact() {
React.useEffect(() => {
// do something
}, [props.value])
React.useEffect(() => {
// do something
}, [props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value, props.value])
}

0 comments on commit fa7eafa

Please sign in to comment.