@putout/printer 1.33.0 → 1.35.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/is.js +1 -0
- package/lib/tokenize/jsx/index.js +13 -7
- package/lib/tokenize/jsx/jsx-attribute.js +21 -0
- package/lib/tokenize/jsx/jsx-element.js +5 -3
- package/lib/tokenize/tokenize.js +6 -1
- package/lib/tokenize/typescript/index.js +17 -19
- package/lib/tokenize/typescript/ts-type-literal.js +35 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/lib/tokenize/is.js
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {JSXElement} = require('./jsx-element');
|
|
4
|
+
const {JSXAttribute} = require('./jsx-attribute');
|
|
5
|
+
const {isCoupleLines} = require('../is');
|
|
4
6
|
|
|
5
7
|
module.exports = {
|
|
6
8
|
JSXElement,
|
|
7
|
-
|
|
9
|
+
JSXAttribute,
|
|
10
|
+
JSXOpeningElement(path, {print, maybe}) {
|
|
8
11
|
print('<');
|
|
9
12
|
print('__name');
|
|
10
13
|
|
|
14
|
+
const noCoupleLine = !isCoupleLines(path);
|
|
15
|
+
|
|
11
16
|
for (const attr of path.get('attributes')) {
|
|
12
|
-
print.space();
|
|
17
|
+
maybe.print.space(noCoupleLine);
|
|
13
18
|
print(attr);
|
|
14
19
|
}
|
|
15
20
|
|
|
21
|
+
if (isCoupleLines(path))
|
|
22
|
+
print.breakline();
|
|
23
|
+
|
|
24
|
+
if (path.node.selfClosing)
|
|
25
|
+
print('/');
|
|
26
|
+
|
|
16
27
|
print('>');
|
|
17
28
|
},
|
|
18
29
|
JSXExpressionContainer(path, {print}) {
|
|
@@ -20,11 +31,6 @@ module.exports = {
|
|
|
20
31
|
print('__expression');
|
|
21
32
|
print('}');
|
|
22
33
|
},
|
|
23
|
-
JSXAttribute(path, {print}) {
|
|
24
|
-
print('__name');
|
|
25
|
-
print('=');
|
|
26
|
-
print('__value');
|
|
27
|
-
},
|
|
28
34
|
JSXIdentifier(path, {write}) {
|
|
29
35
|
write(path.node.name);
|
|
30
36
|
},
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {isCoupleLines} = require('../is');
|
|
4
|
+
|
|
5
|
+
module.exports.JSXAttribute = {
|
|
6
|
+
condition(path) {
|
|
7
|
+
return isCoupleLines(path.parentPath);
|
|
8
|
+
},
|
|
9
|
+
before(path, {print, indent}) {
|
|
10
|
+
indent.inc();
|
|
11
|
+
print.breakline();
|
|
12
|
+
},
|
|
13
|
+
print(path, {print}) {
|
|
14
|
+
print('__name');
|
|
15
|
+
print('=');
|
|
16
|
+
print('__value');
|
|
17
|
+
},
|
|
18
|
+
after(path, {indent}) {
|
|
19
|
+
indent.dec();
|
|
20
|
+
},
|
|
21
|
+
};
|
|
@@ -4,9 +4,8 @@ module.exports.JSXElement = {
|
|
|
4
4
|
condition,
|
|
5
5
|
before(path, {write, indent}) {
|
|
6
6
|
write('(');
|
|
7
|
-
write.breakline();
|
|
8
|
-
indent();
|
|
9
7
|
indent.inc();
|
|
8
|
+
write.breakline();
|
|
10
9
|
},
|
|
11
10
|
print(path, {print, traverse}) {
|
|
12
11
|
print('__openingElement');
|
|
@@ -21,5 +20,8 @@ module.exports.JSXElement = {
|
|
|
21
20
|
};
|
|
22
21
|
|
|
23
22
|
function condition(path) {
|
|
24
|
-
|
|
23
|
+
if (path.parentPath.isReturnStatement())
|
|
24
|
+
return true;
|
|
25
|
+
|
|
26
|
+
return path.parentPath.isVariableDeclarator();
|
|
25
27
|
}
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -114,7 +114,12 @@ module.exports.tokenize = (ast, overrides = {}) => {
|
|
|
114
114
|
const breakline = () => {
|
|
115
115
|
addToken({
|
|
116
116
|
type: TYPES.NEWLINE,
|
|
117
|
-
value:
|
|
117
|
+
value: '\n',
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
addToken({
|
|
121
|
+
type: TYPES.SPACE,
|
|
122
|
+
value: printIndent(i, format.indent),
|
|
118
123
|
});
|
|
119
124
|
};
|
|
120
125
|
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {exists} = require('../is');
|
|
4
|
+
const {TSTypeLiteral} = require('./ts-type-literal');
|
|
5
|
+
|
|
3
6
|
module.exports = {
|
|
7
|
+
TSTypeLiteral,
|
|
4
8
|
TSTypeParameterDeclaration(path, {print}) {
|
|
5
9
|
print('<');
|
|
6
10
|
|
|
@@ -28,6 +32,10 @@ module.exports = {
|
|
|
28
32
|
TSNumberKeyword(path, {write}) {
|
|
29
33
|
write('number');
|
|
30
34
|
},
|
|
35
|
+
TSInstantiationExpression(path, {print}) {
|
|
36
|
+
print('__expression');
|
|
37
|
+
print('__typeParameters');
|
|
38
|
+
},
|
|
31
39
|
TSAnyKeyword(path, {write}) {
|
|
32
40
|
write('any');
|
|
33
41
|
},
|
|
@@ -63,27 +71,17 @@ module.exports = {
|
|
|
63
71
|
print('__typeAnnotation');
|
|
64
72
|
print(';');
|
|
65
73
|
},
|
|
66
|
-
|
|
67
|
-
write('{');
|
|
68
|
-
write.newline();
|
|
69
|
-
indent.inc();
|
|
70
|
-
|
|
71
|
-
for (const member of path.get('members')) {
|
|
72
|
-
indent();
|
|
73
|
-
traverse(member);
|
|
74
|
-
write(';');
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
write.newline();
|
|
78
|
-
indent.dec();
|
|
79
|
-
write('}');
|
|
80
|
-
},
|
|
81
|
-
TSPropertySignature(path, {print, maybe}) {
|
|
74
|
+
TSPropertySignature(path, {print, maybe, traverse}) {
|
|
82
75
|
const {optional} = path.node;
|
|
76
|
+
const typeAnnotation = path.get('typeAnnotation');
|
|
77
|
+
|
|
83
78
|
print('__key');
|
|
84
79
|
maybe.print(optional, '?');
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
80
|
+
|
|
81
|
+
if (exists(typeAnnotation)) {
|
|
82
|
+
print(':');
|
|
83
|
+
print.space();
|
|
84
|
+
traverse(typeAnnotation);
|
|
85
|
+
}
|
|
88
86
|
},
|
|
89
87
|
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.TSTypeLiteral = (path, {indent, traverse, write, maybe}) => {
|
|
4
|
+
const members = path.get('members');
|
|
5
|
+
write('{');
|
|
6
|
+
|
|
7
|
+
const is = isNewline(path);
|
|
8
|
+
|
|
9
|
+
if (is) {
|
|
10
|
+
write.newline();
|
|
11
|
+
indent.inc();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
for (const member of members) {
|
|
15
|
+
indent();
|
|
16
|
+
traverse(member);
|
|
17
|
+
maybe.write(is, ';');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (is) {
|
|
21
|
+
write.newline();
|
|
22
|
+
indent.inc();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
write('}');
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function isNewline(path) {
|
|
29
|
+
const members = path.get('members');
|
|
30
|
+
|
|
31
|
+
if (members.length && members[0].node.typeAnnotation)
|
|
32
|
+
return true;
|
|
33
|
+
|
|
34
|
+
return false;
|
|
35
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.35.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",
|