@putout/printer 1.80.4 → 1.81.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
CHANGED
package/README.md
CHANGED
package/lib/tokenize/comments.js
CHANGED
|
@@ -4,7 +4,10 @@ const {isNext} = require('./is');
|
|
|
4
4
|
|
|
5
5
|
const {markBefore} = require('./mark');
|
|
6
6
|
|
|
7
|
-
module.exports.parseLeadingComments = (path, {print, maybe, indent}) => {
|
|
7
|
+
module.exports.parseLeadingComments = (path, {print, maybe, indent}, format) => {
|
|
8
|
+
if (!format.comments)
|
|
9
|
+
return;
|
|
10
|
+
|
|
8
11
|
const {leadingComments} = path.node;
|
|
9
12
|
|
|
10
13
|
if (!leadingComments || !leadingComments.length)
|
|
@@ -45,7 +48,10 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}) => {
|
|
|
45
48
|
}
|
|
46
49
|
};
|
|
47
50
|
|
|
48
|
-
module.exports.parseTrailingComments = (path, {write, maybe}) => {
|
|
51
|
+
module.exports.parseTrailingComments = (path, {write, maybe}, format) => {
|
|
52
|
+
if (!format.comments)
|
|
53
|
+
return;
|
|
54
|
+
|
|
49
55
|
const {trailingComments} = path.node;
|
|
50
56
|
|
|
51
57
|
if (!trailingComments || !trailingComments.length)
|
|
@@ -4,10 +4,17 @@ const {markAfter} = require('../../mark');
|
|
|
4
4
|
const {isLast} = require('../../is');
|
|
5
5
|
const {parseSpecifiers} = require('./parse-specifiers');
|
|
6
6
|
|
|
7
|
+
const options = {
|
|
8
|
+
imports: {
|
|
9
|
+
maxOneLineSpecifiers: 2,
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
|
|
7
13
|
module.exports.ImportDeclaration = {
|
|
8
14
|
print(path, {print, maybe, write, traverse, indent}) {
|
|
9
15
|
const isType = path.node.importKind === 'type';
|
|
10
16
|
const specifiers = path.get('specifiers');
|
|
17
|
+
const {maxOneLineSpecifiers} = options.imports;
|
|
11
18
|
|
|
12
19
|
print('import ');
|
|
13
20
|
maybe.print(isType, 'type ');
|
|
@@ -56,12 +63,12 @@ module.exports.ImportDeclaration = {
|
|
|
56
63
|
write(spec.node.local.name);
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
if (importsCount <=
|
|
66
|
+
if (importsCount <= maxOneLineSpecifiers && notLast) {
|
|
60
67
|
maybe.write(n, ',');
|
|
61
68
|
maybe.write.space(n);
|
|
62
69
|
}
|
|
63
70
|
|
|
64
|
-
if (importsCount >
|
|
71
|
+
if (importsCount > maxOneLineSpecifiers) {
|
|
65
72
|
maybe.write(n, ',');
|
|
66
73
|
maybe.write.newline(index === n);
|
|
67
74
|
}
|
|
@@ -18,13 +18,19 @@ module.exports.VariableDeclaration = {
|
|
|
18
18
|
before(path, {print}) {
|
|
19
19
|
print.breakline();
|
|
20
20
|
},
|
|
21
|
-
print(path, {maybe, print, store}) {
|
|
21
|
+
print(path, {maybe, print, store, write}) {
|
|
22
22
|
maybe.indent(isParentBlock(path));
|
|
23
23
|
print(`${path.node.kind} `);
|
|
24
24
|
print('__declarations.0.id');
|
|
25
25
|
|
|
26
26
|
const initPath = path.get('declarations.0.init');
|
|
27
|
-
|
|
27
|
+
|
|
28
|
+
if (exists(initPath)) {
|
|
29
|
+
write.space();
|
|
30
|
+
write('=');
|
|
31
|
+
write.space();
|
|
32
|
+
}
|
|
33
|
+
|
|
28
34
|
print('__declarations.0.init');
|
|
29
35
|
maybe.print(isParentBlock(path), ';');
|
|
30
36
|
|
|
@@ -130,3 +136,4 @@ const isNextAssign = (path) => {
|
|
|
130
136
|
.get('expression')
|
|
131
137
|
.isAssignmentExpression();
|
|
132
138
|
};
|
|
139
|
+
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -48,8 +48,11 @@ const get = (path, command) => path.get(command.replace(GET, ''));
|
|
|
48
48
|
|
|
49
49
|
function initFormat(format) {
|
|
50
50
|
return {
|
|
51
|
-
...format,
|
|
52
51
|
indent: ' ',
|
|
52
|
+
newline: '\n',
|
|
53
|
+
space: ' ',
|
|
54
|
+
comments: true,
|
|
55
|
+
...format,
|
|
53
56
|
};
|
|
54
57
|
}
|
|
55
58
|
|
|
@@ -107,7 +110,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
107
110
|
const space = () => {
|
|
108
111
|
addToken({
|
|
109
112
|
type: TYPES.SPACE,
|
|
110
|
-
value:
|
|
113
|
+
value: format.space,
|
|
111
114
|
});
|
|
112
115
|
};
|
|
113
116
|
|
|
@@ -119,7 +122,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
119
122
|
const newline = () => {
|
|
120
123
|
addToken({
|
|
121
124
|
type: TYPES.NEWLINE,
|
|
122
|
-
value:
|
|
125
|
+
value: format.newline,
|
|
123
126
|
});
|
|
124
127
|
};
|
|
125
128
|
|
|
@@ -209,12 +212,12 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
209
212
|
maybeThrow(!currentTraverse, path, `Node type '{{ type }}' is not supported yet: '{{ path }}'`);
|
|
210
213
|
|
|
211
214
|
const currentIndent = i;
|
|
212
|
-
parseLeadingComments(path, printer);
|
|
215
|
+
parseLeadingComments(path, printer, format);
|
|
213
216
|
|
|
214
217
|
// this is main thing
|
|
215
218
|
maybePlugin(currentTraverse, path, printer);
|
|
216
219
|
|
|
217
|
-
parseTrailingComments(path, printer);
|
|
220
|
+
parseTrailingComments(path, printer, format);
|
|
218
221
|
maybeThrow(i !== currentIndent, path, `☝️Looks like indent level changed after token visitor: '{{ type }}', for code: '{{ path }}'`);
|
|
219
222
|
debug(path.type);
|
|
220
223
|
}
|
package/package.json
CHANGED