diff --git a/CHANGELOG.md b/CHANGELOG.md index d5cbb09..85a3d28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Changed - Fixed linter problem reports when global scope is used + - Fixed formatting of certain `@warning_ignore` annotations ## [4.3.3] 2024-11-02 diff --git a/gdtoolkit/common/ast.py b/gdtoolkit/common/ast.py index 89149e2..f3ca564 100644 --- a/gdtoolkit/common/ast.py +++ b/gdtoolkit/common/ast.py @@ -3,7 +3,7 @@ from lark import Tree -from ..formatter.annotation import STANDALONE_ANNOTATIONS +from ..formatter.annotation import is_non_standalone_annotation from .utils import find_name_token_among_children, find_tree_among_children from .exceptions import GDToolkitError @@ -124,9 +124,7 @@ def _load_data_from_node_children(self, node: Tree) -> None: offset = 1 if node.data == "class_def" else 0 annotations = [] for stmt in node.children[offset:]: - if stmt.data == "annotation" and not _is_annotation_standalone( - Annotation(stmt) - ): + if stmt.data == "annotation" and is_non_standalone_annotation(stmt): annotations.append(Annotation(stmt)) continue if stmt.data == "property_body_def": @@ -159,7 +157,3 @@ def __init__(self, parse_tree: Tree): self.root_class = Class(parse_tree) self.all_classes = [self.root_class] + self.root_class.all_sub_classes self.all_functions = self.root_class.all_functions - - -def _is_annotation_standalone(annotation: Annotation) -> bool: - return annotation.name in STANDALONE_ANNOTATIONS diff --git a/gdtoolkit/formatter/annotation.py b/gdtoolkit/formatter/annotation.py index 6204c7c..091fb57 100644 --- a/gdtoolkit/formatter/annotation.py +++ b/gdtoolkit/formatter/annotation.py @@ -8,13 +8,15 @@ from .expression import format_concrete_expression from .expression_to_str import expression_to_str -STANDALONE_ANNOTATIONS = [ +_STANDALONE_ANNOTATIONS = [ "export_category", "export_group", "export_subgroup", "icon", "tool", - "warning_ignore", +] +_NON_STANDALONE_WARNING_IGNORES = [ + "unused_parameter", ] @@ -22,7 +24,14 @@ def is_non_standalone_annotation(statement: Tree) -> bool: if statement.data != "annotation": return False name = statement.children[0].value - return name not in STANDALONE_ANNOTATIONS + if name in _STANDALONE_ANNOTATIONS: + return False + if name != "warning_ignore": + return True + ignoree = statement.children[1].children[0].children[0].value.strip('"') + if ignoree in _NON_STANDALONE_WARNING_IGNORES: + return True + return False def prepend_annotations_to_formatted_line( @@ -78,8 +87,8 @@ def _annotations_have_standalone_comments( return any( comment is not None for comment in standalone_comments[ - get_line(annotations[0]) : last_line - if last_line is not None - else get_end_line(annotations[-1]) + get_line(annotations[0]) : ( + last_line if last_line is not None else get_end_line(annotations[-1]) + ) ] ) diff --git a/tests/formatter/input-output-pairs/simple_function_annotations.in.gd b/tests/formatter/input-output-pairs/simple_function_annotations.in.gd index 16dcc5f..8508e1b 100644 --- a/tests/formatter/input-output-pairs/simple_function_annotations.in.gd +++ b/tests/formatter/input-output-pairs/simple_function_annotations.in.gd @@ -8,6 +8,7 @@ func b(): func d(): @warning_ignore("unused_variable") @warning_ignore("unused_variable") var x: Array[int] = [ 1, 2, ] +@warning_ignore("unused_parameter") func e(): @warning_ignore("unused_variable") @warning_ignore("unused_variable") diff --git a/tests/formatter/input-output-pairs/simple_function_annotations.out.gd b/tests/formatter/input-output-pairs/simple_function_annotations.out.gd index 17c5c51..f776d85 100644 --- a/tests/formatter/input-output-pairs/simple_function_annotations.out.gd +++ b/tests/formatter/input-output-pairs/simple_function_annotations.out.gd @@ -23,6 +23,7 @@ func d(): ] +@warning_ignore("unused_parameter") func e(): @warning_ignore("unused_variable") @warning_ignore("unused_variable")