@putout/plugin-types 1.2.0 → 1.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 +62 -3
- package/lib/apply-is-array/index.js +17 -0
- package/lib/index.js +2 -0
- package/lib/remove-useless-typeof/index.js +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,11 +19,12 @@ npm i putout @putout/plugin-types -D
|
|
|
19
19
|
"types/declare": "on",
|
|
20
20
|
"types/convert-typeof-to-istype": "on",
|
|
21
21
|
"types/remove-useless-conversion": "on",
|
|
22
|
-
"types/remove-double-negations": "on"
|
|
22
|
+
"types/remove-double-negations": "on",
|
|
23
|
+
"types/remove-useless-typeof": "on",
|
|
24
|
+
"types/apply-is-array": "on"
|
|
23
25
|
}
|
|
24
26
|
}
|
|
25
27
|
```
|
|
26
|
-
|
|
27
28
|
## Rules
|
|
28
29
|
|
|
29
30
|
### declare
|
|
@@ -89,7 +90,6 @@ if (isBool(a))
|
|
|
89
90
|
return x;
|
|
90
91
|
```
|
|
91
92
|
|
|
92
|
-
|
|
93
93
|
### remove-useless-conversion
|
|
94
94
|
|
|
95
95
|
#### ❌ Example of incorrect code
|
|
@@ -129,6 +129,65 @@ if (a) {
|
|
|
129
129
|
}
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
+
### remove-useless-typeof
|
|
133
|
+
|
|
134
|
+
> The `typeof` operator returns a string indicating the type of the unevaluated operand.
|
|
135
|
+
>
|
|
136
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
137
|
+
|
|
138
|
+
#### ❌ Example of incorrect code
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
typeof typeof 'hello';
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### ✅ Example of correct code
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
typeof 'hello';
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### apply-is-array
|
|
151
|
+
|
|
152
|
+
> The `Array.isArray()` method determines whether the passed value is an `Array`.
|
|
153
|
+
> When checking for `Array` instance, `Array.isArray()` is preferred over `instanceof` because it works through `iframes`.
|
|
154
|
+
|
|
155
|
+
#### ❌ Example of incorrect code
|
|
156
|
+
|
|
157
|
+
```js
|
|
158
|
+
x instanceof Array;
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
#### ✅ Example of correct code
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
const {isArray} = Array;
|
|
165
|
+
isArray(x);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
In case of using `inline` option:
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"rules": {
|
|
173
|
+
"apply-is-array": ["on", {
|
|
174
|
+
"inline": true
|
|
175
|
+
}]
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`Array.isArray` will be inlined:
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
Array.isArray(x);
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|
|
189
|
+
|
|
190
|
+
|
|
132
191
|
## Comparison
|
|
133
192
|
|
|
134
193
|
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
|
+
|
package/lib/index.js
CHANGED
|
@@ -5,9 +5,11 @@ 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'),
|
|
11
12
|
...getRule('remove-double-negations'),
|
|
13
|
+
...getRule('remove-useless-typeof'),
|
|
12
14
|
};
|
|
13
15
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-types",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.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",
|