fun-refactor

Design

Inspect the code behind a change.

Renaming the wrong function can leave code that compiles but behaves differently. For example, two objects can have methods named send. Inspect which declaration each use refers to, review the proposed diff and run behavior checks.

The goal is to help people and coding agents complete checked investigations, refactorings, behavior changes, translations and proofs. Give the user relevant source, a precise preview and useful check results. Measure complete outcomes and costs within stated limits.

For example, a rename must preserve the behavior its callers use. A bug repair must change the failing behavior. A Lean proof must keep the requested theorem and state its source assumptions. See the three task routes.

Choose tools for the question

Find-and-replace covers every language and parses none. It renames the string in a comment, a substring inside another identifier, and the same-named field on an unrelated type.

A language server can supply compiler and project knowledge for its supported languages. Its support for configuration files and unfinished code depends on the server. Keep those tools available when their checks answer your question.

This tool

It builds one index across nineteen languages out of syntax alone. A named rule resolves each reference: the scope chain, an import, the enclosing package. Every answer carries which rule answered it.

It rewrites the references it resolved exactly or through an import, and lists the rest: weakly resolved sites, names appearing in strings, files that did not parse. It prints the diff, then the list.

fr capabilities prints the live language support matrix.

Historical defects found on real repositories

Each of these lived in this tool, produced output that parsed, and turned up when somebody ran it over an existing codebase instead of a fixture.

A guard clause that moved code out from under its guard

Go's grammar puts a statement_list between a block and its statements. Counting it as a statement made every block look like a block of one. The check "is this if the last thing here?" then passed for an if with code after it, and the rewrite hoisted that code out from under the condition guarding it.

The result parses. No syntax check catches it. Over 250 files of helm's pkg/services, 1,258 of 1,498 applications were wrong, or 84%. The fix refuses those and applies the remaining 240.

A de Morgan rewrite that dropped its own brackets

!(a && b) is one operand; !a || !b is two, and the brackets that held it together left with the negation. Inside another operator, x && !(a && b) became x && !a || !b: valid, and a different program.

References that were never found at all

A Go package is a directory: code in one file calls a function in the next with no import and no qualifier. The index treated only Terraform that way, so fr refs returned zero for symbols helm calls from the file next door. Dead-code analysis reported 238 internal Go symbols as dead where 47 are. A rename rewrote the definition and filed the call sites it could not see under "unresolved".

That last one reported the gap instead of rewriting wrongly. It was still a bug, and it is fixed.

Where it fits

Keep rust-analyzer and gopls. They read types; this reads syntax, and syntax stops where a receiver's type would tell you which method you meant. At that point the tool says field-based and leaves the line alone.

Use fr for these supported tasks, then check the reported limits:

  • Inspect recognized declarations across supported source formats; omitted files remain explicit.
  • Find a Helm values key and the supported template uses that a rename would change.
  • Inspect readable syntax even when a project does not currently compile.
  • An explicit account of what it did not do, every time.