@putout/printer 12.18.0 → 12.19.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
+ 2025.01.28, v12.19.0
2
+
3
+ feature:
4
+ - 8d2c91a @putout/printer: MemberExpression: chain: comments: improve
5
+
1
6
  2025.01.27, v12.18.0
2
7
 
3
8
  feature:
@@ -8,6 +8,8 @@ const {
8
8
  isNext,
9
9
  } = require('../is');
10
10
 
11
+ const {isLooksLikeChain} = require('../expressions/member-expression/is-looks-like-chain');
12
+
11
13
  const {
12
14
  isDecorator,
13
15
  isMemberExpression,
@@ -47,15 +49,12 @@ const isNewlineAfter = (path) => {
47
49
  module.exports.isTrailingIsLeading = isTrailingIsLeading;
48
50
 
49
51
  function isCommentOnNextLine(path) {
50
- const {node, parentPath} = path;
52
+ const {node} = path;
51
53
  const {
52
54
  loc,
53
55
  trailingComments,
54
56
  } = node;
55
57
 
56
- if (parentPath.isMemberExpression())
57
- return false;
58
-
59
58
  if (path.isClassMethod())
60
59
  return false;
61
60
 
@@ -84,7 +83,9 @@ function isCommentOnNextLine(path) {
84
83
  return isNextLine || isNextLineAfterNewline;
85
84
  }
86
85
 
87
- module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
86
+ module.exports.parseTrailingComments = (path, {write, maybe, indent}, semantics) => {
87
+ const {parentPath} = path;
88
+
88
89
  if (!semantics.comments)
89
90
  return;
90
91
 
@@ -97,15 +98,24 @@ module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
97
98
  return;
98
99
 
99
100
  const n = trailingComments.length - 1;
101
+ const likeChain = isLooksLikeChain(parentPath);
100
102
 
101
103
  for (const {type, value, loc} of trailingComments) {
102
104
  const sameLine = isSameLine(path, loc);
103
105
  const commentOnNextLine = isCommentOnNextLine(path);
104
106
 
105
107
  if (type === 'CommentLine') {
108
+ const nextLineInChain = commentOnNextLine && likeChain;
109
+ const shouldIndent = !sameLine && (!commentOnNextLine || path.isDecorator());
110
+
106
111
  maybe.write.breakline(commentOnNextLine);
107
112
  maybe.write.space(sameLine);
108
- maybe.indent(!sameLine && (!commentOnNextLine || path.isDecorator()));
113
+
114
+ if (shouldIndent || nextLineInChain) {
115
+ maybe.indent.inc(nextLineInChain);
116
+ indent();
117
+ maybe.indent.dec(nextLineInChain);
118
+ }
109
119
 
110
120
  if (hasBody(path)) {
111
121
  maybe.write.breakline(!isNext(path));
@@ -41,6 +41,11 @@ function up(current) {
41
41
  const properties = [];
42
42
 
43
43
  while (current.isMemberExpression()) {
44
+ if (current.node.object.trailingComments)
45
+ properties.push({
46
+ type: 'CommentLine',
47
+ });
48
+
44
49
  current = current.parentPath;
45
50
 
46
51
  if (current.isCallExpression()) {
@@ -10,10 +10,11 @@ const {
10
10
  isCallExpression,
11
11
  } = types;
12
12
 
13
- const isArgOfCall = (path) => path.parentPath.isCallExpression() && path.parentPath.get('arguments.0') === path;
13
+ const isArgOfCall = (path) => path.parentPath?.isCallExpression() && path.parentPath.get('arguments.0') === path;
14
14
  const isCall = (a) => a.type === 'CallExpression';
15
15
 
16
16
  const isExcludedFromChain = satisfy([isUnaryExpression, isIfStatement]);
17
+ const hasComment = ({type}) => type === 'CommentLine';
17
18
 
18
19
  module.exports.isLooksLikeChain = (path) => {
19
20
  const [root, properties] = chain(path);
@@ -24,6 +25,9 @@ module.exports.isLooksLikeChain = (path) => {
24
25
  if (properties.find(isPathGet))
25
26
  return false;
26
27
 
28
+ if (properties.find(hasComment))
29
+ return true;
30
+
27
31
  if (path.find(isIfUp))
28
32
  return false;
29
33
 
@@ -16,6 +16,8 @@ module.exports.MemberExpression = (path, printer) => {
16
16
  const isParens = object.isAssignmentExpression();
17
17
  const {computed} = path.node;
18
18
 
19
+ const isChain = isLooksLikeChain(path);
20
+
19
21
  maybe.print(isParens, '(');
20
22
  traverse(object);
21
23
  maybe.print(isParens, ')');
@@ -23,8 +25,6 @@ module.exports.MemberExpression = (path, printer) => {
23
25
  if (computed)
24
26
  return maybePrintComputed(path, property, printer);
25
27
 
26
- const isChain = isLooksLikeChain(path);
27
-
28
28
  maybe.indent.inc(isChain);
29
29
  maybe.print.breakline(isChain);
30
30
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.18.0",
3
+ "version": "12.19.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",