@putout/plugin-types 1.1.0 → 1.3.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
@@ -17,7 +17,10 @@ npm i putout @putout/plugin-types -D
17
17
  {
18
18
  "rules": {
19
19
  "types/declare": "on",
20
- "types/convert-typeof-to-istype": "on"
20
+ "types/convert-typeof-to-istype": "on",
21
+ "types/remove-useless-conversion": "on",
22
+ "types/remove-double-negations": "on",
23
+ "types/remove-useless-typeof": "on"
21
24
  }
22
25
  }
23
26
  ```
@@ -41,7 +44,6 @@ Supported assertions:
41
44
  #### ❌ Example of incorrect code
42
45
 
43
46
  ```js
44
- const isString = (a) => typeof a === 'string';
45
47
  isString('hello');
46
48
  ```
47
49
 
@@ -88,6 +90,70 @@ if (isBool(a))
88
90
  return x;
89
91
  ```
90
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
+ ### 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
+ ## Comparison
151
+
152
+ Linter | Rule | Fix
153
+ --------|-------|------------|
154
+ 🐊 **Putout** | [`types`](https://github.com/coderaiser/putout/tree/master/packages/plugin-types#readme)| ✅
155
+ ⏣ **ESLint** | [`no-implicit-coercion`](https://eslint.org/docs/rules/no-implicit-coercion) | ✅
156
+
91
157
  ## License
92
158
 
93
159
  MIT
package/lib/index.js CHANGED
@@ -7,5 +7,8 @@ const getRule = (a) => ({
7
7
  module.exports.rules = {
8
8
  ...getRule('declare'),
9
9
  ...getRule('convert-typeof-to-is-type'),
10
+ ...getRule('remove-useless-conversion'),
11
+ ...getRule('remove-double-negations'),
12
+ ...getRule('remove-useless-typeof'),
10
13
  };
11
14
 
@@ -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
+
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ module.exports.report = () => `Useless typeof should be avoided`;
4
+
5
+ module.exports.replace = () => ({
6
+ 'typeof typeof __a': 'typeof __a',
7
+ });
8
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-types",
3
- "version": "1.1.0",
3
+ "version": "1.3.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",