@putout/plugin-printer 8.5.1 → 8.6.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
|
@@ -14,7 +14,8 @@ npm i @putout/plugin-printer -D
|
|
|
14
14
|
## Rules
|
|
15
15
|
|
|
16
16
|
- ✅ [add-args](#add-args);
|
|
17
|
-
- ✅ [add-missing-
|
|
17
|
+
- ✅ [add-missing-spaces-to-type-checker](#add-missing-spaces-to-type-checker);
|
|
18
|
+
- ✅ [add-missing-tuples-to-type-checker](#add-missing-tuples-to-type-checker);
|
|
18
19
|
- ✅ [apply-breakline](#apply-breakline);
|
|
19
20
|
- ✅ [apply-computed-print](#apply-computed-print);
|
|
20
21
|
- ✅ [apply-create-test-url](#apply-create-test-url);
|
|
@@ -27,6 +28,7 @@ npm i @putout/plugin-printer -D
|
|
|
27
28
|
- ✅ [remove-legacy-test-declaration](#remove-legacy-test-declaration);
|
|
28
29
|
- ✅ [remove-trailing-spaces-from-type-checker](#remove-trailing-spaces-from-type-checker)
|
|
29
30
|
- ✅ [remove-useless-spaces-from-type-checker](#remove-useless-spaces-from-type-checker)
|
|
31
|
+
- ✅ [remove-useless-tuples-from-type-checker](#remove-useless-tuples-from-type-checker)
|
|
30
32
|
|
|
31
33
|
## Config
|
|
32
34
|
|
|
@@ -34,7 +36,7 @@ npm i @putout/plugin-printer -D
|
|
|
34
36
|
{
|
|
35
37
|
"rules": {
|
|
36
38
|
"printer/add-args": "on",
|
|
37
|
-
"printer/add-missing-
|
|
39
|
+
"printer/add-missing-tuples-to-type-checker": "on",
|
|
38
40
|
"printer/apply-breakline": "on",
|
|
39
41
|
"printer/apply-linebreak": "on",
|
|
40
42
|
"printer/apply-computed-print": "on",
|
|
@@ -45,7 +47,8 @@ npm i @putout/plugin-printer -D
|
|
|
45
47
|
"printer/remove-args": "on",
|
|
46
48
|
"printer/remove-useless-maybe": "on",
|
|
47
49
|
"printer/remove-trailing-spaces-from-type-checker": "on",
|
|
48
|
-
"printer/remove-useless-spaces-from-type-checker": "on"
|
|
50
|
+
"printer/remove-useless-spaces-from-type-checker": "on",
|
|
51
|
+
"printer/remove-useless-tuples-from-type-checker": "on"
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
54
|
```
|
|
@@ -102,7 +105,7 @@ module.exports = {
|
|
|
102
105
|
};
|
|
103
106
|
```
|
|
104
107
|
|
|
105
|
-
## add-missing-
|
|
108
|
+
## add-missing-spaces-to-type-checker
|
|
106
109
|
|
|
107
110
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/2451cd51ebd3dfd20c32b6d22ca176b7/bd593860ff4bb2d66e06cf95bbe103cb1203129d).
|
|
108
111
|
|
|
@@ -124,6 +127,34 @@ export const beforeIf = createTypeChecker([
|
|
|
124
127
|
]);
|
|
125
128
|
```
|
|
126
129
|
|
|
130
|
+
## add-missing-tuple-to-type-checker
|
|
131
|
+
|
|
132
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9fdfda423df3af9b0f08541710706299/0259bba117997861fe7573bad5477282f4df8554).
|
|
133
|
+
|
|
134
|
+
### ❌ Example of incorrect code
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
export const beforeIf = createTypeChecker([
|
|
138
|
+
'- : -> !StringLiteral',
|
|
139
|
+
'- : -> BlockStatement',
|
|
140
|
+
'- : -> WrongType',
|
|
141
|
+
['- : ->', isBlockStatement],
|
|
142
|
+
['-', isBlockStatement],
|
|
143
|
+
]);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### ✅ Example of correct code
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
export const beforeIf = createTypeChecker([
|
|
150
|
+
['- : -> !StringLiteral'],
|
|
151
|
+
['- : -> BlockStatement'],
|
|
152
|
+
['- : -> WrongType'],
|
|
153
|
+
['- : ->', isBlockStatement],
|
|
154
|
+
['-', isBlockStatement],
|
|
155
|
+
]);
|
|
156
|
+
```
|
|
157
|
+
|
|
127
158
|
## apply-computed-print
|
|
128
159
|
|
|
129
160
|
### ❌ Example of incorrect code
|
|
@@ -210,6 +241,28 @@ export const beforeIf = createTypeChecker([
|
|
|
210
241
|
]);
|
|
211
242
|
```
|
|
212
243
|
|
|
244
|
+
## remove-useless-tuples-from-type-checker
|
|
245
|
+
|
|
246
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/f53f386b607115e0a96f7e294e34761e/cefd72aba4fcade59bbf8faa59739bac5c2fe057).
|
|
247
|
+
|
|
248
|
+
### ❌ Example of incorrect code
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
export const allStrings = createTypeChecker([
|
|
252
|
+
['- : -> BlockStatement'],
|
|
253
|
+
['- : -> WrongType'],
|
|
254
|
+
]);
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### ✅ Example of correct code
|
|
258
|
+
|
|
259
|
+
```js
|
|
260
|
+
export const allStrings = createTypeChecker([
|
|
261
|
+
'- : -> BlockStatement',
|
|
262
|
+
'- : -> WrongType',
|
|
263
|
+
]);
|
|
264
|
+
```
|
|
265
|
+
|
|
213
266
|
## remove-trailing-spaces-from-type-checker
|
|
214
267
|
|
|
215
268
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/91d33e39485c31e76b534ad8447ba4db/abca8d21ad6edc1f6fed23edb05fa166677f11d6).
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {operator, types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isStringLiteral,
|
|
5
|
+
isArrayExpression,
|
|
6
|
+
isIdentifier,
|
|
7
|
+
isCallExpression,
|
|
8
|
+
arrayExpression,
|
|
9
|
+
stringLiteral,
|
|
10
|
+
} = types;
|
|
11
|
+
|
|
12
|
+
const {replaceWith} = operator;
|
|
13
|
+
|
|
14
|
+
export const report = (path) => {
|
|
15
|
+
return `Add missing tuple around: ${path}`;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const fix = (path) => {
|
|
19
|
+
const {node} = path;
|
|
20
|
+
|
|
21
|
+
if (isIdentifier(path)) {
|
|
22
|
+
replaceWith(path, arrayExpression([stringLiteral('+'), node]));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
replaceWith(path, arrayExpression([node]));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const traverse = ({push}) => ({
|
|
30
|
+
ArrayExpression(path) {
|
|
31
|
+
const {parentPath} = path;
|
|
32
|
+
|
|
33
|
+
if (!isCallExpression(parentPath))
|
|
34
|
+
return;
|
|
35
|
+
|
|
36
|
+
if (!isIdentifier(parentPath.node.callee, {name: 'createTypeChecker'}))
|
|
37
|
+
return;
|
|
38
|
+
|
|
39
|
+
const elements = path.get('elements');
|
|
40
|
+
|
|
41
|
+
if (isConsistent(elements))
|
|
42
|
+
return;
|
|
43
|
+
|
|
44
|
+
for (const element of elements) {
|
|
45
|
+
if (!isArrayExpression(element))
|
|
46
|
+
push(element);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
function isConsistent(elements) {
|
|
52
|
+
const arraysCount = elements.filter(isArrayExpression).length;
|
|
53
|
+
|
|
54
|
+
if (elements.length === arraysCount)
|
|
55
|
+
return true;
|
|
56
|
+
|
|
57
|
+
const stringsCount = elements.filter(isStringLiteral).length;
|
|
58
|
+
|
|
59
|
+
if (elements.length === stringsCount)
|
|
60
|
+
return true;
|
|
61
|
+
|
|
62
|
+
const identifiersCount = elements.filter(isIdentifier).length;
|
|
63
|
+
|
|
64
|
+
return elements.length === identifiersCount;
|
|
65
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as removeUselessTuplesFromTypeChecker from './remove-useless-tuples-from-type-checker/index.js';
|
|
2
|
+
import * as addMissingTuplesToTypeChecker from './add-missing-tuples-to-type-checker/index.js';
|
|
1
3
|
import * as checkTypePassedToTypeChecker from './check-type-passed-to-type-checker/index.js';
|
|
2
4
|
import * as removeUselessSpacesFromTypeChecker from './remove-useless-spaces-from-type-checker/index.js';
|
|
3
5
|
import * as addMissingSpacesToTypeChecker from './add-missing-spaces-to-type-checker/index.js';
|
|
@@ -28,4 +30,6 @@ export const rules = {
|
|
|
28
30
|
'add-missing-spaces-to-type-checker': addMissingSpacesToTypeChecker,
|
|
29
31
|
'remove-useless-spaces-from-type-checker': removeUselessSpacesFromTypeChecker,
|
|
30
32
|
'check-type-passed-to-type-checker': checkTypePassedToTypeChecker,
|
|
33
|
+
'add-missing-tuples-to-type-checker': addMissingTuplesToTypeChecker,
|
|
34
|
+
'remove-useless-tuples-from-type-checker': removeUselessTuplesFromTypeChecker,
|
|
31
35
|
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {operator, types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isStringLiteral,
|
|
5
|
+
isArrayExpression,
|
|
6
|
+
isIdentifier,
|
|
7
|
+
isCallExpression,
|
|
8
|
+
} = types;
|
|
9
|
+
|
|
10
|
+
const {replaceWith} = operator;
|
|
11
|
+
|
|
12
|
+
export const report = (path) => {
|
|
13
|
+
return `Remove useless tuple from: ${path}`;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const fix = (path) => {
|
|
17
|
+
const [element] = path.node.elements;
|
|
18
|
+
replaceWith(path, element);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const traverse = ({push}) => ({
|
|
22
|
+
ArrayExpression(path) {
|
|
23
|
+
const {parentPath} = path;
|
|
24
|
+
|
|
25
|
+
if (!isCallExpression(parentPath))
|
|
26
|
+
return;
|
|
27
|
+
|
|
28
|
+
if (!isIdentifier(parentPath.node.callee, {name: 'createTypeChecker'}))
|
|
29
|
+
return;
|
|
30
|
+
|
|
31
|
+
const elements = path.get('elements');
|
|
32
|
+
|
|
33
|
+
if (isConsistent(elements))
|
|
34
|
+
return;
|
|
35
|
+
|
|
36
|
+
for (const element of elements) {
|
|
37
|
+
if (isArrayExpression(element))
|
|
38
|
+
push(element);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const isTwoElementTuple = (a) => {
|
|
44
|
+
if (!isArrayExpression(a))
|
|
45
|
+
return false;
|
|
46
|
+
|
|
47
|
+
return a.node.elements.length === 2;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function isConsistent(elements) {
|
|
51
|
+
const stringsCount = elements.filter(isStringLiteral).length;
|
|
52
|
+
|
|
53
|
+
if (elements.length === stringsCount)
|
|
54
|
+
return true;
|
|
55
|
+
|
|
56
|
+
const identifiersCount = elements.filter(isIdentifier).length;
|
|
57
|
+
|
|
58
|
+
if (elements.length === identifiersCount)
|
|
59
|
+
return true;
|
|
60
|
+
|
|
61
|
+
return elements.filter(isTwoElementTuple).length;
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-printer",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.6.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",
|