Skip to content

Commit

Permalink
Avoid parens around spread arguments
Browse files Browse the repository at this point in the history
Reviewed By: tmikov

Differential Revision: D33072069

fbshipit-source-id: 2dfcd20c7f82c4f717a6c5b23a4efc6abc34159c
  • Loading branch information
avp authored and facebook-github-bot committed Jan 20, 2022
1 parent deabbfd commit bd64b09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 19 additions & 2 deletions unsupported/juno/crates/juno/src/gen_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,12 @@ impl<W: Write> GenJS<W> {
if i > 0 {
self.comma();
}
self.print_comma_expression(ctx, *arg, Path::new(node, NodeField::arguments));
self.print_child(
ctx,
Some(*arg),
Path::new(node, NodeField::arguments),
ChildPos::Anywhere,
);
}
out!(self, ")");
}
Expand Down Expand Up @@ -948,7 +953,12 @@ impl<W: Write> GenJS<W> {
if i > 0 {
self.comma();
}
self.print_comma_expression(ctx, *arg, Path::new(node, NodeField::arguments));
self.print_child(
ctx,
Some(*arg),
Path::new(node, NodeField::arguments),
ChildPos::Anywhere,
);
}
out!(self, ")");
}
Expand Down Expand Up @@ -3556,6 +3566,13 @@ impl<W: Write> GenJS<W> {
// Nullish coalescing always requires parens when mixed with any
// other logical operations.
return NeedParens::Yes;
} else if matches!(
path.parent,
Node::CallExpression(_) | Node::OptionalCallExpression(_)
) && matches!(child, Node::SpreadElement(_))
{
// It's illegal to place parens around spread arguments.
return NeedParens::No;
}

let (child_prec, _child_assoc) = self.get_precedence(child);
Expand Down
2 changes: 2 additions & 0 deletions unsupported/juno/crates/juno/tests/gen_js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ fn test_calls() {
test_roundtrip("f();");
test_roundtrip("f(1);");
test_roundtrip("f(1, 2);");
test_roundtrip("f(1, (2,3), 4);");
test_roundtrip("(f?.(1, 2))(3);");
test_roundtrip("f?.(1, 2)?.(3)(5);");
test_roundtrip("f(...x)");
test_roundtrip("new f();");
test_roundtrip("new f(1);");
test_roundtrip("new(a.b);");
Expand Down

0 comments on commit bd64b09

Please sign in to comment.