@putout/printer 1.117.0 → 1.119.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,13 @@
1
+ 2023.05.29, v1.119.0
2
+
3
+ feature:
4
+ - 971dad5 @putout/printer: improve support of RestSpread inside ObjectPattern
5
+
6
+ 2023.05.25, v1.118.0
7
+
8
+ feature:
9
+ - 438921c @putout/printer: VariableDeclaration: space after keyword
10
+
1
11
  2023.05.25, v1.117.0
2
12
 
3
13
  feature:
@@ -1062,7 +1072,7 @@ feature:
1062
1072
  2023.03.23, v1.7.1
1063
1073
 
1064
1074
  feature:
1065
- - f1db3bd @putout/printer: print: path.get(__a) => __a
1075
+ - f1db3bd @putout/printer: write: path.get(__a) => __a
1066
1076
 
1067
1077
  2023.03.23, v1.7.0
1068
1078
 
@@ -1122,7 +1132,7 @@ feature:
1122
1132
  2023.03.20, v1.6.3
1123
1133
 
1124
1134
  feature:
1125
- - 052b966 @putout/printer: write -> print
1135
+ - 052b966 @putout/printer: write -> write
1126
1136
 
1127
1137
  2023.03.20, v1.6.2
1128
1138
 
@@ -1157,7 +1167,7 @@ feature:
1157
1167
  2023.03.18, v1.5.1
1158
1168
 
1159
1169
  feature:
1160
- - a79e4f6 @putout/printer: print, indent, maybe
1170
+ - a79e4f6 @putout/printer: write, indent, maybe
1161
1171
 
1162
1172
  2023.03.18, v1.5.0
1163
1173
 
package/README.md CHANGED
@@ -46,11 +46,11 @@ To benefit from it.
46
46
  ## API
47
47
 
48
48
  ```js
49
- const {print} = require('@putout/printer');
49
+ const {write} = require('@putout/printer');
50
50
  const {parse} = require('@babel/parser');
51
51
  const ast = parse('const a = (b, c) => {const d = 5; return a;}');
52
52
 
53
- print(ast);
53
+ write(ast);
54
54
  // returns
55
55
  `
