@putout/plugin-types 1.3.0 → 1.5.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,7 +20,8 @@ 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
  ```
@@ -33,6 +34,7 @@ Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/
33
34
  Supported assertions:
34
35
 
35
36
  - `isString`;
37
+ - `isEmptyString`;
36
38
  - `isNumber`;
37
39
  - `isFn`;
38
40
  - `isBool`;
@@ -147,6 +149,46 @@ typeof typeof 'hello';
147
149
  typeof 'hello';
148
150
  ```
149
151
 
152
+ ### apply-is-array
153
+
154
+ > The `Array.isArray()` method determines whether the passed value is an `Array`.
155
+ > When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
156
+
157
+ #### ❌ Example of incorrect code
158
+
159
+ ```js
160
+ x instanceof Array;
161
+ ```
162
+
163
+ #### ✅ Example of correct code
164
+
165
+ ```js
166
+ const {isArray} = Array;
167
+ isArray(x);
168
+ ```
169
+
170
+ In case of using `inline` option:
171
+
172
+ ```json
173
+ {
174
+ "rules": {
175
+ "apply-is-array": ["on", {
176
+ "inline": true
177
+ }]
178
+ }
179
+ }
180
+ ```
181
+
182
+ `Array.isArray` will be inlined:
183
+
184
+ ```js
185
+ Array.isArray(x);
186
+ ```
187
+
188
+ ## License
189
+
190
+ MIT
191
+
150
192
  ## Comparison
151
193
 
152
194
  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
+
@@ -2,6 +2,7 @@
2
2
 
3
3
  module.exports.declare = () => ({
4
4
  isString: `const isString = (a) => typeof a === 'string'`,
5
+ isEmptyString: `const isEmptyString = (a) => !a && isString(a)`,
5
6
  isNumber: `const isNumber = (a) => typeof a === 'number'`,
6
7
  isFn: `const isFn = (a) => typeof a === 'function'`,
7
8
  isBool: `const isBool = (a) => typeof a === 'boolean'`,
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.5.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",
@@ -36,7 +36,7 @@
36
36
  "c8": "^7.5.0",
37
37
  "eslint": "^8.0.1",
38
38
  "eslint-plugin-n": "^15.2.4",
39
- "eslint-plugin-putout": "^16.0.0",
39
+ "eslint-plugin-putout": "^17.0.0",
40
40
  "lerna": "^6.0.1",
41
41
  "madrun": "^9.0.0",
42
42
  "montag": "^1.2.1",