@putout/plugin-types 5.0.0 → 6.0.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
@@ -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"
@@ -52,6 +54,7 @@ Supported assertions:
52
54
  - `isBigInt`;
53
55
  - `isArray`;
54
56
  - `isEmptyArray`;
57
+ - `isError`;
55
58
 
56
59
  ### ❌ Example of incorrect code
57
60
 
@@ -115,6 +118,30 @@ const b = Boolean([1].includes(1));
115
118
  const a = [1].includes(1);
116
119
  ```
117
120
 
121
+ ## remove-useless-constructor
122
+
123
+ > Wrapper classes have surprising behaviour, such as `new Boolean(false)` evaluating to `true`.
124
+ >
125
+ > (c) [Google TypeScript Style Guide](https://google.github.io/styleguide/tsguide.html#primitive-types-wrapper-classes)
126
+
127
+ 🐊[**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).
128
+
129
+ ### ❌ Example of incorrect code
130
+
131
+ ```js
132
+ const s = String('hello');
133
+ const b = Boolean(false);
134
+ const n = Number(5);
135
+ ```
136
+
137
+ ### ✅ Example of correct code
138
+
139
+ ```js
140
+ const s = 'hello';
141
+ const b = false;
142
+ const n = 5;
143
+ ```
144
+
118
145
  ## remove-double-negations
119
146
 
120
147
  > 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.
@@ -16,6 +16,9 @@ const NAMES = {
16
16
  const EQUAL = 'typeof __a === "__b"';
17
17
  const NOT_EQUAL = 'typeof __a !== "__b"';
18
18
 
19
+ const EQUAL_INSTANCE_OF = '__a instanceof __b';
20
+ const NOT_EQUAL_INSTANCE_OF = '!(__a instanceof __b)';
21
+
19
22
  const BODIES = {
20
23
  function: `typeof __a === 'function'`,
21
24
  string: `typeof __a === 'string'`,
@@ -24,6 +27,7 @@ const BODIES = {
24
27
  undefined: `typeof __a === 'undefined'`,
25
28
  symbol: `typeof __a === 'symbol'`,
26
29
  bigint: `typeof __a === 'bigint'`,
30
+ error: `__a instanceof Error`,
27
31
  };
28
32
 
29
33
  const NOT_BODIES = {
@@ -34,12 +38,15 @@ const NOT_BODIES = {
34
38
  undefined: `typeof __a !== 'undefined'`,
35
39
  symbol: `typeof __a !== 'symbol'`,
36
40
  bigint: `typeof __a !== 'bigint'`,
41
+ error: `!(__a instanceof Error)`,
37
42
  };
38
43
 
39
- module.exports.report = () => `Use function to check type instead of 'typeof'`;
44
+ module.exports.report = () => `Use function to check type instead of 'typeof' or 'instanceof'`;
40
45
  module.exports.match = () => ({
41
46
  [EQUAL]: check,
42
47
  [NOT_EQUAL]: check,
48
+ [EQUAL_INSTANCE_OF]: check,
49
+ [NOT_EQUAL_INSTANCE_OF]: check,
43
50
  });
44
51
 
45
52
  module.exports.replace = () => ({
@@ -50,6 +57,7 @@ module.exports.replace = () => ({
50
57
  [BODIES.undefined]: 'isUndefined(__a)',
51
58
  [BODIES.symbol]: 'isSymbol(__a)',
52
59
  [BODIES.bigint]: 'isBigInt(__a)',
60
+ [BODIES.error]: 'isError(__a)',
53
61
  [NOT_BODIES.function]: '!isFn(__a)',
54
62
  [NOT_BODIES.string]: '!isString(__a)',
55
63
  [NOT_BODIES.number]: '!isNumber(__a)',
@@ -57,6 +65,7 @@ module.exports.replace = () => ({
57
65
  [NOT_BODIES.undefined]: '!isUndefined(__a)',
58
66
  [NOT_BODIES.symbol]: '!isSymbol(__a)',
59
67
  [NOT_BODIES.bigint]: '!isBigInt(__a)',
68
+ [NOT_BODIES.error]: '!isError(__a)',
60
69
  });
61
70
 
62
71
  function check({__a, __b}, path) {
@@ -13,4 +13,5 @@ module.exports.declare = () => ({
13
13
  isNull: `const isNull = (a) => !a && typeof a === 'object'`,
14
14
  isArray: 'const {isArray} = Array',
15
15
  isEmptyArray: 'const isEmptyArray = (a) => isArray(a) && !a.length',
16
+ isError: 'const isError = (a) => a instanceof Error',
16
17
  });
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.0.0",
3
+ "version": "6.0.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",
@@ -11,7 +11,7 @@
11
11
  "changelog": false,
12
12
  "repository": {
13
13
  "type": "git",
14
- "url": "git://github.com/coderaiser/putout.git"
14
+ "url": "git+https://github.com/coderaiser/putout.git"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "madrun test",
@@ -43,7 +43,7 @@
43
43
  "nodemon": "^3.0.1"
44
44
  },
45
45
  "peerDependencies": {
46
- "putout": ">=36"
46
+ "putout": ">=37"
47
47
  },
48
48
  "license": "MIT",
49
49
  "engines": {