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
46 changes: 28 additions & 18 deletions nominal_refactor_advisor/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""CLI and top-level analysis helpers.

This module contains the programmatic entrypoints used by tests and automation as
Expand Down Expand Up @@ -3725,29 +3725,39 @@
"projected_findings": json_report_object(report),
}

def _emit_application_blocked(
self,
synthesis_report: FindingRecipeSynthesisReport,
) -> int:
print(
json.dumps(
{
**self.unexecuted_payload(),
"application_blocked": True,
"application_block_reason": (
synthesis_report.application_block_reason
),
},
indent=2,
)
)
return 1

def run(self) -> int:
synthesis_report = self.finding_plan.report
if not synthesis_report.application_blocked:
if (
self.execution_request.mode.applies_changes
and synthesis_report.application_blocked
):
return self._emit_application_blocked(synthesis_report)
if self.plan_out is not None:
exportable = self.finding_plan.exportable_plan_document
if exportable is None:
return self._emit_application_blocked(synthesis_report)
write_cli_json_artifact(
self.plan_out,
json_report_object(self.finding_plan.document),
json_report_object(exportable),
)
if (
self.execution_request.mode.applies_changes or self.plan_out is not None
) and synthesis_report.application_blocked:
print(
json.dumps(
{
**self.unexecuted_payload(),
"application_blocked": True,
"application_block_reason": (
synthesis_report.application_block_reason
),
},
indent=2,
)
)
return 1
if not self.execution_request.mode.requested:
print(json.dumps(self.unexecuted_payload(), indent=2))
return 0
Expand Down
21 changes: 21 additions & 0 deletions nominal_refactor_advisor/codemod_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3433,6 +3433,27 @@ def application_blocked(self) -> bool:
def application_block_reason(self) -> str:
return self.report.application_block_reason

@property
def exportable_plan_document(self) -> CodemodPlanDocument | None:
"""The unique clean plan document safe to emit for `--codemod-plan-out`.

Returns the full document when unblocked; otherwise the single clean
trajectory frontier branch or the proved conflict-free candidate batch
when non-empty, leaving `needs_input`, `needs_operation`, and
conflicting sibling findings reported beside it.
"""

if not self.application_blocked:
return self.document
if (
len(self.trajectory_frontier.branches) == 1
and self.trajectory_frontier.branches[0].document.has_recipes
):
return self.trajectory_frontier.branches[0].document
if self.document.has_recipes:
return self.document
return None

def simulate(
self,
snapshot: CodemodSourceSnapshot,
Expand Down
67 changes: 67 additions & 0 deletions nominal_refactor_advisor/declared_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,46 @@ def member(
return [f"{attribute}={source}"], []


@dataclass(frozen=True)
class ForbiddenImportEdge:
"""One architectural layer boundary declared in ``[tool.nominal-refactor-advisor]``:
no module under ``source_prefix`` may load any module under ``target_prefix``
at import time, directly or transitively."""

source_prefix: str
target_prefix: str

def applies_to(self, module_name: str) -> bool:
return _module_matches_prefix(module_name, self.source_prefix)

def forbids(self, module_name: str) -> bool:
return _module_matches_prefix(module_name, self.target_prefix)

@classmethod
def from_table_entry(
cls, pyproject: Path, entry: object
) -> tuple[ForbiddenImportEdge, ...]:
match entry:
case {"from": str(source), "to": str(target)} if source and target:
return (cls(source, target),)
case {"from": str(source), "to": list(targets)} if source and targets:
parsed: list[ForbiddenImportEdge] = []
for target in targets:
if not isinstance(target, str) or not target:
raise ValueError(
f"{pyproject}: forbidden-imports target must be a non-empty string; got {target!r}"
)
parsed.append(cls(source, target))
return tuple(parsed)
raise ValueError(
f"{pyproject}: each forbidden-imports entry requires non-empty `from` and `to`; got {entry!r}"
)


def _module_matches_prefix(module_name: str, prefix: str) -> bool:
return module_name == prefix or module_name.startswith(f"{prefix}.")


@dataclass(frozen=True)
class DerivedSpelling:
"""A family whose members' spellings the repository's own function derives.
Expand Down Expand Up @@ -203,6 +243,33 @@ def keyword_binders_for(cls, path: Path) -> frozenset[str]:

return cls.declared_names_for(path, "keyword-binders")

@classmethod
def forbidden_imports_for(cls, path: Path) -> tuple[ForbiddenImportEdge, ...]:
"""The cross-layer import edges ``path``'s repository forbids at load time::

[tool.nominal-refactor-advisor]
forbidden-imports = [
{ from = "pkg.client", to = ["textual", "pkg.widgets"] },
]
"""

pyproject = cls.nearest_pyproject(path)
if pyproject is None:
return ()
table_data = (
_pyproject_table(pyproject)
if ScanCache.active() is not None
else tomllib.loads(pyproject.read_text())
)
match table_data:
case {"tool": {"nominal-refactor-advisor": {"forbidden-imports": list(entries)}}}:
return tuple(
edge
for entry in entries
for edge in ForbiddenImportEdge.from_table_entry(pyproject, entry)
)
return ()

@classmethod
def declared_names_for(cls, path: Path, key: str) -> frozenset[str]:
"""The qualified names listed under ``key`` in ``path``'s repository's table."""
Expand Down
Loading
Loading