Module rulebook-pylint.rulebook_pylint.comment_trim

Functions

def register(linter: PyLinter)
Expand source code
def register(linter: 'PyLinter') -> None:
    linter.register_checker(CommentTrimChecker(linter))

Classes

class CommentTrimChecker (linter: PyLinter)
Expand source code
class CommentTrimChecker(RulebookTokenChecker):
    """See detail: https://hanggrian.github.io/rulebook/rules/#comment-trim"""
    MSG: str = 'comment-trim'

    name: str = 'comment-trim'
    msgs: dict[str, MessageDefinitionTuple] = Messages.of(MSG)

    def process_tokens(self, tokens: list[TokenInfo]) -> None:
        for i, token in enumerate(tokens):
            # target comment
            if token.type != COMMENT:
                continue

            # continue if this comment is first line
            if i - 2 >= 0 and \
                tokens[i - 1].type == NL and \
                tokens[i - 2].type == COMMENT:
                return

            # iterate to find last
            j: int = i
            while j + 2 < len(tokens) and \
                tokens[j + 1].type == NL and \
                tokens[j + 2].type == COMMENT:
                j += 2
            current_token: TokenInfo = tokens[j]

            # skip blank comment
            if current_token is token:
                return

            # checks for violation
            if is_comment_empty(token):
                self.add_message(self.MSG, line=token.start[0], col_offset=token.start[1])
            if is_comment_empty(current_token):
                self.add_message(
                    self.MSG,
                    line=current_token.start[0],
                    col_offset=current_token.start[1],
                )
            return

See detail: https://hanggrian.github.io/rulebook/rules/#comment-trim

Checker instances should have the linter as argument.

Ancestors

  • rulebook_pylint.checkers.RulebookTokenChecker
  • pylint.checkers.base_checker.BaseTokenChecker
  • pylint.checkers.base_checker.BaseChecker
  • pylint.config.arguments_provider._ArgumentsProvider
  • abc.ABC

Class variables

var MSG : str

The type of the None singleton.

var msgs : dict[str, tuple[str, str, str] | tuple[str, str, str, pylint.typing.ExtraMessageOptions]]

The type of the None singleton.

var name : str

The type of the None singleton.

Methods

def process_tokens(self, tokens: list[tokenize.TokenInfo]) ‑> None
Expand source code
def process_tokens(self, tokens: list[TokenInfo]) -> None:
    for i, token in enumerate(tokens):
        # target comment
        if token.type != COMMENT:
            continue

        # continue if this comment is first line
        if i - 2 >= 0 and \
            tokens[i - 1].type == NL and \
            tokens[i - 2].type == COMMENT:
            return

        # iterate to find last
        j: int = i
        while j + 2 < len(tokens) and \
            tokens[j + 1].type == NL and \
            tokens[j + 2].type == COMMENT:
            j += 2
        current_token: TokenInfo = tokens[j]

        # skip blank comment
        if current_token is token:
            return

        # checks for violation
        if is_comment_empty(token):
            self.add_message(self.MSG, line=token.start[0], col_offset=token.start[1])
        if is_comment_empty(current_token):
            self.add_message(
                self.MSG,
                line=current_token.start[0],
                col_offset=current_token.start[1],
            )
        return

Should be overridden by subclasses.