From 91a46f0340c77168b28756ae419e3ce48cbe4283 Mon Sep 17 00:00:00 2001 From: Taus Date: Fri, 8 May 2026 15:37:29 +0000 Subject: [PATCH] unified: stop `"!"` bleeding through You know the drill. We just make an anonymous node named instead. In this case, however, we have to be a bit more clever about how to rewrite it. We turn the sequence of a type followed by an optional ! into a _choice_ between mere type or type followed by bang (the latter being our new named node). --- .../extractor/tree-sitter-swift/grammar.js | 4 +++- .../tree-sitter-swift/node-types.yml | 23 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/unified/extractor/tree-sitter-swift/grammar.js b/unified/extractor/tree-sitter-swift/grammar.js index 01f714285c7..321ceb01363 100644 --- a/unified/extractor/tree-sitter-swift/grammar.js +++ b/unified/extractor/tree-sitter-swift/grammar.js @@ -401,7 +401,9 @@ module.exports = grammar({ type_annotation: ($) => seq(":", field("type", $._possibly_implicitly_unwrapped_type)), _possibly_implicitly_unwrapped_type: ($) => - seq($._type, optional(token.immediate("!"))), + choice($._type, $.implicitly_unwrapped_type), + implicitly_unwrapped_type: ($) => + seq($._type, token.immediate("!")), _type: ($) => prec.right( PRECS.ty, diff --git a/unified/extractor/tree-sitter-swift/node-types.yml b/unified/extractor/tree-sitter-swift/node-types.yml index 3e9d6bba968..b77df29133b 100644 --- a/unified/extractor/tree-sitter-swift/node-types.yml +++ b/unified/extractor/tree-sitter-swift/node-types.yml @@ -226,7 +226,7 @@ named: body: function_body default_value*: expression name+: [referenceable_operator, simple_identifier, unannotated_type] - return_type*: ["!", type_modifiers, unannotated_type] + return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type] function_modifier: function_type: $children?: [throws, throws_clause] @@ -248,6 +248,9 @@ named: if_statement: $children*: [else, if_statement, statements] condition+: if_condition + implicitly_unwrapped_type: + $children?: type_modifiers + name: unannotated_type import_declaration: $children+: [identifier, modifiers] infix_expression: @@ -257,8 +260,8 @@ named: inheritance_constraint: $children*: attribute constrained_type+: [".", identifier, simple_identifier, unannotated_type] - inherits_from+: ["!", type_modifiers, unannotated_type] - name: unannotated_type + inherits_from+: [implicitly_unwrapped_type, type_modifiers, unannotated_type] + name?: unannotated_type inheritance_modifier: inheritance_specifier: inherits_from: [function_type, suppressed_constraint, user_type] @@ -280,7 +283,7 @@ named: lambda_function_type: $children*: [lambda_function_type_parameters, throws, throws_clause] name?: unannotated_type - return_type*: ["!", type_modifiers, unannotated_type] + return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type] lambda_function_type_parameters: $children+: lambda_parameter lambda_literal: @@ -291,7 +294,7 @@ named: $children?: [parameter_modifiers, self_expression] external_name?: simple_identifier name*: [simple_identifier, unannotated_type] - type*: ["!", type_modifiers, unannotated_type] + type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type] line_str_text: line_string_literal: interpolation*: interpolated_expression @@ -348,7 +351,7 @@ named: $children?: parameter_modifiers external_name?: simple_identifier name+: [simple_identifier, unannotated_type] - type+: ["!", type_modifiers, unannotated_type] + type+: [implicitly_unwrapped_type, type_modifiers, unannotated_type] parameter_modifier: parameter_modifiers: $children+: parameter_modifier @@ -391,7 +394,7 @@ named: $children*: [attribute, modifiers, parameter, statements, throws, throws_clause, type_constraints, type_parameters] default_value*: expression name*: [referenceable_operator, simple_identifier, unannotated_type] - return_type*: ["!", type_modifiers, unannotated_type] + return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type] protocol_property_declaration: $children+: [modifiers, protocol_property_requirements, type_annotation, type_constraints] name: pattern @@ -437,7 +440,7 @@ named: $children+: [attribute, computed_property, modifiers, parameter, type_constraints, type_parameters] default_value*: expression name?: unannotated_type - return_type*: ["!", type_modifiers, unannotated_type] + return_type*: [implicitly_unwrapped_type, type_modifiers, unannotated_type] super_expression: suppressed_constraint: suppressed: type_identifier @@ -472,8 +475,8 @@ named: name*: [simple_identifier, unannotated_type] type*: [type_modifiers, unannotated_type] type_annotation: - name: unannotated_type - type+: ["!", type_modifiers, unannotated_type] + name?: unannotated_type + type+: [implicitly_unwrapped_type, type_modifiers, unannotated_type] type_arguments: $children*: type_modifiers name+: unannotated_type