From 77547ddc632d0c5a396d86a64cce1f3bd405cbb6 Mon Sep 17 00:00:00 2001 From: Tanmoy Bhowmik Date: Sun, 3 Nov 2019 23:26:38 +0530 Subject: [PATCH] feat(prefer-switch): allow if statement with mulitple OR and no else clause (#4873) --- src/rules/preferSwitchRule.ts | 7 ++ test/rules/prefer-switch/default/test.ts.lint | 74 +++++++++++++++++-- .../prefer-switch/min-cases-2/test.ts.lint | 26 ++++++- 3 files changed, 99 insertions(+), 8 deletions(-) diff --git a/src/rules/preferSwitchRule.ts b/src/rules/preferSwitchRule.ts index 17f819ec1e8..99f748181e7 100644 --- a/src/rules/preferSwitchRule.ts +++ b/src/rules/preferSwitchRule.ts @@ -77,6 +77,13 @@ function walk(ctx: Lint.WalkContext): 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) { diff --git a/test/rules/prefer-switch/default/test.ts.lint b/test/rules/prefer-switch/default/test.ts.lint index 37162ed0577..b8d2bd2f44e 100644 --- a/test/rules/prefer-switch/default/test.ts.lint +++ b/test/rules/prefer-switch/default/test.ts.lint @@ -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. diff --git a/test/rules/prefer-switch/min-cases-2/test.ts.lint b/test/rules/prefer-switch/min-cases-2/test.ts.lint index 0bc40ea65ea..f7ae69c9967 100644 --- a/test/rules/prefer-switch/min-cases-2/test.ts.lint +++ b/test/rules/prefer-switch/min-cases-2/test.ts.lint @@ -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 {} @@ -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.