@putout/printer 6.11.5 → 6.12.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
+ 2023.12.01, v6.12.0
2
+
3
+ feature:
4
+ - 5534a06 @putout/printer: couple trailing comments
5
+
6
+ 2023.12.01, v6.11.6
7
+
8
+ feature:
9
+ - 60ae143 @putout/printer: BlockStatement: siplify
10
+
1
11
  2023.12.01, v6.11.5
2
12
 
3
13
  feature:
@@ -26,6 +26,9 @@ function isCommentOnNextLine(path) {
26
26
  if (isTrailingIsLeading(path))
27
27
  return false;
28
28
 
29
+ if (trailingComments.length > 1)
30
+ return false;
31
+
29
32
  const [comment] = trailingComments;
30
33
  const {line} = comment.loc.start;
31
34
 
@@ -9,6 +9,7 @@ const isTwoStringsTupleAndParentIsArrayWithFirstArrayElement = (path) => {
9
9
 
10
10
  return isParentIsArrayWithFirstArrayElement(path);
11
11
  };
12
+
12
13
  const isLotsOfElementsFirstNotObject = (path) => {
13
14
  const {elements} = path.node;
14
15
  const [first] = elements;
@@ -291,4 +292,3 @@ module.exports.isCurrentNewLine = (path) => {
291
292
 
292
293
  return !path.isObjectExpression();
293
294
  };
294
-
@@ -5,6 +5,7 @@ const {
5
5
  isParentProgram,
6
6
  isLast,
7
7
  exists,
8
+ satisfy,
8
9
  } = require('../../is');
9
10
 
10
11
  const {
@@ -12,6 +13,8 @@ const {
12
13
  isObjectMethod,
13
14
  isFunctionDeclaration,
14
15
  isExportDeclaration,
16
+ isDoWhileStatement,
17
+ isBlockStatement,
15
18
  } = require('@putout/babel').types;
16
19
 
17
20
  const {markAfter} = require('../../mark');
@@ -77,35 +80,62 @@ module.exports.BlockStatement = {
77
80
  },
78
81
  };
79
82
 
80
- function shouldAddNewlineAfter(path) {
81
- const {parentPath} = path;
82
-
83
- if (parentPath.isDoWhileStatement())
83
+ const isTopLevelWithNoNext = (path) => {
84
+ if (isNext(path))
84
85
  return false;
85
86
 
86
- if (parentPath.isBlockStatement())
87
- return true;
87
+ return !isNext(path.parentPath) && isParentProgram(path.parentPath);
88
+ };
89
+
90
+ const isInsideIfWithoutElseInsideFn = (path) => {
91
+ return parentIfWithoutElse(path) && path.find(isMethodOrArrow);
92
+ };
93
+
94
+ const isEmptyBodyNoNext = (path) => {
95
+ const {parentPath} = path;
96
+ return parentPath.isStatement() && !path.node.body.length && !isNext(parentPath);
97
+ };
98
+
99
+ const isLooksLikeInsideFn = ({parentPath}) => {
100
+ return /FunctionExpression/.test(parentPath.type);
101
+ };
102
+
103
+ const NEWLINE = true;
104
+ const NO_NEWLINE = false;
105
+
106
+ const isInsideDoWhile = ({parentPath}) => isDoWhileStatement(parentPath);
107
+ const isInsideBlock = ({parentPath}) => isBlockStatement(parentPath);
108
+
109
+ const isNoNewline = satisfy([
110
+ isInsideDoWhile,
111
+ isTopLevelWithNoNext,
112
+ insideIfWithNoBody,
113
+ ]);
114
+
115
+ function shouldAddNewlineAfter(path) {
116
+ if (isInsideBlock(path))
117
+ return NEWLINE;
88
118
 
89
- if (!isNext(path) && !isNext(path.parentPath) && isParentProgram(path.parentPath))
90
- return false;
119
+ if (isNoNewline(path))
120
+ return NO_NEWLINE;
91
121
 
92
- if (insideIfWithNoBody(path))
93
- return false;
122
+ if (isInsideIfWithoutElseInsideFn(path))
123
+ return NEWLINE;
94
124
 
95
- if (parentIfWithoutElse(path) && path.find(isMethodOrArrow))
96
- return true;
125
+ if (isEmptyBodyNoNext(path))
126
+ return NO_NEWLINE;
97
127
 
98
- if (parentPath.isStatement() && !path.node.body.length && !isNext(parentPath))
99
- return false;
128
+ if (isTry(path))
129
+ return NO_NEWLINE;
100
130
 
101
- if (isTry(path) || /FunctionExpression/.test(path.parentPath.type))
102
- return false;
131
+ if (isLooksLikeInsideFn(path))
132
+ return NO_NEWLINE;
103
133
 
104
134
  if (isLast(path))
105
- return false;
135
+ return NO_NEWLINE;
106
136
 
107
137
  if (isExportFunction(path))
108
- return false;
138
+ return NO_NEWLINE;
109
139
 
110
140
  return !isNextIfAlternate(path);
111
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "6.11.5",
3
+ "version": "6.12.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",