Skip to content

Commit

Permalink
fix(js_formatter): format hook with 3 arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
vohoanglong0107 committed Nov 4, 2024
1 parent ec46f37 commit 0eade7a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1205,14 +1205,21 @@ fn is_multiline_template_only_args(arguments: &JsCallArguments) -> bool {
/// useMemo(() => {}, [])
/// ```
fn is_react_hook_with_deps_array(arguments: &JsCallArguments, comments: &JsComments) -> bool {
if arguments.args().len() > 3 || arguments.args().len() < 2 {
return false;
};

use AnyJsExpression::*;
let mut args = arguments.args().iter();
if arguments.args().len() == 3 {
args.next();
}

match (args.next(), args.next()) {
(
Some(Ok(AnyJsCallArgument::AnyJsExpression(JsArrowFunctionExpression(callback)))),
Some(Ok(AnyJsCallArgument::AnyJsExpression(JsArrayExpression(deps)))),
) if arguments.args().len() == 2 => {
) => {
if comments.has_comments(callback.syntax()) || comments.has_comments(deps.syntax()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ expect(
bar;
}
).toThrow(ReferenceError);

useImperativeHandle(ref, () => {
return;
}, []);
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ expect(
bar;
},
).toThrow(ReferenceError);

useImperativeHandle(ref, () => {
return;
}, []);
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ expect(
}
).toThrow(ReferenceError);

useImperativeHandle(ref, () => {
return;
}, []);

```


Expand Down Expand Up @@ -104,7 +108,7 @@ expect(
var bar = "foo";
bar;
};
@@ -28,6 +40,6 @@
@@ -28,9 +40,9 @@
() => {
var bar = "foo";
};
Expand All @@ -114,6 +118,9 @@ expect(
+bar;
+}
+).toThrow(ReferenceError)

useImperativeHandle(ref, () => {
return;
```

# Output
Expand Down Expand Up @@ -164,6 +171,10 @@ expect(
bar;
}
).toThrow(ReferenceError)

useImperativeHandle(ref, () => {
return;
}, []);
```

# Errors
Expand Down Expand Up @@ -394,6 +405,7 @@ call-arguments.js:40:3 parse ━━━━━━━━━━━━━━━━━
> 41 │ ).toThrow(ReferenceError);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
42 │
43 │ useImperativeHandle(ref, () => {
i Expected a statement here.
Expand All @@ -404,6 +416,7 @@ call-arguments.js:40:3 parse ━━━━━━━━━━━━━━━━━
> 41 │ ).toThrow(ReferenceError);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
42 │
43 │ useImperativeHandle(ref, () => {
```

0 comments on commit 0eade7a

Please sign in to comment.