Skip to content

Commit 4a40cac

Browse files
committed
Revert "Disable 'class_definitions_order' linter check until fixed"
This reverts commit f268cc7.
1 parent b74ef6b commit 4a40cac

File tree

2 files changed

+94
-4
lines changed

2 files changed

+94
-4
lines changed

gdtoolkit/linter/class_checks.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,64 @@ def _is_method_private(method_name: str) -> bool:
5959
return method_name.startswith("_") # TODO: consider making configurable
6060

6161

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
6472

6573

6674
def _class_definitions_order_check_for_class(
67-
_class_name: str, _class_children, _order
75+
class_name: str, class_children, order
6876
) -> 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
70120

71121

72122
def _class_var_stmt_visibility(class_var_stmt) -> str:

tests/linter/test_class_checks.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,43 @@ def test_private_method_call_ok(code):
2929
])
3030
def test_private_method_call_nok(code):
3131
simple_nok_check(code, 'private-method-call')
32+
33+
34+
@pytest.mark.skip(reason='to be fixed in a bundle')
35+
@pytest.mark.parametrize('code', [
36+
"""
37+
pass
38+
class_name Foo
39+
extends Node
40+
signal s
41+
enum { A, B, C }
42+
const X = 1
43+
export var k = 1
44+
var x = 1
45+
var _x = 1
46+
onready var y = null
47+
onready var _y = null
48+
class Z:
49+
pass
50+
extends Node
51+
func foo():
52+
pass
53+
""",
54+
])
55+
def test_class_definitions_order_ok(code):
56+
simple_ok_check(code)
57+
58+
59+
@pytest.mark.parametrize('code', [
60+
"""var x
61+
signal s
62+
""",
63+
"""extends Node;var x
64+
signal s
65+
""",
66+
"""
67+
class X: var x;extends Node
68+
""",
69+
])
70+
def test_class_definitions_order_nok(code):
71+
simple_nok_check(code, 'class-definitions-order')

0 commit comments

Comments
 (0)