eslint-plugin-array-func
Advanced tools
Comparing version 3.1.7 to 3.1.8
{ | ||
"name": "eslint-plugin-array-func", | ||
"version": "3.1.7", | ||
"version": "3.1.8", | ||
"description": "Rules dealing with Array functions and methods.", | ||
@@ -10,4 +10,3 @@ "main": "index.js", | ||
"lint": "npm run lint:js && npm run lint:ec", | ||
"test": "npm run lint && nyc ava", | ||
"coverage": "nyc report && codecov" | ||
"test": "npm run lint && nyc ava" | ||
}, | ||
@@ -17,14 +16,13 @@ "author": "Martin Giger (https://humanoids.be)", | ||
"devDependencies": { | ||
"@ava/babel": "^1.0.1", | ||
"@freaktechnik/eslint-config-node": "^7.2.0", | ||
"@freaktechnik/eslint-config-test": "^7.2.0", | ||
"@typescript-eslint/parser": "^3.6.1", | ||
"ava": "^3.10.1", | ||
"codecov": "^3.7.0", | ||
"@freaktechnik/eslint-config-node": "^9.0.3", | ||
"@freaktechnik/eslint-config-test": "^9.0.1", | ||
"@typescript-eslint/parser": "^5.47.1", | ||
"ava": "^5.1.0", | ||
"eclint": "^2.8.1", | ||
"eslint": "^7.4.0", | ||
"eslint": "^8.31.0", | ||
"eslint-ava-rule-tester": "^4.0.0", | ||
"eslint-plugin-eslint-plugin": "^2.3.0", | ||
"eslint-plugin-eslint-plugin": "^5.0.7", | ||
"eslint-plugin-optimize-regex": "^1.2.1", | ||
"nyc": "^15.1.0", | ||
"typescript": "^3.9.7" | ||
"typescript": "^4.9.4" | ||
}, | ||
@@ -54,5 +52,3 @@ "peerDependencies": { | ||
}, | ||
"dependencies": {}, | ||
"ava": { | ||
"babel": true, | ||
"files": [ | ||
@@ -59,0 +55,0 @@ "test/**/*", |
# eslint-plugin-array-func | ||
[![Build Status](https://travis-ci.com/freaktechnik/eslint-plugin-array-func.svg?branch=master)](https://travis-ci.com/freaktechnik/eslint-plugin-array-func) [![codecov](https://codecov.io/gh/freaktechnik/eslint-plugin-array-func/branch/master/graph/badge.svg)](https://codecov.io/gh/freaktechnik/eslint-plugin-array-func) | ||
[![codecov](https://codecov.io/gh/freaktechnik/eslint-plugin-array-func/branch/master/graph/badge.svg)](https://codecov.io/gh/freaktechnik/eslint-plugin-array-func) | ||
@@ -286,4 +286,3 @@ Rules for Array functions and methods. | ||
Use `.flatMap()` to flatten an array and map the values instead of using | ||
`.flat().map()`. | ||
Use `.flatMap()` to map and then flatten an array instead of using `.map().flat()`. | ||
@@ -297,3 +296,3 @@ This rule is auto fixable. | ||
```js | ||
const flattenedAndMapped = array.map((p) => p).flat(); | ||
const mappedAndFlattened = array.map((p) => p).flat(); | ||
@@ -312,3 +311,3 @@ const flatWithDefaultDepth = array.map((r) => r).flat(1); | ||
const mappedThenFlattened = array.flat().map((r) => r + 1); | ||
const flattenedThenMapped = array.flat().map((r) => r + 1); | ||
@@ -315,0 +314,0 @@ const flatMappedWithExtra = array.map((r) => r + 1).reverse().flat(); |
@@ -22,3 +22,6 @@ /** | ||
fixable: "code", | ||
type: "suggestion" | ||
type: "suggestion", | ||
messages: { | ||
avoidReverse: "Prefer using {{ reversed }} over reversing the array and {{ methodName }}" | ||
} | ||
}, | ||
@@ -41,3 +44,7 @@ create(context) { | ||
}, | ||
message: `Prefer using ${reversed} over reversing the array and ${node.callee.property.name}`, | ||
messageId: "avoidReverse", | ||
data: { | ||
reversed, | ||
methodName: node.callee.property.name | ||
}, | ||
fix(fixer) { | ||
@@ -44,0 +51,0 @@ const [ propertyStart ] = parent.callee.property.range, |
@@ -7,3 +7,7 @@ /** | ||
const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type"); | ||
const { ARROW_FUNCTION_EXPRESSION } = require("../lib/type"), | ||
ALL_PARAMS = [ | ||
{ name: 'item' }, | ||
{ name: 'index' } | ||
]; | ||
@@ -18,11 +22,25 @@ module.exports = { | ||
type: "suggestion", | ||
schema: [] | ||
schema: [], | ||
messages: { | ||
useMapCb: "Use mapFn callback of Array.from instead of map()" | ||
} | ||
}, | ||
create(context) { | ||
return { | ||
'CallExpression[callee.type="MemberExpression"] > MemberExpression[property.name="map"] > CallExpression[callee.type="MemberExpression"][callee.property.name="from"][callee.object.type="Identifier"][callee.object.name="Array"]'(node) { | ||
'CallExpression[callee.type="MemberExpression"] > MemberExpression[property.name="map"][property] > CallExpression[callee.type="MemberExpression"][callee.property.name="from"][callee.object.type="Identifier"][callee.object.name="Array"]'(node) { | ||
const parent = node, | ||
callee = node.parent; | ||
callee = node.parent, | ||
[ | ||
mapCallback, | ||
mapThisArgument | ||
] = callee.parent.arguments; | ||
node = callee.parent; | ||
if(mapCallback.type === "Identifier" || | ||
mapCallback.params.length > ALL_PARAMS.length || | ||
mapCallback.params.some((parameter) => parameter.type === "RestElement") | ||
) { | ||
return; | ||
} | ||
context.report({ | ||
@@ -34,3 +52,3 @@ node: callee.property, | ||
}, | ||
message: "Use mapFn callback of Array.from instead of map()", | ||
messageId: "useMapCb", | ||
fix(fixer) { | ||
@@ -46,6 +64,2 @@ const HAS_CBK = 2, | ||
[ | ||
mapCallback, | ||
mapThisArgument | ||
] = node.arguments, | ||
[ | ||
_, // eslint-disable-line no-unused-vars | ||
@@ -55,4 +69,5 @@ callback, | ||
] = parent.arguments, | ||
// Get the params names from the callback that has the most params (since the signature is identical). | ||
parameters = callback.params.length > mapCallback.params.length ? callback.params : mapCallback.params, | ||
parameters = callback.type !== "Identifier" | ||
? callback.params.length > mapCallback.params.length ? callback.params : mapCallback.params | ||
: ALL_PARAMS, | ||
parameterString = parameters.map((p) => p.name).join(PARAM_SEPARATOR), | ||
@@ -59,0 +74,0 @@ getCallback = (cbk, targ, ps) => { |
@@ -39,3 +39,3 @@ /** | ||
loc: argument.loc, | ||
message: "Unnecessary this argument '{{ argument }}' with arrow function as callback to Array.{{ name }}", | ||
messageId: "unnecessaryThisArgStatic", | ||
data: { | ||
@@ -68,3 +68,3 @@ name: node.callee.property.name, | ||
loc: argument.loc, | ||
message: "Unnecessary this argument '{{ argument }}' with an arrow function as callback to {{ name }}", | ||
messageId: "unnecessaryThisArgMethod", | ||
data: { | ||
@@ -85,3 +85,7 @@ name: node.callee.property.name, | ||
fixable: "code", | ||
type: "suggestion" | ||
type: "suggestion", | ||
messages: { | ||
unnecessaryThisArgMethod: "Unnecessary this argument '{{ argument }}' with an arrow function as callback to {{ name }}", | ||
unnecessaryThisArgStatic: "Unnecessary this argument '{{ argument }}' with arrow function as callback to Array.{{ name }}" | ||
} | ||
}, | ||
@@ -91,8 +95,8 @@ create(context) { | ||
"CallExpression:exit"(node) { | ||
for(const func in arrayFunctions) { | ||
checkArrayFunction(func, arrayFunctions[func], node, context); | ||
for(const functionName in arrayFunctions) { | ||
checkArrayFunction(functionName, arrayFunctions[functionName], node, context); | ||
} | ||
for(const func of methods) { | ||
checkMemberFunction(func, node, context); | ||
for(const functionName of methods) { | ||
checkMemberFunction(functionName, node, context); | ||
} | ||
@@ -99,0 +103,0 @@ } |
@@ -20,3 +20,6 @@ /** | ||
fixable: "code", | ||
type: "problem" | ||
type: "problem", | ||
messages: { | ||
preferArrayFrom: "Use Array.from to convert from iterable to array" | ||
} | ||
}, | ||
@@ -29,3 +32,3 @@ create(context) { | ||
node, | ||
message: "Use Array.from to convert from iterable to array", | ||
messageId: 'preferArrayFrom', | ||
fix(fixer) { | ||
@@ -32,0 +35,0 @@ const sourceCode = context.getSourceCode(); |
@@ -15,3 +15,6 @@ /** | ||
schema: [], | ||
type: "suggestion" | ||
type: "suggestion", | ||
messages: { | ||
preferFlatMap: "Use flatMap instead of .map().flat()" | ||
} | ||
}, | ||
@@ -29,3 +32,3 @@ create(context) { | ||
}, | ||
message: "Use flatMap instead of .map().flat()", | ||
messageId: "preferFlatMap", | ||
fix(fixer) { | ||
@@ -32,0 +35,0 @@ const [ |
@@ -18,3 +18,3 @@ /** | ||
docs: { | ||
description: "Prefer using .flat() over concating to flatten an array.", | ||
description: "Prefer using .flat() over concatenating to flatten an array.", | ||
recommended: true | ||
@@ -24,3 +24,6 @@ }, | ||
fixable: "code", | ||
type: "suggestion" | ||
type: "suggestion", | ||
messages: { | ||
preferFlat: "Use flat to flatten an array" | ||
} | ||
}, | ||
@@ -33,3 +36,3 @@ create(context) { | ||
node, | ||
message: "Use flat to flatten an array", | ||
messageId: "preferFlat", | ||
fix(fixer) { | ||
@@ -53,3 +56,3 @@ const sourceCode = context.getSourceCode(); | ||
node: node.parent.parent, | ||
message: "Use flat to flatten an array", | ||
messageId: "preferFlat", | ||
fix(fixer) { | ||
@@ -56,0 +59,0 @@ const sourceCode = context.getSourceCode(); |
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
32350
11
482
435