2024-01-16 09:47:53 +01:00
|
|
|
"""Plugin for checking sorted platforms list."""
|
2024-03-08 16:36:11 +01:00
|
|
|
|
2024-01-16 09:47:53 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from astroid import nodes
|
|
|
|
from pylint.checkers import BaseChecker
|
|
|
|
from pylint.lint import PyLinter
|
|
|
|
|
|
|
|
|
|
|
|
class HassEnforceSortedPlatformsChecker(BaseChecker):
|
|
|
|
"""Checker for sorted platforms list."""
|
|
|
|
|
|
|
|
name = "hass_enforce_sorted_platforms"
|
|
|
|
priority = -1
|
|
|
|
msgs = {
|
|
|
|
"W7451": (
|
|
|
|
"Platforms must be sorted alphabetically",
|
|
|
|
"hass-enforce-sorted-platforms",
|
|
|
|
"Used when PLATFORMS should be sorted alphabetically.",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
options = ()
|
|
|
|
|
2024-02-25 21:18:07 +01:00
|
|
|
def visit_annassign(self, node: nodes.AnnAssign) -> None:
|
|
|
|
"""Check for sorted PLATFORMS const with type annotations."""
|
|
|
|
self._do_sorted_check(node.target, node)
|
|
|
|
|
2024-01-16 09:47:53 +01:00
|
|
|
def visit_assign(self, node: nodes.Assign) -> None:
|
2024-02-25 21:18:07 +01:00
|
|
|
"""Check for sorted PLATFORMS const without type annotations."""
|
2024-01-16 09:47:53 +01:00
|
|
|
for target in node.targets:
|
2024-02-25 21:18:07 +01:00
|
|
|
self._do_sorted_check(target, node)
|
|
|
|
|
|
|
|
def _do_sorted_check(
|
|
|
|
self, target: nodes.NodeNG, node: nodes.Assign | nodes.AnnAssign
|
|
|
|
) -> None:
|
|
|
|
"""Check for sorted PLATFORMS const."""
|
|
|
|
if (
|
|
|
|
isinstance(target, nodes.AssignName)
|
|
|
|
and target.name == "PLATFORMS"
|
|
|
|
and isinstance(node.value, nodes.List)
|
|
|
|
):
|
|
|
|
platforms = [v.as_string() for v in node.value.elts]
|
|
|
|
sorted_platforms = sorted(platforms)
|
|
|
|
if platforms != sorted_platforms:
|
|
|
|
self.add_message("hass-enforce-sorted-platforms", node=node)
|
2024-01-16 09:47:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def register(linter: PyLinter) -> None:
|
|
|
|
"""Register the checker."""
|
|
|
|
linter.register_checker(HassEnforceSortedPlatformsChecker(linter))
|