Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rules for Array functions and methods.

License

NotificationsYou must be signed in to change notification settings

freaktechnik/eslint-plugin-array-func

Repository files navigation

codecov

Rules for Array functions and methods.

Contents

Installation

InstallESLint either locally or globally.

$ npm install -D eslint

If you installedESLint globally, you have to install thearray-func plugin globally too. Otherwise, install it locally.

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

Rules

from-map

Prefer using themapFn callback ofArray.from over an immediate.map() call on theArray.from result.

Array.from has amapFn 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 ofArray.from avoids an iteration. See alsoMDN for an explanation of the potential benefits of using the mapping callback ofArray.from directly.

This rule is auto fixable. It will produce nested function calls if you use theArray.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);constarr=Array.from(iterable);constmappedArray=arr.map((t)=>t.id);

Using the rule

To use this rule, youreslint.config.js should at least contain the following:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/from-map":"error"}}];

Alternatively you can use aconfiguration included with this plugin.

no-unnecessary-this-arg

Avoid thethis parameter when providing arrow function as callback in array functions.

Thethis parameter is useless when providing arrow functions, since thethis 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:

constarray=Array.from("example",(char)=>char.charCodeAt(0),this);conste=array.find((char)=>char===101,this);constexampleAsArray=array.map((char)=>String.fromCharCode(char),this);consteIndex=array.findIndex((char)=>char===101,this);constcontainsE=array.some((char)=>char===101,this);constisOnlyE=array.every((char)=>char===101,this);constonlyEs=array.filter((char)=>char===101,this);array.forEach((char)=>console.log(char),this);

Code that doesn't trigger this rule:

constarray=Array.from("example",(char)=>char.charCodeAt(0));constalternateArray=Array.from("example",function(char){returnchar.charCodeAt(this)},0);conste=array.find((char)=>char===101);constexampleAsArray=array.map((char)=>String.fromCharCode(char));consteIndex=array.findIndex((char)=>char===101);constcontainsE=array.some((char)=>char===101);constisOnlyE=array.every((char)=>char===101);constonlyEs=array.filter(function(char){returnchar===this},101);array.forEach(function(char){this.log(char);},console);array.filter(this.isGood,this);

Using the rule

To use this rule, youreslint.config.js should at least contain the following:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/no-unnecessary-this-arg":"error"}}];

Alternatively you can use aconfiguration included with this plugin.

prefer-array-from

UseArray.from instead of[...iterable].Seefrom-map for additional benefitsArray.from can provide over the spread syntax.

This rule is auto fixable.

Examples

Code that triggers this rule:

constiterable=[..."string"];constarrayCopy=[...iterable];

Code that doesn't trigger this rule:

constarray=[1,2,3];constextendedArray=[0, ...array];constarrayCopy=Array.from(array);constcharacterArray=Array.from("string");

Using the rule

To use this rule, youreslint.config.js should at least contain the following:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/prefer-array-from":"error"}}];

Alternatively you can use aconfiguration included with this plugin.

avoid-reverse

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

There are two operations with such equivalents:reduce withreduceRight.

This rule is auto fixable.

Examples

Code that triggers this rule:

conststring=array.reverse().reduce((p,c)=>p+c,'');constreverseString=array.reverse().reduceRight((p,c)=>p+c,'');

Code that doesn't trigger this rule:

constreverseString=array.reduce((p,c)=>p+c,'');conststring=array.reduceRight((p,c)=>p+c,'');constreverseArray=array.reverse();constreverseMap=array.reverse().map((r)=>r+1);

Using the rule

To use this rule, youreslint.config.js should at least contain the following:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/avoid-reverse":"error"}}];

Alternatively you can use aconfiguration 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:

constmappedAndFlattened=array.map((p)=>p).flat();constflatWithDefaultDepth=array.map((r)=>r).flat(1);

Code that doesn't trigger this rule:

constoneAction=array.flatMap((m)=>m);constflattened=array.flat();constmapped=array.map((r)=>r+1);constflattenedThenMapped=array.flat().map((r)=>r+1);constflatMappedWithExtra=array.map((r)=>r+1).reverse().flat();constflatWithDepth=array.map((p)=>p).flat(99);

Using the rule

To use this rule, youreslint.config.js should at least contain the following:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/prefer-flat-map":"error"}}];

Alternatively you can use aconfiguration included with this plugin.

prefer-flat

Use.flat() to flatten an array of arrays. This rule currently recognizes twopatterns 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:

constconcatFlat=[].concat(...array);constreduceFlat=array.reduce((p,n)=>p.concat(n),[]);

Code that doesn't trigger this rule:

constflattened=array.flat();constreverseFlat=array.reduce((p,n)=>n.concat(p),[]);constotherReduce=array.reduce((p,n)=>n+p,0);

Using the rule

To use this rule, youreslint.config.js should at least contain the following:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[{plugins:{"array-func":arrayFunc},rules:{"array-func/prefer-flat":"error"}}];

Alternatively you can use aconfiguration included with this plugin.

Configurations

recommended Configuration

RuleError levelFixable
array-func/from-mapErrorYes
array-func/no-unnecessary-this-argErrorSometimes
array-func/prefer-array-fromErrorYes
array-func/avoid-reverseErrorYes

Using the Configuration

To enable this configuration, import the plugin and add the config to your eslint config array:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[arrayFunc.configs.recommended,];

all Configuration

The recommended configuration does not include all rules, since some Array methodswere added after ES2015. The all configuration enables all rules the plugincontainsy.

RuleError levelFixable
array-func/from-mapErrorYes
array-func/no-unnecessary-this-argErrorSometimes
array-func/prefer-array-fromErrorYes
array-func/avoid-reverseErrorYes
array-func/prefer-flat-mapErrorYes
array-func/prefer-flatErrorYes

Using the Configuration

To enable this configuration, import the plugin and add the config to your eslint config array:

importarrayFuncfrom"eslint-plugin-array-func";exportdefault[arrayFunc.configs.all,];

License

Thearray-func plugin is licensed under theMIT License.

About

Rules for Array functions and methods.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors12


[8]ページ先頭

©2009-2025 Movatter.jp