@putout/printer 1.80.4 → 1.81.1

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,14 @@
1
+ 2023.05.04, v1.81.1
2
+
3
+ feature:
4
+ - 12f7943 @putout/printer: improve comments support
5
+
6
+ 2023.05.04, v1.81.0
7
+
8
+ feature:
9
+ - 8b4ea75 @putout/printer: add ability to minify whitespaces
10
+ - ee6c8f1 @putout/printer: ImportDeclaration: add potential ability to overide imports.maxOneLineSpecifiers
11
+
1
12
  2023.05.03, v1.80.4
2
13
 
3
14
  fix:
package/README.md CHANGED
@@ -78,6 +78,9 @@ const ast = parse('const {a = 5} = b');
78
78
  print(ast, {
79
79
  format: {
80
80
  indent: ' ',
81
+ newline: '\n',
82
+ space: ' ',
83
+ comments: true,
81
84
  },
82
85
  visitors: {
83
86
  AssignmentPattern(path, {print}) {
@@ -1,13 +1,19 @@
1
1
  'use strict';
2
2
 
3
- const {isNext} = require('./is');
3
+ const {hasTrailingComment} = require('./is');
4
4
 
5
5
  const {markBefore} = require('./mark');
6
6
 
7
- module.exports.parseLeadingComments = (path, {print, maybe, indent}) => {
7
+ module.exports.parseLeadingComments = (path, {print, maybe, indent}, format) => {
8
+ if (!format.comments)
9
+ return;
10
+
8
11
  const {leadingComments} = path.node;
9
12
 
10
- if (!leadingComments || !leadingComments.length)
13
+ if (!leadingComments?.length)
14
+ return;
15
+
16
+ if (hasTrailingComment(path.getPrevSibling()))
11
17
  return;
12
18
 
13
19
  const insideFn = path.parentPath.isFunction();
@@ -45,13 +51,13 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}) => {
45
51
  }
46
52
  };
47
53
 
48
- module.exports.parseTrailingComments = (path, {write, maybe}) => {
49
- const {trailingComments} = path.node;
50
-
51
- if (!trailingComments || !trailingComments.length)
54
+ module.exports.parseTrailingComments = (path, {write, maybe}, format) => {
55
+ if (!format.comments)
52
56
  return;
53
57
 
54
- if (isNext(path))
58
+ const {trailingComments} = path.node;
59
+
60
+ if (!trailingComments?.length)
55
61
  return;
56
62
 
57
63
  for (const {type, value, loc} of trailingComments) {
@@ -63,6 +69,12 @@ module.exports.parseTrailingComments = (path, {write, maybe}) => {
63
69
 
64
70
  write(`//${value}`);
65
71
  write.newline();
72
+ continue;
73
+ }
74
+
75
+ if (type === 'CommentBlock') {
76
+ write(`/*${value}*/`);
77
+ write.newline();
66
78
  }
67
79
  }
68
80
  };
@@ -4,10 +4,17 @@ const {markAfter} = require('../../mark');
4
4
  const {isLast} = require('../../is');
5
5
  const {parseSpecifiers} = require('./parse-specifiers');
6
6
 
7
+ const options = {
8
+ imports: {
9
+ maxOneLineSpecifiers: 2,
10
+ },
11
+ };
12
+
7
13
  module.exports.ImportDeclaration = {
8
14
  print(path, {print, maybe, write, traverse, indent}) {
9
15
  const isType = path.node.importKind === 'type';
10
16
  const specifiers = path.get('specifiers');
17
+ const {maxOneLineSpecifiers} = options.imports;
11
18
 
12
19
  print('import ');
13
20
  maybe.print(isType, 'type ');
@@ -56,12 +63,12 @@ module.exports.ImportDeclaration = {
56
63
  write(spec.node.local.name);
57
64
  }
58
65
 
59
- if (importsCount <= 2 && notLast) {
66
+ if (importsCount <= maxOneLineSpecifiers && notLast) {
60
67
  maybe.write(n, ',');
61
68
  maybe.write.space(n);
62
69
  }
63
70
 
64
- if (importsCount > 2) {
71
+ if (importsCount > maxOneLineSpecifiers) {
65
72
  maybe.write(n, ',');
66
73
  maybe.write.newline(index === n);
67
74
  }
@@ -5,6 +5,7 @@ const {
5
5
  isCoupleLines,
6
6
  isNewlineBetweenSiblings,
7
7
  exists,
8
+ noTrailingComment,
8
9
  } = require('../is');
9
10
 
10
11
  const {hasPrevNewline} = require('../mark');
@@ -18,19 +19,25 @@ module.exports.VariableDeclaration = {
18
19
  before(path, {print}) {
19
20
  print.breakline();
20
21
  },
21
- print(path, {maybe, print, store}) {
22
+ print(path, {maybe, print, store, write}) {
22
23
  maybe.indent(isParentBlock(path));
23
24
  print(`${path.node.kind} `);
24
25
  print('__declarations.0.id');
25
26
 
26
27
  const initPath = path.get('declarations.0.init');
27
- maybe.print(exists(initPath), ' = ');
28
+
29
+ if (exists(initPath)) {
30
+ write.space();
31
+ write('=');
32
+ write.space();
33
+ }
34
+
28
35
  print('__declarations.0.init');
29
36
  maybe.print(isParentBlock(path), ';');
30
37
 
31
38
  let wasNewline = false;
32
39
 
33
- if (isParentBlock(path) && isNext(path)) {
40
+ if (isParentBlock(path) && isNext(path) && (noTrailingComment(path) || isNewlineBetweenSiblings(path))) {
34
41
  print.newline();
35
42
  wasNewline = true;
36
43
  }
@@ -48,8 +48,11 @@ const get = (path, command) => path.get(command.replace(GET, ''));
48
48
 
49
49
  function initFormat(format) {
50
50
  return {
51
- ...format,
52
51
  indent: ' ',
52
+ newline: '\n',
53
+ space: ' ',
54
+ comments: true,
55
+ ...format,
53
56
  };
54
57
  }
55
58
 
@@ -107,7 +110,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
107
110
  const space = () => {
108
111
  addToken({
109
112
  type: TYPES.SPACE,
110
- value: ' ',
113
+ value: format.space,
111
114
  });
112
115
  };
113
116
 
@@ -119,7 +122,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
119
122
  const newline = () => {
120
123
  addToken({
121
124
  type: TYPES.NEWLINE,
122
- value: '\n',
125
+ value: format.newline,
123
126
  });
124
127
  };
125
128
 
@@ -209,12 +212,12 @@ module.exports.tokenize = (ast, overrides = {}) => {
209
212
  maybeThrow(!currentTraverse, path, `Node type '{{ type }}' is not supported yet: '{{ path }}'`);
210
213
 
211
214
  const currentIndent = i;
212
- parseLeadingComments(path, printer);
215
+ parseLeadingComments(path, printer, format);
213
216
 
214
217
  // this is main thing
215
218
  maybePlugin(currentTraverse, path, printer);
216
219
 
217
- parseTrailingComments(path, printer);
220
+ parseTrailingComments(path, printer, format);
218
221
  maybeThrow(i !== currentIndent, path, `☝️Looks like indent level changed after token visitor: '{{ type }}', for code: '{{ path }}'`);
219
222
  debug(path.type);
220
223
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.80.4",
3
+ "version": "1.81.1",
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 fro 🐊Putout",