noConfusingLabels
Summary
Section titled Summary- Rule available since:
v1.0.0
- Diagnostic Category:
lint/suspicious/noConfusingLabels
- This rule is recommended, which means is enabled by default.
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
no-labels
- Inspired from
Description
Section titled DescriptionDisallow labeled statements that are not loops.
Labeled statements in JavaScript are used in conjunction with break
and continue
to control flow around multiple loops.
Their use for other statements is suspicious and unfamiliar.
The rule ignores reactive Svelte statements in Svelte components.
Examples
Section titled ExamplesInvalid
Section titled Invalidlabel: f();
code-block.js:1:1 lint/suspicious/noConfusingLabels ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected label.
> 1 │ label: f();
│ ^^^^^
2 │
ℹ Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.
label: { f(); break label;}
code-block.js:1:1 lint/suspicious/noConfusingLabels ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected label.
> 1 │ label: {
│ ^^^^^
2 │ f();
3 │ break label;
ℹ Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.
label: if (a) { f() break label;}
code-block.js:1:1 lint/suspicious/noConfusingLabels ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected label.
> 1 │ label: if (a) {
│ ^^^^^
2 │ f()
3 │ break label;
ℹ Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.
label: switch (a) { case 0: break label;}
code-block.js:1:1 lint/suspicious/noConfusingLabels ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Unexpected label.
> 1 │ label: switch (a) {
│ ^^^^^
2 │ case 0:
3 │ break label;
ℹ Only loops should be labeled.
The use of labels for other statements is suspicious and unfamiliar.
Valid
Section titled Validouter: while (a) { while(b) { break outer; }}
<script>$: { /* reactive block */ }</script>
Options
Section titled OptionsUse the options to allow specific labels in your code. Labels can be used to mark code that should be removed under certain conditions, such as in production builds. Some bundlers, such as esbuild and Vite, can be configured to remove labeled statements.
{ "options": { "allowedLabels": ["DEV"] }}
DEV: assertSomeCondition();
How to configure
Section titled How to configure{ "linter": { "rules": { "suspicious": { "noConfusingLabels": "error" } } }}