|
4 | 4 |
|
5 | 5 | Rules for Array functions and methods.
|
6 | 6 |
|
| 7 | +## Contents |
| 8 | + - [Installation](#Installation) |
| 9 | + - [Rules](#Rules) |
| 10 | + - [`from-map`](#from-map) |
| 11 | + - [Examples](#Examples) |
| 12 | + - [`no-unnecessary-this-arg`](#no-unnecessary-this-arg) |
| 13 | + - [Checked Functions](#checked-functions) |
| 14 | + - [Checked Methods](#checked-methods) |
| 15 | + - [Examples](#Examples2) |
| 16 | + - [`prefer-array-from`](#prefer-array-from) |
| 17 | + - [Examples](#Examples3) |
| 18 | + - [`array-func/recommended` Configuration](#array-func-recommended-configuration) |
| 19 | + - [Using the Configuration](#using-the-configuration) |
| 20 | + - [License](#License) |
| 21 | + |
7 | 22 | ## Installation
|
8 | 23 |
|
9 | 24 | Install [ESLint](https://www.github.com/eslint/eslint) either locally or globally.
|
@@ -52,10 +67,10 @@ The `this` parameter is useless when providing arrow functions, since the `this`
|
52 | 67 |
|
53 | 68 | 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.
|
54 | 69 |
|
55 | | -#### Checked functions |
| 70 | +#### Checked Functions |
56 | 71 | - `from` (fixable)
|
57 | 72 |
|
58 | | -#### Checked methods |
| 73 | +#### Checked Methods |
59 | 74 | - `every`
|
60 | 75 | - `filter`
|
61 | 76 | - `find`
|
@@ -112,15 +127,40 @@ array.forEach(function(char) {
|
112 | 127 | array.filter(this.isGood, this);
|
113 | 128 | ```
|
114 | 129 |
|
| 130 | +### `prefer-array-from` |
| 131 | +Use `Array.from` instead of `[...iterable]` for performance benefits. |
| 132 | + |
| 133 | +This rule is auto fixable. |
| 134 | + |
| 135 | +#### Examples |
| 136 | +Code that triggers this rule: |
| 137 | +```js |
| 138 | +const iterable = [..."string"]; |
| 139 | + |
| 140 | +const arrayCopy = [...iterable]; |
| 141 | +``` |
| 142 | + |
| 143 | +Code that doesn't trigger this rule: |
| 144 | +```js |
| 145 | +const array = [1, 2, 3]; |
| 146 | + |
| 147 | +const extendedArray = [0, ...array]; |
| 148 | + |
| 149 | +const arrayCopy = Array.from(array); |
| 150 | + |
| 151 | +const characterArray = Array.from("string"); |
| 152 | +``` |
| 153 | + |
115 | 154 | ## `array-func/recommended` Configuration
|
116 | 155 | The recommended configuration will set your parser ECMA Version to 2015, since that's when the Array functions and methods were added.
|
117 | 156 |
|
118 | | -Rule | Error level |
119 | | ----- | ----------- |
120 | | -`from-map` | Error |
121 | | -`no-unnecessary-this-arg` | Error |
| 157 | +Rule | Error level | Fixable |
| 158 | +---- | ----------- | ------- |
| 159 | +`from-map` | Error | Yes |
| 160 | +`no-unnecessary-this-arg` | Error | Sometimes |
| 161 | +`prefer-array-from` | Error | Yes |
122 | 162 |
|
123 | | -### Using the configuration |
| 163 | +### Using the Configuration |
124 | 164 | To enable this configuration use the `extends` property in your `.eslintrc.json` config file (may look different for other config file styles):
|
125 | 165 | ```json
|
126 | 166 | {
|
|
0 commit comments