@putout/printer 1.45.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 CHANGED
@@ -1,3 +1,8 @@
1
+ 2023.04.14, v1.46.0
2
+
3
+ feature:
4
+ - 7fafdd3 @putout/printer: add support of TSInterfaceDeclaration
5
+
1
6
  2023.04.14, v1.45.0
2
7
 
3
8
  feature:
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 without any configuration and plugins 🤷‍, also works directly with **Babel AST** only.
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
- ## Maybe
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
@@ -49,9 +49,32 @@ module.exports = {
49
49
  write(`${operator} `);
50
50
  print('__typeAnnotation');
51
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('}');
71
+ },
52
72
  TSUndefinedKeyword(path, {write}) {
53
73
  write('undefined');
54
74
  },
75
+ TSBooleanKeyword(path, {write}) {
76
+ write('boolean');
77
+ },
55
78
  TSUnionType(path, {traverse, write}) {
56
79
  const types = path.get('types');
57
80
  const n = types.length - 1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.45.0",
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",