@putout/printer 1.75.0 → 1.76.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.04.28, v1.76.0
2
+
3
+ feature:
4
+ - a515fbc @putout/printer: improve support of newlines in with comments
5
+
1
6
  2023.04.27, v1.75.0
2
7
 
3
8
  feature:
@@ -31,8 +31,7 @@ function CallExpression(path, {indent, print, maybe, traverse}) {
31
31
  const isObject = arg.isObjectExpression();
32
32
 
33
33
  if (isParentCall && !isObject && n) {
34
- print.newline();
35
- indent();
34
+ print.breakline();
36
35
  }
37
36
 
38
37
  print(arg);
@@ -31,7 +31,9 @@ function classVisitor(path, {print, indent, maybe}) {
31
31
 
32
32
  if (node.implements) {
33
33
  print(' implements ');
34
- path.get('implements').forEach(print);
34
+ path
35
+ .get('implements')
36
+ .forEach(print);
35
37
  }
36
38
 
37
39
  if (node.superClass) {
@@ -7,7 +7,8 @@ const {
7
7
  noTrailingComment,
8
8
  isNewlineBetweenSiblings,
9
9
  noLeadingComment,
10
- noComment,
10
+ hasLeadingComment,
11
+ exists,
11
12
  } = require('../is');
12
13
 
13
14
  const {isFunction} = require('@babel/types');
@@ -52,8 +53,11 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
52
53
  }
53
54
 
54
55
  print(property);
55
- maybe.print.newline(manyLines && noTrailingComment(property) && noComment(property));
56
- maybe.print.linebreak(isNewlineBetweenSiblings(property));
56
+
57
+ if (!hasNextLeadingComment(property)) {
58
+ maybe.print.newline(manyLines && noTrailingComment(property));
59
+ maybe.print.linebreak(isNewlineBetweenSiblings(property));
60
+ }
57
61
  }
58
62
 
59
63
  indent.dec();
@@ -63,6 +67,15 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
63
67
  maybe.print(parens, ')');
64
68
  };
65
69
 
70
+ const hasNextLeadingComment = (path) => {
71
+ const next = path.getNextSibling();
72
+
73
+ if (!exists(next))
74
+ return false;
75
+
76
+ return hasLeadingComment(next);
77
+ };
78
+
66
79
  function shouldAddNewline(path) {
67
80
  if (!path.parentPath.isLogicalExpression())
68
81
  return false;
@@ -85,7 +85,7 @@ module.exports.satisfy = (conditions) => (path) => {
85
85
  };
86
86
 
87
87
  module.exports.hasTrailingComment = (path) => path.node?.trailingComments?.length;
88
+ module.exports.hasLeadingComment = (path) => path.node?.leadingComments?.length;
88
89
 
89
90
  module.exports.noTrailingComment = (path) => !path.node.trailingComments?.length;
90
- module.exports.noComment = (path) => !path.node.comments?.length;
91
91
  module.exports.noLeadingComment = (path) => !path.node.leadingComments?.length;
@@ -9,16 +9,14 @@ module.exports.JSXText = (path, {write, indent}) => {
9
9
 
10
10
  if (isSpacesOnly && hasNext) {
11
11
  indent.inc();
12
- write.newline();
13
- write.indent();
12
+ write.breakline();
14
13
  indent.dec();
15
14
 
16
15
  return;
17
16
  }
18
17
 
19
18
  if (isSpacesOnly) {
20
- write.newline();
21
- write.indent();
19
+ write.breakline();
22
20
 
23
21
  return;
24
22
  }
@@ -9,15 +9,30 @@ const {
9
9
  satisfy,
10
10
  noTrailingComment,
11
11
  hasTrailingComment,
12
+ exists,
12
13
  } = require('../is');
13
14
 
15
+ const satisfyAfter = satisfy([
16
+ isParentBlockOrNotLastOrParentLast,
17
+ isParentBlock,
18
+ isNext,
19
+ isNextUp,
20
+ ]);
21
+
14
22
  const isNextLiteral = (path) => {
15
23
  const next = path.getNextSibling();
24
+
25
+ if (!exists(next))
26
+ return false;
27
+
16
28
  const expression = next.get('expression');
17
29
 
18
30
  if (expression.isTemplateLiteral())
19
31
  return true;
20
32
 
33
+ if (expression.isArrayExpression())
34
+ return true;
35
+
21
36
  return expression.isLiteral();
22
37
  };
23
38
 
@@ -39,15 +54,22 @@ module.exports.ExpressionStatement = {
39
54
  store(true);
40
55
  }
41
56
  },
42
- afterSatisfy: () => [
43
- isParentBlockOrNotLastOrParentLast,
44
- isParentBlock,
45
- isNext,
46
- isNextUp,
47
- ],
48
- after(path, {print, maybe, store}) {
57
+ afterIf: (path) => {
49
58
  if (hasTrailingComment(path) && isNextLiteral(path))
50
- return;
59
+ return false;
60
+
61
+ if (satisfyAfter(path))
62
+ return true;
63
+
64
+ if (hasTrailingComment(path) && isLast(path))
65
+ return true;
66
+
67
+ return false;
68
+ },
69
+ after(path, {print, maybe, store}) {
70
+ if (hasTrailingComment(path) && isLast(path)) {
71
+ print.breakline();
72
+ }
51
73
 
52
74
  print.newline();
53
75
  maybe.markAfter(store(), path);
@@ -8,7 +8,9 @@ module.exports.ForInStatement = (path, {print}) => {
8
8
  print('__right');
9
9
  print(')');
10
10
 
11
- if (path.get('body').isBlockStatement())
11
+ if (path
12
+ .get('body')
13
+ .isBlockStatement())
12
14
  print.space();
13
15
 
14
16
  print('__body');
@@ -64,7 +64,9 @@ module.exports.ImportDeclaration = {
64
64
  if (isLast(path))
65
65
  return false;
66
66
 
67
- if (path.getNextSibling().isImportDeclaration())
67
+ if (path
68
+ .getNextSibling()
69
+ .isImportDeclaration())
68
70
  return false;
69
71
 
70
72
  return true;
@@ -20,7 +20,9 @@ module.exports.SwitchStatement = {
20
20
  print(switchCase.get('test'));
21
21
  print(':');
22
22
 
23
- const isBlock = switchCase.get('consequent.0').isBlockStatement();
23
+ const isBlock = switchCase
24
+ .get('consequent.0')
25
+ .isBlockStatement();
24
26
 
25
27
  maybe.indent.inc(!isBlock);
26
28
  maybe.print.newline(!isBlock);
@@ -125,5 +125,7 @@ const isNextAssign = (path) => {
125
125
  if (parentPath.isBlockStatement() && parentPath.node.body.length < 3)
126
126
  return false;
127
127
 
128
- return nextPath.get('expression').isAssignmentExpression();
128
+ return nextPath
129
+ .get('expression')
130
+ .isAssignmentExpression();
129
131
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.75.0",
3
+ "version": "1.76.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 fro 🐊Putout",