From 95c17c42ab0fde2782d10caa53d6ff13fd3e8140 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 26 Nov 2026 00:02:40 +0000 Subject: [PATCH] Name a Swift module after the workspace when both bundles exist. Coverage already runs the .xcworkspace instead of the .xcodeproj beside it, but the module name came from the project. A file next to Proj.xcodeproj and Work.xcworkspace was Demo'd as Proj.ContentView. The namer now checks the workspace first, and that test expects Work.ContentView. A directory with only a project is unchanged. Co-authored-by: Robert C. Martin --- README.md | 2 +- src/crapper/languages/swift.py | 7 ++++--- tests/test_swift.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bb16874..2e082d3 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ Swift counts `if` (each `else if` is another `if`), `guard`, `for`, `while`, `re Entries are top-level functions, methods on classes, structs, enums, actors, extensions, and protocols, initializers, deinitializers, subscripts, computed properties, and property observers. A requirement or accessor with no statements, such as `{ get set }`, is not an entry. `get`, `set`, `willSet`, and `didSet` stay inside the property. A closure stored directly in a `let` or `var` is an entry. Any other closure stays inside the enclosing function, the same way a nested callback stays inside its function in TypeScript. A type declared inside a function keeps its own methods, and those methods are not also charged to the outer function. -The namespace is the module, or `Module.Type` for a member (`Module.Outer.Inner` for a nested type, `Module.Array` for an extension). The module is the SwiftPM target whose path contains the file, otherwise the directory under `Sources/`, otherwise the nearest `.xcodeproj` or `.xcworkspace` name, otherwise the dotted directory path with `src/` removed. uml-viewer groups on that namespace and joins `:name`. mutator uses the same function list, including each entry's byte span, so a nested closure's mutations belong to the enclosing function. +The namespace is the module, or `Module.Type` for a member (`Module.Outer.Inner` for a nested type, `Module.Array` for an extension). The module is the SwiftPM target whose path contains the file, otherwise the directory under `Sources/`, otherwise the nearest `.xcworkspace`, or an `.xcodeproj` when that directory has no workspace, otherwise the dotted directory path with `src/` removed. uml-viewer groups on that namespace and joins `:name`. mutator uses the same function list, including each entry's byte span, so a nested closure's mutations belong to the enclosing function. ## Coverage commands diff --git a/src/crapper/languages/swift.py b/src/crapper/languages/swift.py index adb1394..5d4fdcf 100644 --- a/src/crapper/languages/swift.py +++ b/src/crapper/languages/swift.py @@ -27,8 +27,9 @@ are their own entries and are not also charged to the enclosing function. The namespace is the module, or `Module.Type` for a member. The module is the SwiftPM target whose path contains the file, otherwise the directory -under `Sources/`, otherwise the nearest `.xcodeproj` or `.xcworkspace` -name, otherwise the dotted directory path with `src/` removed. The +under `Sources/`, otherwise the nearest `.xcworkspace`, or an +`.xcodeproj` when that directory has no workspace, otherwise the +dotted directory path with `src/` removed. The manifest is parsed, so a comment is not a target and a parenthesis inside a string does not end one. Nested types keep each owner (`Module.Outer.Inner`). An extension uses the extended type @@ -404,7 +405,7 @@ def _target_for(package_dir: Path, file: Path) -> str | None: def _project_name(directory: Path) -> str | None: - for pattern in ("*.xcodeproj", "*.xcworkspace"): + for pattern in ("*.xcworkspace", "*.xcodeproj"): matches = sorted(directory.glob(pattern)) if matches: return matches[0].stem diff --git a/tests/test_swift.py b/tests/test_swift.py index a035c1c..b72fc63 100644 --- a/tests/test_swift.py +++ b/tests/test_swift.py @@ -244,6 +244,17 @@ def test_package_and_xcode_names_are_the_module(tmp_path): assert [(fn.namespace, fn.name) for fn in ios] == [("IOS.ContentView", "body")] +def test_a_workspace_names_the_module_when_a_project_is_beside_it(tmp_path): + (tmp_path / "Proj.xcodeproj").mkdir() + (tmp_path / "Work.xcworkspace").mkdir() + functions = functions_in_source( + "struct ContentView { func body() {} }\n", + "App/ContentView.swift", + str(tmp_path), + ) + assert [(fn.namespace, fn.name) for fn in functions] == [("Work.ContentView", "body")] + + def test_a_commented_target_does_not_name_the_module(tmp_path): (tmp_path / "Package.swift").write_text( "\n".join(