Ejemplo n.º 1
0
 def and_builder(left: cst.BaseExpression,
                 right: cst.BaseExpression) -> cst.BooleanOperation:
     return cst.BooleanOperation(
         left=left,
         operator=cst.And(),
         right=right,
     )
Ejemplo n.º 2
0
    def visit_BooleanOperation(self, node: cst.BooleanOperation) -> None:
        if node in self.seen_boolean_operations:
            return None

        stack = tuple(self.unwrap(node))
        operands, targets = self.collect_targets(stack)

        # If nothing gets collapsed, just exit from this short-path
        if len(operands) == len(stack):
            return None

        replacement = None
        for operand in operands:
            if operand in targets:
                matches = targets[operand]
                if len(matches) == 1:
                    arg = cst.Arg(value=matches[0])
                else:
                    arg = cst.Arg(cst.Tuple([cst.Element(match) for match in matches]))
                operand = cst.Call(cst.Name("isinstance"), [cst.Arg(operand), arg])

            if replacement is None:
                replacement = operand
            else:
                replacement = cst.BooleanOperation(
                    left=replacement, right=operand, operator=cst.Or()
                )

        if replacement is not None:
            self.report(node, replacement=replacement)
Ejemplo n.º 3
0
def boolean_op_with_whitespace(draw):  # type: ignore
    # for BooleanOperation, some expressions require whitespace before
    # and/or after e.g. a or b whereas (1)or(2) is OK.
    left = draw(st.from_type(libcst.BaseExpression))
    right = draw(st.from_type(libcst.BaseExpression))
    op = draw(st.from_type(libcst.BaseBooleanOp))
    if op.whitespace_before.empty and not left._safe_to_use_with_word_operator(
            ExpressionPosition.LEFT):  # pragma: no cover
        op = op.with_changes(whitespace_before=libcst.SimpleWhitespace(" "))
    if op.whitespace_after.empty and not right._safe_to_use_with_word_operator(
            ExpressionPosition.RIGHT):  # pragma: no cover
        op = op.with_changes(whitespace_after=libcst.SimpleWhitespace(" "))
    return libcst.BooleanOperation(left, op, right)
Ejemplo n.º 4
0
class BooleanOperationTest(CSTNodeTest):
    @data_provider(
        (
            # Simple boolean operations
            # pyre-fixme[6]: Incompatible parameter type
            {
                "node": cst.BooleanOperation(
                    cst.Name("foo"), cst.And(), cst.Name("bar")
                ),
                "code": "foo and bar",
                "parser": parse_expression,
                "expected_position": None,
            },
            {
                "node": cst.BooleanOperation(
                    cst.Name("foo"), cst.Or(), cst.Name("bar")
                ),
                "code": "foo or bar",
                "parser": parse_expression,
                "expected_position": None,
            },
            # Parenthesized boolean operation
            {
                "node": cst.BooleanOperation(
                    lpar=(cst.LeftParen(),),
                    left=cst.Name("foo"),
                    operator=cst.Or(),
                    right=cst.Name("bar"),
                    rpar=(cst.RightParen(),),
                ),
                "code": "(foo or bar)",
                "parser": parse_expression,
                "expected_position": None,
            },
            {
                "node": cst.BooleanOperation(
                    left=cst.Name(
                        "foo", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
                    ),
                    operator=cst.Or(
                        whitespace_before=cst.SimpleWhitespace(""),
                        whitespace_after=cst.SimpleWhitespace(""),
                    ),
                    right=cst.Name(
                        "bar", lpar=(cst.LeftParen(),), rpar=(cst.RightParen(),)
                    ),
                ),
                "code": "(foo)or(bar)",
                "parser": parse_expression,
                "expected_position": CodeRange.create((1, 0), (1, 12)),
            },
            # Make sure that spacing works
            {
                "node": cst.BooleanOperation(
                    lpar=(cst.LeftParen(whitespace_after=cst.SimpleWhitespace(" ")),),
                    left=cst.Name("foo"),
                    operator=cst.And(
                        whitespace_before=cst.SimpleWhitespace("  "),
                        whitespace_after=cst.SimpleWhitespace("  "),
                    ),
                    right=cst.Name("bar"),
                    rpar=(cst.RightParen(whitespace_before=cst.SimpleWhitespace(" ")),),
                ),
                "code": "( foo  and  bar )",
                "parser": parse_expression,
                "expected_position": None,
            },
        )
    )
    def test_valid(self, **kwargs: Any) -> None:
        self.validate_node(**kwargs)

    @data_provider(
        (
            {
                "get_node": lambda: cst.BooleanOperation(
                    cst.Name("foo"), cst.And(), cst.Name("bar"), lpar=(cst.LeftParen(),)
                ),
                "expected_re": "left paren without right paren",
            },
            {
                "get_node": lambda: cst.BooleanOperation(
                    cst.Name("foo"),
                    cst.And(),
                    cst.Name("bar"),
                    rpar=(cst.RightParen(),),
                ),
                "expected_re": "right paren without left paren",
            },
            {
                "get_node": lambda: cst.BooleanOperation(
                    left=cst.Name("foo"),
                    operator=cst.Or(
                        whitespace_before=cst.SimpleWhitespace(""),
                        whitespace_after=cst.SimpleWhitespace(""),
                    ),
                    right=cst.Name("bar"),
                ),
                "expected_re": "at least one space around boolean operator",
            },
        )
    )
    def test_invalid(self, **kwargs: Any) -> None:
        self.assert_invalid(**kwargs)