diff --git a/README.md b/README.md index feedb47..5ee1f50 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, 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. +A Swift computed property includes a SwiftUI `body`. A method on a struct, class, actor, enum, or extension is a candidate, and so is a deinit and 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/cli.py b/src/dryer/cli.py index c98438b..df725ce 100644 --- a/src/dryer/cli.py +++ b/src/dryer/cli.py @@ -60,7 +60,7 @@ named {_skipped_directory_text()} are skipped, as are *_test.go, *.test.cts, Clojure compares every top-level form except ns. The other languages compare functions and methods. Swift also compares computed properties, including a -SwiftUI view body, and a closure bound at file scope. A form is only compared +SwiftUI view body, a closure bound at file scope, and deinit. A form is only compared with forms in the same language. Exit codes: diff --git a/src/dryer/extract.py b/src/dryer/extract.py index 0357638..7a00e06 100644 --- a/src/dryer/extract.py +++ b/src/dryer/extract.py @@ -4,7 +4,7 @@ Clojure compares every top-level list except `ns`, matching dry4clj. The other languages compare the functions and methods crapper scores: bodies, not signatures alone, and not callbacks nested inside another function. Rust functions inside `mod tests` are test code and are skipped. Swift also -compares computed properties and a closure bound at file scope. +compares computed properties, a closure bound at file scope, and deinit. """ from __future__ import annotations @@ -131,8 +131,9 @@ def _typescript_nodes(data: bytes, root) -> list: return found -# A closure, a local function, and a function written inside a view body stay -# inside the form that contains them. A protocol requirement has no body. +# A closure, a local function, and a function written inside a view body or a +# deinit stay inside the form that contains them. A protocol requirement has +# no body. deinit itself is a method with a body. _SWIFT_NEST = { "function_declaration", "protocol_function_declaration", @@ -140,7 +141,9 @@ _SWIFT_NEST = { "computed_property", "init_declaration", "subscript_declaration", + "deinit_declaration", } +_SWIFT_FUNCTION = {"function_declaration", "deinit_declaration"} def _swift_property(node) -> bool: @@ -152,7 +155,7 @@ def _swift_nodes(data: bytes, root) -> list: for node in descendants(root): if _inside(node, _SWIFT_NEST): continue - if node.type == "function_declaration": + if node.type in _SWIFT_FUNCTION: found.append(node) elif node.type == "protocol_function_declaration" and _has(node, "statements"): found.append(node) diff --git a/tests/test_swift.py b/tests/test_swift.py index 700a1ca..cbbfe6a 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -372,6 +372,36 @@ def test_a_swift_package_skips_its_tests(tmp_path): assert relative == ["Sources/App.swift"] +def test_a_deinit_is_compared_and_keeps_its_nested_function(tmp_path): + source = """\ +class Panel { + deinit { + func hidden() -> Int { + return cache() + } + close(handle) + } + + func stop() { + close(handle) + } +} +""" + entries, warning = entries_in_source("swift", source, "Panel.swift", "Panel.swift") + assert warning is None + assert [(entry.start_line, entry.end_line) for entry in entries] == [(2, 7), (9, 11)] + assert _scored( + tmp_path, + "class A {\n deinit {\n close(handle)\n release(buffer)\n }\n}\n", + "class B {\n deinit {\n close(other)\n release(kept)\n }\n}\n", + ) == 1.0 + assert _scored( + tmp_path, + "class A {\n deinit {\n close(handle)\n }\n}\n", + "class B {\n deinit {\n shutdown(other)\n }\n}\n", + ) < 1.0 + + def test_a_top_level_swift_closure_matches_its_renamed_copy(tmp_path): left = """\ let alpha = { (xs: [Int]) -> [Int] in