@putout/plugin-types 10.5.0 → 10.6.1

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
@@ -13,14 +13,15 @@ npm i putout @putout/plugin-types -D
13
13
 
14
14
  ## Rules
15
15
 
16
+ - ✅ [apply-boolean](#apply-boolean);
16
17
  - ✅ [apply-is-array](#apply-is-array);
18
+ - ✅ [apply-is-nan](#apply-is-nan);
17
19
  - ✅ [apply-number](#apply-number);
18
- - ✅ [apply-boolean](#apply-boolean);
19
20
  - ✅ [convert-typeof-to-is-type](#convert-typeof-to-is-type);
20
21
  - ✅ [declare](#declare);
21
22
  - ✅ [remove-double-negations](#remove-double-negations);
22
- - ✅ [remove-useless-conversion](#remove-useless-conversion);
23
23
  - ✅ [remove-useless-constructor](#remove-useless-constructor);
24
+ - ✅ [remove-useless-conversion](#remove-useless-conversion);
24
25
  - ✅ [remove-useless-typeof](#remove-useless-typeof);
25
26
 
26
27
  ## Config
@@ -29,13 +30,18 @@ npm i putout @putout/plugin-types -D
29
30
  {
30
31
  "rules": {
31
32
  "types/apply-boolean": "on",
33
+
32
34
  "types/apply-is-array": "on",
35
+ "types/apply-is-nan": "on",
33
36
  "types/apply-number": "on",
34
- "types/declare": "on",
37
+
35
38
  "types/convert-typeof-to-istype": "on",
36
- "types/remove-useless-conversion": "on",
37
- "types/remove-useless-constructor": "on",
39
+ "types/declare": "on",
40
+
38
41
  "types/remove-double-negations": "on",
42
+ "types/remove-useless-constructor": "on",
43
+ "types/remove-useless-conversion": "on",
44
+
39
45
  "types/remove-useless-typeof": "on"
40
46
  }
41
47
  }
@@ -61,6 +67,66 @@ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/150f85ffc7
61
67
  Boolean(a || b);
62
68
  ```
63
69
 
70
+ ## apply-is-array
71
+
72
+ > The `Array.isArray()` method determines whether the passed value is an `Array`.
73
+ > When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
74
+ >
75
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
76
+
77
+ ### ❌ Example of incorrect code
78
+
79
+ ```js
80
+ x instanceof Array;
81
+ ```
82
+
83
+ ### ✅ Example of correct code
84
+
85
+ ```js
86
+ const {isArray} = Array;
87
+ isArray(x);
88
+ ```
89
+
90
+ In case of using `inline` option:
91
+
92
+ ```json
93
+ {
94
+ "rules": {
95
+ "types/apply-is-array": ["on", {
96
+ "inline": true
97
+ }]
98
+ }
99
+ }
100
+ ```
101
+
102
+ `Array.isArray` will be inlined:
103
+
104
+ ```js
105
+ Array.isArray(x);
106
+ ```
107
+
108
+ ## apply-is-nan
109
+
110
+ > The `NaN` global property is a value representing Not-A-Number: `NaN !== NaN`.
111
+ >
112
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN)
113
+
114
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/e8ff80a3f522626c2d049b771692ced1/7ea6eb75d7d39f20654753fc2d800f7d881abaca)
115
+
116
+ ### ❌ Example of incorrect code
117
+
118
+ ```js
119
+ if (a === NaN)
120
+ console.log();
121
+ ```
122
+
123
+ ### ✅ Example of correct code
124
+
125
+ ```js
126
+ if (Number.isNaN(a))
127
+ console.log();
128
+ ```
129
+
64
130
  ## apply-number
65
131
 
66
132
  > The `Number` constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the `Number()` function
@@ -93,40 +159,26 @@ const a = {
93
159
  };
94
160
  ```
95
161
 
96
- ## apply-is-array
162
+ ## convert-typeof-to-is-type
97
163
 
98
- > The `Array.isArray()` method determines whether the passed value is an `Array`.
99
- > When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
164
+ > The `typeof` operator returns a string indicating the type of the unevaluated operand.
165
+ >
166
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
100
167
 
101
168
  ### ❌ Example of incorrect code
102
169
 
103
170
  ```js
104
- x instanceof Array;
171
+ if (typeof a === 'boolean')
172
+ return x;
105
173
  ```
106
174
 
107
175
  ### ✅ Example of correct code
108
176
 
109
177
  ```js
110
- const {isArray} = Array;
111
- isArray(x);
112
- ```
113
-
114
- In case of using `inline` option:
115
-
116
- ```json
117
- {
118
- "rules": {
119
- "types/apply-is-array": ["on", {
120
- "inline": true
121
- }]
122
- }
123
- }
124
- ```
125
-
126
- `Array.isArray` will be inlined:
178
+ const isBool = (a) => typeof a === 'boolean';
127
179
 
128
- ```js
129
- Array.isArray(x);
180
+ if (isBool(a))
181
+ return x;
130
182
  ```
131
183
 
132
184
  ## declare
@@ -181,41 +233,26 @@ When you want to skip some declaration use `dismiss`:
181
233
  }
182
234
  ```
183
235
 
184
- ## convert-typeof-to-is-type
236
+ ## remove-double-negations
185
237
 
186
- > The `typeof` operator returns a string indicating the type of the unevaluated operand.
238
+ > It is possible to use a couple of **NOT** operators (`!!`) in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value.
187
239
  >
188
- > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
189
-
190
- ### ❌ Example of incorrect code
191
-
192
- ```js
193
- if (typeof a === 'boolean')
194
- return x;
195
- ```
196
-
197
- ### ✅ Example of correct code
198
-
199
- ```js
200
- const isBool = (a) => typeof a === 'boolean';
201
-
202
- if (isBool(a))
203
- return x;
204
- ```
205
-
206
- ## remove-useless-conversion
240
+ > The same conversion can be done through the `Boolean` function.
241
+ >
242
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT)
207
243
 
208
244
  ### ❌ Example of incorrect code
209
245
 
210
246
  ```js
211
- const a = !![1].includes(1);
212
- const b = Boolean([1].includes(1));
247
+ if (!!a)
248
+ console.log('hi');
213
249
  ```
214
250
 
215
251
  ### ✅ Example of correct code
216
252
 
217
253
  ```js
218
- const a = [1].includes(1);
254
+ if (a)
255
+ console.log('hi');
219
256
  ```
220
257
 
221
258
  ## remove-useless-constructor
@@ -242,26 +279,19 @@ const b = false;
242
279
  const n = 5;
243
280
  ```
244
281
 
245
- ## remove-double-negations
246
-
247
- > It is possible to use a couple of **NOT** operators (`!!`) in series to explicitly force the conversion of any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value.
248
- >
249
- > The same conversion can be done through the `Boolean` function.
250
- >
251
- > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT)
282
+ ## remove-useless-conversion
252
283
 
253
284
  ### ❌ Example of incorrect code
254
285
 
255
286
  ```js
256
- if (!!a)
257
- console.log('hi');
287
+ const a = !![1].includes(1);
288
+ const b = Boolean([1].includes(1));
258
289
  ```
259
290
 
260
291
  ### ✅ Example of correct code
261
292
 
262
293
  ```js
263
- if (a)
264
- console.log('hi');
294
+ const a = [1].includes(1);
265
295
  ```
266
296
 
267
297
  ## remove-useless-typeof
@@ -0,0 +1,5 @@
1
+ export const report = () => `Use 'isNaN' instead of comparing`;
2
+
3
+ export const replace = () => ({
4
+ '__a === NaN': 'Number.isNaN(__a)',
5
+ });
@@ -92,7 +92,7 @@ export const replace = () => ({
92
92
  [NOT_BODIES.error]: '!isError(__a)',
93
93
  [NOT_BODIES.array]: '!isArray(__a)',
94
94
  [NOT_BODIES.object]: '!isObject(__a)',
95
- [NOT_BODIES.regexp]: 'isRegExp(__a)',
95
+ [NOT_BODIES.regexp]: '!isRegExp(__a)',
96
96
  [NOT_BODIES.equalUndefined]: '!isUndefined(__a)',
97
97
  });
98
98
 
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as applyIsNan from './apply-is-nan/index.js';
1
2
  import * as applyBoolean from './apply-boolean/index.js';
2
3
  import * as applyNumber from './apply-number/index.js';
3
4
  import * as applyIsArray from './apply-is-array/index.js';
@@ -18,4 +19,5 @@ export const rules = {
18
19
  'remove-useless-typeof': removeUselessTypeof,
19
20
  'apply-number': applyNumber,
20
21
  'apply-boolean': applyBoolean,
22
+ 'apply-is-nan': applyIsNan,
21
23
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-types",
3
- "version": "10.5.0",
3
+ "version": "10.6.1",
4
4
  "type": "module",
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",