@putout/plugin-types 1.0.0 → 1.2.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
@@ -16,7 +16,10 @@ 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",
21
+ "types/remove-useless-conversion": "on",
22
+ "types/remove-double-negations": "on"
20
23
  }
21
24
  }
22
25
  ```
@@ -26,7 +29,7 @@ npm i putout @putout/plugin-types -D
26
29
  ### declare
27
30
 
28
31
  Based on [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare#putoutoperator-declare-).
29
- Suported assertions:
32
+ Supported assertions:
30
33
 
31
34
  - `isString`;
32
35
  - `isNumber`;
@@ -64,6 +67,75 @@ When you want to skip some declaration use `dismiss`:
64
67
  }
65
68
  ```
66
69
 
70
+ ### convert-typeof-to-is-type
71
+
72
+ > The `typeof` operator returns a string indicating the type of the unevaluated operand.
73
+ >
74
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
75
+
76
+ #### ❌ Example of incorrect code
77
+
78
+ ```js
79
+ if (typeof a === 'boolean')
80
+ return x;
81
+ ```
82
+
83
+ #### ✅ Example of correct code
84
+
85
+ ```js
86
+ const isBool = (a) => typeof a === 'boolean';
87
+
88
+ if (isBool(a))
89
+ return x;
90
+ ```
91
+
92
+
93
+ ### remove-useless-conversion
94
+
95
+ #### ❌ Example of incorrect code
96
+
97
+ ```js
98
+ const a = !![1].includes(1);
99
+ const b = Boolean([1].includes(1));
100
+ ```
101
+
102
+ #### ✅ Example of correct code
103
+
104
+ ```js
105
+ const a = [1].includes(1);
106
+ ```
107
+
108
+ ### remove-double-negations
109
+
110
+ > 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.
111
+ >
112
+ > The same conversion can be done through the `Boolean` function.
113
+ >
114
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT)
115
+
116
+ #### ❌ Example of incorrect code
117
+
118
+ ```js
119
+ if (!!a) {
120
+ console.log('hi');
121
+ }
122
+ ```
123
+
124
+ #### ✅ Example of correct code
125
+
126
+ ```js
127
+ if (a) {
128
+ console.log('hi');
129
+ }
130
+ ```
131
+
132
+ ## Comparison
133
+
134
+ Linter | Rule | Fix
135
+ --------|-------|------------|
136
+ 🐊 **Putout** | [`types`](https://github.com/coderaiser/putout/tree/master/packages/plugin-types#readme)| ✅
137
+ ⏣ **ESLint** | [`no-implicit-coercion`](https://eslint.org/docs/rules/no-implicit-coercion) | ✅
138
+
67
139
  ## License
68
140
 
69
141
  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
@@ -6,5 +6,8 @@ const getRule = (a) => ({
6
6
 
7
7
  module.exports.rules = {
8
8
  ...getRule('declare'),
9
+ ...getRule('convert-typeof-to-is-type'),
10
+ ...getRule('remove-useless-conversion'),
11
+ ...getRule('remove-double-negations'),
9
12
  };
10
13
 
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Avoid double negations in conditions`;
4
+
5
+ module.exports.filter = ({parentPath}) => !parentPath.isJSXExpressionContainer();
6
+
7
+ module.exports.replace = () => ({
8
+ 'if (!!__a) __b': 'if (__a) __b',
9
+ '!!__a ? __b : __c': '__a ? __b : __c',
10
+ '!!__a && __b': '__a && __b',
11
+ '!!__a.includes(__b)': '__a.includes(__b)',
12
+ 'while(!!__a) __b': 'while(__a) __b',
13
+ 'do __a; while(!!__b)': 'do __a; while(__b)',
14
+ 'for (__a; !!__b; __c) __d': 'for (__a; __b; __c) __d',
15
+ });
16
+
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Avoid useless type conversion`;
4
+
5
+ module.exports.replace = () => ({
6
+ 'Boolean(__a.includes(__b))': '__a.includes(__b)',
7
+ 'String(typeof __a)': 'typeof __a',
8
+ 'if (Boolean(__a)) __b': 'if (__a) __b',
9
+ 'while (Boolean(__a)) __b': 'while (__a) __b',
10
+ 'do __b; while (Boolean(__a))': 'do __b; while (__a)',
11
+ 'for (__a; Boolean(__b); __c) __d': 'for (__a; __b; __c) __d',
12
+ 'for (__a; Boolean(__b);) __d': 'for (__a; __b;) __d',
13
+ 'for (; Boolean(__b);__c) __d': 'for (; __b; __c) __d',
14
+ 'for (; Boolean(__b);) __d': 'for (; __b;) __d',
15
+ });
16
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-types",
3
- "version": "1.0.0",
3
+ "version": "1.2.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": "^5.0.0",
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",