Two functions in a file have the same body

tx0/ts:identical-function-bodiesstrictwarningmaintainability

What it finds

Two functions with the same body are one piece of logic kept twice: a fix applied to one is easily forgotten in the other. One can call the other, or both a shared helper.

Functions whose bodies are blocks of at least three statements, with the same tokens apart from comments and spacing, are compared within a file. Each function after the first of a group is reported, pointing at the first. Bodies of more than 2000 tokens are not compared, which keeps the cost linear in practice.

From the rule's source, identical_function_bodies.rs.

Example from the rule's tests
export function first(items: number[]) {  const sorted = items.slice();  sorted.sort();  return sorted;}export function second(items: number[]) {tx0 reports: this body repeats the one of first  const sorted = items.slice();  sorted.sort();  return sorted;}export function short(items: number[]) {  return items;}export function shorter(items: number[]) {  return items;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:identical-function-bodies" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:identical-function-bodies] reason="..."

tx0 explain tx0/ts:identical-function-bodies shows the same in the terminal. Found a false positive? Report it.