-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
353 additions
and
7 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,31 @@ | ||
# Prefer `Set#has()` over `Array#includes()` when checking for existence or non-existence. | ||
|
||
[`Set#has()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/has) is faster than [`Array#includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes). | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const array = [1, 2, 3]; | ||
function isExists(find) { | ||
return array.includes(find); | ||
} | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const set = new Set([1, 2, 3]); | ||
function isExists(find) { | ||
return set.has(find); | ||
} | ||
``` | ||
|
||
```js | ||
const array = [1, 2]; | ||
function isExists(find) { | ||
return array.includes(find); | ||
} | ||
array.push(3); | ||
``` |
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
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
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,97 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const getReferences = require('./utils/get-references'); | ||
|
||
const selector = [ | ||
':not(ExportNamedDeclaration)', | ||
'>', | ||
'VariableDeclaration', | ||
'>', | ||
'VariableDeclarator', | ||
'[init.type="ArrayExpression"]', | ||
'>', | ||
'Identifier' | ||
].join(''); | ||
|
||
const MESSAGE_ID = 'preferSetHas'; | ||
|
||
const isIncludesCall = node => { | ||
/* istanbul ignore next */ | ||
if (!node.parent || !node.parent.parent) { | ||
return false; | ||
} | ||
|
||
const {type, optional, callee, arguments: parameters} = node.parent.parent; | ||
return ( | ||
type === 'CallExpression' && | ||
!optional, | ||
callee && | ||
callee.type === 'MemberExpression' && | ||
!callee.computed && | ||
callee.object === node && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === 'includes' && | ||
parameters.length === 1 && | ||
parameters[0].type !== 'SpreadElement' | ||
); | ||
}; | ||
|
||
const create = context => { | ||
const scope = context.getScope(); | ||
const declarations = new Set(); | ||
|
||
return { | ||
[selector]: node => { | ||
declarations.add(node); | ||
}, | ||
'Program:exit'() { | ||
if (declarations.size === 0) { | ||
return; | ||
} | ||
|
||
const references = getReferences(scope); | ||
for (const declaration of declarations) { | ||
declarations.delete(declaration); | ||
|
||
const variable = references | ||
.find(({identifier}) => identifier === declaration) | ||
.resolved; | ||
const nodes = variable.references | ||
.map(({identifier}) => identifier) | ||
.filter(node => node !== declaration); | ||
|
||
if ( | ||
nodes.length > 0 && | ||
nodes.every(node => isIncludesCall(node)) | ||
) { | ||
context.report({ | ||
node: declaration, | ||
messageId: MESSAGE_ID, | ||
data: { | ||
name: declaration.name | ||
}, | ||
fix: fixer => [ | ||
fixer.insertTextBefore(declaration.parent.init, 'new Set('), | ||
fixer.insertTextAfter(declaration.parent.init, ')'), | ||
...nodes.map(node => fixer.replaceText(node.parent.property, 'has')) | ||
] | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
[MESSAGE_ID]: '`{{name}}` should be a `Set`, and use `{{name}}.has()` to check existence or non-existence.' | ||
} | ||
} | ||
}; |
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,10 @@ | ||
'use strict'; | ||
const {uniq} = require('lodash'); | ||
|
||
const getReferences = scope => uniq( | ||
scope.references.concat( | ||
...scope.childScopes.map(scope => getReferences(scope)) | ||
) | ||
); | ||
|
||
module.exports = getReferences; |
Oops, something went wrong.