no-label-var
Disallow labels that share a name with a variable
Labels and variables live in separate namespaces, so a label can have the same name as a variable that's in scope. Then the same word means two things a few lines apart. continue rows jumps to the label, and rows.length reads the variable.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-label-var" = "error" # or "warn"Examples#
example.js1 problem
const rows = table.rows;
rows: for (const row of rows) {Found identifier with same name as label.
for (const cell of row.cells) {
if (cell.isEmpty) continue rows;
}
print(row);
}example.jsNo problems
const rows = table.rows;
eachRow: for (const row of rows) {
for (const cell of row.cells) {
if (cell.isEmpty) continue eachRow;
}
print(row);
}Globals count as variables too. top is a global in browsers:
example.js1 problem
top: for (const row of table.rows) {Found identifier with same name as label.
for (const cell of row.cells) {
if (cell.isEmpty) continue top;
}
print(row);
}