@putout/plugin-types 10.1.0 → 10.3.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
@@ -14,6 +14,7 @@ npm i putout @putout/plugin-types -D
14
14
  ## Rules
15
15
 
16
16
  - ✅ [apply-is-array](#apply-is-array);
17
+ - ✅ [apply-number](#apply-number);
17
18
  - ✅ [convert-typeof-to-is-type](#convert-typeof-to-is-type);
18
19
  - ✅ [declare](#declare);
19
20
  - ✅ [remove-double-negations](#remove-double-negations);
@@ -26,17 +27,86 @@ npm i putout @putout/plugin-types -D
26
27
  ```json
27
28
  {
28
29
  "rules": {
30
+ "types/apply-is-array": "on",
31
+ "types/apply-number": "on",
29
32
  "types/declare": "on",
30
33
  "types/convert-typeof-to-istype": "on",
31
34
  "types/remove-useless-conversion": "on",
32
35
  "types/remove-useless-constructor": "on",
33
36
  "types/remove-double-negations": "on",
34
- "types/remove-useless-typeof": "on",
35
- "types/apply-is-array": "on"
37
+ "types/remove-useless-typeof": "on"
36
38
  }
37
39
  }
38
40
  ```
39
41
 
42
+ ## apply-number
43
+
44
+ > The `Number` constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the `Number()` function
45
+ >
46
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
47
+
48
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/05ad2a403eb6d765a190297c492fd6d1/3cf396d108bbb4822317b6d6856db3a4c26fa3c1).
49
+
50
+ ### ❌ Example of incorrect code
51
+
52
+ ```js
53
+ const a = {
54
+ BooleanLiteral(node) {
55
+ return {
56
+ value: node.value ? 1 : 0,
57
+ };
58
+ },
59
+ };
60
+ ```
61
+
62
+ ### ✅ Example of correct code
63
+
64
+ ```js
65
+ const a = {
66
+ BooleanLiteral(node) {
67
+ return {
68
+ value: Number(node.value),
69
+ };
70
+ },
71
+ };
72
+ ```
73
+
74
+ ## apply-is-array
75
+
76
+ > The `Array.isArray()` method determines whether the passed value is an `Array`.
77
+ > When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
78
+
79
+ ### ❌ Example of incorrect code
80
+
81
+ ```js
82
+ x instanceof Array;
83
+ ```
84
+
85
+ ### ✅ Example of correct code
86
+
87
+ ```js
88
+ const {isArray} = Array;
89
+ isArray(x);
90
+ ```
91
+
92
+ In case of using `inline` option:
93
+
94
+ ```json
95
+ {
96
+ "rules": {
97
+ "types/apply-is-array": ["on", {
98
+ "inline": true
99
+ }]
100
+ }
101
+ }
102
+ ```
103
+
104
+ `Array.isArray` will be inlined:
105
+
106
+ ```js
107
+ Array.isArray(x);
108
+ ```
109
+
40
110
  ## declare
41
111
 
42
112
  Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare#putoutoperator-declare-).
@@ -57,6 +127,7 @@ Supported assertions:
57
127
  - `isArray`;
58
128
  - `isEmptyArray`;
59
129
  - `isError`;
130
+ - `isRegExp`;
60
131
 
61
132
  ### ❌ Example of incorrect code
62
133
 
@@ -189,52 +260,12 @@ typeof typeof 'hello';
189
260
  typeof 'hello';
190
261
  ```
191
262
 
192
- ## apply-is-array
193
-
194
- > The `Array.isArray()` method determines whether the passed value is an `Array`.
195
- > When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
196
-
197
- ### ❌ Example of incorrect code
198
-
199
- ```js
200
- x instanceof Array;
201
- ```
202
-
203
- ### ✅ Example of correct code
204
-
205
- ```js
206
- const {isArray} = Array;
207
- isArray(x);
208
- ```
209
-
210
- In case of using `inline` option:
211
-
212
- ```json
213
- {
214
- "rules": {
215
- "types/apply-is-array": ["on", {
216
- "inline": true
217
- }]
218
- }
219
- }
220
- ```
221
-
222
- `Array.isArray` will be inlined:
223
-
224
- ```js
225
- Array.isArray(x);
226
- ```
227
-
228
- ## License
229
-
230
- MIT
231
-
232
263
  ## Comparison
233
264
 
234
- Linter | Rule | Fix
235
- --------|-------|------------|
236
- 🐊 **Putout** | [`types`](https://github.com/coderaiser/putout/tree/master/packages/plugin-types#readme)| ✅
237
- ⏣ **ESLint** | [`no-implicit-coercion`](https://eslint.org/docs/rules/no-implicit-coercion) | ✅
265
+ | Linter | Rule | Fix |
266
+ |---------------|------|-----|
267
+ | 🐊 **Putout** | [`types`](https://github.com/coderaiser/putout/tree/master/packages/plugin-types#readme) | ✅ |
268
+ | ⏣ **ESLint** | [`no-implicit-coercion`](https://eslint.org/docs/rules/no-implicit-coercion) | ✅ |
238
269
 
239
270
  ## License
240
271
 
@@ -0,0 +1,5 @@
1
+ export const report = () => `Use 'Number()' instead of 'ternary expression'`;
2
+
3
+ export const replace = () => ({
4
+ '__a ? 1 : 0': 'Number(__a)',
5
+ });
@@ -39,6 +39,7 @@ const BODIES = {
39
39
  error: `__a instanceof Error`,
40
40
  array: `__a instanceof Array`,
41
41
  object: `__a instanceof Object`,
42
+ regexp: `__a instanceof RegExp`,
42
43
  equalUndefined: '__a === undefined',
43
44
  };
44
45
 
@@ -53,6 +54,7 @@ const NOT_BODIES = {
53
54
  error: `!(__a instanceof Error)`,
54
55
  array: `!(__a instanceof Array)`,
55
56
  object: `!(__a instanceof Object)`,
57
+ regexp: `!(__a instanceof RegExp)`,
56
58
  equalUndefined: '__a !== undefined',
57
59
  };
58
60
 
@@ -78,6 +80,7 @@ export const replace = () => ({
78
80
  [BODIES.error]: 'isError(__a)',
79
81
  [BODIES.array]: 'isArray(__a)',
80
82
  [BODIES.object]: 'isObject(__a)',
83
+ [BODIES.regexp]: 'isRegExp(__a)',
81
84
  [BODIES.equalUndefined]: 'isUndefined(__a)',
82
85
  [NOT_BODIES.function]: '!isFn(__a)',
83
86
  [NOT_BODIES.string]: '!isString(__a)',
@@ -89,6 +92,7 @@ export const replace = () => ({
89
92
  [NOT_BODIES.error]: '!isError(__a)',
90
93
  [NOT_BODIES.array]: '!isArray(__a)',
91
94
  [NOT_BODIES.object]: '!isObject(__a)',
95
+ [NOT_BODIES.regexp]: 'isRegExp(__a)',
92
96
  [NOT_BODIES.equalUndefined]: '!isUndefined(__a)',
93
97
  });
94
98
 
@@ -14,4 +14,5 @@ export const declare = () => ({
14
14
  isArray: 'const {isArray} = Array',
15
15
  isEmptyArray: 'const isEmptyArray = (a) => isArray(a) && !a.length',
16
16
  isError: 'const isError = (a) => a instanceof Error',
17
+ isRegExp: 'const isRegExp = (a) => a instanceof RegExp',
17
18
  });
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as applyNumber from './apply-number/index.js';
1
2
  import * as applyIsArray from './apply-is-array/index.js';
2
3
  import * as declare from './declare/index.js';
3
4
  import * as convertTypeofToIsType from './convert-typeof-to-is-type/index.js';
@@ -14,4 +15,5 @@ export const rules = {
14
15
  'remove-useless-constructor': removeUselessConstructor,
15
16
  'remove-double-negations': removeDoubleNegations,
16
17
  'remove-useless-typeof': removeUselessTypeof,
18
+ 'apply-number': applyNumber,
17
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-types",
3
- "version": "10.1.0",
3
+ "version": "10.3.0",
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",
@@ -36,10 +36,9 @@
36
36
  "@putout/plugin-putout": "*",
37
37
  "@putout/test": "^15.0.0",
38
38
  "eslint": "^10.0.0",
39
- "eslint-plugin-n": "^17.0.0",
40
39
  "eslint-plugin-putout": "^31.0.0",
41
40
  "madrun": "^13.0.0",
42
- "montag": "^1.2.1",
41
+ "montag": "^2.0.0",
43
42
  "nodemon": "^3.0.1",
44
43
  "superc8": "^12.0.0"
45
44
  },