lightsoutAlpha

reuse-common-code

Look for existing shared code before writing new code.

Agent checkAdvises by default

The rule

Agents read this text as written when they write and review code.

Before you write a helper, type or constant, look for one in every common/ folder in the package.

  • If one does what you need, import it. Never write a second copy.
  • If it sits in another module's common/, move it up as file-placement says, then import it.
  • If one nearly does, extend it rather than write a near copy, as long as its current callers keep working.

Two copies drift apart, and a fix then reaches only one of them.

Examples

The agent flags code like the incorrect example and accepts code like the correct one. Each example is a small repo, because this rule looks across files. It opens on the file that matters; the other files are the repo around it.

Incorrect

srccommonutils
src/getInvoiceLabel.ts
// Incorrect: writes its own copy of formatMoney when src/common/utils/ already
// has one. A fix to either copy now misses the other.
const formatMoney = ({ cents }: { cents: number }): string => `$${(cents / 100).toFixed(2)}`;
export const getInvoiceLabel = ({ cents }: { cents: number }): string => `Invoiced ${formatMoney({ cents })}`;

Correct

srccommonutils
src/getInvoiceLabel.ts
import { formatMoney } from './common/utils/formatMoney.ts';
// Correct: imports the formatMoney that src/common/utils/ already has.
export const getInvoiceLabel = ({ cents }: { cents: number }): string => `Invoiced ${formatMoney({ cents })}`;

Configure

  • Block"blocking"Stops a run when a file the run changed breaks the rule.
  • Advise"advisory"Reports it and hands it to the refactor agent. Never stops a run.
    Default
  • Off"off"Not checked. Use it when your own linter already enforces the rule.

Add this to your lightsout.config.json, then change the value.

lightsout.config.json
{
"standards-rule-settings": {
"reuse-common-code": "advisory"
}
}

Packs that select it