-
-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
428 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Enforce consistent styling when checking for element existence using `indexOf()` | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs). | ||
|
||
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). | ||
|
||
<!-- end auto-generated rule header --> | ||
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
||
Enforce consistent style when checking for element existence with `indexOf()`. | ||
|
||
Prefer using `index === -1` to check if an element does not exist and `index !== -1` to check if an element does exist. | ||
|
||
Similar to the [`explicit-length-check`](explicit-length-check.md) rule. | ||
|
||
## Examples | ||
|
||
```js | ||
// ❌ | ||
const index = foo.indexOf('bar'); | ||
if (index < 0) {} | ||
|
||
// ✅ | ||
const index = foo.indexOf('bar'); | ||
if (index === -1) {} | ||
``` | ||
|
||
```js | ||
// ❌ | ||
const index = foo.indexOf('bar'); | ||
if (index >= 0) {} | ||
|
||
// ✅ | ||
const index = foo.indexOf('bar'); | ||
if (index !== -1) {} | ||
``` |
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,175 @@ | ||
'use strict'; | ||
const resolveVariableName = require('./utils/resolve-variable-name.js'); | ||
|
||
const MESSAGE_ID = 'consistent-indexof-check'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Prefer `{{replacement}}` over `{{value}}`.', | ||
}; | ||
|
||
/** | ||
Check if the node is a call expression of `indexOf` method. | ||
@param {import('estree').Node} node | ||
@returns {node is import('estree').CallExpression} | ||
*/ | ||
function isIndexOfCallExpression(node) { | ||
return ( | ||
node | ||
&& node.type === 'CallExpression' | ||
&& node.callee.type === 'MemberExpression' | ||
&& node.callee.property.name === 'indexOf' | ||
&& node.arguments.length === 1 | ||
); | ||
} | ||
|
||
/** | ||
Check if the operator is `<` or `<=` and the value is `0` or `-1`. | ||
@param {string} operator | ||
@param {string} value | ||
@returns {boolean} | ||
*/ | ||
function isCheckNotExists(operator, value) { | ||
return ( | ||
(operator === '<' && value <= 0) | ||
|| (operator === '<=' && value <= -1) | ||
); | ||
} | ||
|
||
/** | ||
Check if the operator is `>` or `>=` and the value is `-1` or `0`. | ||
@param {string} operator | ||
@param {number} value | ||
@returns {boolean} | ||
*/ | ||
function isCheckExists(operator, value) { | ||
return ( | ||
(operator === '>' && value === -1) | ||
|| (operator === '>=' && value === 0) | ||
); | ||
} | ||
|
||
/** | ||
Reverse the operator. | ||
@param {string} operator | ||
@returns {string} | ||
*/ | ||
function reverseOperator(operator) { | ||
switch (operator) { | ||
case '<': { | ||
return '>'; | ||
} | ||
|
||
case '<=': { | ||
return '>='; | ||
} | ||
|
||
case '>': { | ||
return '<'; | ||
} | ||
|
||
case '>=': { | ||
return '<='; | ||
} | ||
|
||
default: { | ||
return operator; | ||
} | ||
} | ||
} | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = context => ({ | ||
/** @param {import('estree').IfStatement} node */ | ||
IfStatement(node) { | ||
if (node.test.type !== 'BinaryExpression') { | ||
return; | ||
} | ||
|
||
/** @type {import('estree').BinaryExpression | undefined} */ | ||
let testNode; | ||
let variableName = ''; | ||
let literalValue = 0; | ||
let operator = ''; | ||
|
||
if ( | ||
// Match case: index === -1 | ||
node.test.left.type === 'Identifier' | ||
&& node.test.right.type === 'Literal' | ||
) { | ||
testNode = node.test; | ||
variableName = testNode.left.name; | ||
literalValue = testNode.right.value; | ||
operator = testNode.operator; | ||
} else if ( | ||
// Match case: -1 === index | ||
node.test.right.type === 'Identifier' | ||
&& node.test.left.type === 'Literal' | ||
) { | ||
testNode = node.test; | ||
variableName = testNode.right.name; | ||
literalValue = testNode.left.value; | ||
operator = reverseOperator(testNode.operator); | ||
} | ||
|
||
if (!testNode) { | ||
return; | ||
} | ||
|
||
let replacement = ''; | ||
|
||
// For better performance, early checking of operators can avoid looking up variables in scope. | ||
if (isCheckNotExists(operator, literalValue)) { | ||
replacement = `${variableName} === -1`; | ||
} else if (isCheckExists(operator, literalValue)) { | ||
replacement = `${variableName} !== -1`; | ||
} | ||
|
||
if (!replacement) { | ||
return; | ||
} | ||
|
||
const variable = resolveVariableName( | ||
variableName, | ||
context.sourceCode.getScope(node), | ||
); | ||
|
||
if (!variable) { | ||
return; | ||
} | ||
|
||
for (const definition of variable.defs) { | ||
if (definition.type === 'Variable') { | ||
if (!isIndexOfCallExpression(definition.node.init)) { | ||
break; | ||
} | ||
|
||
context.report({ | ||
node: testNode, | ||
messageId: MESSAGE_ID, | ||
data: { | ||
value: context.sourceCode.getText(testNode), | ||
replacement, | ||
}, | ||
fix: fixer => fixer.replaceText(testNode, replacement), | ||
}); | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforce consistent styling when checking for element existence using `indexOf()`', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
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
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,64 @@ | ||
import outdent from 'outdent'; | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (index === -1) {} | ||
`, | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (-1 === index) {} | ||
`, | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (index !== -1) {} | ||
`, | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (-1 !== index) {} | ||
`, | ||
outdent` | ||
const index = 0; // index not from indexOf | ||
if (index < 0) {} | ||
`, | ||
], | ||
invalid: [ | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (index < 0) {} | ||
`, | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (0 > index) {} | ||
`, | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (index >= 0) {} | ||
`, | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
if (0 <= index) {} | ||
`, | ||
outdent` | ||
const index = foo.indexOf('bar'); | ||
function foo () { | ||
// It will search the scope chain for 'index' and find the 'index' variable declared above. | ||
if (index < 0) {} | ||
} | ||
`, | ||
], | ||
}); |
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
Oops, something went wrong.