@putout/printer 8.20.0 → 8.21.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
+ 2024.04.29, v8.21.0
2
+
3
+ feature:
4
+ - 2abb228 @putout/printer: comments: improve
5
+
6
+ 2024.04.24, v8.20.1
7
+
8
+ feature:
9
+ - 4551754 @putout/printer: ArrayExpression: CallExpression, ObjectExpression
10
+
1
11
  2024.04.23, v8.20.0
2
12
 
3
13
  feature:
@@ -54,7 +54,7 @@ function isCommentOnNextLine(path) {
54
54
  if (isTrailingIsLeading(path))
55
55
  return false;
56
56
 
57
- if (trailingComments.length > 1)
57
+ if (path.isThrowStatement())
58
58
  return false;
59
59
 
60
60
  const [comment] = trailingComments;
@@ -82,7 +82,7 @@ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
82
82
 
83
83
  const n = trailingComments.length - 1;
84
84
 
85
- for (const {type, value, loc} of trailingComments) {
85
+ for (const [index, {type, value, loc}] of trailingComments.entries()) {
86
86
  const sameLine = isSameLine(path, loc);
87
87
  const commentOnNextLine = isCommentOnNextLine(path);
88
88
 
@@ -36,6 +36,24 @@ const isPrevObject = (a) => a.getPrevSibling().isObjectExpression();
36
36
  const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a);
37
37
  const isObjectAfterIdentifier = (a) => isIdentifier(a) && isNextObject(a) && !isPrevObject(a);
38
38
  const isObjectAfterSimple = (a) => isObjectAfterSpread(a) || isObjectAfterIdentifier(a);
39
+
40
+ const isSpreadBeforeObject = (a) => {
41
+ if (!a.isObjectExpression())
42
+ return false;
43
+
44
+ const prev = a.getPrevSibling();
45
+
46
+ if (!prev.isSpreadElement())
47
+ return false;
48
+
49
+ if (prev.getPrevSibling().isObjectExpression())
50
+ return false;
51
+
52
+ return prev
53
+ .get('argument')
54
+ .isCallExpression();
55
+ };
56
+
39
57
  const isNextSimple = (a) => {
40
58
  const next = a.getNextSibling();
41
59
 
@@ -115,14 +133,13 @@ module.exports.ArrayExpression = {
115
133
  if (index < n || trailingComma)
116
134
  maybe.print(is, ',');
117
135
 
118
- maybe.print.newline(is && !isNextObject(element));
136
+ maybe.print.newline((is || isSpreadBeforeObject(element)) && !isNextObject(element));
119
137
  maybe.print.space(is && isObjectAfterSimple(element));
120
138
 
121
139
  if (!is && index < n) {
122
140
  print(',');
123
141
 
124
142
  if (isNextSimpleBetweenObjects(element) || !(element.isObjectExpression() && isNextSimple(element)))
125
- //if (!(element.isObjectExpression() && isNextSimple(element)))
126
143
  print.space();
127
144
  }
128
145
  }
@@ -123,7 +123,17 @@ module.exports.satisfy = (conditions) => (path) => {
123
123
  return false;
124
124
  };
125
125
 
126
- module.exports.hasTrailingComment = (path) => path.node?.trailingComments?.length;
126
+ const parseNode = (path) => path.node || path;
127
+
128
+ module.exports.hasCoupleTrailingComments = (path) => {
129
+ const node = parseNode(path);
130
+ return node?.trailingComments?.length > 1;
131
+ };
132
+
133
+ module.exports.hasTrailingComment = (path) => {
134
+ const node = parseNode(path);
135
+ return node.trailingComments?.length;
136
+ };
127
137
  module.exports.hasLeadingComment = (path) => path.node?.leadingComments?.length;
128
138
 
129
139
  module.exports.noTrailingComment = (path) => !path.node.trailingComments?.length;
@@ -2,6 +2,10 @@
2
2
 
3
3
  const {parseComments} = require('../../comment/comment');
4
4
  const {getDirectives} = require('../block-statement/get-directives');
5
+ const {
6
+ hasTrailingComment,
7
+ hasCoupleTrailingComments,
8
+ } = require('../../is');
5
9
 
6
10
  module.exports.Program = (path, printer, semantics) => {
7
11
  const {body} = path.node;
@@ -28,5 +32,8 @@ module.exports.Program = (path, printer, semantics) => {
28
32
  if (directives.length && !body.length)
29
33
  return;
30
34
 
35
+ if (body.length && hasCoupleTrailingComments(body.at(-1)))
36
+ return;
37
+
31
38
  write.endOfFile();
32
39
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.20.0",
3
+ "version": "8.21.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",