44 get_distribution as _get_distribution ,
55 DistributionNotFound as _DistributionNotFound ,
66)
7- from typing import List , Tuple , Iterator , Union , Optional
7+ from typing import List , Tuple , Iterator , Union , Optional , Type
88
99
1010try :
@@ -23,7 +23,7 @@ class Plugin:
2323 def __init__ (self , tree : ast .Module ):
2424 self ._tree = tree
2525
26- def run (self ):
26+ def run (self ) -> List [ Tuple [ int , int , str , Type [ "Plugin" ]]] :
2727 visitor = HookRulesVisitor ()
2828 visitor .visit (self ._tree )
2929 cls = type (self )
@@ -66,8 +66,8 @@ def _visit_loop(self, node: ast.AST) -> None:
6666
6767 def _check_if_hook_defined_in_function (self , node : ast .FunctionDef ) -> None :
6868 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 )
7171
7272 def _check_if_propper_hook_usage (self , node : Union [ast .Name , ast .Attribute ]):
7373 if isinstance (node , ast .Name ):
@@ -79,8 +79,8 @@ def _check_if_propper_hook_usage(self, node: Union[ast.Name, ast.Attribute]):
7979 return
8080
8181 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 )
8484 return
8585
8686 _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]):
9494 ast .While : "while loop" ,
9595 }
9696 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 )
9999 return
100100
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+
101104 @contextmanager
102105 def _set_current (self , ** attrs ) -> Iterator [None ]:
103106 old_attrs = {k : getattr (self , f"_current_{ k } " ) for k in attrs }
0 commit comments