Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-array-func

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-array-func - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

4

package.json
{
"name": "eslint-plugin-array-func",
"version": "1.0.0",
"version": "1.0.1",
"description": "Rules dealing with Array functions and methods.",

@@ -19,3 +19,3 @@ "main": "index.js",

"eslint-plugin-eslint-plugin": "^1.2.0",
"eslint-plugin-freaktechnik": "^3.0.11",
"eslint-plugin-freaktechnik": "^4.0.0",
"nyc": "^11.2.1"

@@ -22,0 +22,0 @@ },

@@ -12,7 +12,31 @@ # eslint-plugin-array-func

`Array.from` has a `mapFn` callback that lets you map the items of the iterable to an array like you would with `.map()` except that values have not yet been truncated to fit types allowed in an array. Some iterables can't be directly converted to an array and thus have to be iterated either way. In that case using the mapping callback of `Array.from` avoids an iteration. See also [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Description) for an explanation of the potential benefits of using the mapping callback of `Array.from` directly.
This rule is auto fixable. It will produce nested function calls if you use the `Array.from` map callback and have a `.map()` call following it.
#### Examples
Code that triggers this rule:
```js
Array.from(iterable).map((t) => t.id);
Array.from(iterable, (t) => t.id).map((id) => id[0]);
```
Code that doesn't trigger this rule:
```js
Array.from(iterable, (t) => t.id);
Array.from(iterable, function(t) { this.format(t); }, this);
const arr = Array.from(iterable);
const mappedArray = arr.map((t) => t.id);
```
### `no-unnecessary-this-arg`
Avoid the `this` parameter when providing arrow function as callback in array functions.
The `this` parameter is useless when providing arrow functions, since the `this` of arrow functions can not be rebound, thus the parameter has no effect.
The fix is usually to omit the parameter. The Array methods can't be auto-fixed, since the detection of array methods is not confident enough to know that the method is being called on an array.
#### Checked functions

@@ -30,2 +54,48 @@ - `from` (fixable)

#### Examples
Code that triggers this rule:
```js
const array = Array.from("example", (char) => char.charCodeAt(0), this);
const e = array.find((char) => char === 101, this);
const exampleAsArray = array.map((char) => String.fromCharCode(char), this);
const eIndex = array.findIndex((char) => char === 101, this);
const containsE = array.some((char) => char === 101, this);
const isOnlyE = array.every((char) => char === 101, this);
const onlyEs = array.filter((char) => char === 101, this);
array.forEach((char) => console.log(char), this);
```
Code that doesn't trigger this rule:
```js
const array = Array.from("example", (char) => char.charCodeAt(0));
const alternateArray = Array.from("example", function(char) {
return char.charCodeAt(this)
}, 0);
const e = array.find((char) => char === 101);
const exampleAsArray = array.map((char) => String.fromCharCode(char));
const eIndex = array.findIndex((char) => char === 101);
const containsE = array.some((char) => char === 101);
const isOnlyE = array.every((char) => char === 101);
const onlyEs = array.filter(function(char) {
return char === this
}, 101);
array.forEach(function(char) {
this.log(char);
}, console);
```
## `array-func/recommended` Configuration

@@ -32,0 +102,0 @@ The recommended configuration will set your parser ECMA Version to 2015, since that's when the Array functions and methods were added.

@@ -54,2 +54,3 @@ /**

] = parent.arguments,
// Get the params names from the callback that has the most params (since the signature is identical).
params = callback.params.length > mapCallback.params.length ? callback.params : mapCallback.params,

@@ -66,2 +67,3 @@ paramString = params.map((p) => p.name).join(PARAM_SEPARATOR),

// Try to use an arrow function for the wrapping function, fall back to a function expression if a this is specified.
let functionStart = `(${paramString}) => `,

@@ -80,2 +82,3 @@ functionEnd = "",

}
// The original map callback from Array.from gets nested as a parameter to the callback from map.
const lastCallback = getCallback(mapCallback, mapThisArg, `${firstCallback}${restParamString}`),

@@ -82,0 +85,0 @@ restParams = sourceCode.getText().substring(callback.end, parent.end);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc