From 9dddd93460cf70dd2802101bb9fc68b3d26d7769 Mon Sep 17 00:00:00 2001 From: Taus Date: Fri, 8 May 2026 21:40:53 +0000 Subject: [PATCH] unified: add field declarations for statements and members Part 1 of N of "getting rid of $children" in node-types.yml Note: in one of the cases the affected node still has the $children field present. This is because there's some weirdness about recording multiline comments as class member separators that I did not want to figure out how to address right now. --- unified/extractor/tree-sitter-swift/grammar.js | 16 ++++++++-------- .../extractor/tree-sitter-swift/node-types.yml | 12 +++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/unified/extractor/tree-sitter-swift/grammar.js b/unified/extractor/tree-sitter-swift/grammar.js index 5dbfd7fdbbf..76e8020b0b8 100644 --- a/unified/extractor/tree-sitter-swift/grammar.js +++ b/unified/extractor/tree-sitter-swift/grammar.js @@ -255,11 +255,11 @@ module.exports = grammar({ //////////////////////////////// source_file: ($) => seq( - optional($.shebang_line), + optional(field("shebang", $.shebang_line)), optional( seq( - $._top_level_statement, - repeat(seq($._semi, $._top_level_statement)), + field("statement", $._top_level_statement), + repeat(seq($._semi, field("statement", $._top_level_statement))), optional($._semi) ) ) @@ -1182,8 +1182,8 @@ module.exports = grammar({ prec.left( // Left precedence is required in switch statements seq( - $._local_statement, - repeat(seq($._semi, $._local_statement)), + field("statement", $._local_statement), + repeat(seq($._semi, field("statement", $._local_statement))), optional($._semi) ) ), @@ -1605,7 +1605,7 @@ module.exports = grammar({ _class_member_separator: ($) => choice($._semi, $.multiline_comment), _class_member_declarations: ($) => seq( - sep1($.type_level_declaration, $._class_member_separator), + sep1(field("member", $.type_level_declaration), $._class_member_separator), optional($._class_member_separator) ), _function_value_parameters: ($) => @@ -1673,7 +1673,7 @@ module.exports = grammar({ throws_clause: ($) => seq($._throws_keyword, "(", field("type", $.unannotated_type), ")"), enum_class_body: ($) => - seq("{", repeat(choice($.enum_entry, $.type_level_declaration)), "}"), + seq("{", repeat(field("member", choice($.enum_entry, $.type_level_declaration))), "}"), enum_entry: ($) => seq( optional($.modifiers), @@ -1725,7 +1725,7 @@ module.exports = grammar({ protocol_body: ($) => seq("{", optional($._protocol_member_declarations), "}"), _protocol_member_declarations: ($) => - seq(sep1($.protocol_member_declaration, $._semi), optional($._semi)), + seq(sep1(field("member", $.protocol_member_declaration), $._semi), optional($._semi)), protocol_member_declaration: ($) => choice( $.protocol_function_declaration, diff --git a/unified/extractor/tree-sitter-swift/node-types.yml b/unified/extractor/tree-sitter-swift/node-types.yml index c4bf650944b..7d9d096d9c9 100644 --- a/unified/extractor/tree-sitter-swift/node-types.yml +++ b/unified/extractor/tree-sitter-swift/node-types.yml @@ -166,7 +166,8 @@ named: target: expression type: type class_body: - $children*: [multiline_comment, type_level_declaration] + $children*: multiline_comment + member*: type_level_declaration class_declaration: $children*: [attribute, inheritance_modifier, inheritance_specifier, modifiers, ownership_modifier, property_behavior_modifier, type_constraints, type_parameters] body: [class_body, enum_class_body] @@ -226,7 +227,7 @@ named: $children*: [catch_block, statements] else: enum_class_body: - $children*: [enum_entry, type_level_declaration] + member*: [enum_entry, type_level_declaration] enum_entry: $children?: modifiers data_contents*: enum_type_parameters @@ -410,7 +411,7 @@ named: value*: expression property_modifier: protocol_body: - $children*: protocol_member_declaration + member*: protocol_member_declaration protocol_composition_type: $children+: unannotated_type protocol_declaration: @@ -459,11 +460,12 @@ named: shebang_line: simple_identifier: source_file: - $children*: [do_statement, expression, for_statement, global_declaration, guard_statement, repeat_while_statement, shebang_line, statement_label, throw_keyword, while_statement] + shebang?: shebang_line + statement*: [do_statement, expression, for_statement, global_declaration, guard_statement, repeat_while_statement, statement_label, throw_keyword, while_statement] special_literal: statement_label: statements: - $children+: [control_transfer_statement, do_statement, expression, for_statement, guard_statement, local_declaration, repeat_while_statement, statement_label, while_statement] + statement+: [control_transfer_statement, do_statement, expression, for_statement, guard_statement, local_declaration, repeat_while_statement, statement_label, while_statement] str_escaped_char: subscript_declaration: $children+: [attribute, computed_property, modifiers, parameter, type_constraints, type_parameters]