@putout/plugin-types 1.0.0 → 1.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 +26 -2
- package/lib/convert-typeof-to-is-type/index.js +64 -0
- package/lib/index.js +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -16,7 +16,8 @@ npm i putout @putout/plugin-types -D
|
|
|
16
16
|
```json
|
|
17
17
|
{
|
|
18
18
|
"rules": {
|
|
19
|
-
"types/declare": "on"
|
|
19
|
+
"types/declare": "on",
|
|
20
|
+
"types/convert-typeof-to-istype": "on"
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
```
|
|
@@ -26,7 +27,7 @@ npm i putout @putout/plugin-types -D
|
|
|
26
27
|
### declare
|
|
27
28
|
|
|
28
29
|
Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare#putoutoperator-declare-).
|
|
29
|
-
|
|
30
|
+
Supported assertions:
|
|
30
31
|
|
|
31
32
|
- `isString`;
|
|
32
33
|
- `isNumber`;
|
|
@@ -40,6 +41,7 @@ Suported assertions:
|
|
|
40
41
|
#### ❌ Example of incorrect code
|
|
41
42
|
|
|
42
43
|
```js
|
|
44
|
+
const isString = (a) => typeof a === 'string';
|
|
43
45
|
isString('hello');
|
|
44
46
|
```
|
|
45
47
|
|
|
@@ -64,6 +66,28 @@ When you want to skip some declaration use `dismiss`:
|
|
|
64
66
|
}
|
|
65
67
|
```
|
|
66
68
|
|
|
69
|
+
### convert-typeof-to-is-type
|
|
70
|
+
|
|
71
|
+
> The `typeof` operator returns a string indicating the type of the unevaluated operand.
|
|
72
|
+
>
|
|
73
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
|
|
74
|
+
|
|
75
|
+
#### ❌ Example of incorrect code
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
if (typeof a === 'boolean')
|
|
79
|
+
return x;
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### ✅ Example of correct code
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
const isBool = (a) => typeof a === 'boolean';
|
|
86
|
+
|
|
87
|
+
if (isBool(a))
|
|
88
|
+
return x;
|
|
89
|
+
```
|
|
90
|
+
|
|
67
91
|
## License
|
|
68
92
|
|
|
69
93
|
MIT
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {operator} = require('putout');
|
|
4
|
+
const {compare, getBindingPath} = operator;
|
|
5
|
+
|
|
6
|
+
const NAMES = {
|
|
7
|
+
function: 'isFn',
|
|
8
|
+
string: 'isString',
|
|
9
|
+
number: 'isNumber',
|
|
10
|
+
boolean: 'isBool',
|
|
11
|
+
undefined: 'isUndefined',
|
|
12
|
+
symbol: 'isSymbol',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const BODIES = {
|
|
16
|
+
function: `typeof __a === 'function'`,
|
|
17
|
+
string: `typeof __a === 'string'`,
|
|
18
|
+
number: `typeof __a === 'number'`,
|
|
19
|
+
boolean: `typeof __a === 'boolean'`,
|
|
20
|
+
undefined: `typeof __a === 'undefined'`,
|
|
21
|
+
symbol: `typeof __a === 'symbol'`,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports.report = () => `Use function to check type instead of 'typeof'`;
|
|
25
|
+
module.exports.match = () => ({
|
|
26
|
+
'typeof __a === "__b"': ({__a, __b}, path) => {
|
|
27
|
+
if (path.parentPath.isFunction())
|
|
28
|
+
return false;
|
|
29
|
+
|
|
30
|
+
if (path.parentPath.isVariableDeclarator())
|
|
31
|
+
return false;
|
|
32
|
+
|
|
33
|
+
if (isBind(path, __b.value))
|
|
34
|
+
return false;
|
|
35
|
+
|
|
36
|
+
if (!getBindingPath(path, __a))
|
|
37
|
+
return false;
|
|
38
|
+
|
|
39
|
+
return true;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
module.exports.replace = () => ({
|
|
44
|
+
[BODIES.function]: 'isFn(__a)',
|
|
45
|
+
[BODIES.string]: 'isString(__a)',
|
|
46
|
+
[BODIES.number]: 'isNumber(__a)',
|
|
47
|
+
[BODIES.boolean]: 'isBool(__a)',
|
|
48
|
+
[BODIES.undefined]: 'isUndefined(__a)',
|
|
49
|
+
[BODIES.symbol]: 'isSymbol(__a)',
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
function isBind(path, name) {
|
|
53
|
+
const fnName = NAMES[name];
|
|
54
|
+
const fnBody = BODIES[name];
|
|
55
|
+
const bindingPath = getBindingPath(path, fnName);
|
|
56
|
+
|
|
57
|
+
if (!bindingPath)
|
|
58
|
+
return false;
|
|
59
|
+
|
|
60
|
+
const {body} = bindingPath.node.init;
|
|
61
|
+
|
|
62
|
+
return !compare(body, fnBody);
|
|
63
|
+
}
|
|
64
|
+
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-types",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.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",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@putout/plugin-putout": "*",
|
|
35
|
-
"@putout/test": "^
|
|
35
|
+
"@putout/test": "^6.0.0",
|
|
36
36
|
"c8": "^7.5.0",
|
|
37
37
|
"eslint": "^8.0.1",
|
|
38
38
|
"eslint-plugin-n": "^15.2.4",
|