56
56
  const a = (b, c) => {
@@ -65,9 +65,9 @@ const a = (b, c) => {
65
65
  When you need to extend syntax of `@putout/printer` just pass a function which receives:
66
66
 
67
67
  - `path`, Babel Path
68
- - `print`, a function to output result of printing into token array;
68
+ - `write`, a function to output result of printing into token array;
69
69
 
70
- When `path` contains to dashes `__` and name, it is the same as: `print(path.get('right'))`, and this is
70
+ When `path` contains to dashes `__` and name, it is the same as: `write(path.get('right'))`, and this is
71
71
  actually `traverse(path.get('right'))` shortened to simplify read and process.
72
72
 
73
73
  Here is how you can override `AssignmentPattern`:
@@ -75,7 +75,7 @@ Here is how you can override `AssignmentPattern`:
75
75
  ```js
76
76
  const ast = parse('const {a = 5} = b');
77
77
 
78
- print(ast, {
78
+ write(ast, {
79
79
  format: {
80
80
  indent: ' ',
81
81
  newline: '\n',
@@ -84,9 +84,9 @@ print(ast, {
84
84
  splitter: '\n',
85
85
  },
86
86
  visitors: {
87
- AssignmentPattern(path, {print}) {
88
- print('/* [hello world] */= ');
89
- print('__right');
87
+ AssignmentPattern(path, {write}) {
88
+ write('/* [hello world] */= ');
89
+ write('__right');
90
90
  },
91
91
  },
92
92
  });
@@ -148,21 +148,21 @@ else
148
148
  console.log('not ok');
149
149
  ```
150
150
 
151
- ### `print`
151
+ ### `write`
152
152
 
153
- Used in previous example `print` can be used for a couple purposes:
153
+ Used in previous example `write` can be used for a couple purposes:
154
154
 
155
- - to print `string`;
156
- - to print `node` when `object` passed;
157
- - to print `node` when `string` started with `__`;
155
+ - to write `string`;
156
+ - to write `node` when `object` passed;
157
+ - to write `node` when `string` started with `__`;
158
158
 
159
159
  ```js
160
- print(ast, {
160
+ write(ast, {
161
161
  visitors: {
162
- AssignmentPattern(path, {print, maybe}) {
163
- maybe.print.newline(path.parentPath.isCallExpression());
164
- print('/* [hello world] */= ');
165
- print('__right');
162
+ AssignmentPattern(path, {write, maybe}) {
163
+ maybe.write.newline(path.parentPath.isCallExpression());
164
+ write('/* [hello world] */= ');
165
+ write('__right');
166
166
  },
167
167
  },
168
168
  });
@@ -171,15 +171,15 @@ print(ast, {
171
171
  ### `maybe`
172
172
 
173
173
  When you need some condition use `maybe`. For example, to add newline only when parent node is `CallExpression` you
174
- can use `maybe.print.newline(condition)`:
174
+ can use `maybe.write.newline(condition)`:
175
175
 
176
176
  ```js
177
- print(ast, {
177
+ write(ast, {
178
178
  visitors: {
179
- AssignmentPattern(path, {print, maybe}) {
180
- maybe.print.newline(path.parentPath.isCallExpression());
181
- print(' /* [hello world] */= ');
182
- print('__right');
179
+ AssignmentPattern(path, {write, maybe}) {
180
+ maybe.write.newline(path.parentPath.isCallExpression());
181
+ write(' /* [hello world] */= ');
182
+ write('__right');
183
183
  },
184
184
  },
185
185
  });
@@ -190,7 +190,7 @@ print(ast, {
190
190
  When are you going to output string you can use low-level function `write`:
191
191
 
192
192
  ```js
193
- print(ast, {
193
+ write(ast, {
194
194
  visitors: {
195
195
  BlockStatement(path, {write}) {
196
196
  write('hello');
@@ -205,7 +205,7 @@ When you need to add indentation use `indent`, for example when you output body,
205
205
  you need to increment indentation, and then decrement it back:
206
206
 
207
207
  ```js
208
- print(ast, {
208
+ write(ast, {
209
209
  visitors: {
210
210
  BlockStatement(path, {write, indent}) {
211
211
  write('{');
@@ -224,7 +224,7 @@ print(ast, {
224
224
  When are you needing to traverse node, you can use `traverse`:
225
225
 
226
226
  ```js
227
- print(ast, {
227
+ write(ast, {
228
228
  visitors: {
229
229
  AssignmentExpression(path, {traverse}) {
230
230
  traverse(path.get('left'));
@@ -233,7 +233,7 @@ print(ast, {
233
233
  });
234
234
  ```
235
235
 
236
- This is the same as `print('__left')` but more low-level, and supports only objects.
236
+ This is the same as `write('__left')` but more low-level, and supports only objects.
237
237
 
238
238
  ## Speed Comparison
239
239
 
@@ -27,7 +27,9 @@ module.exports.ObjectPattern = {
27
27
 
28
28
  for (const [i, property] of properties.entries()) {
29
29
  if (property.isRestElement()) {
30
+ maybe.indent(n > 1);
30
31
  print(property);
32
+ maybe.print.newline(n > 1);
31
33
  continue;
32
34
  }
33
35
 
@@ -67,7 +69,10 @@ module.exports.ObjectPattern = {
67
69
  continue;
68
70
  }
69
71
 
70
- maybe.print(i < n, ', ');
72
+ if (i < n) {
73
+ print(',');
74
+ print.space();
75
+ }
71
76
  }
72
77
 
73
78
  indent.dec();
@@ -111,3 +116,4 @@ function shouldAddNewline(path) {
111
116
 
112
117
  return false;
113
118
  }
119
+
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {ExpressionStatement} = require('./expression-statement');
4
- const {VariableDeclaration} = require('./variable-declaration');
4
+ const {VariableDeclaration} = require('./variable-declaration/variable-declaration');
5
5
  const {IfStatement} = require('./if-statement');
6
6
  const {ForOfStatement} = require('./for-of-statement');
7
7
  const {BlockStatement} = require('./block-statement');
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ module.exports.maybeSpaceAfterKeyword = (path, {write}) => {
4
+ const {id} = path.node.declarations[0];
5
+
6
+ if (id.type === 'ArrayPattern' || id.type === 'ObjectPattern')
7
+ return write.space();
8
+
9
+ write(' ');
10
+ };
@@ -6,10 +6,11 @@ const {
6
6
  isNewlineBetweenSiblings,
7
7
  exists,
8
8
  noTrailingComment,
9
- } = require('../is');
9
+ } = require('../../is');
10
10
 
11
- const {hasPrevNewline} = require('../mark');
11
+ const {hasPrevNewline} = require('../../mark');
12
12
  const {isExportDeclaration} = require('@babel/types');
13
+ const {maybeSpaceAfterKeyword} = require('./maybe-space-after-keyword');
13
14
 
14
15
  const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
15
16
  const isInsideBlock = (path) => /^(Program|BlockStatement)$/.test(path.parentPath.type);
@@ -21,7 +22,8 @@ module.exports.VariableDeclaration = {
21
22
  },
22
23
  print(path, {maybe, store, write, traverse}) {
23
24
  maybe.indent(isInsideBlock(path));
24
- write(`${path.node.kind} `);
25
+ write(String(path.node.kind));
26
+ maybeSpaceAfterKeyword(path, {write});
25
27
 
26
28
  const declarations = path.get('declarations');
27
29
  const n = declarations.length - 1;
@@ -148,3 +150,4 @@ const isNextAssign = (path) => {
148
150
  .get('expression')
149
151
  .isAssignmentExpression();
150
152
  };
153
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.117.0",
3
+ "version": "1.119.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",