-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-useless-switch-case
rule (#1779)
- Loading branch information
Showing
11 changed files
with
733 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Disallow useless case in switch statements | ||
|
||
<!-- Do not manually modify RULE_NOTICE part. Run: `npm run generate-rule-notices` --> | ||
<!-- RULE_NOTICE --> | ||
✅ *This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.* | ||
|
||
💡 *This rule provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).* | ||
<!-- /RULE_NOTICE --> | ||
|
||
An empty case before the last default case is useless. | ||
|
||
## Fail | ||
|
||
```js | ||
switch (foo) { | ||
case 1: | ||
default: | ||
handleDefaultCase(); | ||
break; | ||
} | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
switch (foo) { | ||
case 1: | ||
case 2: | ||
handleCase1And2(); | ||
break; | ||
} | ||
``` | ||
|
||
```js | ||
switch (foo) { | ||
case 1: | ||
handleCase1(); | ||
break; | ||
default: | ||
handleDefaultCase(); | ||
break; | ||
} | ||
``` | ||
|
||
```js | ||
switch (foo) { | ||
case 1: | ||
handleCase1(); | ||
// Fallthrough | ||
default: | ||
handleDefaultCase(); | ||
break; | ||
} | ||
``` | ||
|
||
```js | ||
switch (foo) { | ||
// This is actually useless, but we only check cases where the last case is the `default` case | ||
case 1: | ||
default: | ||
handleDefaultCase(); | ||
break; | ||
case 2: | ||
handleCase2(); | ||
break; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
module.exports = { | ||
isEmptyNode: require('./is-empty-node.js'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
function isEmptyNode(node, additionalEmpty) { | ||
const {type} = node; | ||
|
||
if (type === 'BlockStatement') { | ||
return node.body.every(currentNode => isEmptyNode(currentNode, additionalEmpty)); | ||
} | ||
|
||
if (type === 'EmptyStatement') { | ||
return true; | ||
} | ||
|
||
if (additionalEmpty && additionalEmpty(node)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
module.exports = isEmptyNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
const {isEmptyNode} = require('./ast/index.js'); | ||
const getSwitchCaseHeadLocation = require('./utils/get-switch-case-head-location.js'); | ||
|
||
const MESSAGE_ID_ERROR = 'no-useless-switch-case/error'; | ||
const MESSAGE_ID_SUGGESTION = 'no-useless-switch-case/suggestion'; | ||
const messages = { | ||
[MESSAGE_ID_ERROR]: 'Useless case in switch statement.', | ||
[MESSAGE_ID_SUGGESTION]: 'Remove this case.', | ||
}; | ||
|
||
const isEmptySwitchCase = node => node.consequent.every(node => isEmptyNode(node)); | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => ({ | ||
* 'SwitchStatement[cases.length>1]'(switchStatement) { | ||
const {cases} = switchStatement; | ||
|
||
// TypeScript allows multiple `default` cases | ||
const defaultCases = cases.filter(switchCase => switchCase.test === null); | ||
if (defaultCases.length !== 1) { | ||
return; | ||
} | ||
|
||
const [defaultCase] = defaultCases; | ||
|
||
// We only check cases where the last case is the `default` case | ||
if (defaultCase !== cases[cases.length - 1]) { | ||
return; | ||
} | ||
|
||
const uselessCases = []; | ||
|
||
for (let index = cases.length - 2; index >= 0; index--) { | ||
const node = cases[index]; | ||
if (isEmptySwitchCase(node)) { | ||
uselessCases.unshift(node); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
for (const node of uselessCases) { | ||
yield { | ||
node, | ||
loc: getSwitchCaseHeadLocation(node, context.getSourceCode()), | ||
messageId: MESSAGE_ID_ERROR, | ||
suggest: [ | ||
{ | ||
messageId: MESSAGE_ID_SUGGESTION, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => fixer.remove(node), | ||
}, | ||
], | ||
}; | ||
} | ||
}, | ||
}); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow useless case in switch statements.', | ||
}, | ||
hasSuggestions: true, | ||
messages, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const {isColonToken} = require('eslint-utils'); | ||
|
||
/** | ||
@typedef {line: number, column: number} Position | ||
Get the location of the given `SwitchCase` node for reporting. | ||
@param {Node} node - The `SwitchCase` node to get. | ||
@param {SourceCode} sourceCode - The source code object to get tokens from. | ||
@returns {{start: Position, end: Position}} The location of the class node for reporting. | ||
*/ | ||
function getSwitchCaseHeadLocation(node, sourceCode) { | ||
const startToken = node.test || sourceCode.getFirstToken(node); | ||
const colonToken = sourceCode.getTokenAfter(startToken, isColonToken); | ||
|
||
return {start: node.loc.start, end: colonToken.loc.end}; | ||
} | ||
|
||
module.exports = getSwitchCaseHeadLocation; |
Oops, something went wrong.