A declaration hides a variable of an enclosing scope

tx0/ts:shadowed-variablestrictwarningsmell

What it finds

A declaration that reuses the name of a variable from an enclosing scope hides that variable for the rest of its own scope. Code written later may mean the outer one and silently get the inner one, and a reader has to check which is meant at each use.

Variables, functions, classes, parameters and catch parameters are reported when an enclosing function, block or the file declares a value of the same name. Well-known globals and names declared by other scripts are not seen, and types, which live apart from values, do not count.

From the rule's source, shadowed_variable.rs.

Example from the rule's tests
const limit = 10;export function check(items: number[]) {  const limit = 5;tx0 reports: limit hides a variable  return items.filter((item) => {    const items = [item];tx0 reports: items hides a variable    return items.length < limit;  });}export function fresh(value: number) {  return value;}
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:shadowed-variable" = "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:shadowed-variable] reason="..."

tx0 explain tx0/ts:shadowed-variable shows the same in the terminal. Found a false positive? Report it.