A var is read before its declaration in the same function, where it is still undefined

tx0/ts:var-read-before-declaredrecommendedwarningreliability/logicdepth fastbug

What it finds

var declarations are hoisted: the variable exists from the start of the function but holds undefined until the declaration runs. Reading it earlier gives undefined silently, where let and const would throw and point at the mistake.

Reported are reads of a var that come before its only declaration in the text, in the same function and outside nested functions and loops.

From the rule's source, var_read_before_declared.rs.

Example from the rule's tests
export function total() {  const first = sum;tx0 reports: sum is read before its var declaration  var sum = 1;  return first + sum;}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:var-read-before-declared" = "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:var-read-before-declared] reason="..."

tx0 explain tx0/ts:var-read-before-declared shows the same in the terminal. Found a false positive? Report it.