Example #1
0
 def test_generator_many_argument_function_call(self) -> None:
     node = libcst.parse_expression("(x for x in foo)").with_changes(
         lpar=[], rpar=[]
     )
     new_node = parenthesize.parenthesize_using_parent(
         node, libcst.parse_expression("max((x for x in foo), foo)")
     )
     self.assert_has_parentheses(new_node)
Example #2
0
 def test_generator_return(self) -> None:
     node = libcst.parse_expression("(x for x in foo)").with_changes(
         lpar=[], rpar=[]
     )
     new_node = parenthesize.parenthesize_using_parent(
         node, libcst.parse_statement("return (x for x in foo)")
     )
     self.assert_has_parentheses(new_node)
Example #3
0
 def leave_Name(self, original_node: libcst.Name,
                updated_node: libcst.Name) -> libcst.BaseExpression:
     new_node = self.replacements.get(updated_node.value)
     if not new_node:
         return updated_node
     parent = self.get_metadata(libcst.metadata.ParentNodeProvider,
                                original_node)
     if not parent:
         raise Exception("cannot find parent for node")
     # TODO: check if we need to clone the node before returning it. It may
     # have some implications for the MetadataWrapper, as we are forcing to
     # not copy the tree.
     return parenthesize.parenthesize_using_parent(
         cast(libcst.BaseExpression, new_node), parent)
Example #4
0
 def test_expression_same_precedence(
     self, node: libcst.CSTNode, parent: libcst.CSTNode
 ) -> None:
     new_node = parenthesize.parenthesize_using_parent(node, parent)
     self.assertIs(new_node, node)
Example #5
0
 def test_tuple_return(self) -> None:
     node = libcst.parse_expression("1, 2, 3")
     new_node = parenthesize.parenthesize_using_parent(node, libcst.Return())
     self.assertIs(new_node, node)
Example #6
0
 def test_tuple_requires_paren(self) -> None:
     node = libcst.parse_expression("1, 2, 3")
     new_node = parenthesize.parenthesize_using_parent(
         node, libcst.Call(func=libcst.Name("func"))
     )
     self.assert_has_parentheses(new_node)
Example #7
0
 def test_not_parenthesizable(self) -> None:
     node = libcst.parse_statement("return foo")
     new_node = parenthesize.parenthesize_using_parent(
         node, libcst.parse_expression("a * (a + b)")
     )
     self.assertIs(new_node, node)
Example #8
0
 def test_expression_already_parenthesized(self) -> None:
     node = libcst.parse_expression("(a + b)")
     new_node = parenthesize.parenthesize_using_parent(
         node, libcst.parse_expression("a * (a + b)")
     )
     self.assertIs(new_node, node)