@putout/printer 15.6.0 → 15.8.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
+ 2025.07.02, v15.8.0
2
+
3
+ feature:
4
+ - 3576b80 @putout/printer: ImportDeclaration: comments before ExpressionStatement
5
+
6
+ 2025.07.02, v15.7.0
7
+
8
+ feature:
9
+ - 54874b0 @putout/printer: ObjectPattern: useless indent inside VariableDeclaration
10
+
1
11
  2025.06.26, v15.6.0
2
12
 
3
13
  feature:
@@ -21,13 +21,27 @@ module.exports.printLeadingComments = (path, printer, semantics, {currentTravers
21
21
  leadingComments = [],
22
22
  } = path.node;
23
23
 
24
- const {printLeadingCommentLine} = currentTraverse;
24
+ const {
25
+ printLeadingCommentLine,
26
+ printLeadingCommentBlock,
27
+ } = currentTraverse;
28
+
29
+ const n = leadingComments.length - 1;
25
30
 
26
31
  for (const [index, {type, value}] of leadingComments.entries()) {
27
- if (type === 'CommentLine')
32
+ if (type === 'CommentLine') {
28
33
  printLeadingCommentLine?.(path, printer, semantics, {
29
34
  index,
30
35
  printComment: createPrintCommentLine(print, value),
36
+ isLast: index === n,
37
+ });
38
+ continue;
39
+ }
40
+
41
+ if (type === 'CommentBlock')
42
+ printLeadingCommentBlock?.(path, printer, semantics, {
43
+ index,
44
+ printComment: createPrintCommentBlock(print, value),
31
45
  });
32
46
  }
33
47
  };
