@putout/plugin-printer 8.7.2 → 8.8.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
|
@@ -23,6 +23,7 @@ npm i @putout/plugin-printer -D
|
|
|
23
23
|
- ✅ [apply-types](#apply-types);
|
|
24
24
|
- ✅ [check-type-passed-to-type-checker](#check-type-passed-to-type-checker);
|
|
25
25
|
- ✅ [check-if-success-possible-in-type-checker](#check-if-success-possible-in-type-checker);
|
|
26
|
+
- ✅ [merge-tuple-of-type-checker](#merge-tuple-of-type-checker);
|
|
26
27
|
- ✅ [declare](#declare);
|
|
27
28
|
- ✅ [remove-args](#remove-args);
|
|
28
29
|
- ✅ [remove-useless-maybe](#remove-useless-maybe);
|
|
@@ -46,6 +47,7 @@ npm i @putout/plugin-printer -D
|
|
|
46
47
|
"printer/check-type-passed-to-type-checker": "on",
|
|
47
48
|
"printer/check-if-success-possible-in-type-checker": "on",
|
|
48
49
|
"printer/declare": "on",
|
|
50
|
+
"printer/merge-tuple-of-type-checker": "on",
|
|
49
51
|
"printer/remove-args": "on",
|
|
50
52
|
"printer/remove-useless-maybe": "on",
|
|
51
53
|
"printer/remove-trailing-spaces-from-type-checker": "on",
|
|
@@ -384,6 +386,30 @@ test('', (t) => {
|
|
|
384
386
|
});
|
|
385
387
|
```
|
|
386
388
|
|
|
389
|
+
## merge-tuple-of-type-checker
|
|
390
|
+
|
|
391
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/7351b8359ac3da967e9b4b80d49b51da/74de1543f6e129f6e22f38e2eb597abce6c122a6).
|
|
392
|
+
|
|
393
|
+
### ❌ Example of incorrect code
|
|
394
|
+
|
|
395
|
+
```js
|
|
396
|
+
export const isNewlineAfterComma = createTypeChecker([
|
|
397
|
+
['+: -> !', isObjectExpression],
|
|
398
|
+
['+: ->', isStringLiteral],
|
|
399
|
+
['+', isStringLiteral],
|
|
400
|
+
]);
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### ✅ Example of correct code
|
|
404
|
+
|
|
405
|
+
```js
|
|
406
|
+
export const isNewlineAfterComma = createTypeChecker([
|
|
407
|
+
['+: -> !ObjectExpression'],
|
|
408
|
+
['+: -> StringLiteral'],
|
|
409
|
+
['+: StringLiteral'],
|
|
410
|
+
]);
|
|
411
|
+
```
|
|
412
|
+
|
|
387
413
|
## remove-legacy-test-declaration
|
|
388
414
|
|
|
389
415
|
```diff
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {types} from 'putout';
|
|
2
|
+
import {isTypeExists} from '#types';
|
|
2
3
|
|
|
3
4
|
const addQuotes = (a) => `'${a}'`;
|
|
4
5
|
const {
|
|
@@ -12,14 +13,6 @@ const ARROW_LENGTH = ARROW.length;
|
|
|
12
13
|
const ARROW_NOT_LENGTH = ARROW_NOT.length;
|
|
13
14
|
const NOT = '!';
|
|
14
15
|
|
|
15
|
-
const TYPES_EXISTS = new Set([
|
|
16
|
-
'CommentBlock',
|
|
17
|
-
]);
|
|
18
|
-
|
|
19
|
-
const TYPES_NOT_EXISTS = new Set([
|
|
20
|
-
'ExportDeclaration',
|
|
21
|
-
]);
|
|
22
|
-
|
|
23
16
|
const INSTEAD = new Map([
|
|
24
17
|
['ExportDeclaration', [
|
|
25
18
|
'ExportAllDeclaration',
|
|
@@ -53,7 +46,7 @@ export const traverse = ({push}) => ({
|
|
|
53
46
|
const {value} = path.node;
|
|
54
47
|
|
|
55
48
|
if (!value.includes(ARROW)) {
|
|
56
|
-
if (/^[a-zA-Z].+/.test(value) && !
|
|
49
|
+
if (/^[a-zA-Z].+/.test(value) && !isTypeExists(value))
|
|
57
50
|
push({
|
|
58
51
|
path,
|
|
59
52
|
type: value,
|
|
@@ -68,7 +61,7 @@ export const traverse = ({push}) => ({
|
|
|
68
61
|
|
|
69
62
|
const type = value.slice(typeNotIndex, value.length);
|
|
70
63
|
|
|
71
|
-
if (!
|
|
64
|
+
if (!isTypeExists(type))
|
|
72
65
|
push({
|
|
73
66
|
path,
|
|
74
67
|
type,
|
|
@@ -82,20 +75,10 @@ export const traverse = ({push}) => ({
|
|
|
82
75
|
|
|
83
76
|
const type = value.slice(typeIndex, value.length);
|
|
84
77
|
|
|
85
|
-
if (!
|
|
78
|
+
if (!isTypeExists(type))
|
|
86
79
|
push({
|
|
87
80
|
path,
|
|
88
81
|
type,
|
|
89
82
|
});
|
|
90
83
|
},
|
|
91
84
|
});
|
|
92
|
-
|
|
93
|
-
function isExists(type) {
|
|
94
|
-
if (TYPES_NOT_EXISTS.has(type))
|
|
95
|
-
return false;
|
|
96
|
-
|
|
97
|
-
if (types[`is${type}`])
|
|
98
|
-
return true;
|
|
99
|
-
|
|
100
|
-
return TYPES_EXISTS.has(type);
|
|
101
|
-
}
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as mergeTuplesOfTypeCheckers from './merge-tuples-of-type-checkers/index.js';
|
|
1
2
|
import * as checkIfSuccessPossibleInTypeChecker from './check-if-success-possible-in-type-checker/index.js';
|
|
2
3
|
import * as removeUselessTuplesFromTypeChecker from './remove-useless-tuples-from-type-checker/index.js';
|
|
3
4
|
import * as addMissingTuplesToTypeChecker from './add-missing-tuples-to-type-checker/index.js';
|
|
@@ -34,4 +35,5 @@ export const rules = {
|
|
|
34
35
|
'add-missing-tuples-to-type-checker': addMissingTuplesToTypeChecker,
|
|
35
36
|
'remove-useless-tuples-from-type-checker': removeUselessTuplesFromTypeChecker,
|
|
36
37
|
'check-if-success-possible-in-type-checker': checkIfSuccessPossibleInTypeChecker,
|
|
38
|
+
'merge-tuples-of-type-checkers': mergeTuplesOfTypeCheckers,
|
|
37
39
|
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {types, operator} from 'putout';
|
|
2
|
+
import {isTypeExists} from '#types';
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
isArrayExpression,
|
|
6
|
+
isIdentifier,
|
|
7
|
+
isCallExpression,
|
|
8
|
+
} = types;
|
|
9
|
+
|
|
10
|
+
const {setLiteralValue} = operator;
|
|
11
|
+
const cutIS = (a) => a.slice(2);
|
|
12
|
+
|
|
13
|
+
const prepareValue = (a) => {
|
|
14
|
+
if (/^[+-]$/.test(a))
|
|
15
|
+
return `${a}: `;
|
|
16
|
+
|
|
17
|
+
if (a.endsWith('!'))
|
|
18
|
+
return a;
|
|
19
|
+
|
|
20
|
+
return `${a} `;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const report = () => `Merge tuple of type checker`;
|
|
24
|
+
|
|
25
|
+
export const fix = ({path, name}) => {
|
|
26
|
+
const [first] = path.node.elements;
|
|
27
|
+
const {value} = first;
|
|
28
|
+
|
|
29
|
+
setLiteralValue(first, `${prepareValue(value)}${name}`);
|
|
30
|
+
path.node.elements.pop();
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const traverse = ({push}) => ({
|
|
34
|
+
ArrayExpression(path) {
|
|
35
|
+
const {parentPath} = path;
|
|
36
|
+
|
|
37
|
+
if (!isCallExpression(parentPath))
|
|
38
|
+
return;
|
|
39
|
+
|
|
40
|
+
const {callee} = parentPath.node;
|
|
41
|
+
|
|
42
|
+
if (!isIdentifier(callee, {name: 'createTypeChecker'}))
|
|
43
|
+
return;
|
|
44
|
+
|
|
45
|
+
const elements = path.get('elements');
|
|
46
|
+
|
|
47
|
+
for (const element of elements) {
|
|
48
|
+
if (!isArrayExpression(element))
|
|
49
|
+
continue;
|
|
50
|
+
|
|
51
|
+
const [, checker] = element.get('elements');
|
|
52
|
+
|
|
53
|
+
if (!isIdentifier(checker))
|
|
54
|
+
continue;
|
|
55
|
+
|
|
56
|
+
const checkerName = checker.node.name;
|
|
57
|
+
|
|
58
|
+
if (!checkerName.startsWith('is'))
|
|
59
|
+
return;
|
|
60
|
+
|
|
61
|
+
const name = cutIS(checkerName);
|
|
62
|
+
|
|
63
|
+
if (isTypeExists(name))
|
|
64
|
+
push({
|
|
65
|
+
path: element,
|
|
66
|
+
name,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
});
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const TYPES_EXISTS = new Set([
|
|
4
|
+
'CommentBlock',
|
|
5
|
+
]);
|
|
6
|
+
|
|
7
|
+
const TYPES_NOT_EXISTS = new Set([
|
|
8
|
+
'ExportDeclaration',
|
|
9
|
+
]);
|
|
10
|
+
|
|
11
|
+
export function isTypeExists(type) {
|
|
12
|
+
if (TYPES_NOT_EXISTS.has(type))
|
|
13
|
+
return false;
|
|
14
|
+
|
|
15
|
+
if (types[`is${type}`])
|
|
16
|
+
return true;
|
|
17
|
+
|
|
18
|
+
return TYPES_EXISTS.has(type);
|
|
19
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-printer",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout plugin adds support of transformations for @putout/printer",
|
|
7
7
|
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-printer#readme",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
|
+
"imports": {
|
|
10
|
+
"#types": "./lib/types.js"
|
|
11
|
+
},
|
|
9
12
|
"release": false,
|
|
10
13
|
"tag": false,
|
|
11
14
|
"changelog": false,
|