@putout/plugin-types 1.1.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
@@ -17,7 +17,9 @@ 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"
21
23
  }
22
24
  }
23
25
  ```
@@ -41,7 +43,6 @@ Supported assertions:
41
43
  #### ❌ Example of incorrect code
42
44
 
43
45
  ```js
44
- const isString = (a) => typeof a === 'string';
45
46
  isString('hello');
46
47
  ```
47
48
 
@@ -88,6 +89,53 @@ if (isBool(a))
88
89
  return x;
89
90
  ```
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
+
91
139
  ## License
92
140
 
93
141
  MIT
package/lib/index.js CHANGED
@@ -7,5 +7,7 @@ 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'),
10
12
  };
11
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.1.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",