@putout/printer 6.9.0 → 6.10.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,8 @@
1
+ 2023.11.30, v6.10.0
2
+
3
+ feature:
4
+ - dfe625e @putout/printer: ObjectProperty: comments: improve
5
+
1
6
  2023.11.30, v6.9.0
2
7
 
3
8
  feature:
@@ -28,6 +28,33 @@ const isProperty = satisfy([
28
28
  isSpreadElement,
29
29
  ]);
30
30
 
31
+ function isCommentOfPrevious(path) {
32
+ const [comment] = path.node.leadingComments;
33
+ const {line} = comment.loc.start;
34
+
35
+ return path.getPrevSibling().node?.loc.start.line === line;
36
+ }
37
+
38
+ function isCommentOnPreviousLine(path) {
39
+ const {
40
+ loc,
41
+ leadingComments,
42
+ } = path.node;
43
+
44
+ if (!isProperty(path))
45
+ return false;
46
+
47
+ const [comment] = leadingComments;
48
+ const {line} = comment.loc.start;
49
+
50
+ if (isCommentOfPrevious(path))
51
+ return false;
52
+
53
+ return line === loc.start.line - 1;
54
+ }
55
+
56
+ const isFirst = (path) => path === path.parentPath?.get('properties')[0];
57
+
31
58
  module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
32
59
  if (!semantics.comments)
33
60
  return;
@@ -45,7 +72,7 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
45
72
  const insideFn = path.parentPath.isFunction();
46
73
 
47
74
  const propIs = isProperty(path);
48
- const isIndent = !looksLikeSwitchCase && !path.isClassMethod() && !insideFn && !propIs;
75
+ const isIndent = isFirst(path) || !looksLikeSwitchCase && !path.isClassMethod() && !insideFn && !propIs;
49
76
 
50
77
  for (const {type, value} of deduplicate(leadingComments)) {
51
78
  maybe.indent(isIndent);
@@ -56,7 +83,11 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
56
83
  indent,
57
84
  });
58
85
 
59
- maybe.print.space(propIs && !path.isClassProperty());
86
+ if (isCommentOnPreviousLine(path))
87
+ maybe.print.breakline(!isFirst(path));
88
+ else
89
+ maybe.print.space(propIs && !path.isClassProperty());
90
+
60
91
  print(`//${value}`);
61
92
 
62
93
  const isParentSwitch = path.parentPath.isSwitchCase();
@@ -15,6 +15,8 @@ function isSameLine(path, loc) {
15
15
 
16
16
  const isTrailingIsLeading = (path) => path.node.trailingComments === path.getNextSibling().node?.leadingComments;
17
17
 
18
+ module.exports.isTrailingIsLeading = isTrailingIsLeading;
19
+
18
20
  function isCommentOnNextLine(path) {
19
21
  const {
20
22
  loc,
@@ -72,4 +74,3 @@ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
72
74
  }
73
75
  }
74
76
  };
75
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "6.9.0",
3
+ "version": "6.10.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",