Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
8d32574
commit cda72bd
Showing
14 changed files
with
701 additions
and
64 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 |
---|---|---|
@@ -1,19 +1,25 @@ | ||
# Prefer the spread operator over `Array.from()` | ||
# Prefer the spread operator over `Array.from()` and `Array#concat()` | ||
|
||
Enforces the use of the spread operator over `Array.from()`. This rule adds on to the built-in [prefer-spread](https://eslint.org/docs/rules/prefer-spread) rule, which only flags uses of `.apply()`. Does not enforce for `TypedArray.from()`; | ||
|
||
This rule is fixable. | ||
Enforces the use of the spread operator over `Array.from()` and `Array#concat()`. This rule adds on to the built-in [prefer-spread](https://eslint.org/docs/rules/prefer-spread) rule, which only flags uses of `.apply()`. Does not enforce for `TypedArray.from()`; | ||
|
||
This rule is partly fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
Array.from(set).map(() => {}); | ||
Array.from(set).map(element => foo(element)); | ||
``` | ||
|
||
```js | ||
const array = array1.concat(array2); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
[...set].map(() => {}); | ||
[...set].map(element => foo(element)); | ||
``` | ||
|
||
```js | ||
const array = [...array1, ...array2]; | ||
``` |
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
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 {isOpeningParenToken} = require('eslint-utils'); | ||
|
||
/** | ||
Get the text of the arguments list of `CallExpression`. | ||
@param {Node} node - The `CallExpression` node. | ||
@param {SourceCode} sourceCode - The source code object. | ||
@returns {string} | ||
*/ | ||
const getCallExpressionArgumentsText = (node, sourceCode) => { | ||
const openingParenthesisToken = sourceCode.getTokenAfter(node.callee, isOpeningParenToken); | ||
const closingParenthesisToken = sourceCode.getLastToken(node); | ||
|
||
return sourceCode.text.slice( | ||
openingParenthesisToken.range[1], | ||
closingParenthesisToken.range[0] | ||
); | ||
}; | ||
|
||
module.exports = getCallExpressionArgumentsText; |
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,25 @@ | ||
'use strict'; | ||
const {isOpeningParenToken, isClosingParenToken} = require('eslint-utils'); | ||
const getParenthesizedTimes = require('./get-parenthesized-times'); | ||
|
||
/* | ||
Get the first opening parenthesis token and the last closing parenthesis token of a parenthesized node. | ||
@param {Node} node - The node to be checked. | ||
@param {SourceCode} sourceCode - The source code object. | ||
@returns {Token[]} | ||
*/ | ||
function getParentheses(node, sourceCode) { | ||
const parenthesizedTimes = getParenthesizedTimes(node, sourceCode); | ||
|
||
if (parenthesizedTimes > 0) { | ||
return [ | ||
sourceCode.getTokenBefore(node, {skip: parenthesizedTimes - 1, filter: isOpeningParenToken}), | ||
sourceCode.getTokenAfter(node, {skip: parenthesizedTimes - 1, filter: isClosingParenToken}) | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
module.exports = getParentheses; |
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
'use strict'; | ||
const {uniq} = require('lodash'); | ||
const {uniq, flatten} = require('lodash'); | ||
|
||
const getReferences = scope => uniq( | ||
scope.references.concat( | ||
...scope.childScopes.map(scope => getReferences(scope)) | ||
) | ||
); | ||
const getReferences = scope => uniq([ | ||
...scope.references, | ||
...flatten(scope.childScopes.map(scope => getReferences(scope))) | ||
]); | ||
|
||
module.exports = getReferences; |
Oops, something went wrong.