@putout/printer 1.116.0 → 1.118.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/ChangeLog +13 -3
- package/README.md +28 -28
- package/lib/tokenize/expressions/object-pattern.js +5 -1
- package/lib/tokenize/statements/import-declaration/import-declaration.js +6 -3
- package/lib/tokenize/statements/index.js +1 -1
- package/lib/tokenize/statements/variable-declaration/maybe-space-after-keyword.js +10 -0
- package/lib/tokenize/statements/{variable-declaration.js → variable-declaration/variable-declaration.js} +6 -3
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
2023.05.25, v1.118.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 438921c @putout/printer: VariableDeclaration: space after keyword
|
|
5
|
+
|
|
6
|
+
2023.05.25, v1.117.0
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- 9607830 @putout/printer: ImportDeclaration: space
|
|
10
|
+
|
|
1
11
|
2023.05.25, v1.116.0
|
|
2
12
|
|
|
3
13
|
feature:
|
|
@@ -1057,7 +1067,7 @@ feature:
|
|
|
1057
1067
|
2023.03.23, v1.7.1
|
|
1058
1068
|
|
|
1059
1069
|
feature:
|
|
1060
|
-
- f1db3bd @putout/printer:
|
|
1070
|
+
- f1db3bd @putout/printer: write: path.get(__a) => __a
|
|
1061
1071
|
|
|
1062
1072
|
2023.03.23, v1.7.0
|
|
1063
1073
|
|
|
@@ -1117,7 +1127,7 @@ feature:
|
|
|
1117
1127
|
2023.03.20, v1.6.3
|
|
1118
1128
|
|
|
1119
1129
|
feature:
|
|
1120
|
-
- 052b966 @putout/printer: write ->
|
|
1130
|
+
- 052b966 @putout/printer: write -> write
|
|
1121
1131
|
|
|
1122
1132
|
2023.03.20, v1.6.2
|
|
1123
1133
|
|
|
@@ -1152,7 +1162,7 @@ feature:
|
|
|
1152
1162
|
2023.03.18, v1.5.1
|
|
1153
1163
|
|
|
1154
1164
|
feature:
|
|
1155
|
-
- a79e4f6 @putout/printer:
|
|
1165
|
+
- a79e4f6 @putout/printer: write, indent, maybe
|
|
1156
1166
|
|
|
1157
1167
|
2023.03.18, v1.5.0
|
|
1158
1168
|
|
package/README.md
CHANGED
|
@@ -46,11 +46,11 @@ To benefit from it.
|
|
|
46
46
|
## API
|
|
47
47
|
|
|
48
48
|
```js
|
|
49
|
-
const {
|
|
49
|
+
const {write} = require('@putout/printer');
|
|
50
50
|
const {parse} = require('@babel/parser');
|
|
51
51
|
const ast = parse('const a = (b, c) => {const d = 5; return a;}');
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
write(ast);
|
|
54
54
|
// returns
|
|
55
55
|
`
|
|
56
56
|
const a = (b, c) => {
|
|
@@ -65,9 +65,9 @@ const a = (b, c) => {
|
|
|
65
65
|
When you need to extend syntax of `@putout/printer` just pass a function which receives:
|
|
66
66
|
|
|
67
67
|
- `path`, Babel Path
|
|
68
|
-
- `
|
|
68
|
+
- `write`, a function to output result of printing into token array;
|
|
69
69
|
|
|
70
|
-
When `path` contains to dashes `__` and name, it is the same as: `
|
|
70
|
+
When `path` contains to dashes `__` and name, it is the same as: `write(path.get('right'))`, and this is
|
|
71
71
|
actually `traverse(path.get('right'))` shortened to simplify read and process.
|
|
72
72
|
|
|
73
73
|
Here is how you can override `AssignmentPattern`:
|
|
@@ -75,7 +75,7 @@ Here is how you can override `AssignmentPattern`:
|
|
|
75
75
|
```js
|
|
76
76
|
const ast = parse('const {a = 5} = b');
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
write(ast, {
|
|
79
79
|
format: {
|
|
80
80
|
indent: ' ',
|
|
81
81
|
newline: '\n',
|
|
@@ -84,9 +84,9 @@ print(ast, {
|
|
|
84
84
|
splitter: '\n',
|
|
85
85
|
},
|
|
86
86
|
visitors: {
|
|
87
|
-
AssignmentPattern(path, {
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
AssignmentPattern(path, {write}) {
|
|
88
|
+
write('/* [hello world] */= ');
|
|
89
|
+
write('__right');
|
|
90
90
|
},
|
|
91
91
|
},
|
|
92
92
|
});
|
|
@@ -148,21 +148,21 @@ else
|
|
|
148
148
|
console.log('not ok');
|
|
149
149
|
```
|
|
150
150
|
|
|
151
|
-
### `
|
|
151
|
+
### `write`
|
|
152
152
|
|
|
153
|
-
Used in previous example `
|
|
153
|
+
Used in previous example `write` can be used for a couple purposes:
|
|
154
154
|
|
|
155
|
-
- to
|
|
156
|
-
- to
|
|
157
|
-
- to
|
|
155
|
+
- to write `string`;
|
|
156
|
+
- to write `node` when `object` passed;
|
|
157
|
+
- to write `node` when `string` started with `__`;
|
|
158
158
|
|
|
159
159
|
```js
|
|
160
|
-
|
|
160
|
+
write(ast, {
|
|
161
161
|
visitors: {
|
|
162
|
-
AssignmentPattern(path, {
|
|
163
|
-
maybe.
|
|
164
|
-
|
|
165
|
-
|
|
162
|
+
AssignmentPattern(path, {write, maybe}) {
|
|
163
|
+
maybe.write.newline(path.parentPath.isCallExpression());
|
|
164
|
+
write('/* [hello world] */= ');
|
|
165
|
+
write('__right');
|
|
166
166
|
},
|
|
167
167
|
},
|
|
168
168
|
});
|
|
@@ -171,15 +171,15 @@ print(ast, {
|
|
|
171
171
|
### `maybe`
|
|
172
172
|
|
|
173
173
|
When you need some condition use `maybe`. For example, to add newline only when parent node is `CallExpression` you
|
|
174
|
-
can use `maybe.
|
|
174
|
+
can use `maybe.write.newline(condition)`:
|
|
175
175
|
|
|
176
176
|
```js
|
|
177
|
-
|
|
177
|
+
write(ast, {
|
|
178
178
|
visitors: {
|
|
179
|
-
AssignmentPattern(path, {
|
|
180
|
-
maybe.
|
|
181
|
-
|
|
182
|
-
|
|
179
|
+
AssignmentPattern(path, {write, maybe}) {
|
|
180
|
+
maybe.write.newline(path.parentPath.isCallExpression());
|
|
181
|
+
write(' /* [hello world] */= ');
|
|
182
|
+
write('__right');
|
|
183
183
|
},
|
|
184
184
|
},
|
|
185
185
|
});
|
|
@@ -190,7 +190,7 @@ print(ast, {
|
|
|
190
190
|
When are you going to output string you can use low-level function `write`:
|
|
191
191
|
|
|
192
192
|
```js
|
|
193
|
-
|
|
193
|
+
write(ast, {
|
|
194
194
|
visitors: {
|
|
195
195
|
BlockStatement(path, {write}) {
|
|
196
196
|
write('hello');
|
|
@@ -205,7 +205,7 @@ When you need to add indentation use `indent`, for example when you output body,
|
|
|
205
205
|
you need to increment indentation, and then decrement it back:
|
|
206
206
|
|
|
207
207
|
```js
|
|
208
|
-
|
|
208
|
+
write(ast, {
|
|
209
209
|
visitors: {
|
|
210
210
|
BlockStatement(path, {write, indent}) {
|
|
211
211
|
write('{');
|
|
@@ -224,7 +224,7 @@ print(ast, {
|
|
|
224
224
|
When are you needing to traverse node, you can use `traverse`:
|
|
225
225
|
|
|
226
226
|
```js
|
|
227
|
-
|
|
227
|
+
write(ast, {
|
|
228
228
|
visitors: {
|
|
229
229
|
AssignmentExpression(path, {traverse}) {
|
|
230
230
|
traverse(path.get('left'));
|
|
@@ -233,7 +233,7 @@ print(ast, {
|
|
|
233
233
|
});
|
|
234
234
|
```
|
|
235
235
|
|
|
236
|
-
This is the same as `
|
|
236
|
+
This is the same as `write('__left')` but more low-level, and supports only objects.
|
|
237
237
|
|
|
238
238
|
## Speed Comparison
|
|
239
239
|
|
|
@@ -67,7 +67,10 @@ module.exports.ObjectPattern = {
|
|
|
67
67
|
continue;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
if (i < n) {
|
|
71
|
+
print(',');
|
|
72
|
+
print.space();
|
|
73
|
+
}
|
|
71
74
|
}
|
|
72
75
|
|
|
73
76
|
indent.dec();
|
|
@@ -111,3 +114,4 @@ function shouldAddNewline(path) {
|
|
|
111
114
|
|
|
112
115
|
return false;
|
|
113
116
|
}
|
|
117
|
+
|
|
@@ -16,8 +16,8 @@ module.exports.ImportDeclaration = {
|
|
|
16
16
|
const specifiers = path.get('specifiers');
|
|
17
17
|
const {maxOneLineSpecifiers} = options.imports;
|
|
18
18
|
|
|
19
|
-
print('import
|
|
20
|
-
maybe.print(isType, 'type
|
|
19
|
+
print('import');
|
|
20
|
+
maybe.print(isType, ' type');
|
|
21
21
|
|
|
22
22
|
let wasSpecifier = false;
|
|
23
23
|
const n = specifiers.length - 1;
|
|
@@ -28,6 +28,8 @@ module.exports.ImportDeclaration = {
|
|
|
28
28
|
imports,
|
|
29
29
|
} = parseSpecifiers(specifiers);
|
|
30
30
|
|
|
31
|
+
maybe.print(specifiers.length, ' ');
|
|
32
|
+
|
|
31
33
|
for (const spec of defaults) {
|
|
32
34
|
traverse(spec.get('local'));
|
|
33
35
|
maybe.write(n, ',');
|
|
@@ -77,7 +79,8 @@ module.exports.ImportDeclaration = {
|
|
|
77
79
|
maybe.write(last, '}');
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
maybe.print(specifiers.length, ' from
|
|
82
|
+
maybe.print(specifiers.length, ' from');
|
|
83
|
+
print.space();
|
|
81
84
|
|
|
82
85
|
print('__source');
|
|
83
86
|
print(';');
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {ExpressionStatement} = require('./expression-statement');
|
|
4
|
-
const {VariableDeclaration} = require('./variable-declaration');
|
|
4
|
+
const {VariableDeclaration} = require('./variable-declaration/variable-declaration');
|
|
5
5
|
const {IfStatement} = require('./if-statement');
|
|
6
6
|
const {ForOfStatement} = require('./for-of-statement');
|
|
7
7
|
const {BlockStatement} = require('./block-statement');
|
|
@@ -6,10 +6,11 @@ const {
|
|
|
6
6
|
isNewlineBetweenSiblings,
|
|
7
7
|
exists,
|
|
8
8
|
noTrailingComment,
|
|
9
|
-
} = require('
|
|
9
|
+
} = require('../../is');
|
|
10
10
|
|
|
11
|
-
const {hasPrevNewline} = require('
|
|
11
|
+
const {hasPrevNewline} = require('../../mark');
|
|
12
12
|
const {isExportDeclaration} = require('@babel/types');
|
|
13
|
+
const {maybeSpaceAfterKeyword} = require('./maybe-space-after-keyword');
|
|
13
14
|
|
|
14
15
|
const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
|
|
15
16
|
const isInsideBlock = (path) => /^(Program|BlockStatement)$/.test(path.parentPath.type);
|
|
@@ -21,7 +22,8 @@ module.exports.VariableDeclaration = {
|
|
|
21
22
|
},
|
|
22
23
|
print(path, {maybe, store, write, traverse}) {
|
|
23
24
|
maybe.indent(isInsideBlock(path));
|
|
24
|
-
write(
|
|
25
|
+
write(String(path.node.kind));
|
|
26
|
+
maybeSpaceAfterKeyword(path, {write});
|
|
25
27
|
|
|
26
28
|
const declarations = path.get('declarations');
|
|
27
29
|
const n = declarations.length - 1;
|
|
@@ -148,3 +150,4 @@ const isNextAssign = (path) => {
|
|
|
148
150
|
.get('expression')
|
|
149
151
|
.isAssignmentExpression();
|
|
150
152
|
};
|
|
153
|
+
|
package/package.json
CHANGED