Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit bfa9f51

Browse files
no-duplicate-branches: Fix switch/case reporting (#114)
1 parent cd2257a commit bfa9f51

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/rules/no-duplicated-branches.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,20 @@ const rule: Rule.RuleModule = {
7272
function visitSwitchStatement(switchStmt: estree.SwitchStatement) {
7373
const { cases } = switchStmt;
7474
const { endsWithDefault } = collectSwitchBranches(switchStmt);
75-
const nonEmptyCases = cases
76-
.map(c => takeWithoutBreak(expandSingleBlockStatement(c.consequent)))
77-
.filter(c => c.length > 0);
75+
const nonEmptyCases = cases.filter(c => takeWithoutBreak(expandSingleBlockStatement(c.consequent)).length > 0);
76+
const casesWithoutBreak = nonEmptyCases.map(c => takeWithoutBreak(expandSingleBlockStatement(c.consequent)));
7877

79-
if (allEquivalentWithoutDefault(nonEmptyCases, endsWithDefault)) {
80-
cases.slice(1).forEach((caseStmt, i) => reportIssue(caseStmt, cases[i], "case"));
78+
if (allEquivalentWithoutDefault(casesWithoutBreak, endsWithDefault)) {
79+
nonEmptyCases.slice(1).forEach((caseStmt, i) => reportIssue(caseStmt, nonEmptyCases[i], "case"));
8180
return;
8281
}
8382

84-
for (let i = 1; i < nonEmptyCases.length; i++) {
85-
const firstClauseWithoutBreak = nonEmptyCases[i];
83+
for (let i = 1; i < cases.length; i++) {
84+
const firstClauseWithoutBreak = takeWithoutBreak(expandSingleBlockStatement(cases[i].consequent));
8685

8786
if (hasRequiredSize(firstClauseWithoutBreak)) {
8887
for (let j = 0; j < i; j++) {
89-
const secondClauseWithoutBreak = nonEmptyCases[j];
88+
const secondClauseWithoutBreak = takeWithoutBreak(expandSingleBlockStatement(cases[j].consequent));
9089

9190
if (areEquivalent(firstClauseWithoutBreak, secondClauseWithoutBreak, context.getSourceCode())) {
9291
reportIssue(cases[i], cases[j], "case");

tests/rules/no-duplicated-branches.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ ruleTester.run("no-duplicated-branches switch", rule, {
233233
},
234234
],
235235
invalid: [
236+
{
237+
code: `
238+
switch(a) {
239+
case 2:
240+
case 1:
241+
first();
242+
second();
243+
break;
244+
case 3:
245+
first();
246+
second();
247+
break;
248+
}`,
249+
errors: [{ line: 8 }],
250+
},
236251
{
237252
code: `
238253
switch (a) {
@@ -353,5 +368,24 @@ ruleTester.run("no-duplicated-branches switch", rule, {
353368
}`,
354369
errors: [{ line: 6, message: "This case's code block is the same as the block for the case on line 3." }],
355370
},
371+
{
372+
code: `
373+
switch(a) {
374+
case 0:
375+
foo();
376+
bar();
377+
break;
378+
case 2:
379+
case 1:
380+
first();
381+
second();
382+
break;
383+
case 3:
384+
first();
385+
second();
386+
break;
387+
}`,
388+
errors: [{ line: 12, message: "This case's code block is the same as the block for the case on line 8." }],
389+
},
356390
],
357391
});

0 commit comments

Comments
 (0)