@putout/plugin-types 10.2.0 → 10.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
@@ -14,6 +14,8 @@ 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);
18
+ - ✅ [apply-boolean](#apply-boolean);
17
19
  - ✅ [convert-typeof-to-is-type](#convert-typeof-to-is-type);
18
20
  - ✅ [declare](#declare);
19
21
  - ✅ [remove-double-negations](#remove-double-negations);
@@ -26,17 +28,107 @@ npm i putout @putout/plugin-types -D
26
28
  ```json
27
29
  {
28
30
  "rules": {
31
+ "types/apply-boolean": "on",
32
+ "types/apply-is-array": "on",
33
+ "types/apply-number": "on",
29
34
  "types/declare": "on",
30
35
  "types/convert-typeof-to-istype": "on",
31
36
  "types/remove-useless-conversion": "on",
32
37
  "types/remove-useless-constructor": "on",
33
38
  "types/remove-double-negations": "on",
34
- "types/remove-useless-typeof": "on",
35
- "types/apply-is-array": "on"
39
+ "types/remove-useless-typeof": "on"
36
40
  }
37
41
  }
38
42
  ```
39
43
 
44
+ ## apply-boolean
45
+
46
+ > The `Boolean()` constructor creates `Boolean` objects. When called as a function, it returns primitive values of type `Boolean`.
47
+ >
48
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
49
+
50
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/150f85ffc72f6f84249e75f38e21c081/d1588cd27f8854232cb08a4656bf238412501ef9).
51
+
52
+ ### ❌ Example of incorrect code
53
+
54
+ ```js
55
+ !!(a || b);
56
+ ```
57
+
58
+ ### ✅ Example of correct code
59
+
60
+ ```js
61
+ Boolean(a || b);
62
+ ```
63
+
64
+ ## apply-number
65
+
66
+ > The `Number` constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the `Number()` function
67
+ >
68
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)
69
+
70
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/05ad2a403eb6d765a190297c492fd6d1/3cf396d108bbb4822317b6d6856db3a4c26fa3c1).
71
+
72
+ ### ❌ Example of incorrect code
73
+
74
+ ```js
75
+ const a = {
76
+ BooleanLiteral(node) {
77
+ return {
78
+ value: node.value ? 1 : 0,
79
+ };
80
+ },
81
+ };
82
+ ```
83
+
84
+ ### ✅ Example of correct code
85
+
86
+ ```js
87
+ const a = {
88
+ BooleanLiteral(node) {
89
+ return {
90
+ value: Number(node.value),
91
+ };
92
+ },
93
+ };
94
+ ```
95
+
96
+ ## apply-is-array
97
+
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`.
100
+
101
+ ### ❌ Example of incorrect code
102
+
103
+ ```js
104
+ x instanceof Array;
105
+ ```
106
+
107
+ ### ✅ Example of correct code
108
+
109
+ ```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:
127
+
128
+ ```js
129
+ Array.isArray(x);
130
+ ```
131
+
40
132
  ## declare
41
133
 
42
134
  Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare#putoutoperator-declare-).
@@ -190,52 +282,12 @@ typeof typeof 'hello';
190
282
  typeof 'hello';
191
283
  ```
192
284
 
193
- ## apply-is-array
194
-
195
- > The `Array.isArray()` method determines whether the passed value is an `Array`.
196
- > When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
197
-
198
- ### ❌ Example of incorrect code
199
-
200
- ```js
201
- x instanceof Array;
202
- ```
203
-
204
- ### ✅ Example of correct code
205
-
206
- ```js
207
- const {isArray} = Array;
208
- isArray(x);
209
- ```
210
-
211
- In case of using `inline` option:
212
-
213
- ```json
214
- {
215
- "rules": {
216
- "types/apply-is-array": ["on", {
217
- "inline": true
218
- }]
219
- }
220
- }
221
- ```
222
-
223
- `Array.isArray` will be inlined:
224
-
225
- ```js
226
- Array.isArray(x);
227
- ```
228
-
229
- ## License
230
-
231
- MIT
232
-
233
285
  ## Comparison
234
286
 
235
- Linter | Rule | Fix
236
- --------|-------|------------|
237
- 🐊 **Putout** | [`types`](https://github.com/coderaiser/putout/tree/master/packages/plugin-types#readme)| ✅
238
- ⏣ **ESLint** | [`no-implicit-coercion`](https://eslint.org/docs/rules/no-implicit-coercion) | ✅
287
+ | Linter | Rule | Fix |
288
+ |---------------|------|-----|
289
+ | 🐊 **Putout** | [`types`](https://github.com/coderaiser/putout/tree/master/packages/plugin-types#readme) | ✅ |
290
+ | ⏣ **ESLint** | [`no-implicit-coercion`](https://eslint.org/docs/rules/no-implicit-coercion) | ✅ |
239
291
 
240
292
  ## License
241
293
 
@@ -0,0 +1,14 @@
1
+ import {operator} from 'putout';
2
+
3
+ const {removeParens} = operator;
4
+
5
+ export const report = () => `Use 'Boolean' instead of '!!'`;
6
+
7
+ export const replace = () => ({
8
+ '!!__a': (vars, path) => {
9
+ const __aPath = path.get('argument.argument');
10
+ removeParens(__aPath);
11
+
12
+ return 'Boolean(__a)';
13
+ },
14
+ });
@@ -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
+ });
package/lib/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import * as applyBoolean from './apply-boolean/index.js';
2
+ import * as applyNumber from './apply-number/index.js';
1
3
  import * as applyIsArray from './apply-is-array/index.js';
2
4
  import * as declare from './declare/index.js';
3
5
  import * as convertTypeofToIsType from './convert-typeof-to-is-type/index.js';
@@ -14,4 +16,6 @@ export const rules = {
14
16
  'remove-useless-constructor': removeUselessConstructor,
15
17
  'remove-double-negations': removeDoubleNegations,
16
18
  'remove-useless-typeof': removeUselessTypeof,
19
+ 'apply-number': applyNumber,
20
+ 'apply-boolean': applyBoolean,
17
21
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-types",
3
- "version": "10.2.0",
3
+ "version": "10.4.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
  },