Skip to content

Commit 90ebbd1

Browse files
committed
Fix formatting of certain '@warning_ignore' annotations, fixes #267
1 parent e9ec1f5 commit 90ebbd1

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Changed
66
- Fixed linter problem reports when global scope is used
7+
- Fixed formatting of certain `@warning_ignore` annotations
78

89
## [4.3.3] 2024-11-02
910

gdtoolkit/common/ast.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from lark import Tree
55

6-
from ..formatter.annotation import STANDALONE_ANNOTATIONS
6+
from ..formatter.annotation import is_non_standalone_annotation
77

88
from .utils import find_name_token_among_children, find_tree_among_children
99
from .exceptions import GDToolkitError
@@ -124,9 +124,7 @@ def _load_data_from_node_children(self, node: Tree) -> None:
124124
offset = 1 if node.data == "class_def" else 0
125125
annotations = []
126126
for stmt in node.children[offset:]:
127-
if stmt.data == "annotation" and not _is_annotation_standalone(
128-
Annotation(stmt)
129-
):
127+
if stmt.data == "annotation" and is_non_standalone_annotation(stmt):
130128
annotations.append(Annotation(stmt))
131129
continue
132130
if stmt.data == "property_body_def":
@@ -159,7 +157,3 @@ def __init__(self, parse_tree: Tree):
159157
self.root_class = Class(parse_tree)
160158
self.all_classes = [self.root_class] + self.root_class.all_sub_classes
161159
self.all_functions = self.root_class.all_functions
162-
163-
164-
def _is_annotation_standalone(annotation: Annotation) -> bool:
165-
return annotation.name in STANDALONE_ANNOTATIONS

gdtoolkit/formatter/annotation.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,30 @@
88
from .expression import format_concrete_expression
99
from .expression_to_str import expression_to_str
1010

11-
STANDALONE_ANNOTATIONS = [
11+
_STANDALONE_ANNOTATIONS = [
1212
"export_category",
1313
"export_group",
1414
"export_subgroup",
1515
"icon",
1616
"tool",
17-
"warning_ignore",
17+
]
18+
_NON_STANDALONE_WARNING_IGNORES = [
19+
"unused_parameter",
1820
]
1921

2022

2123
def is_non_standalone_annotation(statement: Tree) -> bool:
2224
if statement.data != "annotation":
2325
return False
2426
name = statement.children[0].value
25-
return name not in STANDALONE_ANNOTATIONS
27+
if name in _STANDALONE_ANNOTATIONS:
28+
return False
29+
if name != "warning_ignore":
30+
return True
31+
ignoree = statement.children[1].children[0].children[0].value.strip('"')
32+
if ignoree in _NON_STANDALONE_WARNING_IGNORES:
33+
return True
34+
return False
2635

2736

2837
def prepend_annotations_to_formatted_line(
@@ -78,8 +87,8 @@ def _annotations_have_standalone_comments(
7887
return any(
7988
comment is not None
8089
for comment in standalone_comments[
81-
get_line(annotations[0]) : last_line
82-
if last_line is not None
83-
else get_end_line(annotations[-1])
90+
get_line(annotations[0]) : (
91+
last_line if last_line is not None else get_end_line(annotations[-1])
92+
)
8493
]
8594
)

tests/formatter/input-output-pairs/simple_function_annotations.in.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ func b():
88
func d():
99
@warning_ignore("unused_variable") @warning_ignore("unused_variable") var x: Array[int] = [ 1, 2, ]
1010

11+
@warning_ignore("unused_parameter")
1112
func e():
1213
@warning_ignore("unused_variable")
1314
@warning_ignore("unused_variable")

tests/formatter/input-output-pairs/simple_function_annotations.out.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func d():
2323
]
2424

2525

26+
@warning_ignore("unused_parameter")
2627
func e():
2728
@warning_ignore("unused_variable")
2829
@warning_ignore("unused_variable")

0 commit comments

Comments
 (0)