All Projects → freaktechnik → eslint-plugin-array-func

freaktechnik / eslint-plugin-array-func

Licence: MIT license
Rules for Array functions and methods.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to eslint-plugin-array-func

eslint-plugin-lodash-template
ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.
Stars: ✭ 15 (-80.52%)
Mutual labels:  eslint-plugin, eslintplugin
Typescript Eslint
✨ Monorepo for all the tooling which enables ESLint to support TypeScript
Stars: ✭ 10,831 (+13966.23%)
Mutual labels:  eslint-plugin, eslintplugin
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.
Stars: ✭ 19 (-75.32%)
Mutual labels:  eslint-plugin, eslintplugin
eslint-plugin-yml
This ESLint plugin provides linting rules for YAML.
Stars: ✭ 40 (-48.05%)
Mutual labels:  eslint-plugin, eslintplugin
eslint-plugin-aura
Salesforce Lightning (Aura) specific linting rules for ESLint
Stars: ✭ 24 (-68.83%)
Mutual labels:  eslint-plugin, eslintplugin
Eslint Import Resolver Alias
a simple Node behavior import resolution plugin for eslint-plugin-import, supporting module alias
Stars: ✭ 121 (+57.14%)
Mutual labels:  eslint-plugin
Eslint Import Resolver Babel Module
Custom eslint resolve for babel-plugin-module-resolver
Stars: ✭ 236 (+206.49%)
Mutual labels:  eslint-plugin
Eslint Plugin I18n Json
Fully extendable eslint plugin for JSON i18n translation files.
Stars: ✭ 101 (+31.17%)
Mutual labels:  eslint-plugin
Eslint Mdx
ESLint Parser/Plugin for MDX
Stars: ✭ 89 (+15.58%)
Mutual labels:  eslint-plugin
eslint-plugin-gridsome
ESLint plugin for Gridsome
Stars: ✭ 45 (-41.56%)
Mutual labels:  eslint-plugin
ultimate-hot-boilerplate
🚀 node-react universal app boilerplate with everything on hot reload, SSR, GraphQL, Flow included
Stars: ✭ 35 (-54.55%)
Mutual labels:  eslint-plugin
Eslint Plugin Ava
ESLint rules for AVA
Stars: ✭ 209 (+171.43%)
Mutual labels:  eslint-plugin
Eslint Plugin Unicorn
Various awesome ESLint rules
Stars: ✭ 2,157 (+2701.3%)
Mutual labels:  eslint-plugin
Eslint Plugin Ember
An ESlint plugin that provides set of rules for Ember Applications based on commonly known good practices.
Stars: ✭ 240 (+211.69%)
Mutual labels:  eslint-plugin
Eslint Plugin Css Modules
Project status: NOT MAINTAINED; Checks that you are using the existent css/scss classes, no more no less
Stars: ✭ 115 (+49.35%)
Mutual labels:  eslint-plugin
eslint-plugin-license-header
Rules to validate the presence of license headers in source files.
Stars: ✭ 21 (-72.73%)
Mutual labels:  eslint-plugin
Eslint Plugin Lodash
ESLint rules for lodash
Stars: ✭ 208 (+170.13%)
Mutual labels:  eslint-plugin
Eslint Plugin Mocha
ESLint rules for mocha
Stars: ✭ 249 (+223.38%)
Mutual labels:  eslint-plugin
You Dont Need Lodash Underscore
List of JavaScript methods which you can use natively + ESLint Plugin
Stars: ✭ 13,915 (+17971.43%)
Mutual labels:  eslint-plugin
Eslint Plugin Eslint Comments
Additional ESLint rules for directive comments of ESLint.
Stars: ✭ 221 (+187.01%)
Mutual labels:  eslint-plugin

eslint-plugin-array-func

codecov

Rules for Array functions and methods.

Contents

Installation

Install ESLint either locally or globally.

$ npm install -D eslint

If you installed ESLint globally, you have to install the array-func plugin globally too. Otherwise, install it locally.

$ npm install -D eslint-plugin-array-func

Rules

from-map

Prefer using the mapFn callback of Array.from over an immediate .map() call on the Array.from result.

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 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:

Array.from(iterable).map((t) => t.id);

Array.from(iterable, (t) => t.id).map((id) => id[0]);

Code that doesn't trigger this rule:

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);

Using the rule

To use this rule, your .eslintrc.json should at least contain the following (may look different for other config file styles):

{
  "plugins": [
    "array-func"
  ],
  "rules": {
    "array-func/from-map": "error"
  }
}

Alternatively you can use a configuration included with this plugin.

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

  • from (fixable)

Checked Methods

  • every
  • filter
  • find
  • findIndex
  • forEach
  • map
  • some

