Module rulebook-pylint.rulebook_pylint.case_separator
Functions
def register(linter: PyLinter)
-
Expand source code
def register(linter: 'PyLinter') -> None: linter.register_checker(CaseSeparatorChecker(linter))
Classes
class CaseSeparatorChecker (linter: PyLinter)
-
Expand source code
class CaseSeparatorChecker(RulebookFileChecker): """See detail: https://hanggrian.github.io/rulebook/rules/#case-separator""" MSG_MISSING: str = 'case-separator-missing' MSG_UNEXPECTED: str = 'case-separator-unexpected' name: str = 'case-separator' msgs: dict[str, MessageDefinitionTuple] = Messages.of(MSG_MISSING, MSG_UNEXPECTED) def visit_match(self, node: Match) -> None: # collect cases match_cases: list[MatchCase] = node.cases for (i, match_case) in enumerate(match_cases): # targeting switch, skip first branch if i == 0: continue last_match_case: MatchCase = match_cases[i - 1] match_case_fromlineno: int = \ get_fromlineno_before(self.lines, match_case, last_match_case) last_body: NodeNG = last_match_case.body[-1] # checks for violation if is_multiline(last_match_case) or has_comment_above(self.lines, last_match_case): if last_body.tolineno - 1 != match_case_fromlineno - 2: self.add_message( self.MSG_MISSING, line=last_body.lineno, end_lineno=last_body.end_lineno, col_offset=last_body.col_offset, end_col_offset=last_body.end_col_offset, ) continue if last_body.tolineno - 1 == match_case_fromlineno - 1: continue self.add_message( self.MSG_UNEXPECTED, line=last_body.lineno, end_lineno=last_body.end_lineno, col_offset=last_body.col_offset, end_col_offset=last_body.end_col_offset, )
See detail: https://hanggrian.github.io/rulebook/rules/#case-separator
Checker instances should have the linter as argument.
Ancestors
- rulebook_pylint.checkers.RulebookFileChecker
- pylint.checkers.base_checker.BaseRawFileChecker
- pylint.checkers.base_checker.BaseChecker
- pylint.config.arguments_provider._ArgumentsProvider
- abc.ABC
Class variables
var MSG_MISSING : str
-
The type of the None singleton.
var MSG_UNEXPECTED : 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 visit_match(self, node: astroid.nodes.node_classes.Match) ‑> None
-
Expand source code
def visit_match(self, node: Match) -> None: # collect cases match_cases: list[MatchCase] = node.cases for (i, match_case) in enumerate(match_cases): # targeting switch, skip first branch if i == 0: continue last_match_case: MatchCase = match_cases[i - 1] match_case_fromlineno: int = \ get_fromlineno_before(self.lines, match_case, last_match_case) last_body: NodeNG = last_match_case.body[-1] # checks for violation if is_multiline(last_match_case) or has_comment_above(self.lines, last_match_case): if last_body.tolineno - 1 != match_case_fromlineno - 2: self.add_message( self.MSG_MISSING, line=last_body.lineno, end_lineno=last_body.end_lineno, col_offset=last_body.col_offset, end_col_offset=last_body.end_col_offset, ) continue if last_body.tolineno - 1 == match_case_fromlineno - 1: continue self.add_message( self.MSG_UNEXPECTED, line=last_body.lineno, end_lineno=last_body.end_lineno, col_offset=last_body.col_offset, end_col_offset=last_body.end_col_offset, )