4
4
get_distribution as _get_distribution ,
5
5
DistributionNotFound as _DistributionNotFound ,
6
6
)
7
- from typing import List , Tuple , Iterator , Union , Optional
7
+ from typing import List , Tuple , Iterator , Union , Optional , Type
8
8
9
9
10
10
try :
@@ -23,7 +23,7 @@ class Plugin:
23
23
def __init__ (self , tree : ast .Module ):
24
24
self ._tree = tree
25
25
26
- def run (self ):
26
+ def run (self ) -> List [ Tuple [ int , int , str , Type [ "Plugin" ]]] :
27
27
visitor = HookRulesVisitor ()
28
28
visitor .visit (self ._tree )
29
29
cls = type (self )
@@ -66,8 +66,8 @@ def _visit_loop(self, node: ast.AST) -> None:
66
66
67
67
def _check_if_hook_defined_in_function (self , node : ast .FunctionDef ) -> None :
68
68
if self ._current_function is not None and _is_hook_or_element_def (node ):
69
- msg = f"Hook { node .name !r} defined inside another function. "
70
- self .errors . append (( node . lineno , node . col_offset , msg ) )
69
+ msg = f"hook { node .name !r} defined as closure in function { self . _current_function . name !r } "
70
+ self ._save_error ( 100 , node , msg )
71
71
72
72
def _check_if_propper_hook_usage (self , node : Union [ast .Name , ast .Attribute ]):
73
73
if isinstance (node , ast .Name ):
@@ -79,8 +79,8 @@ def _check_if_propper_hook_usage(self, node: Union[ast.Name, ast.Attribute]):
79
79
return
80
80
81
81
if not _is_hook_or_element_def (self ._current_function ):
82
- msg = f"Hook { name !r} used outside element or hook definition. "
83
- self .errors . append (( node . lineno , node . col_offset , msg ) )
82
+ msg = f"hook { name !r} used outside element or hook definition"
83
+ self ._save_error ( 101 , node , msg )
84
84
return
85
85
86
86
_loop_or_conditional = self ._current_conditional or self ._current_loop
@@ -94,10 +94,13 @@ def _check_if_propper_hook_usage(self, node: Union[ast.Name, ast.Attribute]):
94
94
ast .While : "while loop" ,
95
95
}
96
96
node_name = node_type_to_name [node_type ]
97
- msg = f"Hook { name !r} used inside { node_name } . "
98
- self .errors . append (( node . lineno , node . col_offset , msg ) )
97
+ msg = f"hook { name !r} used inside { node_name } "
98
+ self ._save_error ( 102 , node , msg )
99
99
return
100
100
101
+ def _save_error (self , error_code : int , node : ast .AST , message : str ):
102
+ self .errors .append ((node .lineno , node .col_offset , f"ROH{ error_code } { message } " ))
103
+
101
104
@contextmanager
102
105
def _set_current (self , ** attrs ) -> Iterator [None ]:
103
106
old_attrs = {k : getattr (self , f"_current_{ k } " ) for k in attrs }
0 commit comments