This repository was archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflake8_plugin.py
More file actions
77 lines (66 loc) · 2.55 KB
/
flake8_plugin.py
File metadata and controls
77 lines (66 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations
import ast
from argparse import Namespace
from flake8.options.manager import OptionManager
from reactpy_flake8 import __version__
from reactpy_flake8.run import (
DEFAULT_COMPONENT_DECORATOR_PATTERN,
DEFAULT_HOOK_FUNCTION_PATTERN,
run_checks,
)
from .exhaustive_deps import HOOKS_WITH_DEPS
class Plugin:
name = __name__
version = __version__
exhaustive_hook_deps: bool
component_decorator_pattern: str
hook_function_pattern: str
def add_options(self, option_manager: OptionManager) -> None:
option_manager.add_option(
"--exhaustive-hook-deps",
action="store_true",
default=False,
help=f"Whether to check hook dependencies for {', '.join(HOOKS_WITH_DEPS)}",
dest="exhaustive_hook_deps",
parse_from_config=True,
)
option_manager.add_option(
"--component-decorator-pattern",
nargs="?",
default=DEFAULT_COMPONENT_DECORATOR_PATTERN,
help=(
"The pattern which should match the component decorators. "
"Useful if you import the component decorator under an alias."
),
dest="component_decorator_pattern",
parse_from_config=True,
)
option_manager.add_option(
"--hook-function-pattern",
nargs="?",
default=DEFAULT_HOOK_FUNCTION_PATTERN,
help=(
"The pattern which should match the name of hook functions. Best used "
"if you have existing functions with 'use_*' names that are not hooks."
),
dest="hook_function_pattern",
parse_from_config=True,
)
def parse_options(self, options: Namespace) -> None:
self.exhaustive_hook_deps = options.exhaustive_hook_deps
self.component_decorator_pattern = options.component_decorator_pattern
self.hook_function_pattern = options.hook_function_pattern
def __call__(self, tree: ast.Module) -> list[tuple[int, int, str, type[Plugin]]]:
return [
error + (self.__class__,)
for error in run_checks(
tree,
self.exhaustive_hook_deps,
self.component_decorator_pattern,
self.hook_function_pattern,
)
]
def __init__(self) -> None:
# Hack to convince flake8 to accept plugins that are instances
# see: https://github.com/PyCQA/flake8/pull/1674
self.__init__ = self.__call__ # type: ignore