eslint-plugin-array-func
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -10,3 +10,4 @@ /** | ||
"from-map": require("./rules/from-map"), | ||
"no-unnecessary-this-arg": require("./rules/no-unnecessary-this-arg") | ||
"no-unnecessary-this-arg": require("./rules/no-unnecessary-this-arg"), | ||
"prefer-array-from": require("./rules/prefer-array-from") | ||
}, | ||
@@ -21,3 +22,4 @@ configs: { | ||
"array-func/from-map": "error", | ||
"array-func/no-unnecessary-this-arg": "error" | ||
"array-func/no-unnecessary-this-arg": "error", | ||
"array-func/prefer-array-from": "error" | ||
} | ||
@@ -24,0 +26,0 @@ } |
{ | ||
"name": "eslint-plugin-array-func", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Rules dealing with Array functions and methods.", | ||
@@ -14,9 +14,10 @@ "main": "index.js", | ||
"devDependencies": { | ||
"ava": "^0.22.0", | ||
"codecov": "^2.3.0", | ||
"eslint": "^4.8.0", | ||
"@freaktechnik/eslint-config-node": "^5.2.1", | ||
"@freaktechnik/eslint-config-test": "^5.2.1", | ||
"ava": "^0.23.0", | ||
"codecov": "^3.0.0", | ||
"eslint": "^4.10.0", | ||
"eslint-ava-rule-tester": "^2.0.2", | ||
"eslint-plugin-eslint-plugin": "^1.2.0", | ||
"eslint-plugin-freaktechnik": "^4.0.0", | ||
"nyc": "^11.2.1" | ||
"nyc": "^11.3.0" | ||
}, | ||
@@ -23,0 +24,0 @@ "peerDependencies": { |
@@ -7,2 +7,17 @@ # eslint-plugin-array-func | ||
## Contents | ||
- [Installation](#Installation) | ||
- [Rules](#Rules) | ||
- [`from-map`](#from-map) | ||
- [Examples](#Examples) | ||
- [`no-unnecessary-this-arg`](#no-unnecessary-this-arg) | ||
- [Checked Functions](#checked-functions) | ||
- [Checked Methods](#checked-methods) | ||
- [Examples](#Examples2) | ||
- [`prefer-array-from`](#prefer-array-from) | ||
- [Examples](#Examples3) | ||
- [`array-func/recommended` Configuration](#array-func-recommended-configuration) | ||
- [Using the Configuration](#using-the-configuration) | ||
- [License](#License) | ||
## Installation | ||
@@ -16,3 +31,3 @@ | ||
If you installed `ESLint` globally, you have to install array-func plugin globally too. Otherwise, install it locally. | ||
If you installed `ESLint` globally, you have to install the `array-func` plugin globally too. Otherwise, install it locally. | ||
@@ -57,6 +72,6 @@ ```sh | ||
#### Checked functions | ||
#### Checked Functions | ||
- `from` (fixable) | ||
#### Checked methods | ||
#### Checked Methods | ||
- `every` | ||
@@ -114,13 +129,40 @@ - `filter` | ||
}, console); | ||
array.filter(this.isGood, this); | ||
``` | ||
### `prefer-array-from` | ||
Use `Array.from` instead of `[...iterable]` for performance benefits. | ||
This rule is auto fixable. | ||
#### Examples | ||
Code that triggers this rule: | ||
```js | ||
const iterable = [..."string"]; | ||
const arrayCopy = [...iterable]; | ||
``` | ||
Code that doesn't trigger this rule: | ||
```js | ||
const array = [1, 2, 3]; | ||
const extendedArray = [0, ...array]; | ||
const arrayCopy = Array.from(array); | ||
const characterArray = Array.from("string"); | ||
``` | ||
## `array-func/recommended` Configuration | ||
The recommended configuration will set your parser ECMA Version to 2015, since that's when the Array functions and methods were added. | ||
Rule | Error level | ||
---- | ----------- | ||
`from-map` | Error | ||
`no-unnecessary-this-arg` | Error | ||
Rule | Error level | Fixable | ||
---- | ----------- | ------- | ||
`from-map` | Error | Yes | ||
`no-unnecessary-this-arg` | Error | Sometimes | ||
`prefer-array-from` | Error | Yes | ||
### Using the configuration | ||
### Using the Configuration | ||
To enable this configuration use the `extends` property in your `.eslintrc.json` config file (may look different for other config file styles): | ||
@@ -127,0 +169,0 @@ ```json |
@@ -7,2 +7,9 @@ /** | ||
const { | ||
isMethod, | ||
getParent, | ||
isOnObject | ||
} = require("../lib/helpers/call-expression"); | ||
const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type"); | ||
module.exports = { | ||
@@ -20,9 +27,9 @@ meta: { | ||
"CallExpression:exit"(node) { | ||
if(!node.callee || node.callee.type !== "MemberExpression" || node.callee.property.name !== "map") { | ||
if(!isMethod(node, "map")) { | ||
return; | ||
} | ||
const { callee } = node, | ||
{ object: parent } = callee; | ||
parent = getParent(node); | ||
if(!parent.callee || parent.callee.type !== "MemberExpression" || parent.callee.property.name !== "from" || !parent.callee.object || parent.callee.object.type !== "Identifier" || parent.callee.object.name !== "Array") { | ||
if(!isMethod(parent, "from") || !isOnObject(parent, "Array")) { | ||
return; | ||
@@ -61,3 +68,3 @@ } | ||
const source = `(${sourceCode.getText(cbk)})`; | ||
if(targ && cbk.type !== "ArrowFunctionExpression") { | ||
if(targ && cbk.type !== ARROW_FUNCTION_EXPRESSION) { | ||
return `${source}.call(${targ.name}${PARAM_SEPARATOR}${ps})`; | ||
@@ -73,3 +80,3 @@ } | ||
restParamString = ''; | ||
if(thisArg && callback.type !== "ArrowFunctionExpression") { | ||
if(thisArg && callback.type !== ARROW_FUNCTION_EXPRESSION) { | ||
functionStart = `function(${paramString}) { return `; | ||
@@ -76,0 +83,0 @@ functionEnd = "; }"; |
@@ -7,2 +7,8 @@ /** | ||
const { | ||
isMethod, | ||
isOnObject | ||
} = require("../lib/helpers/call-expression"); | ||
const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type"); | ||
const arrayFunctions = { | ||
@@ -24,5 +30,6 @@ from: 3 | ||
FUNC_POS = -2, | ||
checkFunction = (node, functionName, argumentPosition) => !node.callee || node.callee.type !== "MemberExpression" || node.callee.property.name !== functionName || node.arguments.length < argumentPosition || node.arguments[argumentPosition + FUNC_POS].type !== "ArrowFunctionExpression", | ||
//TODO should also check if the identifier is not an arrow function expression. Similar problem to array detection. | ||
checkFunction = (node, functionName, argumentPosition) => !isMethod(node, functionName) || node.arguments.length < argumentPosition || node.arguments[argumentPosition + FUNC_POS].type !== ARROW_FUNCTION_EXPRESSION, | ||
checkArrayFunction = (functionName, paramPosition, node, context) => { | ||
if(checkFunction(node, functionName, paramPosition) || node.callee.object.name !== "Array") { | ||
if(checkFunction(node, functionName, paramPosition) || !isOnObject(node, "Array")) { | ||
return; | ||
@@ -29,0 +36,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18333
9
274
175
8