Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
feat(prefer-switch): allow if statement with mulitple OR and no else …
Browse files Browse the repository at this point in the history
…clause (#4873)
  • Loading branch information
tanmoyopenroot authored and adidahiya committed Nov 3, 2019
1 parent ab0e116 commit 77547dd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/rules/preferSwitchRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ function walk(ctx: Lint.WalkContext<number>): void {
function check(node: ts.IfStatement, sourceFile: ts.SourceFile, minCases: number): boolean {
let switchVariable: ts.Expression | undefined;
let casesSeen = 0;

const { elseStatement } = node;

if (elseStatement === undefined) {
return false;
}

const couldBeSwitch = everyCase(node, expr => {
casesSeen++;
if (switchVariable !== undefined) {
Expand Down
74 changes: 67 additions & 7 deletions test/rules/prefer-switch/default/test.ts.lint
Original file line number Diff line number Diff line change
@@ -1,19 +1,79 @@
if (x === 1 || x === 2) {}

if (x === 1 || x === 2 || x === 3) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (x === 1 || x === 2 || x === 3) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (x === 1 || x === 2 || x === -3) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (x === 1 || x === 2 || x === null) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (x === 1 || x === 2 || x === true) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (x === 1 || x === 2 || x === false) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (x === 1 || x === 2 || x === `123`) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (x === 1 || x === 2 && x === `123`) {}

if (x === 1) {}
~~~~~~~ [0]
else if (x === 2) {}
else if (x === 3) {}

if (x === 1) {}
~~~~~~~ [0]
else if (x === 2) {}
else if (x === 3) {}
else {}

if (x === 1 || x === 2) {}
~~~~~~~~~~~~~~~~~~ [0]
else if (x === 3) {}

if (x === 1 || x === 2) {}
else {}

if (x === 1 || x === 2 || && x === 3) {}
else if (x === 4) {}
else if (x === 5) {}
else {}

if (x === 1 && x === 2) {}
else if (x === 3) {}
else if (x === 4) {}

if (x === 1 && x === 2) {}
else if (x === 3) {}
else if (x === 4) {}
else {}


if (x === 1 || y === 2) {}
else if (x === 3) {}
else if (x === 4) {}
else {}


if (x === 1 && y === 2) {}
else if (x === 3) {}
else if (x === 4 && x === 5) {}
else {}

if (x === 1 && y === 2) {}
else if (x === 3) {}
else if (x === 4 && x === 5) {}

export enum ItemType {
FIRST = "FIRST",
SECOND = "SECOND",
THIRD = "THIRD",
}

if (
item.type === ItemType.FIRST ||
item.type === ItemType.SECOND ||
item.type === ItemType.THIRD
) {}

[0]: Use a switch statement instead of using multiple '===' checks.
26 changes: 25 additions & 1 deletion test/rules/prefer-switch/min-cases-2/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ if (x === 1) {} else if (x === 2) {}

// Works with `||`
if (this === 1 || this === 2) {}
~~~~~~~~~~~~~~~~~~~~~~~~ [0]

if (this === 1 || this === 2 || this === 3) {}


if (x === 1) {}
~~~~~~~ [0]
else if (x === 2) {}
else if (x === 3) {}

if (x === 1) {}
~~~~~~~ [0]
else if (x === 2) {}


// Default minumum of 2 cases.
if (x === 1) {} else {}
Expand All @@ -25,4 +37,16 @@ if (x === f()) {} else if (x === g()) {}
if (x.y.z === a.b) else if (x.y.z === c.d) {}
~~~~~~~~~~~~~ [0]

export enum ItemType {
FIRST = "FIRST",
SECOND = "SECOND",
THIRD = "THIRD",
}

if (
item.type === ItemType.FIRST ||
item.type === ItemType.SECOND ||
item.type === ItemType.THIRD
) {}

[0]: Use a switch statement instead of using multiple '===' checks.

0 comments on commit 77547dd

Please sign in to comment.