Document Swift as a supported language.

The scan already reports Swift pairs in the text report and in
.metrics/dry.edn. The help text, the language table, and the project
description still listed the previous six languages.

Proved: test_help_does_not_scan failed until the help named
Swift (.swift) and *Tests.swift, then passed. test_swift_report_writes_the_same_snapshot
checks the text lines and :language \"swift\" in dry.edn. The shared
language tests now include Swift. 92 tests passed.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-25 23:35:40 +00:00
co-authored by Robert C. Martin
parent f667ad03ba
commit 1bb1936ad5
9 changed files with 78 additions and 17 deletions
+12 -2
View File
@@ -1,8 +1,8 @@
# dryer # dryer
dryer finds candidate duplicate code in Clojure, Java, Go, TypeScript, Rust, and Python. One run detects the language of each source file, normalizes that language's forms, and reports pairs whose structure is close enough to review. dryer finds candidate duplicate code in Clojure, Java, Go, TypeScript, Rust, Python, and Swift. One run detects the language of each source file, normalizes that language's forms, and reports pairs whose structure is close enough to review.
Clojure follows [dry4clj](https://github.com/unclebob/dry4clj). The other languages use the same idea on the functions and methods [crapper](https://github.com/unclebob/crapper) and [mutator](https://github.com/unclebob/mutator) already treat as one unit. A form is only compared with forms written in the same language. Clojure follows [dry4clj](https://github.com/unclebob/dry4clj). Java, Go, TypeScript, Rust, and Python use the same idea on the functions and methods [crapper](https://github.com/unclebob/crapper) and [mutator](https://github.com/unclebob/mutator) already treat as one unit. Swift uses that same unit. A form is only compared with forms written in the same language.
The score is Jaccard similarity of structural fingerprints: The score is Jaccard similarity of structural fingerprints:
@@ -39,6 +39,13 @@ def beta(items):
return map(kept, dec) return map(kept, dec)
``` ```
```swift
func alpha(xs: [Int]) -> [Int] {
let ys = filter(xs, odd)
return map(ys, inc)
}
```
What stays is the call or method name, the operator, and the shape of the tree. What drops out is the local name, the field name, the literal, and the keyword. A Clojure keyword call `(:kind m)` and `(:id row)` is the same shape. So is `order.total` and `row.amount`. What stays is the call or method name, the operator, and the shape of the tree. What drops out is the local name, the field name, the literal, and the keyword. A Clojure keyword call `(:kind m)` and `(:id row)` is the same shape. So is `order.total` and `row.amount`.
## Run ## Run
@@ -101,6 +108,9 @@ DUPLICATE score=0.89
| TypeScript | functions, methods, and top-level arrow functions | callbacks nested inside another function | | TypeScript | functions, methods, and top-level arrow functions | callbacks nested inside another function |
| Rust | functions and methods with a body | functions inside `mod tests`, and closures | | Rust | functions and methods with a body | functions inside `mod tests`, and closures |
| Python | functions and methods | functions nested inside another function | | 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.
Clojure reader conditionals keep the `:clj` branch, as dry4clj does. Syntax-quote is kept as a `syntax-quote` form rather than expanded. Clojure reader conditionals keep the `:clj` branch, as dry4clj does. Syntax-quote is kept as a `syntax-quote` form rather than expanded.
+1 -1
View File
@@ -1,7 +1,7 @@
[project] [project]
name = "dryer" name = "dryer"
version = "0.1.0" version = "0.1.0"
description = "Structural duplicate finder for Clojure, Java, Go, TypeScript, Rust, and Python." description = "Structural duplicate finder for Clojure, Java, Go, TypeScript, Rust, Python, and Swift."
readme = "README.md" readme = "README.md"
license = { file = "LICENSE" } license = { file = "LICENSE" }
classifiers = ["License :: OSI Approved :: Zero-Clause BSD (0BSD)"] classifiers = ["License :: OSI Approved :: Zero-Clause BSD (0BSD)"]
+1 -1
View File
@@ -1,3 +1,3 @@
"""Find structural duplicates in Clojure, Java, Go, TypeScript, Rust, and Python.""" """Find structural duplicates in Clojure, Java, Go, TypeScript, Rust, Python, and Swift."""
__version__ = "0.1.0" __version__ = "0.1.0"
+10 -8
View File
@@ -21,16 +21,16 @@ def _skipped_directory_text() -> str:
HELP = f"""\ HELP = f"""\
Usage: dryer [options] [path-or-filter ...] Usage: dryer [options] [path-or-filter ...]
Find candidate duplicate code in Clojure, Java, Go, TypeScript, Rust, and Find candidate duplicate code in Clojure, Java, Go, TypeScript, Rust,
Python. Each form is normalized — call and method names, operators, and Python, and Swift. Each form is normalized — call and method names, operators,
tree shape stay; local names, field names, and literals do not — and compared and tree shape stay; local names, field names, and literals do not — and
with the other forms of the same language. The score is Jaccard similarity of compared with the other forms of the same language. The score is Jaccard
those structural fingerprints, the same score as dry4clj. similarity of those structural fingerprints, the same score as dry4clj.
Prints a report and writes .metrics/dry.edn. Prints a report and writes .metrics/dry.edn.
Languages: Clojure (.clj .cljc .cljs .cljd .bb), Java (.java), Go (.go), Languages: Clojure (.clj .cljc .cljs .cljd .bb), Java (.java), Go (.go),
TypeScript (.ts .tsx .mts .cts), Rust (.rs), Python (.py). TypeScript (.ts .tsx .mts .cts), Rust (.rs), Python (.py), Swift (.swift).
Options: Options:
-h, --help Print this help and exit. -h, --help Print this help and exit.
@@ -56,10 +56,12 @@ Arguments:
With no paths, source files under the project root are compared. Directories With no paths, source files under the project root are compared. Directories
named {_skipped_directory_text()} are skipped, as are *_test.go, *.test.cts, named {_skipped_directory_text()} are skipped, as are *_test.go, *.test.cts,
*.spec.cts, *.spec.ts, test_*.py, and *_test.py files. *.spec.cts, *.spec.ts, test_*.py, *_test.py, and *Tests.swift files.
Clojure compares every top-level form except ns. The other languages compare Clojure compares every top-level form except ns. The other languages compare
functions and methods. A form is only compared with forms in the same language. 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
with forms in the same language.
Exit codes: Exit codes:
0 the report was written 0 the report was written
+1 -1
View File
@@ -1,7 +1,7 @@
"""Find source files and decide which language each one is written in. """Find source files and decide which language each one is written in.
The extensions and the skipped directories match crapper and mutator, with The extensions and the skipped directories match crapper and mutator, with
`.cljd` added because dry4clj reads it. `.cljd` added because dry4clj reads it and `.swift` added for Swift.
""" """
import os import os
+2 -1
View File
@@ -3,7 +3,8 @@
Clojure compares every top-level list except `ns`, matching dry4clj. The other Clojure compares every top-level list except `ns`, matching dry4clj. The other
languages compare the functions and methods crapper scores: bodies, not languages compare the functions and methods crapper scores: bodies, not
signatures alone, and not callbacks nested inside another function. Rust signatures alone, and not callbacks nested inside another function. Rust
functions inside `mod tests` are test code and are skipped. functions inside `mod tests` are test code and are skipped. Swift also
compares computed properties and a closure bound at file scope.
""" """
from __future__ import annotations from __future__ import annotations
+2
View File
@@ -40,6 +40,8 @@ def test_help_does_not_scan(capsys):
assert "Usage: dryer" in out assert "Usage: dryer" in out
assert "__pycache__" in out assert "__pycache__" in out
assert ".test.cts" in out assert ".test.cts" in out
assert "Swift (.swift)" in out
assert "*Tests.swift" in out
assert "--threshold" in out assert "--threshold" in out
+34 -3
View File
@@ -126,6 +126,18 @@ def test_each_language_matches_renamed_locals(tmp_path):
"right.tsx": TSX_RIGHT, "right.tsx": TSX_RIGHT,
"left.rs": RUST_LEFT, "left.rs": RUST_LEFT,
"right.rs": RUST_RIGHT, "right.rs": RUST_RIGHT,
"left.swift": """\
func alpha(xs: [Int]) -> [Int] {
let ys = filter(xs, odd)
return map(ys, inc)
}
""",
"right.swift": """\
func beta(items: [Int]) -> [Int] {
let kept = filter(items, even)
return map(kept, dec)
}
""",
} }
for name, source in samples.items(): for name, source in samples.items():
write_source(tmp_path, name, source) write_source(tmp_path, name, source)
@@ -133,7 +145,7 @@ def test_each_language_matches_renamed_locals(tmp_path):
by_language = {} by_language = {}
for item in found: for item in found:
by_language.setdefault(item.language, []).append(item) by_language.setdefault(item.language, []).append(item)
assert set(by_language) == {"java", "go", "python", "typescript", "rust"} assert set(by_language) == {"java", "go", "python", "typescript", "rust", "swift"}
assert [(item.left.file, item.right.file) for item in by_language["typescript"]] == [ assert [(item.left.file, item.right.file) for item in by_language["typescript"]] == [
("left.ts", "right.ts"), ("left.ts", "right.ts"),
("left.tsx", "right.tsx"), ("left.tsx", "right.tsx"),
@@ -237,7 +249,14 @@ class Right {
def _suffix(language: str) -> str: def _suffix(language: str) -> str:
return {"python": "py", "go": "go", "rust": "rs", "java": "java", "typescript": "ts"}[language] return {
"python": "py",
"go": "go",
"rust": "rs",
"java": "java",
"typescript": "ts",
"swift": "swift",
}[language]
def test_callee_names_stay_and_member_names_do_not(tmp_path): def test_callee_names_stay_and_member_names_do_not(tmp_path):
@@ -307,6 +326,18 @@ export function alpha(xs: number[]): number {
export function beta(items: number[]): number { export function beta(items: number[]): number {
return foo<number>(items.baz); return foo<number>(items.baz);
} }
""",
"left.swift": """\
func alpha(xs: Order) -> Int {
let ys = xs.filter(odd)
return xs.total
}
""",
"right.swift": """\
func beta(items: Order) -> Int {
let kept = items.filter(even)
return items.count
}
""", """,
} }
for name, source in samples.items(): for name, source in samples.items():
@@ -315,7 +346,7 @@ export function beta(items: number[]): number {
by_language = {} by_language = {}
for item in found: for item in found:
by_language.setdefault(item.language, []).append(item) by_language.setdefault(item.language, []).append(item)
assert set(by_language) == {"python", "go", "rust", "java", "typescript"} assert set(by_language) == {"python", "go", "rust", "java", "typescript", "swift"}
for language, group in by_language.items(): for language, group in by_language.items():
assert [(item.left.file, item.right.file, item.score) for item in group] == [ assert [(item.left.file, item.right.file, item.score) for item in group] == [
(f"left.{_suffix(language)}", f"right.{_suffix(language)}", 1.0) (f"left.{_suffix(language)}", f"right.{_suffix(language)}", 1.0)
+15
View File
@@ -1,5 +1,6 @@
from pathlib import Path from pathlib import Path
from dryer.cli import run
from dryer.discover import iter_source_files, language_of from dryer.discover import iter_source_files, language_of
from dryer.extract import entries_in_source from dryer.extract import entries_in_source
from dryer.scan import find_duplicates, scan_files from dryer.scan import find_duplicates, scan_files
@@ -297,6 +298,20 @@ def test_swiftui_views_match_when_only_the_data_changes(tmp_path):
assert _scored(tmp_path, RECEIPT, LABEL_VIEW) < 1.0 assert _scored(tmp_path, RECEIPT, LABEL_VIEW) < 1.0
def test_swift_report_writes_the_same_snapshot(tmp_path, capsys):
write_source(tmp_path, "left.swift", SWIFT_LEFT)
write_source(tmp_path, "right.swift", SWIFT_RIGHT)
assert run(["--root", str(tmp_path)]) == 0
captured = capsys.readouterr()
assert "DUPLICATE score=1.00" in captured.out
assert "left.swift:1-4" in captured.out
assert "right.swift:1-4" in captured.out
text = (tmp_path / ".metrics" / "dry.edn").read_text(encoding="utf-8")
assert ':language "swift"' in text
assert ':file "left.swift"' in text
assert "Wrote" in captured.err
def test_a_swift_package_skips_its_tests(tmp_path): def test_a_swift_package_skips_its_tests(tmp_path):
body = "func alpha() -> Int {\n return 1\n}\n" body = "func alpha() -> Int {\n return 1\n}\n"
write_source(tmp_path, "Sources/App.swift", body) write_source(tmp_path, "Sources/App.swift", body)