@putout/plugin-printer 8.2.1 → 8.3.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 +24 -0
- package/lib/add-missing-spaces-to-type-checker/index.js +87 -0
- package/lib/index.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ npm i @putout/plugin-printer -D
|
|
|
14
14
|
## Rules
|
|
15
15
|
|
|
16
16
|
- ✅ [add-args](#add-args);
|
|
17
|
+
- ✅ [add-missing-spacess-to-type-checker](#add-missing-spacess-to-type-checker);
|
|
17
18
|
- ✅ [apply-breakline](#apply-breakline);
|
|
18
19
|
- ✅ [apply-computed-print](#apply-computed-print);
|
|
19
20
|
- ✅ [apply-create-test-url](#apply-create-test-url);
|
|
@@ -31,6 +32,7 @@ npm i @putout/plugin-printer -D
|
|
|
31
32
|
{
|
|
32
33
|
"rules": {
|
|
33
34
|
"printer/add-args": "on",
|
|
35
|
+
"printer/add-missing-spacess-to-type-checker": "on",
|
|
34
36
|
"printer/apply-breakline": "on",
|
|
35
37
|
"printer/apply-linebreak": "on",
|
|
36
38
|
"printer/apply-computed-print": "on",
|
|
@@ -96,6 +98,28 @@ module.exports = {
|
|
|
96
98
|
};
|
|
97
99
|
```
|
|
98
100
|
|
|
101
|
+
## add-missing-spacess-to-type-checker
|
|
102
|
+
|
|
103
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/2451cd51ebd3dfd20c32b6d22ca176b7/bd593860ff4bb2d66e06cf95bbe103cb1203129d).
|
|
104
|
+
|
|
105
|
+
### ❌ Example of incorrect code
|
|
106
|
+
|
|
107
|
+
```js
|
|
108
|
+
export const beforeIf = createTypeChecker([
|
|
109
|
+
['-:->!', isInsideArray],
|
|
110
|
+
['-:parentPath->', isCoupleLines],
|
|
111
|
+
]);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### ✅ Example of correct code
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
export const beforeIf = createTypeChecker([
|
|
118
|
+
['-: -> !', isInsideArray],
|
|
119
|
+
['-: parentPath ->', isCoupleLines],
|
|
120
|
+
]);
|
|
121
|
+
```
|
|
122
|
+
|
|
99
123
|
## apply-computed-print
|
|
100
124
|
|
|
101
125
|
### ❌ Example of incorrect code
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {operator, types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
isIdentifier,
|
|
5
|
+
isCallExpression,
|
|
6
|
+
} = types;
|
|
7
|
+
|
|
8
|
+
const {setLiteralValue} = operator;
|
|
9
|
+
|
|
10
|
+
export const report = ({path, where}) => {
|
|
11
|
+
const {value} = path.node;
|
|
12
|
+
const withSpaces = addSpaces(value, where);
|
|
13
|
+
|
|
14
|
+
return `Add missing spaces: '${value}' -> '${withSpaces}'`;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const fix = ({path, where}) => {
|
|
18
|
+
const {value} = path.node;
|
|
19
|
+
setLiteralValue(path, addSpaces(value, where));
|
|
20
|
+
};
|
|
21
|
+
export const traverse = ({push}) => ({
|
|
22
|
+
StringLiteral(path) {
|
|
23
|
+
const call = path.find(isCallExpression);
|
|
24
|
+
|
|
25
|
+
if (!call)
|
|
26
|
+
return;
|
|
27
|
+
|
|
28
|
+
if (!isIdentifier(call.node.callee, {name: 'createTypeChecker'}))
|
|
29
|
+
return;
|
|
30
|
+
|
|
31
|
+
const {value} = path.node;
|
|
32
|
+
const where = createWhere(value);
|
|
33
|
+
|
|
34
|
+
if (where.length)
|
|
35
|
+
push({
|
|
36
|
+
path,
|
|
37
|
+
where,
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function createWhere(value) {
|
|
43
|
+
const where = [];
|
|
44
|
+
const includes = value.includes.bind(value);
|
|
45
|
+
const endsWith = value.endsWith.bind(value);
|
|
46
|
+
const push = where.push.bind(where);
|
|
47
|
+
|
|
48
|
+
const tools = {
|
|
49
|
+
push,
|
|
50
|
+
includes,
|
|
51
|
+
endsWith,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
if (includes('->'))
|
|
55
|
+
traceArrow(tools);
|
|
56
|
+
|
|
57
|
+
if (includes(':'))
|
|
58
|
+
traceColon(tools);
|
|
59
|
+
|
|
60
|
+
return where.filter(Boolean);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function traceColon({push, includes}) {
|
|
64
|
+
if (!includes(': ') && !includes(':->'))
|
|
65
|
+
push('after-colon');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function traceArrow({includes, endsWith, push}) {
|
|
69
|
+
if (!includes(' ->'))
|
|
70
|
+
push('before-arrow');
|
|
71
|
+
|
|
72
|
+
if (!includes(' ->') && !endsWith('->'))
|
|
73
|
+
push('after-arrow');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function addSpaces(value, where) {
|
|
77
|
+
if (where.includes('before-arrow'))
|
|
78
|
+
value = value.replace('->', ' ->');
|
|
79
|
+
|
|
80
|
+
if (where.includes('after-arrow'))
|
|
81
|
+
value = value.replace('->', '-> ');
|
|
82
|
+
|
|
83
|
+
if (where.includes('after-colon'))
|
|
84
|
+
value = value.replace(':', ': ');
|
|
85
|
+
|
|
86
|
+
return value;
|
|
87
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as addMissingSpacesToTypeChecker from './add-missing-spaces-to-type-checker/index.js';
|
|
1
2
|
import * as removeTrailingSpacesFromTypeChecker from './remove-trailing-spaces-from-type-checker/index.js';
|
|
2
3
|
import * as removeUselessMaybe from './remove-useless-maybe/index.js';
|
|
3
4
|
import * as applyCreateTestUrl from './apply-create-test-url/index.js';
|
|
@@ -22,4 +23,5 @@ export const rules = {
|
|
|
22
23
|
'apply-create-test-url': applyCreateTestUrl,
|
|
23
24
|
'remove-useless-maybe': removeUselessMaybe,
|
|
24
25
|
'remove-trailing-spaces-from-type-checker': removeTrailingSpacesFromTypeChecker,
|
|
26
|
+
'add-missing-spaces-to-type-checker': addMissingSpacesToTypeChecker,
|
|
25
27
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/plugin-printer",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.3.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",
|