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 <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-25 23:49:45 +00:00
co-authored by Robert C. Martin
parent 121fb2f4ba
commit 105ff6f50c
3 changed files with 48 additions and 3 deletions
+1 -1
View File
@@ -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.
+10 -2
View File
@@ -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
+37
View File
@@ -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,