@putout/plugin-types 5.0.0 → 5.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 -0
- package/lib/index.js +2 -0
- package/lib/remove-useless-constructor/index.js +20 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ npm i putout @putout/plugin-types -D
|
|
|
18
18
|
- ✅ [declare](#declare);
|
|
19
19
|
- ✅ [remove-double-negations](#remove-double-negations);
|
|
20
20
|
- ✅ [remove-useless-conversion](#remove-useless-conversion);
|
|
21
|
+
- ✅ [remove-useless-constructor](#remove-useless-constructor);
|
|
21
22
|
- ✅ [remove-useless-typeof](#remove-useless-typeof);
|
|
22
23
|
|
|
23
24
|
## Config
|
|
@@ -28,6 +29,7 @@ npm i putout @putout/plugin-types -D
|
|
|
28
29
|
"types/declare": "on",
|
|
29
30
|
"types/convert-typeof-to-istype": "on",
|
|
30
31
|
"types/remove-useless-conversion": "on",
|
|
32
|
+
"types/remove-useless-constructor": "on",
|
|
31
33
|
"types/remove-double-negations": "on",
|
|
32
34
|
"types/remove-useless-typeof": "on",
|
|
33
35
|
"types/apply-is-array": "on"
|
|
@@ -115,6 +117,30 @@ const b = Boolean([1].includes(1));
|
|
|
115
117
|
const a = [1].includes(1);
|
|
116
118
|
```
|
|
117
119
|
|
|
120
|
+
## remove-useless-constructor
|
|
121
|
+
|
|
122
|
+
> Wrapper classes have surprising behaviour, such as `new Boolean(false)` evaluating to `true`.
|
|
123
|
+
>
|
|
124
|
+
> (c) [Google TypeScript Style Guide](https://google.github.io/styleguide/tsguide.html#primitive-types-wrapper-classes)
|
|
125
|
+
|
|
126
|
+
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to remove useless `constructor`. Use with [`new/remove-useless`](https://github.com/coderaiser/putout/tree/master/packages/plugin-new#readme).
|
|
127
|
+
|
|
128
|
+
### ❌ Example of incorrect code
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
const s = String('hello');
|
|
132
|
+
const b = Boolean(false);
|
|
133
|
+
const n = Number(5);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### ✅ Example of correct code
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
const s = 'hello';
|
|
140
|
+
const b = false;
|
|
141
|
+
const n = 5;
|
|
142
|
+
```
|
|
143
|
+
|
|
118
144
|
## remove-double-negations
|
|
119
145
|
|
|
120
146
|
> 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.
|
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const applyIsArray = require('./apply-is-array');
|
|
|
4
4
|
const declare = require('./declare');
|
|
5
5
|
const convertTypeofToIsType = require('./convert-typeof-to-is-type');
|
|
6
6
|
const removeUselessConversion = require('./remove-useless-conversion');
|
|
7
|
+
const removeUselessConstructor = require('./remove-useless-constructor');
|
|
7
8
|
const removeDoubleNegations = require('./remove-double-negations');
|
|
8
9
|
const removeUselessTypeof = require('./remove-useless-typeof');
|
|
9
10
|
|
|
@@ -12,6 +13,7 @@ module.exports.rules = {
|
|
|
12
13
|
declare,
|
|
13
14
|
'convert-typeof-to-is-type': convertTypeofToIsType,
|
|
14
15
|
'remove-useless-conversion': removeUselessConversion,
|
|
16
|
+
'remove-useless-constructor': removeUselessConstructor,
|
|
15
17
|
'remove-double-negations': removeDoubleNegations,
|
|
16
18
|
'remove-useless-typeof': removeUselessTypeof,
|
|
17
19
|
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {types} = require('putout');
|
|
4
|
+
const {
|
|
5
|
+
isBooleanLiteral,
|
|
6
|
+
isNumericLiteral,
|
|
7
|
+
} = types;
|
|
8
|
+
|
|
9
|
+
module.exports.report = () => 'Avoid useless constructor';
|
|
10
|
+
|
|
11
|
+
module.exports.match = () => ({
|
|
12
|
+
'Boolean(__a)': ({__a}) => isBooleanLiteral(__a),
|
|
13
|
+
'Number(__a)': ({__a}) => isNumericLiteral(__a),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
module.exports.replace = () => ({
|
|
17
|
+
'String("__a")': '__a',
|
|
18
|
+
'Boolean(__a)': '__a',
|
|
19
|
+
'Number(__a)': '__a',
|
|
20
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-types",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.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",
|