コード例 #1
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_previous(
         node, libcst.parse_statement("return (x for x in foo)")
     )
     self.assert_has_parentheses(new_node)
コード例 #2
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_previous(
         node, libcst.parse_expression("max((x for x in foo), foo)")
     )
     self.assert_has_parentheses(new_node)
コード例 #3
0
ファイル: transformer.py プロジェクト: sk-/craftier
 def custom_on_leave(
     self: CraftierTransformer,
     original_node: libcst.CSTNode,
     updated_node: libcst.CSTNode,
 ) -> libcst.CSTNode:
     new_node = updated_node
     status = "unchanged"
     start = time.perf_counter_ns()
     # We don't support wildcard patterns yet, so we can safely cast the
     # results to single nodes.
     matches = cast(
         Dict[str, libcst.CSTNode],
         libcst.matchers.extract(updated_node, transform.before),
     )
     if matches is not None:
         actual_matches = check_matches(matches)
         if actual_matches is not None:
             # TODO: make sure all new nodes have a different id
             new_node = _replace_names(transform.after,
                                       transform.wrapper,
                                       actual_matches)
             logger.debug(
                 "{} changed file {}",
                 perf_name,
                 self.context.filename,
             )
             new_node = parenthesize.parenthesize_using_previous(
                 new_node, updated_node)
             status = "modified"
             self._mark_as_modified()
     end = time.perf_counter_ns()
     performance.write({
         "name": perf_name,
         "time": (end - start) / 1e6,
         "status": status,
     })
     return new_node
コード例 #4
0
 def test_expression_same_precedence(
     self, node: libcst.CSTNode, parent: libcst.CSTNode
 ) -> None:
     new_node = parenthesize.parenthesize_using_previous(node, parent)
     self.assertIs(new_node, node)
コード例 #5
0
 def test_expression_higher_precedence(
     self, node: libcst.CSTNode, parent: libcst.CSTNode
 ) -> None:
     new_node = parenthesize.parenthesize_using_previous(node, parent)
     self.assert_has_parentheses(new_node)
コード例 #6
0
 def test_tuple_return(self) -> None:
     node = libcst.parse_expression("1, 2, 3")
     new_node = parenthesize.parenthesize_using_previous(
         node, libcst.Return()
     )
     self.assert_has_parentheses(new_node)
コード例 #7
0
 def test_tuple_requires_paren(self) -> None:
     node = libcst.parse_expression("1, 2, 3")
     new_node = parenthesize.parenthesize_using_previous(
         node, libcst.Call(func=libcst.Name("func"))
     )
     self.assert_has_parentheses(new_node)
コード例 #8
0
 def test_not_parenthesizable(self) -> None:
     node = libcst.parse_statement("return foo")
     new_node = parenthesize.parenthesize_using_previous(
         node, libcst.parse_expression("a * (a + b)")
     )
     self.assertIs(new_node, node)
コード例 #9
0
 def test_expression_already_parenthesized(self) -> None:
     node = libcst.parse_expression("(a + b)")
     new_node = parenthesize.parenthesize_using_previous(
         node, libcst.parse_expression("a * (a + b)")
     )
     self.assertIs(new_node, node)
コード例 #10
0
 def test_expression_previous_parenthesized(self) -> None:
     node = libcst.parse_expression("a + b")
     new_node = parenthesize.parenthesize_using_previous(
         node, libcst.parse_expression("(a + b)")
     )
     self.assert_has_parentheses(new_node)