@putout/plugin-types 4.0.0 → 4.1.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 +28 -21
- package/lib/remove-useless-conversion/index.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,7 +11,16 @@
|
|
|
11
11
|
npm i putout @putout/plugin-types -D
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
##
|
|
14
|
+
## Rules
|
|
15
|
+
|
|
16
|
+
- ✅ [apply-is-array](#apply-is-array);
|
|
17
|
+
- ✅ [convert-typeof-to-is-type](#convert-typeof-to-is-type);
|
|
18
|
+
- ✅ [declare](#declare);
|
|
19
|
+
- ✅ [remove-double-negations](#remove-double-negations);
|
|
20
|
+
- ✅ [remove-useless-conversion](#remove-useless-conversion);
|
|
21
|
+
- ✅ [remove-useless-typeof](#remove-useless-typeof);
|
|
22
|
+
|
|
23
|
+
## Config
|
|
15
24
|
|
|
16
25
|
```json
|
|
17
26
|
{
|
|
@@ -26,9 +35,7 @@ npm i putout @putout/plugin-types -D
|
|
|
26
35
|
}
|
|
27
36
|
```
|
|
28
37
|
|
|
29
|
-
##
|
|
30
|
-
|
|
31
|
-
### declare
|
|
38
|
+
## declare
|
|
32
39
|
|
|
33
40
|
Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare#putoutoperator-declare-).
|
|
34
41
|
Supported assertions:
|
|
@@ -46,13 +53,13 @@ Supported assertions:
|
|
|
46
53
|
- `isArray`;
|
|
47
54
|
- `isEmptyArray`;
|
|
48
55
|
|
|
49
|
-
|
|
56
|
+
### ❌ Example of incorrect code
|
|
50
57
|
|
|
51
58
|
```js
|
|
52
59
|
isString('hello');
|
|
53
60
|
```
|
|
54
61
|
|
|
55
|
-
|
|
62
|
+
### ✅ Example of correct code
|
|
56
63
|
|
|
57
64
|
```js
|
|
58
65
|
const isString = (a) => typeof a === 'string';
|
|
@@ -71,20 +78,20 @@ When you want to skip some declaration use `dismiss`:
|
|
|
71
78
|
}
|
|
72
79
|
```
|
|
73
80
|
|
|
74
|
-
|
|
81
|
+
## convert-typeof-to-is-type
|
|
75
82
|
|
|
76
83
|
> The `typeof` operator returns a string indicating the type of the unevaluated operand.
|
|
77
84
|
>
|
|
78
85
|
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
79
86
|
|
|
80
|
-
|
|
87
|
+
### ❌ Example of incorrect code
|
|
81
88
|
|
|
82
89
|
```js
|
|
83
90
|
if (typeof a === 'boolean')
|
|
84
91
|
return x;
|
|
85
92
|
```
|
|
86
93
|
|
|
87
|
-
|
|
94
|
+
### ✅ Example of correct code
|
|
88
95
|
|
|
89
96
|
```js
|
|
90
97
|
const isBool = (a) => typeof a === 'boolean';
|
|
@@ -93,22 +100,22 @@ if (isBool(a))
|
|
|
93
100
|
return x;
|
|
94
101
|
```
|
|
95
102
|
|
|
96
|
-
|
|
103
|
+
## remove-useless-conversion
|
|
97
104
|
|
|
98
|
-
|
|
105
|
+
### ❌ Example of incorrect code
|
|
99
106
|
|
|
100
107
|
```js
|
|
101
108
|
const a = !![1].includes(1);
|
|
102
109
|
const b = Boolean([1].includes(1));
|
|
103
110
|
```
|
|
104
111
|
|
|
105
|
-
|
|
112
|
+
### ✅ Example of correct code
|
|
106
113
|
|
|
107
114
|
```js
|
|
108
115
|
const a = [1].includes(1);
|
|
109
116
|
```
|
|
110
117
|
|
|
111
|
-
|
|
118
|
+
## remove-double-negations
|
|
112
119
|
|
|
113
120
|
> 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.
|
|
114
121
|
>
|
|
@@ -116,50 +123,50 @@ const a = [1].includes(1);
|
|
|
116
123
|
>
|
|
117
124
|
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT)
|
|
118
125
|
|
|
119
|
-
|
|
126
|
+
### ❌ Example of incorrect code
|
|
120
127
|
|
|
121
128
|
```js
|
|
122
129
|
if (!!a)
|
|
123
130
|
console.log('hi');
|
|
124
131
|
```
|
|
125
132
|
|
|
126
|
-
|
|
133
|
+
### ✅ Example of correct code
|
|
127
134
|
|
|
128
135
|
```js
|
|
129
136
|
if (a)
|
|
130
137
|
console.log('hi');
|
|
131
138
|
```
|
|
132
139
|
|
|
133
|
-
|
|
140
|
+
## remove-useless-typeof
|
|
134
141
|
|
|
135
142
|
> The `typeof` operator returns a string indicating the type of the unevaluated operand.
|
|
136
143
|
>
|
|
137
144
|
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
138
145
|
|
|
139
|
-
|
|
146
|
+
### ❌ Example of incorrect code
|
|
140
147
|
|
|
141
148
|
```js
|
|
142
149
|
typeof typeof 'hello';
|
|
143
150
|
```
|
|
144
151
|
|
|
145
|
-
|
|
152
|
+
### ✅ Example of correct code
|
|
146
153
|
|
|
147
154
|
```js
|
|
148
155
|
typeof 'hello';
|
|
149
156
|
```
|
|
150
157
|
|
|
151
|
-
|
|
158
|
+
## apply-is-array
|
|
152
159
|
|
|
153
160
|
> The `Array.isArray()` method determines whether the passed value is an `Array`.
|
|
154
161
|
> When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
|
|
155
162
|
|
|
156
|
-
|
|
163
|
+
### ❌ Example of incorrect code
|
|
157
164
|
|
|
158
165
|
```js
|
|
159
166
|
x instanceof Array;
|
|
160
167
|
```
|
|
161
168
|
|
|
162
|
-
|
|
169
|
+
### ✅ Example of correct code
|
|
163
170
|
|
|
164
171
|
```js
|
|
165
172
|
const {isArray} = Array;
|
|
@@ -6,6 +6,7 @@ module.exports.replace = () => ({
|
|
|
6
6
|
'Boolean(__a.includes(__b))': '__a.includes(__b)',
|
|
7
7
|
'String(typeof __a)': 'typeof __a',
|
|
8
8
|
'if (Boolean(__a)) __b': 'if (__a) __b',
|
|
9
|
+
'Boolean(__a) ? __b : __c': '__a ? __b : __c',
|
|
9
10
|
'while (Boolean(__a)) __b': 'while (__a) __b',
|
|
10
11
|
'do __b; while (Boolean(__a))': 'do __b; while (__a)',
|
|
11
12
|
'for (__a; Boolean(__b); __c) __d': 'for (__a; __b; __c) __d',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-types",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.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",
|