From 105ff6f50ce276d10fdd69550c18cc0a3267f442 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 25 Nov 2026 23:49:45 +0000 Subject: [PATCH] Keep the label on a Swift trailing closure. The grammar puts that label on the call as an identifier, so label: and action: scored 1.0. The spelling now stays, as a parenthesized argument label does. A closure body still drops its locals. Proved by test_a_trailing_closure_label_is_part_of_the_call. Co-authored-by: Robert C. Martin --- README.md | 2 +- src/dryer/astnorm.py | 12 ++++++++++-- tests/test_swift.py | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 26e7901..feedb47 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ DUPLICATE score=0.89 | Python | functions and methods | functions nested inside another function | | Swift | functions and methods with a body, computed properties, and file-level closures | initializers, subscripts, protocol requirements, and closures or functions nested inside another form | -A Swift computed property includes a SwiftUI `body`. A method on a struct, class, enum, or extension is a candidate, and so is a protocol method that has a default body. A protocol requirement does not. A closure written inside a function stays in that function. A closure bound with `let` or `var` at file scope is its own candidate, as a top-level arrow function is in TypeScript. Argument labels stay, because in Swift they are part of the call. `>=`, `!=`, `...`, and `..<` stay. Locals, field names, and literals, including `nil`, do not. An interpolated string keeps the interpolation. +A Swift computed property includes a SwiftUI `body`. A method on a struct, class, enum, or extension is a candidate, and so is a protocol method that has a default body. A protocol requirement does not. A closure written inside a function stays in that function. A closure bound with `let` or `var` at file scope is its own candidate, as a top-level arrow function is in TypeScript. Argument labels stay, including the label of a trailing closure, because in Swift they are part of the call. `>=`, `!=`, `...`, and `..<` stay. Locals, field names, and literals, including `nil`, do not. An interpolated string keeps the interpolation. Clojure reader conditionals keep the `:clj` branch, as dry4clj does. Syntax-quote is kept as a `syntax-quote` form rather than expanded. diff --git a/src/dryer/astnorm.py b/src/dryer/astnorm.py index d7b863f..50311df 100644 --- a/src/dryer/astnorm.py +++ b/src/dryer/astnorm.py @@ -139,7 +139,8 @@ _ATTRIBUTES = { } # The spelling is the structure: Swift spells >= as custom_operator, and an -# argument label is part of the call name. +# argument label is part of the call name. A trailing closure's label is an +# identifier placed directly on the call_suffix, not a value_argument_label. _TEXT_SYMBOLS = {"custom_operator", "value_argument_label"} _INTERPOLATED = {"interpolation", "interpolated_expression"} @@ -206,6 +207,13 @@ def _named_leaf(node, head: bool): return _PENDING +def _trailing_label(node) -> bool: + """A trailing-closure label sits on the call, so its spelling stays.""" + + parent = node.parent + return parent is not None and parent.type == "call_suffix" and node.type in _IDENTIFIERS + + def _try_leaf(node, mode: str, head: bool): if mode == "callee": return _callee_leaf(node) @@ -215,7 +223,7 @@ def _try_leaf(node, mode: str, head: bool): return None if node.type in _OPERATORS: return [K("symbol"), node.type] - if node.type in _TEXT_SYMBOLS: + if node.type in _TEXT_SYMBOLS or _trailing_label(node): return [K("symbol"), _text(node)] if not node.is_named: return None diff --git a/tests/test_swift.py b/tests/test_swift.py index 9dd633b..700a1ca 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -131,6 +131,43 @@ def test_a_swift_argument_label_is_part_of_the_call(tmp_path): ) < 1.0 +def test_a_trailing_closure_label_is_part_of_the_call(tmp_path): + labeled = """\ +func alpha() -> Int { + return Button { + save() + } label: { + Text(title) + } +} +""" + renamed = """\ +func beta() -> Int { + return Button { + save() + } label: { + Text(heading) + } +} +""" + relabeled = """\ +func beta() -> Int { + return Button { + save() + } action: { + Text(title) + } +} +""" + assert _scored(tmp_path, labeled, renamed) == 1.0 + assert _scored(tmp_path, labeled, relabeled) < 1.0 + assert _scored( + tmp_path, + "func alpha(xs: [Int]) -> [Int] {\n return xs.filter { item in\n odd(item)\n }\n}\n", + "func beta(items: [Int]) -> [Int] {\n return items.filter { value in\n odd(value)\n }\n}\n", + ) == 1.0 + + def test_swift_literals_normalize_and_interpolation_does_not(tmp_path): assert _scored( tmp_path,