@putout/printer 6.11.4 → 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,13 @@
1
+ 2023.12.01, v6.11.6
2
+
3
+ feature:
4
+ - 60ae143 @putout/printer: BlockStatement: siplify
5
+
6
+ 2023.12.01, v6.11.5
7
+
8
+ feature:
9
+ - 96f6f4b @putout/printer: ArrayExpression: newline: isMultiLine
10
+
1
11
  2023.12.01, v6.11.4
2
12
 
3
13
  feature:
@@ -1,6 +1,40 @@
1
1
  'use strict';
2
2
 
3
3
  const {isSimple} = require('@putout/operate');
4
+ const isTwoStringsTupleAndParentIsArrayWithFirstArrayElement = (path) => {
5
+ const {elements} = path.node;
6
+
7
+ if (!isStringAndString(elements))
8
+ return false;
9
+
10
+ return isParentIsArrayWithFirstArrayElement(path);
11
+ };
12
+
13
+ const isLotsOfElementsFirstNotObject = (path) => {
14
+ const {elements} = path.node;
15
+ const [first] = elements;
16
+
17
+ return elements.length > 3 && !isObjectExpression(first);
18
+ };
19
+
20
+ const isTwoSimplesInsideObjectProperty = (path) => {
21
+ const {node, parentPath} = path;
22
+
23
+ const {elements} = node;
24
+ const {length} = elements;
25
+ const [a, b] = elements;
26
+
27
+ if (length > 2)
28
+ return false;
29
+
30
+ if (!parentPath.isObjectProperty())
31
+ return false;
32
+
33
+ if (!isStringLiteral(a) || !isStringLiteral(b))
34
+ return false;
35
+
36
+ return !isCoupleLines(path);
37
+ };
4
38
 
5
39
  const {
6
40
  isObjectExpression,
@@ -49,7 +83,7 @@ const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
49
83
  const ONE_LINE = false;
50
84
  const MULTI_LINE = true;
51
85
 
52
- const isOneLine = satisfy([
86
+ const isOneLineElements = satisfy([
53
87
  isOneSimple,
54
88
  isOneSpread,
55
89
  isIdentifierAndIdentifier,
@@ -64,35 +98,34 @@ const isOneLine = satisfy([
64
98
  isSimpleAndObject,
65
99
  ]);
66
100
 
101
+ const isOneLinePath = satisfy([
102
+ isCallInsideArrow,
103
+ notIncreaseIndent,
104
+ isInsideLoop,
105
+ isTwoSimplesInsideObjectProperty,
106
+ isTwoStringsTupleAndParentIsArrayWithFirstArrayElement,
107
+ ]);
108
+
109
+ const isMultiLinePath = satisfy([
110
+ tooLong,
111
+ isCoupleLines,
112
+ notNumbersInsideForOf,
113
+ ]);
114
+
67
115
  module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
68
- if (elements.length > 3 && !isObjectExpression(elements[0]))
116
+ if (isLotsOfElementsFirstNotObject(path))
69
117
  return MULTI_LINE;
70
118
 
71
- if (isOneLine(elements))
72
- return ONE_LINE;
73
-
74
- if (isCallInsideArrow(path))
119
+ if (isOneLineElements(elements))
75
120
  return ONE_LINE;
76
121
 
77
- if (notIncreaseIndent(path))
78
- return ONE_LINE;
79
-
80
- if (isInsideLoop(path))
122
+ if (isOneLinePath(path))
81
123
  return ONE_LINE;
82
124
 
83
125
  if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
84
126
  return ONE_LINE;
85
127
 
86
- if (isTwoSimplesInsideObjectProperty(path))
87
- return ONE_LINE;
88
-
89
- if (isStringAndString(elements) && isParentIsArrayWithFirstArrayElement(path))
90
- return ONE_LINE;
91
-
92
- if (tooLong(path) || isCoupleLines(path))
93
- return MULTI_LINE;
94
-
95
- if (notNumbersInsideForOf(path))
128
+ if (isMultiLinePath(path))
96
129
  return MULTI_LINE;
97
130
 
98
131
  return ONE_LINE;
@@ -136,25 +169,6 @@ const isShortTwoSimplesInsideCall = (path, short) => {
136
169
  return length < short;
137
170
  };
138
171
 
139
- const isTwoSimplesInsideObjectProperty = (path) => {
140
- const {node, parentPath} = path;
141
-
142
- const {elements} = node;
143
- const {length} = elements;
144
- const [a, b] = elements;
145
-
146
- if (length > 2)
147
- return false;
148
-
149
- if (!parentPath.isObjectProperty())
150
- return false;
151
-
152
- if (!isStringLiteral(a) || !isStringLiteral(b))
153
- return false;
154
-
155
- return !isCoupleLines(path);
156
- };
157
-
158
172
  function isOneSimple(elements) {
159
173
  if (elements.length !== 1)
160
174
  return false;
@@ -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.4",
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",