@putout/printer 5.35.2 → 5.37.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 +10 -0
- package/lib/tokenize/comment/parse-leading-comments.js +30 -6
- package/lib/tokenize/is.js +6 -0
- package/lib/tokenize/typescript/function/ts-call-signature-declaration.js +2 -0
- package/lib/tokenize/typescript/function/ts-construct-signature-declaration.js +3 -0
- package/lib/tokenize/typescript/function/ts-method-signature.js +3 -0
- package/lib/tokenize/typescript/index.js +6 -4
- package/lib/tokenize/typescript/interface/ts-interface-body.js +8 -3
- package/lib/tokenize/typescript/interface/ts-interface-declaration.js +5 -5
- package/lib/tokenize/typescript/ts-property-signature.js +26 -4
- package/lib/tokenize/typescript/type/ts-type-literal.js +2 -8
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,10 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {types} = require('@putout/babel');
|
|
4
|
+
const {
|
|
5
|
+
hasTrailingComment,
|
|
6
|
+
satisfy,
|
|
7
|
+
isPrev,
|
|
8
|
+
} = require('../is');
|
|
4
9
|
|
|
5
10
|
const {markBefore} = require('../mark');
|
|
6
11
|
const {maybeInsideFn} = require('./maybe-inside-fn');
|
|
7
12
|
|
|
13
|
+
const {
|
|
14
|
+
isObjectProperty,
|
|
15
|
+
isVariableDeclarator,
|
|
16
|
+
isClassProperty,
|
|
17
|
+
isTSPropertySignature,
|
|
18
|
+
} = types;
|
|
19
|
+
|
|
20
|
+
const isProperty = satisfy([
|
|
21
|
+
isObjectProperty,
|
|
22
|
+
isVariableDeclarator,
|
|
23
|
+
isClassProperty,
|
|
24
|
+
isTSPropertySignature,
|
|
25
|
+
]);
|
|
26
|
+
|
|
8
27
|
module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
|
|
9
28
|
if (!semantics.comments)
|
|
10
29
|
return;
|
|
@@ -20,8 +39,9 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
|
|
|
20
39
|
return;
|
|
21
40
|
|
|
22
41
|
const insideFn = path.parentPath.isFunction();
|
|
23
|
-
|
|
24
|
-
const
|
|
42
|
+
|
|
43
|
+
const propIs = isProperty(path);
|
|
44
|
+
const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn && !propIs;
|
|
25
45
|
|
|
26
46
|
for (const {type, value} of leadingComments) {
|
|
27
47
|
maybe.indent(isIndent);
|
|
@@ -32,12 +52,12 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
|
|
|
32
52
|
indent,
|
|
33
53
|
});
|
|
34
54
|
|
|
35
|
-
maybe.print.space(
|
|
55
|
+
maybe.print.space(propIs && !path.isClassProperty());
|
|
36
56
|
print(`//${value}`);
|
|
37
57
|
|
|
38
58
|
const isParentSwitch = path.parentPath.isSwitchCase();
|
|
39
|
-
maybe.print.breakline(
|
|
40
|
-
maybe.print.newline(!
|
|
59
|
+
maybe.print.breakline(propIs || isParentSwitch);
|
|
60
|
+
maybe.print.newline(!propIs && !isParentSwitch);
|
|
41
61
|
continue;
|
|
42
62
|
}
|
|
43
63
|
|
|
@@ -46,11 +66,15 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
|
|
|
46
66
|
const looksLikeDirective = path.isDirective();
|
|
47
67
|
const looksLikeProp = path.isObjectProperty();
|
|
48
68
|
|
|
69
|
+
// || path.isTSPropertySignature();
|
|
49
70
|
if (looksLikeProp)
|
|
50
71
|
print.breakline();
|
|
51
72
|
|
|
52
73
|
print(`/*${value}*/`);
|
|
53
74
|
|
|
75
|
+
if (isTSPropertySignature(path) && !isPrev(path))
|
|
76
|
+
print.breakline();
|
|
77
|
+
|
|
54
78
|
if (path.isStatement() || path.isTSEnumMember() || looksLikeDirective || looksLikeMethod || looksLikeProp || looksLikeSwitchCase) {
|
|
55
79
|
print.newline();
|
|
56
80
|
markBefore(path);
|
package/lib/tokenize/is.js
CHANGED
|
@@ -23,12 +23,18 @@ const isNext = (path) => {
|
|
|
23
23
|
return !next.isEmptyStatement();
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
+
const isPrev = (path) => {
|
|
27
|
+
const next = path.getPrevSibling();
|
|
28
|
+
return next.node;
|
|
29
|
+
};
|
|
30
|
+
|
|
26
31
|
const isNextParent = (path) => isNext(path.parentPath);
|
|
27
32
|
const isLast = (path) => isParentProgram(path) && !isNext(path);
|
|
28
33
|
|
|
29
34
|
module.exports.isFirst = (path) => path.node === path.parentPath.node.body?.[0];
|
|
30
35
|
module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
|
|
31
36
|
module.exports.isNext = isNext;
|
|
37
|
+
module.exports.isPrev = isPrev;
|
|
32
38
|
module.exports.isNextParent = isNextParent;
|
|
33
39
|
module.exports.isParentProgram = isParentProgram;
|
|
34
40
|
module.exports.isParentBlock = isParentBlock;
|
|
@@ -34,6 +34,7 @@ const {
|
|
|
34
34
|
maybeParenOpen,
|
|
35
35
|
maybeParenClose,
|
|
36
36
|
} = require('../expressions/unary-expression/parens');
|
|
37
|
+
const {maybePrintTypeAnnotation} = require('../maybe/maybe-type-annotation');
|
|
37
38
|
|
|
38
39
|
module.exports = {
|
|
39
40
|
TSAsExpression,
|
|
@@ -180,13 +181,14 @@ module.exports = {
|
|
|
180
181
|
print('__typeAnnotation');
|
|
181
182
|
},
|
|
182
183
|
TSConstructSignatureDeclaration,
|
|
183
|
-
TSIndexSignature(path,
|
|
184
|
+
TSIndexSignature(path, printer) {
|
|
185
|
+
const {print} = printer;
|
|
184
186
|
print('[');
|
|
185
187
|
print('__parameters.0');
|
|
186
188
|
print(']');
|
|
187
|
-
|
|
188
|
-
print
|
|
189
|
-
print(
|
|
189
|
+
maybePrintTypeAnnotation(path, printer);
|
|
190
|
+
print(';');
|
|
191
|
+
print.newline();
|
|
190
192
|
},
|
|
191
193
|
TSExpressionWithTypeArguments(path, {print}) {
|
|
192
194
|
print('__expression');
|
|
@@ -10,12 +10,17 @@ module.exports.TSInterfaceBody = (path, {traverse, write, indent, maybe}) => {
|
|
|
10
10
|
for (const item of body) {
|
|
11
11
|
indent();
|
|
12
12
|
traverse(item);
|
|
13
|
-
write(';');
|
|
14
|
-
write.newline();
|
|
15
13
|
}
|
|
16
14
|
|
|
17
15
|
indent.dec();
|
|
18
16
|
indent();
|
|
19
17
|
write('}');
|
|
20
|
-
maybe.write.newline(path
|
|
18
|
+
maybe.write.newline(findTSModuleBlock(path));
|
|
21
19
|
};
|
|
20
|
+
|
|
21
|
+
function findTSModuleBlock(path) {
|
|
22
|
+
if (path.parentPath.parentPath.isTSModuleBlock())
|
|
23
|
+
return true;
|
|
24
|
+
|
|
25
|
+
return path.parentPath.parentPath.parentPath?.isTSModuleBlock();
|
|
26
|
+
}
|
|
@@ -9,10 +9,10 @@ const {isNext, isNextParent} = require('../../is');
|
|
|
9
9
|
const {maybeDeclare} = require('../../maybe/maybe-declare');
|
|
10
10
|
|
|
11
11
|
module.exports.TSInterfaceDeclaration = {
|
|
12
|
-
print: maybeDeclare((path, {print,
|
|
13
|
-
const {node} = path;
|
|
12
|
+
print: maybeDeclare((path, {print, maybe}) => {
|
|
13
|
+
const {node, parentPath} = path;
|
|
14
14
|
|
|
15
|
-
indent();
|
|
15
|
+
maybe.indent(!isExportDeclaration(parentPath));
|
|
16
16
|
print('interface ');
|
|
17
17
|
print('__id');
|
|
18
18
|
|
|
@@ -28,10 +28,10 @@ module.exports.TSInterfaceDeclaration = {
|
|
|
28
28
|
}),
|
|
29
29
|
afterSatisfy: () => [isNext, isNextParent],
|
|
30
30
|
after(path, {print}) {
|
|
31
|
-
const next = path.parentPath.getNextSibling();
|
|
32
|
-
|
|
33
31
|
print.breakline();
|
|
34
32
|
|
|
33
|
+
const next = path.parentPath.getNextSibling();
|
|
34
|
+
|
|
35
35
|
if (!isTSTypeAliasDeclaration(next) && !isExportDeclaration(next))
|
|
36
36
|
print.breakline();
|
|
37
37
|
},
|
|
@@ -1,10 +1,32 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {maybePrintTypeAnnotation} = require('../maybe/maybe-type-annotation');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const {
|
|
6
|
+
hasTrailingComment,
|
|
7
|
+
isNext,
|
|
8
|
+
} = require('../is');
|
|
9
|
+
|
|
10
|
+
module.exports.TSPropertySignature = (path, printer) => {
|
|
11
|
+
const {
|
|
12
|
+
print,
|
|
13
|
+
maybe,
|
|
14
|
+
write,
|
|
15
|
+
} = printer;
|
|
16
|
+
|
|
17
|
+
const {optional, readonly} = path.node;
|
|
7
18
|
|
|
19
|
+
maybe.print(readonly, 'readonly ');
|
|
8
20
|
print('__key');
|
|
9
21
|
maybe.print(optional, '?');
|
|
10
|
-
|
|
22
|
+
|
|
23
|
+
maybePrintTypeAnnotation(path, printer);
|
|
24
|
+
|
|
25
|
+
if (!path.parentPath.parentPath.isTSTypeParameterInstantiation()) {
|
|
26
|
+
write(';');
|
|
27
|
+
write.newline();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (isNext(path) && hasTrailingComment(path))
|
|
31
|
+
write.newline();
|
|
32
|
+
};
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
|
|
3
|
+
module.exports.TSTypeLiteral = (path, {indent, traverse, write}) => {
|
|
6
4
|
const members = path.get('members');
|
|
7
5
|
write('{');
|
|
8
6
|
|
|
@@ -16,15 +14,11 @@ module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
|
|
|
16
14
|
for (const member of members) {
|
|
17
15
|
indent();
|
|
18
16
|
traverse(member);
|
|
19
|
-
maybe.write(is, ';');
|
|
20
|
-
|
|
21
|
-
if (is && isNext(member))
|
|
22
|
-
write.newline();
|
|
23
17
|
}
|
|
24
18
|
|
|
25
19
|
if (is) {
|
|
26
20
|
indent.dec();
|
|
27
|
-
write.
|
|
21
|
+
write.indent();
|
|
28
22
|
}
|
|
29
23
|
|
|
30
24
|
write('}');
|
package/package.json
CHANGED