@putout/printer 1.44.0 → 1.46.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/README.md +78 -2
- package/lib/printer.js +1 -3
- package/lib/tokenize/comments.js +0 -2
- package/lib/tokenize/statements/export-declarations.js +1 -0
- package/lib/tokenize/typescript/index.js +43 -1
- package/lib/tokenize/typescript/ts-mapped-type.js +36 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/README.md
CHANGED
|
@@ -7,7 +7,13 @@ Prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readabl
|
|
|
7
7
|
|
|
8
8
|
- ☝️ Similar to **Recast**, but simpler and easier in maintenance, since it supports only **Babel**.
|
|
9
9
|
- ☝️ As opinionated as **Prettier**, but has more user-friendly output and works directly with **AST**.
|
|
10
|
-
- ☝️ Like **ESLint** but
|
|
10
|
+
- ☝️ Like **ESLint** but works directly with **Babel AST**.
|
|
11
|
+
- ☝️ Easily extandable with help of [Overrides](h#overrides).
|
|
12
|
+
|
|
13
|
+
Supports:
|
|
14
|
+
- ✅ **ES2023**;
|
|
15
|
+
- ✅ **JSX**;
|
|
16
|
+
- ✅ **TypeScript**;
|
|
11
17
|
|
|
12
18
|
## Install
|
|
13
19
|
|
|
@@ -64,7 +70,27 @@ print(ast, {
|
|
|
64
70
|
'const {a /* [hello world] */= 5} = b;\n';
|
|
65
71
|
```
|
|
66
72
|
|
|
67
|
-
|
|
73
|
+
### `print`
|
|
74
|
+
|
|
75
|
+
Used in previous example `print` can be used for a couple purposes:
|
|
76
|
+
|
|
77
|
+
- to print `string`;
|
|
78
|
+
- to print `node` when `object` passed;
|
|
79
|
+
- to print `node` when `string` started with `__`;
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
print(ast, {
|
|
83
|
+
visitors: {
|
|
84
|
+
AssignmentPattern(path, {print, maybe}) {
|
|
85
|
+
maybe.print.newline(path.parentPath.isCallExpression());
|
|
86
|
+
print(' /* [hello world] */= ');
|
|
87
|
+
print('__right');
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `maybe`
|
|
68
94
|
|
|
69
95
|
When you need some condition use `maybe`. For example, to add newline only when parent node is `CallExpression` you
|
|
70
96
|
can use `maybe.print.newline(condition)`:
|
|
@@ -81,6 +107,56 @@ print(ast, {
|
|
|
81
107
|
});
|
|
82
108
|
```
|
|
83
109
|
|
|
110
|
+
### `write`
|
|
111
|
+
|
|
112
|
+
When are you going to output string you can use low-level function `write`:
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
print(ast, {
|
|
116
|
+
visitors: {
|
|
117
|
+
BlockStatement(path, {write}) {
|
|
118
|
+
write('hello');
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### `indent`
|
|
125
|
+
|
|
126
|
+
When you need to add indentation use `indent`, for example when you output body,
|
|
127
|
+
you need to increment indentation, and then decrement it back:
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
print(ast, {
|
|
131
|
+
visitors: {
|
|
132
|
+
BlockStatement(path, {write, indent}) {
|
|
133
|
+
write('{');
|
|
134
|
+
indent.inc();
|
|
135
|
+
indent();
|
|
136
|
+
write('some;');
|
|
137
|
+
indent.dec();
|
|
138
|
+
write('{');
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### `traverse`
|
|
145
|
+
|
|
146
|
+
When are you needing to traverse node, you can use `traverse`:
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
print(ast, {
|
|
150
|
+
visitors: {
|
|
151
|
+
AssignmentExpression(path, {traverse}) {
|
|
152
|
+
traverse(path.get('left'));
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
This is the same as `print('__left')` but more low-level, and supports only objects.
|
|
159
|
+
|
|
84
160
|
## License
|
|
85
161
|
|
|
86
162
|
MIT
|
package/lib/printer.js
CHANGED
package/lib/tokenize/comments.js
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
const {exists} = require('../is');
|
|
4
4
|
const {TSTypeLiteral} = require('./ts-type-literal');
|
|
5
5
|
const {TSTypeAliasDeclaration} = require('./ts-type-alias-declaration');
|
|
6
|
+
const {TSMappedType} = require('./ts-mapped-type');
|
|
6
7
|
|
|
7
8
|
module.exports = {
|
|
8
9
|
TSTypeLiteral,
|
|
9
10
|
TSTypeAliasDeclaration,
|
|
11
|
+
TSMappedType,
|
|
10
12
|
TSTypeParameterDeclaration(path, {print}) {
|
|
11
13
|
print('<');
|
|
12
14
|
|
|
@@ -33,12 +35,46 @@ module.exports = {
|
|
|
33
35
|
print('__elementType');
|
|
34
36
|
print('[]');
|
|
35
37
|
},
|
|
36
|
-
TSTypeParameter(path, {write}) {
|
|
38
|
+
TSTypeParameter(path, {write, traverse}) {
|
|
39
|
+
const constraint = path.get('constraint');
|
|
37
40
|
write(path.node.name);
|
|
41
|
+
|
|
42
|
+
if (exists(constraint)) {
|
|
43
|
+
write(' in ');
|
|
44
|
+
traverse(constraint);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
TSTypeOperator(path, {write, print}) {
|
|
48
|
+
const {operator} = path.node;
|
|
49
|
+
write(`${operator} `);
|
|
50
|
+
print('__typeAnnotation');
|
|
51
|
+
},
|
|
52
|
+
TSInterfaceDeclaration(path, {print}) {
|
|
53
|
+
print('interface ');
|
|
54
|
+
print('__id');
|
|
55
|
+
print('__body');
|
|
56
|
+
},
|
|
57
|
+
TSInterfaceBody(path, {traverse, write, indent}) {
|
|
58
|
+
write(' {');
|
|
59
|
+
write.newline();
|
|
60
|
+
indent.inc();
|
|
61
|
+
|
|
62
|
+
for (const item of path.get('body')) {
|
|
63
|
+
indent();
|
|
64
|
+
traverse(item);
|
|
65
|
+
write(';');
|
|
66
|
+
write.newline();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
indent.dec();
|
|
70
|
+
write('}');
|
|
38
71
|
},
|
|
39
72
|
TSUndefinedKeyword(path, {write}) {
|
|
40
73
|
write('undefined');
|
|
41
74
|
},
|
|
75
|
+
TSBooleanKeyword(path, {write}) {
|
|
76
|
+
write('boolean');
|
|
77
|
+
},
|
|
42
78
|
TSUnionType(path, {traverse, write}) {
|
|
43
79
|
const types = path.get('types');
|
|
44
80
|
const n = types.length - 1;
|
|
@@ -56,6 +92,12 @@ module.exports = {
|
|
|
56
92
|
TSNumberKeyword(path, {write}) {
|
|
57
93
|
write('number');
|
|
58
94
|
},
|
|
95
|
+
TSIndexedAccessType(path, {print}) {
|
|
96
|
+
print('__objectType');
|
|
97
|
+
print('[');
|
|
98
|
+
print('__indexType');
|
|
99
|
+
print(']');
|
|
100
|
+
},
|
|
59
101
|
TSStringKeyword(path, {write}) {
|
|
60
102
|
write('string');
|
|
61
103
|
},
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.TSMappedType = (path, {print, write, indent, maybe}) => {
|
|
4
|
+
const {
|
|
5
|
+
readonly,
|
|
6
|
+
optional,
|
|
7
|
+
} = path.node;
|
|
8
|
+
|
|
9
|
+
write('{');
|
|
10
|
+
write.newline();
|
|
11
|
+
indent.inc();
|
|
12
|
+
indent();
|
|
13
|
+
|
|
14
|
+
if (readonly) {
|
|
15
|
+
maybe.write(readonly === '-', '-');
|
|
16
|
+
write('readonly ');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
write('[');
|
|
20
|
+
print('__typeParameter');
|
|
21
|
+
write(']');
|
|
22
|
+
|
|
23
|
+
if (optional) {
|
|
24
|
+
maybe.write(optional === '+', '+');
|
|
25
|
+
maybe.write(optional === '-', '-');
|
|
26
|
+
write('?');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
write(':');
|
|
30
|
+
write.space();
|
|
31
|
+
print('__typeAnnotation');
|
|
32
|
+
write(';');
|
|
33
|
+
indent.dec();
|
|
34
|
+
write.newline();
|
|
35
|
+
write('}');
|
|
36
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.46.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",
|