@@ -59,14 +59,64 @@ def _is_method_private(method_name: str) -> bool:
59
59
return method_name .startswith ("_" ) # TODO: consider making configurable
60
60
61
61
62
- def _class_definitions_order_check (_order , _parse_tree : Tree ) -> List [Problem ]:
63
- return []
62
+ def _class_definitions_order_check (order , parse_tree : Tree ) -> List [Problem ]:
63
+ problems = _class_definitions_order_check_for_class (
64
+ "global scope" , parse_tree .children , order
65
+ )
66
+ for class_def in parse_tree .find_data ("class_def" ):
67
+ class_name = class_def .children [0 ].value
68
+ problems += _class_definitions_order_check_for_class (
69
+ "class {}" .format (class_name ), class_def .children , order
70
+ )
71
+ return problems
64
72
65
73
66
74
def _class_definitions_order_check_for_class (
67
- _class_name : str , _class_children , _order
75
+ class_name : str , class_children , order
68
76
) -> List [Problem ]:
69
- return []
77
+ stmt_to_section_mapping = {
78
+ "tool_stmt" : "tools" ,
79
+ "signal_stmt" : "signals" ,
80
+ "extends_stmt" : "extends" ,
81
+ "classname_stmt" : "classnames" ,
82
+ "const_stmt" : "consts" ,
83
+ "export_stmt" : "exports" ,
84
+ "enum_def" : "enums" ,
85
+ }
86
+ visibility_dependent_stmt_to_section_mapping = {
87
+ "class_var_stmt" : {"pub" : "pubvars" , "prv" : "prvvars" },
88
+ "onready_stmt" : {"pub" : "onreadypubvars" , "prv" : "onreadyprvvars" },
89
+ }
90
+ problems = []
91
+ current_section = order [0 ]
92
+ for class_child in class_children :
93
+ if not isinstance (class_child , Tree ):
94
+ continue
95
+ if class_child .data in ["annotation" ]:
96
+ continue
97
+ stmt = class_child .data
98
+ if stmt == "class_var_stmt" :
99
+ visibility = _class_var_stmt_visibility (class_child )
100
+ section = visibility_dependent_stmt_to_section_mapping [stmt ][visibility ]
101
+ elif stmt == "onready_stmt" :
102
+ class_var_stmt = class_child .children [0 ]
103
+ visibility = _class_var_stmt_visibility (class_var_stmt )
104
+ section = visibility_dependent_stmt_to_section_mapping [stmt ][visibility ]
105
+ else :
106
+ section = stmt_to_section_mapping .get (stmt , "others" )
107
+ section_rank = order .index (section )
108
+ if section_rank >= order .index (current_section ):
109
+ current_section = section
110
+ else :
111
+ problems .append (
112
+ Problem (
113
+ name = "class-definitions-order" ,
114
+ description = "Definition out of order in {}" .format (class_name ),
115
+ line = class_child .line ,
116
+ column = class_child .column ,
117
+ )
118
+ )
119
+ return problems
70
120
71
121
72
122
def _class_var_stmt_visibility (class_var_stmt ) -> str :
0 commit comments