Read SwiftPM targets from the Swift grammar.

A commented `.target` was a real module, so `// .target(name: "Stale"...)`
renamed Sources that the live target owns. Parentheses inside a setting
string could also end the target early. The manifest is parsed instead,
and a fragment's later calls are navigation suffixes ordered by their
end byte. The new test failed with namespace Stale, then passed for a
comment, a test target, an executable target, a string containing `)`,
and a multiline path.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-26 00:02:28 +00:00
co-authored by Robert C. Martin
parent ccbf312645
commit c69b49dea5
2 changed files with 109 additions and 42 deletions
+27
View File
@@ -244,6 +244,33 @@ 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_commented_target_does_not_name_the_module(tmp_path):
(tmp_path / "Package.swift").write_text(
"\n".join(
[
'// .target(name: "Stale", path: "Sources/Demo"),',
'/* .target(name: "Blocked", path: "Sources/Demo") */',
'.testTarget(name: "DemoTests", path: "lib/demo"),',
'.target(name: "Demo", path: "lib/demo"),',
'.executableTarget(name: "Tool", path: "lib/tool"),',
'.target(name: "Weird", path: "lib/odd", cSettings: [.define("FOO", to: "a)b")]),',
'.target(name: "Wide", path: """',
"lib/wide",
'"""),',
]
),
encoding="utf-8",
)
demo = functions_in_source("func open() {}\n", "lib/demo/Box.swift", str(tmp_path))
assert demo[0].namespace == "Demo"
tool = functions_in_source("func main() {}\n", "lib/tool/Main.swift", str(tmp_path))
assert tool[0].namespace == "Tool"
weird = functions_in_source("func open() {}\n", "lib/odd/Box.swift", str(tmp_path))
assert weird[0].namespace == "Weird"
wide = functions_in_source("func open() {}\n", "lib/wide/Box.swift", str(tmp_path))
assert wide[0].namespace == "Wide"
def test_swift_tests_and_build_products_are_not_scored(tmp_path):
source = tmp_path / "Sources" / "Demo" / "Box.swift"
source.parent.mkdir(parents=True)