@putout/plugin-types 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,11 +20,11 @@ npm i putout @putout/plugin-types -D
20
20
  "types/convert-typeof-to-istype": "on",
21
21
  "types/remove-useless-conversion": "on",
22
22
  "types/remove-double-negations": "on",
23
- "types/remove-useless-typeof": "on"
23
+ "types/remove-useless-typeof": "on",
24
+ "types/apply-is-array": "on"
24
25
  }
25
26
  }
26
27
  ```
27
-
28
28
  ## Rules
29
29
 
30
30
  ### declare
@@ -147,6 +147,47 @@ typeof typeof 'hello';
147
147
  typeof 'hello';
148
148
  ```
149
149
 
150
+ ### apply-is-array
151
+
152
+ > The `Array.isArray()` method determines whether the passed value is an `Array`.
153
+ > When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
154
+
155
+ #### ❌ Example of incorrect code
156
+
157
+ ```js
158
+ x instanceof Array;
159
+ ```
160
+
161
+ #### ✅ Example of correct code
162
+
163
+ ```js
164
+ const {isArray} = Array;
165
+ isArray(x);
166
+ ```
167
+
168
+ In case of using `inline` option:
169
+
170
+ ```json
171
+ {
172
+ "rules": {
173
+ "apply-is-array": ["on", {
174
+ "inline": true
175
+ }]
176
+ }
177
+ }
178
+ ```
179
+
180
+ `Array.isArray` will be inlined:
181
+
182
+ ```js
183
+ Array.isArray(x);
184
+ ```
185
+
186
+ ## License
187
+
188
+ MIT
189
+
190
+
150
191
  ## Comparison
151
192
 
152
193
  Linter | Rule | Fix
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Use 'Array.isArray()' instead of 'instanceof'`;
4
+
5
+ module.exports.replace = ({options}) => {
6
+ const {inline} = options;
7
+
8
+ return {
9
+ '__a instanceof Array': () => {
10
+ if (inline)
11
+ return 'Array.isArray(__a)';
12
+
13
+ return 'isArray(__a)';
14
+ },
15
+ };
16
+ };
17
+
package/lib/index.js CHANGED
@@ -5,6 +5,7 @@ const getRule = (a) => ({
5
5
  });
6
6
 
7
7
  module.exports.rules = {
8
+ ...getRule('apply-is-array'),
8
9
  ...getRule('declare'),
9
10
  ...getRule('convert-typeof-to-is-type'),
10
11
  ...getRule('remove-useless-conversion'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-types",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin adds ability to transform code related to types assertions",