Ejemplo n.º 1
0
 def leave_Assign(self, original_node: libcst.Assign,
                  updated_node: libcst.Assign) -> libcst.Assign:
     assign_value = updated_node.value
     assign_whitespace = updated_node.targets[-1].whitespace_after_equal
     if libcst_matchers.matches(assign_whitespace,
                                libcst_matchers.ParenthesizedWhitespace()):
         adjusted_target = updated_node.targets[-1].with_changes(
             whitespace_after_equal=libcst.SimpleWhitespace(value=" "))
         updated_targets = list(updated_node.targets[:-1])
         updated_targets.append(adjusted_target)
         return updated_node.with_changes(
             targets=tuple(updated_targets),
             value=LineBreakTransformer.basic_parenthesize(
                 assign_value, assign_whitespace),
         )
     return updated_node.with_changes(
         value=LineBreakTransformer.basic_parenthesize(assign_value))
Ejemplo n.º 2
0
def is_model_field_type(name: str) -> bool:
    return name.endswith(('Field', 'ForeignKey'))


_any_comment = m.TrailingWhitespace(comment=m.Comment(
    m.MatchIfTrue(lambda n: n.startswith('#'))),
                                    newline=m.Newline())

_django_model_field_name_value = m.Call(func=m.Attribute(
    attr=m.Name(m.MatchIfTrue(is_model_field_type)))) | m.Call(
        func=m.Name(m.MatchIfTrue(is_model_field_type)))

_django_model_field_name_with_leading_comment_value = m.Call(
    func=m.Attribute(attr=m.Name(m.MatchIfTrue(is_model_field_type))),
    whitespace_before_args=m.ParenthesizedWhitespace(_any_comment),
) | m.Call(
    func=m.Name(m.MatchIfTrue(is_model_field_type)),
    whitespace_before_args=m.ParenthesizedWhitespace(_any_comment),
)

_django_model_field_with_leading_comment = m.SimpleStatementLine(body=[
    m.Assign(value=_django_model_field_name_with_leading_comment_value)
    | m.AnnAssign(value=_django_model_field_name_with_leading_comment_value)
])

_django_model_field_with_trailing_comment = m.SimpleStatementLine(
    body=[
        m.Assign(value=_django_model_field_name_value)
        | m.AnnAssign(value=_django_model_field_name_value)
    ],
null_comment = m.TrailingWhitespace(
    comment=m.Comment(m.MatchIfTrue(is_valid_comment)),
    newline=m.Newline(),
)

field_without_comment = m.SimpleStatementLine(
    body=[
        m.Assign(value=(m.Call(
            args=[
                m.ZeroOrMore(),
                m.Arg(keyword=m.Name('null'), value=m.Name('True')),
                m.ZeroOrMore(),
            ],
            whitespace_before_args=m.DoesNotMatch(
                m.ParenthesizedWhitespace(null_comment)),
        )
                        | m.Call(
                            func=m.Attribute(attr=m.Name('NullBooleanField')),
                            whitespace_before_args=m.DoesNotMatch(
                                m.ParenthesizedWhitespace(null_comment)),
                        )
                        | m.Call(
                            func=m.Name('NullBooleanField'),
                            whitespace_before_args=m.DoesNotMatch(
                                m.ParenthesizedWhitespace(null_comment)),
                        )))
    ],
    trailing_whitespace=m.DoesNotMatch(null_comment),
)