diff --git a/src/codexray/report/templates.py b/src/codexray/report/templates.py index c8396ce8..68937951 100644 --- a/src/codexray/report/templates.py +++ b/src/codexray/report/templates.py @@ -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( diff --git a/web/src/screens/decisions/FixFirst.tsx b/web/src/screens/decisions/FixFirst.tsx index 653c3464..69d2f909 100644 --- a/web/src/screens/decisions/FixFirst.tsx +++ b/web/src/screens/decisions/FixFirst.tsx @@ -285,7 +285,7 @@ function FindingDetail({

) ) : ( -

No fix proposed.

+

No improvement proposed.

)}

diff --git a/web/src/screens/decisions/words.ts b/web/src/screens/decisions/words.ts index 183088d5..78034197 100644 --- a/web/src/screens/decisions/words.ts +++ b/web/src/screens/decisions/words.ts @@ -63,7 +63,7 @@ export const ACTION_LABEL: Record = { 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");