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
47 changes: 46 additions & 1 deletion src/codexray/report/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,53 @@ def _summary(reader: _Reader) -> str:
return sentence(reader)


def _shaky_foundation_fix(r: _Reader) -> str:
file = r.name("file")
return (
f"Separate the part of `{file}` that changes often from the part that stays, so its "
f"{r.fact('direct_users')} direct users depend only on the part that stays."
)


def _hidden_partners_fix(r: _Reader) -> str:
partners = r.names("partner")
if len(partners) != 2:
raise r._missing("exactly two targets with role 'partner'")
a, b = partners
return (
f"Move what `{a}` and `{b}` share into one place that both use. If they must stay "
"apart, a note in each that names the other makes the pairing visible."
)


def _large_complex_changed_fix(r: _Reader) -> str:
top = r.raw("top_functions")
if not isinstance(top, str) or not top.strip():
raise r._missing("a non-empty 'top_functions'")
return (
f"Split {top} into smaller functions. No other function in `{r.name('file')}` has "
"more branches."
)


# Fix sentences built from the finding's facts and targets alone; nothing is simulated.
FIX_SENTENCES: dict[str, tuple[str, Callable[[_Reader], str]]] = {
"shaky_foundation": ("split_by_responsibility", _shaky_foundation_fix),
"hidden_partners": ("extract_interface", _hidden_partners_fix),
"large_complex_changed": ("split_by_responsibility", _large_complex_changed_fix),
}


def _fix(reader: _Reader) -> Fix:
if reader.finding.type != "cheap_loop":
kind = reader.finding.type
if kind in FIX_SENTENCES:
action, sentence = FIX_SENTENCES[kind]
try:
text = sentence(reader)
except ValueError: # a value the sentence needs is missing: propose nothing
return Fix(text="", action="none")
return Fix(text=text, action=action)
if kind != "cheap_loop":
return Fix(text="", action="none")
symbol = str(reader.raw("symbol"))
return Fix(
Expand Down
2 changes: 1 addition & 1 deletion web/src/screens/decisions/FixFirst.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function FindingDetail({
</p>
)
) : (
<p className="muted">No fix proposed.</p>
<p className="muted">No improvement proposed.</p>
)}
<p className="detail__sim">
<Badge origin="computed" />
Expand Down
2 changes: 1 addition & 1 deletion web/src/screens/decisions/words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const ACTION_LABEL: Record<Fix["action"], string> = {
invert_dependency: "Invert the dependency",
extract_interface: "Extract an interface",
split_by_responsibility: "Split by responsibility",
none: "No fix proposed",
none: "No improvement proposed",
};

export const num = (value: number) => value.toLocaleString("en-US");
Expand Down