mirror of
https://github.com/unclebob/crapper.git
synced 2026-11-26 18:18:49 +00:00
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:
co-authored by
Robert C. Martin
parent
ccbf312645
commit
c69b49dea5
@@ -28,14 +28,15 @@ 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. Nested
|
||||
types keep each owner (`Module.Outer.Inner`). An extension uses the
|
||||
extended type (`Module.Box.Inner`, `Module.Array`).
|
||||
name, 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
|
||||
(`Module.Box.Inner`, `Module.Array`).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from crapper.languages.treesitter import (
|
||||
@@ -90,8 +91,7 @@ _FIXED_NAMES = {
|
||||
"deinit_declaration": "deinit",
|
||||
"subscript_declaration": "subscript",
|
||||
}
|
||||
_TARGET_MARKERS = (".target(", ".executableTarget(")
|
||||
_FIELD = re.compile(r'{name}:\s*"([^"]+)"')
|
||||
_TARGET_NAMES = {"target", "executableTarget"}
|
||||
|
||||
|
||||
def _is_decision(node) -> bool:
|
||||
@@ -284,52 +284,92 @@ def _directory_module(path: str, project_root: str | None) -> str:
|
||||
return Path(path).stem
|
||||
|
||||
|
||||
def _balanced(text: str, open_at: int) -> str:
|
||||
depth = 0
|
||||
for index in range(open_at, len(text)):
|
||||
depth += _paren_delta(text[index])
|
||||
if depth == 0:
|
||||
return text[open_at : index + 1]
|
||||
return ""
|
||||
|
||||
|
||||
def _paren_delta(char: str) -> int:
|
||||
if char == "(":
|
||||
return 1
|
||||
if char == ")":
|
||||
return -1
|
||||
return 0
|
||||
|
||||
|
||||
def _target_blocks(text: str) -> list[str]:
|
||||
blocks: list[str] = []
|
||||
for marker in _TARGET_MARKERS:
|
||||
start = 0
|
||||
while True:
|
||||
index = text.find(marker, start)
|
||||
if index < 0:
|
||||
break
|
||||
blocks.append(_balanced(text, index + len(marker) - 1))
|
||||
start = index + len(marker)
|
||||
return blocks
|
||||
|
||||
|
||||
def _field(block: str, name: str) -> str | None:
|
||||
match = _FIELD.pattern
|
||||
found = re.search(match.format(name=name), block)
|
||||
if found is None:
|
||||
def _string_parts(data: bytes, node, quote: str, text_type: str) -> str | None:
|
||||
parts = []
|
||||
for child in node.children:
|
||||
if child.type == quote:
|
||||
continue
|
||||
if child.type != text_type:
|
||||
return None
|
||||
return found.group(1)
|
||||
parts.append(node_text(data, child))
|
||||
return "".join(parts)
|
||||
|
||||
|
||||
def _plain_string(data: bytes, node) -> str | None:
|
||||
if node.type == "line_string_literal":
|
||||
return _string_parts(data, node, '"', "line_str_text")
|
||||
if node.type == "multi_line_string_literal":
|
||||
text = _string_parts(data, node, '"""', "multi_line_str_text")
|
||||
if text is None:
|
||||
return None
|
||||
return text.strip()
|
||||
return None
|
||||
|
||||
|
||||
def _suffix_name(data: bytes, node) -> str | None:
|
||||
suffix = None
|
||||
for child in node.children:
|
||||
if child.type == "navigation_suffix":
|
||||
suffix = child
|
||||
if suffix is None:
|
||||
return None
|
||||
return child_text(data, suffix, "simple_identifier")
|
||||
|
||||
|
||||
def _call_name(data: bytes, node) -> str | None:
|
||||
"""The called name. A later `.target` in a fragment is a navigation suffix."""
|
||||
|
||||
callee = child_of_type(node, "prefix_expression", "navigation_expression")
|
||||
if callee is None:
|
||||
return None
|
||||
if callee.type == "prefix_expression":
|
||||
return child_text(data, callee, "simple_identifier")
|
||||
return _suffix_name(data, callee)
|
||||
|
||||
|
||||
def _argument_label(data: bytes, argument) -> str | None:
|
||||
marker = child_of_type(argument, "value_argument_label")
|
||||
if marker is None:
|
||||
return None
|
||||
return child_text(data, marker, "simple_identifier")
|
||||
|
||||
|
||||
def _labeled_string(data: bytes, call, label: str) -> str | None:
|
||||
suffix = child_of_type(call, "call_suffix")
|
||||
if suffix is None:
|
||||
return None
|
||||
arguments = child_of_type(suffix, "value_arguments")
|
||||
if arguments is None:
|
||||
return None
|
||||
for argument in arguments.children:
|
||||
if argument.type != "value_argument" or _argument_label(data, argument) != label:
|
||||
continue
|
||||
for child in argument.children:
|
||||
text = _plain_string(data, child)
|
||||
if text is not None:
|
||||
return text
|
||||
return None
|
||||
|
||||
|
||||
def _target_calls(data: bytes, root) -> list:
|
||||
calls = []
|
||||
for node in descendants(root):
|
||||
if node.type != "call_expression" or _call_name(data, node) not in _TARGET_NAMES:
|
||||
continue
|
||||
calls.append(node)
|
||||
# A fragment chains later calls inside the first, so the outer node starts
|
||||
# at the same byte and ends later. End order is source order.
|
||||
calls.sort(key=lambda node: (node.end_byte, node.start_byte))
|
||||
return calls
|
||||
|
||||
|
||||
def _package_targets(text: str) -> list[tuple[str, str | None]]:
|
||||
data, tree = parse(text, "swift")
|
||||
found = []
|
||||
for block in _target_blocks(text):
|
||||
if not block.endswith(")"):
|
||||
continue
|
||||
name = _field(block, "name")
|
||||
for node in _target_calls(data, tree.root_node):
|
||||
name = _labeled_string(data, node, "name")
|
||||
if name:
|
||||
found.append((name, _field(block, "path")))
|
||||
found.append((name, _labeled_string(data, node, "path")))
|
||||
return found
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user