Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion loopx/semantics/field_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ def _token_pattern(field: str) -> re.Pattern[str]:
def lexical_module_count(field: str, suffix: str, sources: Iterable[SourceFile]) -> int:
"""The pre-B3 metric: modules whose text contains the standalone token."""
token = _token_pattern(field)
return sum(1 for source in sources if source.suffix == suffix and token.search(source.text))
# Literal containment is a prerequisite of the same boundary regex.
return sum(
1 for source in sources
if source.suffix == suffix and field in source.text and token.search(source.text)
)


def scan_field_uses(
Expand Down
15 changes: 15 additions & 0 deletions tests/architecture/test_semantic_field_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
FACTS,
ROLES,
field_use_summary,
lexical_module_count,
python_module_scan,
scan_field_uses,
)
Expand All @@ -25,6 +26,20 @@
FIELDS = frozenset({FIELD})


@pytest.mark.parametrize("suffix, expected", [(".py", 3), (".ts", 1)])
def test_lexical_modules_preserve_boundaries_and_raw_source_text(suffix: str, expected: int) -> None:
sources = [
SourceFile("comment.py", ".py", "# goal_boundary goal_boundary"),
SourceFile("prefix.py", ".py", "goal_boundary_repair repair_goal_boundary"),
SourceFile("unicode.py", ".py", "汉goal_boundary字"),
SourceFile("escaped.py", ".py", r'"goal_\u0062oundary"'),
SourceFile("broken.py", ".py", "def broken(:\n# goal_boundary"),
SourceFile("other.ts", ".ts", "// goal_boundary"),
SourceFile("absent.py", ".py", "payload.get(dynamic_key)"),
]
assert lexical_module_count(FIELD, suffix, iter(sources)) == expected


def python(text: str) -> dict[str, set[str]]:
return python_module_scan(ast.parse(text), FIELDS)[0]

Expand Down
Loading