From f9c1279041bed5435f3acb495d94d46b2ad82fd3 Mon Sep 17 00:00:00 2001 From: Taus Date: Thu, 23 Jul 2026 13:03:01 +0000 Subject: [PATCH] unified: Emit constructor parameters Emit an initializer's parameters on the `constructor_declaration`, captured from the `initializerDecl` signature (as for `functionDecl`). The tree-sitter path dropped them -- its positional `(parameter)*` capture missed the field-attached parameters -- and the mapping matched that for corpus parity; swift-syntax exposes them cleanly, so emitting them is a correctness improvement. Adds a focused `constructor-with-parameters` corpus case and updates `class-with-initializer` to witness the restored parameters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../extractor/src/languages/swift/swift.rs | 10 +-- .../swift/types/class-with-initializer.output | 5 ++ .../types/constructor-with-parameters.output | 82 +++++++++++++++++++ .../types/constructor-with-parameters.swift | 3 + 4 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 unified/extractor/tests/corpus/swift/types/constructor-with-parameters.output create mode 100644 unified/extractor/tests/corpus/swift/types/constructor-with-parameters.swift diff --git a/unified/extractor/src/languages/swift/swift.rs b/unified/extractor/src/languages/swift/swift.rs index ef3672e1b91..6066e411cec 100644 --- a/unified/extractor/src/languages/swift/swift.rs +++ b/unified/extractor/src/languages/swift/swift.rs @@ -1089,18 +1089,18 @@ fn translation_rules() -> Vec> { // A member of a type declaration unwraps to the contained declaration. rule!((memberBlockItem decl: _* @d) => member* { d }), // Init declaration → constructor_declaration. Body statements optional; - // body itself is also optional (protocol requirement). - // - // PARITY(tree-sitter): the parameters are not emitted, because the - // tree-sitter path dropped them (its `(parameter)*` capture missed the - // field-attached parameters). Emitting them is a future improvement. + // body itself is also optional (protocol requirement). The parameters + // nest under `signature` (as for `functionDecl`). rule!( (initializerDecl modifiers: _* @mods + signature: (functionSignature + parameterClause: (functionParameterClause parameters: _* @params)) body: (codeBlock statements: _* @body_stmts)?) => (constructor_declaration modifier: {mods} + parameter: {params} body: (block stmt: {body_stmts})) ), // Deinit declaration → destructor_declaration. Body statements optional. diff --git a/unified/extractor/tests/corpus/swift/types/class-with-initializer.output b/unified/extractor/tests/corpus/swift/types/class-with-initializer.output index 5e712d739e5..32f94417d66 100644 --- a/unified/extractor/tests/corpus/swift/types/class-with-initializer.output +++ b/unified/extractor/tests/corpus/swift/types/class-with-initializer.output @@ -103,6 +103,11 @@ top_level named_type_expr name: identifier "Int" constructor_declaration + parameter: + parameter + pattern: + name_pattern + identifier: identifier "x" body: block stmt: diff --git a/unified/extractor/tests/corpus/swift/types/constructor-with-parameters.output b/unified/extractor/tests/corpus/swift/types/constructor-with-parameters.output new file mode 100644 index 00000000000..c26d04b6ce0 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/types/constructor-with-parameters.output @@ -0,0 +1,82 @@ +struct Size { + init(width w: Int, height h: Int) {} +} + +--- + +sourceFile + endOfFileToken: endOfFile + statements: + codeBlockItem + item: + structDecl + attributes: + name: identifier "Size" + memberBlock: + memberBlock + leftBrace: { + rightBrace: } + members: + memberBlockItem + decl: + initializerDecl + attributes: + body: + codeBlock + leftBrace: { + rightBrace: } + statements: + modifiers: + signature: + functionSignature + parameterClause: + functionParameterClause + leftParen: ( + rightParen: ) + parameters: + functionParameter + colon: : + attributes: + modifiers: + trailingComma: , + type: + identifierType + name: identifier "Int" + firstName: identifier "width" + secondName: identifier "w" + functionParameter + colon: : + attributes: + modifiers: + type: + identifierType + name: identifier "Int" + firstName: identifier "height" + secondName: identifier "h" + initKeyword: init + modifiers: + structKeyword: struct + +--- + +top_level + body: + block + stmt: + class_like_declaration + modifier: modifier "struct" + name: identifier "Size" + member: + constructor_declaration + parameter: + parameter + external_name: identifier "width" + pattern: + name_pattern + identifier: identifier "w" + parameter + external_name: identifier "height" + pattern: + name_pattern + identifier: identifier "h" + body: block "init(width w: Int, height h: Int) {}" diff --git a/unified/extractor/tests/corpus/swift/types/constructor-with-parameters.swift b/unified/extractor/tests/corpus/swift/types/constructor-with-parameters.swift new file mode 100644 index 00000000000..4f6377fc780 --- /dev/null +++ b/unified/extractor/tests/corpus/swift/types/constructor-with-parameters.swift @@ -0,0 +1,3 @@ +struct Size { + init(width w: Int, height h: Int) {} +}