def check(conf, token, prev, next, nextnext, context):
    if prev is None:
        return

    curr_line_indent = token.start_mark.column
    if isinstance(token, yaml.StreamEndToken):
        curr_line_indent = 0

    skip_first_line = True
    if isinstance(prev, yaml.StreamStartToken):
        skip_first_line = False
        prev_line_indent = 0
    else:
        prev_line_indent = get_line_indent(prev)

    if prev_line_indent <= curr_line_indent:
        prev_line_indent = -1  # disable it

    for comment in get_comments_between_tokens(
            prev, token, skip_first_line=skip_first_line):
        if comment.column - 1 == curr_line_indent:
            prev_line_indent = -1  # disable it
        elif comment.column - 1 != prev_line_indent:
            yield LintProblem(comment.line, comment.column,
                              'comment not indented like content')
示例#2
0
    def test_get_line_indent(self):
        tokens = list(yaml.scan('a: 1\n'
                                'b:\n'
                                '  - c: [2, 3, {d: 4}]\n'))

        self.assertEqual(tokens[3].value, 'a')
        self.assertEqual(tokens[5].value, '1')
        self.assertEqual(tokens[7].value, 'b')
        self.assertEqual(tokens[13].value, 'c')
        self.assertEqual(tokens[16].value, '2')
        self.assertEqual(tokens[18].value, '3')
        self.assertEqual(tokens[22].value, 'd')
        self.assertEqual(tokens[24].value, '4')

        for i in (3, 5):
            self.assertEqual(get_line_indent(tokens[i]), 0)
        for i in (7,):
            self.assertEqual(get_line_indent(tokens[i]), 0)
        for i in (13, 16, 18, 22, 24):
            self.assertEqual(get_line_indent(tokens[i]), 2)
def check(conf, comment):
    # Only check block comments
    if (not isinstance(comment.token_before, yaml.StreamStartToken) and
            comment.token_before.end_mark.line + 1 == comment.line_no):
        return

    next_line_indent = comment.token_after.start_mark.column
    if isinstance(comment.token_after, yaml.StreamEndToken):
        next_line_indent = 0

    if isinstance(comment.token_before, yaml.StreamStartToken):
        prev_line_indent = 0
    else:
        prev_line_indent = get_line_indent(comment.token_before)

    # In the following case only the next line indent is valid:
    #     list:
    #         # comment
    #         - 1
    #         - 2
    if prev_line_indent <= next_line_indent:
        prev_line_indent = next_line_indent

    # If two indents are valid but a previous comment went back to normal
    # indent, for the next ones to do the same. In other words, avoid this:
    #     list:
    #         - 1
    #     # comment on valid indent (0)
    #         # comment on valid indent (4)
    #     other-list:
    #         - 2
    if (comment.comment_before is not None and
            not comment.comment_before.is_inline()):
        prev_line_indent = comment.comment_before.column_no - 1

    if (comment.column_no - 1 != prev_line_indent and
            comment.column_no - 1 != next_line_indent):
        yield LintProblem(comment.line_no, comment.column_no,
                          'comment not indented like content')
def check(conf, comment):
    # Only check block comments
    if (not isinstance(comment.token_before, yaml.StreamStartToken)
            and comment.token_before.end_mark.line + 1 == comment.line_no):
        return

    next_line_indent = comment.token_after.start_mark.column
    if isinstance(comment.token_after, yaml.StreamEndToken):
        next_line_indent = 0

    if isinstance(comment.token_before, yaml.StreamStartToken):
        prev_line_indent = 0
    else:
        prev_line_indent = get_line_indent(comment.token_before)

    # In the following case only the next line indent is valid:
    #     list:
    #         # comment
    #         - 1
    #         - 2
    if prev_line_indent <= next_line_indent:
        prev_line_indent = next_line_indent

    # If two indents are valid but a previous comment went back to normal
    # indent, for the next ones to do the same. In other words, avoid this:
    #     list:
    #         - 1
    #     # comment on valid indent (0)
    #         # comment on valid indent (4)
    #     other-list:
    #         - 2
    if (comment.comment_before is not None
            and not comment.comment_before.is_inline()):
        prev_line_indent = comment.comment_before.column_no - 1

    if (comment.column_no - 1 != prev_line_indent
            and comment.column_no - 1 != next_line_indent):
        yield LintProblem(comment.line_no, comment.column_no,
                          'comment not indented like content')