@@ -38,9 +52,23 @@ module.exports.printTrailingComments = (path, printer, semantics, {currentTraver
38
52
  trailingComments = [],
39
53
  } = path.node;
40
54
 
41
- const {printTrailingCommentBlock} = currentTraverse;
55
+ const {
56
+ printTrailingCommentLine,
57
+ printTrailingCommentBlock,
58
+ } = currentTraverse;
59
+
60
+ const n = trailingComments.length - 1;
42
61
 
43
62
  for (const [index, {type, value}] of trailingComments.entries()) {
63
+ if (type === 'CommentLine') {
64
+ printTrailingCommentLine?.(path, printer, semantics, {
65
+ index,
66
+ printComment: createPrintCommentLine(print, value),
67
+ isLast: index === n,
68
+ });
69
+ continue;
70
+ }
71
+
44
72
  if (type === 'CommentBlock')
45
73
  printTrailingCommentBlock?.(path, printer, semantics, {
46
74
  index,
@@ -189,7 +189,7 @@ module.exports.ObjectPattern = {
189
189
 
190
190
  indent.dec();
191
191
 
192
- maybe.indent(is || hasAssignObject(path));
192
+ maybe.indent(is || hasAssignObject(path, maxPropertiesLengthInOneLine));
193
193
  maybe.indent.inc(!shouldIndent);
194
194
  print('}');
195
195
  }),
@@ -220,7 +220,7 @@ function hasAssign(properties) {
220
220
  return false;
221
221
  }
222
222
 
223
- function hasAssignObject(path) {
223
+ function hasAssignObject(path, maxPropertiesLengthInOneLine) {
224
224
  const {parentPath} = path;
225
225
 
226
226
  if (isVariableDeclaration(parentPath.parentPath)) {
@@ -231,12 +231,13 @@ function hasAssignObject(path) {
231
231
  }
232
232
 
233
233
  const properties = path.get('properties');
234
+ const n = properties.length;
234
235
 
235
236
  for (const prop of properties) {
236
237
  const {value} = prop.node;
237
238
 
238
239
  if (isAssignmentPattern(value) && isObjectExpression(value.right))
239
- return true;
240
+ return n > 1 || maxPropertiesLengthInOneLine <= value.left;
240
241
  }
241
242
 
242
243
  return false;
@@ -46,4 +46,3 @@ module.exports = {
46
46
  write('super');
47
47
  },
48
48
  };
49
-
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('@putout/babel');
4
+ const {hasTrailingComment} = require('#is');
5
+
6
+ const {
7
+ isBlockStatement,
8
+ isProgram,
9
+ isIfStatement,
10
+ isClassMethod,
11
+ } = types;
12
+
13
+ module.exports.printLeadingCommentLine = (path, printer, semantics, {index, isLast, printComment}) => {
14
+ const {print, indent} = printer;
15
+ const prev = path.getPrevSibling();
16
+ const {parentPath} = path;
17
+ const parentParentPath = parentPath.parentPath;
18
+
19
+ if (hasTrailingComment(prev))
20
+ return;
21
+
22
+ if (!index && !prev.node && (isIfStatement(parentPath) || isClassMethod(parentParentPath)))
23
+ indent();
24
+
25
+ printComment();
26
+
27
+ print.newline();
28
+
29
+ if (!isLast && !path.parentPath.isIfStatement())
30
+ print.indent();
31
+ };
32
+
33
+ module.exports.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
34
+ const {indent} = printer;
35
+ const prev = path.getPrevSibling();
36
+
37
+ if (hasTrailingComment(prev))
38
+ return;
39
+
40
+ if (isBlockStatement(path.parentPath) && !isProgram(path.parentPath.parentPath))
41
+ indent();
42
+
43
+ printComment();
44
+ };
@@ -15,6 +15,12 @@ const {
15
15
  } = require('../../is');
16
16
 
17
17
  const {isInsideAssignNextAssignFunction} = require('./is-inside-assign-next-assign-function');
18
+
19
+ const {
20
+ printLeadingCommentLine,
21
+ printLeadingCommentBlock,
22
+ } = require('./comments');
23
+
18
24
  const isCommentBlock = (a) => a?.type === 'CommentBlock';
19
25
 
20
26
  const {
@@ -117,6 +123,8 @@ module.exports.ExpressionStatement = {
117
123
  }
118
124
  },
119
125
  };
126
+ module.exports.ExpressionStatement.printLeadingCommentLine = printLeadingCommentLine;
127
+ module.exports.ExpressionStatement.printLeadingCommentBlock = printLeadingCommentBlock;
120
128
 
121
129
  function isTopParentLast({parentPath}) {
122
130
  if (!parentPath.isIfStatement())
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('@putout/babel');
4
+ const {isExportDeclaration} = types;
5
+
6
+ module.exports.printTrailingCommentLine = (path, printer, semantics, {printComment}) => {
7
+ const {print} = printer;
8
+ printComment();
9
+ print.breakline();
10
+ };
11
+
12
+ module.exports.printTrailingCommentBlock = (path, printer, semantics, {printComment}) => {
13
+ const {maybe} = printer;
14
+ const next = path.getNextSibling();
15
+
16
+ maybe.print.breakline(!isExportDeclaration(next));
17
+ printComment();
18
+ };
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const {parseImportSpecifiers} = require('parse-import-specifiers');
4
- const {types} = require('@putout/babel');
4
+
5
5
  const {markAfter} = require('../../mark');
6
6
  const {isLast, isNext} = require('../../is');
7
7
 
@@ -10,7 +10,10 @@ const {
10
10
  ImportAttribute,
11
11
  } = require('./import-attribute');
12
12
 
13
- const {isExportDeclaration} = types;
13
+ const {
14
+ printTrailingCommentBlock,
15
+ printTrailingCommentLine,
16
+ } = require('./comments');
14
17
 
15
18
  module.exports.ImportAttribute = ImportAttribute;
16
19
  module.exports.ImportDeclaration = {
@@ -137,10 +140,5 @@ function parseMaxSpecifiers(imports, semantics) {
137
140
  return maxSpecifiersInOneLine;
138
141
  }
139
142
 
140
- module.exports.ImportDeclaration.printTrailingCommentBlock = (path, printer, semantics, {printComment}) => {
141
- const {maybe} = printer;
142
- const next = path.getNextSibling();
143
-
144
- maybe.print.breakline(!isExportDeclaration(next));
145
- printComment();
146
- };
143
+ module.exports.ImportDeclaration.printTrailingCommentBlock = printTrailingCommentBlock;
144
+ module.exports.ImportDeclaration.printTrailingCommentLine = printTrailingCommentLine;
@@ -10,13 +10,14 @@ const {
10
10
  } = require('../../is');
11
11
 
12
12
  const {hasPrevNewline} = require('../../mark');
13
-
14
13
  const {maybeSpaceAfterKeyword} = require('./maybe-space-after-keyword');
15
14
 
16
15
  const {isConcatenation} = require('../../expressions/binary-expression/concatenate');
17
16
  const {parseLeadingComments} = require('../../comment/comment');
18
17
  const {maybeDeclare} = require('../../maybe/maybe-declare');
18
+
19
19
  const {isExportDeclaration} = types;
20
+
20
21
  const isParentTSModuleBlock = (path) => path.parentPath.isTSModuleBlock();
21
22
  const isParentBlock = (path) => /Program|BlockStatement|Export|LabeledStatement/.test(path.parentPath.type);
22
23
  const isInsideBlock = (path) => /^(Program|BlockStatement|TSModuleBlock|SwitchCase)$/.test(path.parentPath.type);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "15.6.0",
3
+ "version": "15.8.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",