@putout/printer 2.55.0 → 2.56.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,11 @@
1
+ 2023.07.08, v2.56.0
2
+
3
+ feature:
4
+ - 90bd7fb @putout/printer: comments: after directive
5
+ - ec4bebd @putout/printer: program
6
+ - e95e634 @putout/printer: comments: directives
7
+ - 7ecc615 @putout/printer: comments
8
+
1
9
  2023.07.07, v2.55.0
2
10
 
3
11
  feature:
package/README.md CHANGED
@@ -41,14 +41,14 @@ And update `.putout.json`:
41
41
 
42
42
  To benefit from it.
43
43
 
44
- ## API
44
+ ## Example
45
45
 
46
46
  ```js
47
47
  const {print} = require('@putout/printer');
48
48
  const {parse} = require('@babel/parser');
49
49
  const ast = parse('const a = (b, c) => {const d = 5; return a;}');
50
50
 
51
- write(ast);
51
+ print(ast);
52
52
  // returns
53
53
  `
54
54
  const a = (b, c) => {
@@ -90,9 +90,9 @@ print(ast, {
90
90
  maxPropertiesInOneLine: 2,
91
91
  },
92
92
  visitors: {
93
- AssignmentPattern(path, {write}) {
94
- write('/* [hello world] */= ');
95
- write('__right');
93
+ AssignmentPattern(path, {print}) {
94
+ print('/* [hello world] */= ');
95
+ print('__right');
96
96
  },
97
97
  },
98
98
  });
@@ -157,11 +157,17 @@ if(a>3)console.log('ok');else console.log('not ok');
157
157
 
158
158
  Options used to configure logic of output, similar to ESLint rules:
159
159
 
160
- - `maxElementsInOneLine` - count of `ArrayExpression` and `ArrayPattern` elements placed in one line.
160
+ - `maxElementsInOneLine` - count of `ArrayExpression` and `ArrayPattern` elements placed in one line.
161
+ - ✅ `maxVariablesInOneLine` - count of `VariableDeclarators` in one line.
162
+ - ✅ `maxPropertiesInOneLine` - count of `ObjectProperties` in one line.
161
163
 
162
- ### `write`
164
+ ## Visitors API
165
+
166
+ When you want to improve support of existing visitor or extend **Printer** with a new ones, you need next base operations:
163
167
 
164
- Used in previous example `write` can be used for a couple purposes:
168
+ ### `print`
169
+
170
+ Used in previous example `print` can be used for a couple purposes:
165
171
 
166
172
  - to write `string`;
167
173
  - to write `node` when `object` passed;
@@ -170,10 +176,10 @@ Used in previous example `write` can be used for a couple purposes:
170
176
  ```js
171
177
  print(ast, {
172
178
  visitors: {
173
- AssignmentPattern(path, {write, maybe}) {
179
+ AssignmentPattern(path, {print, maybe}) {
174
180
  maybe.write.newline(path.parentPath.isCallExpression());
175
- write('/* [hello world] */= ');
176
- write('__right');
181
+ print('/* [hello world] */= ');
182
+ print('__right');
177
183
  },
178
184
  },
179
185
  });
@@ -198,7 +204,7 @@ print(ast, {
198
204
 
199
205
  ### `write`
200
206
 
201
- When are you going to output string you can use low-level function `write`:
207
+ When you going to output string you can use low-level function `write`:
202
208
 
203
209
  ```js
204
210
  print(ast, {
@@ -232,7 +238,7 @@ print(ast, {
232
238
 
233
239
  ### `traverse`
234
240
 
235
- When are you needing to traverse node, you can use `traverse`:
241
+ When you need to traverse node path, you can use `traverse`:
236
242
 
237
243
  ```js
238
244
  print(ast, {
@@ -251,8 +257,9 @@ This is the same as `write('__left')` but more low-level, and supports only obje
251
257
  About speed, for file `speed.js`:
252
258
 
253
259
  ```js
254
- const putout = require('putout');
255
260
  const {readFileSync} = require('fs');
261
+
262
+ const putout = require('putout');
256
263
  const parser = require('@babel/parser');
257
264
 
258
265
  const code = readFileSync('./lib/tokenize/tokenize.js', 'utf8');
@@ -44,7 +44,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
44
44
  if (type === 'CommentBlock') {
45
45
  print(`/*${value}*/`);
46
46
 
47
- if (path.isStatement() || path.isClassMethod()) {
47
+ if (path.isStatement() || path.isClassMethod() || path.isDirective()) {
48
48
  print.newline();
49
49
  markBefore(path);
50
50
  maybe.indent(path.isClassMethod());
@@ -64,6 +64,9 @@ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
64
64
  if (!trailingComments?.length)
65
65
  return;
66
66
 
67
+ if (path.isDirective())
68
+ return;
69
+
67
70
  for (const {type, value, loc} of trailingComments) {
68
71
  const sameLine = isSameLine(path, loc);
69
72
 
@@ -21,7 +21,8 @@ module.exports = {
21
21
 
22
22
  write(raw || extra?.raw || value);
23
23
  },
24
- Directive(path, {print}) {
24
+ Directive(path, {print, maybe}) {
25
+ maybe.print.breakline(path.node.leadingComments?.length);
25
26
  print('__value');
26
27
  },
27
28
  DirectiveLiteral(path, {write}) {
@@ -71,3 +72,4 @@ module.exports = {
71
72
  write('super');
72
73
  },
73
74
  };
75
+
@@ -15,15 +15,12 @@ const {
15
15
  const {markAfter} = require('../../mark');
16
16
  const {parseComments} = require('../../comments/comments');
17
17
  const {insideIfWithNoBody} = require('./inside-if-with-no-body');
18
+ const {getDirectives} = require('./get-directives');
18
19
 
19
20
  const isFirstStatement = (path) => path.node.body[0];
20
21
  const isFirstDirective = (path) => path.node.directives?.[0];
21
22
  const isMethodOrArrow = (path) => isArrowFunctionExpression(path) || isObjectMethod(path);
22
23
 
23
- const getDirectives = (path) => !path.node.directives ? [] : path.get('directives');
24
-
25
- module.exports._getDirectives = getDirectives;
26
-
27
24
  module.exports.BlockStatement = {
28
25
  print(path, {indent, maybe, write, traverse}, semantics) {
29
26
  const body = path.get('body');
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ module.exports.getDirectives = (path) => !path.node.directives ? [] : path.get('directives');
4
+
@@ -19,7 +19,7 @@ const {ForInStatement} = require('./for-in-statement');
19
19
  const {ExportDefaultDeclaration} = require('./export-declaration/export-default-declaration');
20
20
  const {BreakStatement} = require('./break-statement');
21
21
  const {DoWhileStatement} = require('./do-while-statement');
22
- const {Program} = require('./program');
22
+ const {Program} = require('./program/program');
23
23
 
24
24
  const {
25
25
  ExportNamespaceSpecifier,
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ const {parseComments} = require('../../comments/comments');
4
+ const {getDirectives} = require('../block-statement/get-directives');
5
+
6
+ module.exports.Program = (path, {print, write, traverse, maybe}, semantics) => {
7
+ const {body} = path.node;
8
+ print('__interpreter');
9
+ parseComments(path, {write}, semantics);
10
+
11
+ const directives = getDirectives(path);
12
+
13
+ for (const directive of directives) {
14
+ traverse(directive);
15
+ maybe.print.newline(body.length);
16
+ }
17
+
18
+ path
19
+ .get('body')
20
+ .forEach(print);
21
+
22
+ if (directives.length && !body.length)
23
+ return;
24
+
25
+ print.newline();
26
+ };
27
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.55.0",
3
+ "version": "2.56.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",
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- const {parseComments} = require('../comments/comments');
4
-
5
- module.exports.Program = (path, {print, write}, semantics) => {
6
- print('__interpreter');
7
- parseComments(path, {write}, semantics);
8
-
9
- path
10
- .get('body')
11
- .forEach(print);
12
-
13
- print.newline();
14
- };