Examples

Code that triggers this rule:

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:

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.filter(this.isGood, this);

Using the rule

To use this rule, your .eslintrc.json should at least contain the following (may look different for other config file styles):

{
  "plugins": [
    "array-func"
  ],
  "rules": {
    "array-func/no-unnecessary-this-arg": "error"
  }
}

Alternatively you can use a configuration included with this plugin.

prefer-array-from

Use Array.from instead of [...iterable]. See from-map for additional benefits Array.from can provide over the spread syntax.

This rule is auto fixable.

Examples

Code that triggers this rule:

const iterable = [..."string"];

const arrayCopy = [...iterable];

Code that doesn't trigger this rule:

const array = [1, 2, 3];

const extendedArray =  [0, ...array];

const arrayCopy = Array.from(array);

const characterArray = Array.from("string");

Using the rule

To use this rule, your .eslintrc.json should at least contain the following (may look different for other config file styles):

{
  "plugins": [
    "array-func"
  ],
  "rules": {
    "array-func/prefer-array-from": "error"
  }
}

Alternatively you can use a configuration included with this plugin.

avoid-reverse

Avoid reversing the array and running a method on it if there is an equivalent of the method operating on the array from the other end.

There are two operations with such equivalents: reduce with reduceRight.

This rule is auto fixable.

Examples

Code that triggers this rule:

const string = array.reverse().reduce((p, c) => p + c, '');

const reverseString = array.reverse().reduceRight((p, c) => p + c, '');

Code that doesn't trigger this rule:

const reverseString = array.reduce((p, c) => p + c, '');

const string = array.reduceRight((p, c) => p + c, '');

const reverseArray = array.reverse();

const reverseMap = array.reverse().map((r) => r + 1);

Using the rule

To use this rule, your .eslintrc.json should at least contain the following (may look different for other config file styles):

{
  "plugins": [
    "array-func"
  ],
  "rules": {
    "array-func/avoid-reverse": "error"
  }
}

Alternatively you can use a configuration included with this plugin.

prefer-flat-map

Use .flatMap() to map and then flatten an array instead of using .map().flat().

This rule is auto fixable.

Examples

Code that triggers this rule:

const mappedAndFlattened = array.map((p) => p).flat();

const flatWithDefaultDepth = array.map((r) => r).flat(1);

Code that doesn't trigger this rule:

const oneAction = array.flatMap((m) => m);

const flattened = array.flat();

const mapped = array.map((r) => r + 1);

const flattenedThenMapped = array.flat().map((r) => r + 1);

const flatMappedWithExtra = array.map((r) => r + 1).reverse().flat();

const flatWithDepth = array.map((p) => p).flat(99);

Using the rule

To use this rule, your .eslintrc.json should at least contain the following (may look different for other config file styles):

{
  "plugins": [
    "array-func"
  ],
  "rules": {
    "array-func/prefer-flat-map": "error"
  }
}

Alternatively you can use a configuration included with this plugin.

prefer-flat

Use .flat() to flatten an array of arrays. This rule currently recognizes two patterns and can replace them with a .flat() call:

  • [].concat(...array)
  • array.reduce((p, n) => p.concat(n), [])

This rule is auto fixable.

Examples

Code that triggers this rule:

const concatFlat = [].concat(...array);

const reduceFlat = array.reduce((p, n) => p.concat(n), []);

Code that doesn't trigger this rule:

const flattened = array.flat();

const reverseFlat = array.reduce((p, n) => n.concat(p), []);

const otherReduce = array.reduce((p, n) => n + p, 0);

Using the rule

To use this rule, your .eslintrc.json should at least contain the following (may look different for other config file styles):

{
  "plugins": [
    "array-func"
  ],
  "rules": {
    "array-func/prefer-flat": "error"
  }
}

Alternatively you can use a configuration included with this plugin.

Configurations

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 Fixable
array-func/from-map Error Yes
array-func/no-unnecessary-this-arg Error Sometimes
array-func/prefer-array-from Error Yes
array-func/avoid-reverse Error Yes

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):

{
  "extends": [
    "plugin:array-func/recommended"
  ]
}

array-func/all Configuration

The recommended configuration does not include all rules, since some Array methods were added after ES2015. The all configuration enables all rules the plugin contains and sets the ECMA version appropriately.

Rule Error level Fixable
array-func/from-map Error Yes
array-func/no-unnecessary-this-arg Error Sometimes
array-func/prefer-array-from Error Yes
array-func/avoid-reverse Error Yes
array-func/prefer-flat-map Error Yes
array-func/prefer-flat Error Yes

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):

{
  "extends": [
    "plugin:array-func/all"
  ]
}

License

The array-func plugin is licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].