lightsoutAlpha

react-function-size

How long a function, a hook and a component may grow.

Deterministic checkAdvises by default

The rule

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

Split a function longer than 80 lines. A hook, a function whose name starts with use, may run to 160 lines, and a component, a capitalised function in a .tsx file, may run to 200. Lines run from the signature to the closing brace, and a callback with no name counts toward the function that holds it.

Extract a piece because it deserves a name, not to win back lines.

Examples

The check flags the incorrect code and passes the correct code.

Incorrect

src/reporting/buildReportSummary.ts
interface Row {
region: string;
product: string;
currency: string;
amount: number;
refunded: boolean;
}
interface Params {
rows: Row[];
rates: Record<string, number>;
}
// Every step of the summary inlined into one body — not a linear flow of step
// calls, just a long function, which is the shape the table calls "needs
// splitting".
export const buildReportSummary = ({ rows, rates }: Params): Record<string, number> => {
const regionTotals: Record<string, number> = {};
const regionRefunds: Record<string, number> = {};
const regionCounts: Record<string, number> = {};
for (const row of rows) {
const converted = row.amount * (rates[row.currency] ?? 1);
if (row.refunded) {
regionRefunds[row.region] = (regionRefunds[row.region] ?? 0) + converted;
} else {
regionTotals[row.region] = (regionTotals[row.region] ?? 0) + converted;
}
regionCounts[row.region] = (regionCounts[row.region] ?? 0) + 1;
}
const productTotals: Record<string, number> = {};
const productRefunds: Record<string, number> = {};
const productCounts: Record<string, number> = {};
for (const row of rows) {
const converted = row.amount * (rates[row.currency] ?? 1);
if (row.refunded) {
productRefunds[row.product] = (productRefunds[row.product] ?? 0) + converted;
} else {
productTotals[row.product] = (productTotals[row.product] ?? 0) + converted;
}
productCounts[row.product] = (productCounts[row.product] ?? 0) + 1;
}
const currencyTotals: Record<string, number> = {};
const currencyRefunds: Record<string, number> = {};
const currencyCounts: Record<string, number> = {};
for (const row of rows) {
const converted = row.amount * (rates[row.currency] ?? 1);
if (row.refunded) {
currencyRefunds[row.currency] = (currencyRefunds[row.currency] ?? 0) + converted;
} else {
currencyTotals[row.currency] = (currencyTotals[row.currency] ?? 0) + converted;
}
currencyCounts[row.currency] = (currencyCounts[row.currency] ?? 0) + 1;
}
const summary: Record<string, number> = {};
for (const [region, total] of Object.entries(regionTotals)) {
summary[`region.${region}.total`] = total;
summary[`region.${region}.refunded`] = regionRefunds[region] ?? 0;
summary[`region.${region}.count`] = regionCounts[region] ?? 0;
summary[`region.${region}.average`] = total / (regionCounts[region] ?? 1);
}
for (const [product, total] of Object.entries(productTotals)) {
summary[`product.${product}.total`] = total;
summary[`product.${product}.refunded`] = productRefunds[product] ?? 0;
summary[`product.${product}.count`] = productCounts[product] ?? 0;
summary[`product.${product}.average`] = total / (productCounts[product] ?? 1);
}
for (const [currency, total] of Object.entries(currencyTotals)) {
summary[`currency.${currency}.total`] = total;
summary[`currency.${currency}.refunded`] = currencyRefunds[currency] ?? 0;
summary[`currency.${currency}.count`] = currencyCounts[currency] ?? 0;
summary[`currency.${currency}.average`] = total / (currencyCounts[currency] ?? 1);
}
summary['rows.counted'] = rows.length;
summary['rows.refunded'] = rows.filter((row) => row.refunded).length;
summary['rows.settled'] = rows.filter((row) => !row.refunded).length;
summary['regions.counted'] = Object.keys(regionTotals).length;
summary['products.counted'] = Object.keys(productTotals).length;
summary['currencies.counted'] = Object.keys(currencyTotals).length;
for (const [region, count] of Object.entries(regionCounts)) {
summary[`region.${region}.share`] = count / rows.length;
}
for (const [product, count] of Object.entries(productCounts)) {
summary[`product.${product}.share`] = count / rows.length;
}
for (const [currency, count] of Object.entries(currencyCounts)) {
summary[`currency.${currency}.share`] = count / rows.length;
}
return summary;
};

Correct

src/reporting/buildReportSummary.ts
interface Row {
region: string;
product: string;
currency: string;
amount: number;
refunded: boolean;
}
interface Params {
rows: Row[];
rates: Record<string, number>;
}
// One grouping helper the export reuses three times — the same report, with the
// body under the cap because the repeated work was named once.
const sumBy = ({ rows, rates, key }: { rows: Row[]; rates: Record<string, number>; key: keyof Row }) => {
const totals: Record<string, number> = {};
for (const row of rows) {
const group = String(row[key]);
totals[group] = (totals[group] ?? 0) + row.amount * (rates[row.currency] ?? 1);
}
return totals;
};
export const buildReportSummary = ({ rows, rates }: Params): Record<string, number> => {
const summary: Record<string, number> = {};
for (const key of ['region', 'product', 'currency'] as const) {
for (const [group, total] of Object.entries(sumBy({ rows, rates, key }))) {
summary[`${key}.${group}.total`] = total;
}
}
summary['rows.counted'] = rows.length;
return summary;
};

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": {
"react-function-size": {
"severity": "advisory",
"options": {
"function": 80,
"hook": 160,
"component": 200
}
}
}
}

Packs that select it