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 <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-26 00:02:40 +00:00
co-authored by Robert C. Martin
parent c69b49dea5
commit 95c17c42ab
3 changed files with 16 additions and 4 deletions
+1 -1
View File
@@ -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. 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 ## Coverage commands
+4 -3
View File
@@ -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 namespace is the module, or `Module.Type` for a member. The module is
the SwiftPM target whose path contains the file, otherwise the directory the SwiftPM target whose path contains the file, otherwise the directory
under `Sources/`, otherwise the nearest `.xcodeproj` or `.xcworkspace` under `Sources/`, otherwise the nearest `.xcworkspace`, or an
name, otherwise the dotted directory path with `src/` removed. The `.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 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 inside a string does not end one. Nested types keep each owner
(`Module.Outer.Inner`). An extension uses the extended type (`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: def _project_name(directory: Path) -> str | None:
for pattern in ("*.xcodeproj", "*.xcworkspace"): for pattern in ("*.xcworkspace", "*.xcodeproj"):
matches = sorted(directory.glob(pattern)) matches = sorted(directory.glob(pattern))
if matches: if matches:
return matches[0].stem return matches[0].stem
+11
View File
@@ -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")] 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): def test_a_commented_target_does_not_name_the_module(tmp_path):
(tmp_path / "Package.swift").write_text( (tmp_path / "Package.swift").write_text(
"\n".join( "\n".join(