@putout/printer 6.11.5 → 6.11.6

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