@putout/printer 18.0.16 → 18.1.1
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 +21 -0
- package/lib/tokenize/expressions/array-expression/array-expression.js +6 -9
- package/lib/tokenize/expressions/array-expression/newline.js +151 -134
- package/lib/tokenize/expressions/member-expression/is-looks-like-chain.js +4 -1
- package/lib/tokenize/expressions/object-expression/object-expression.js +4 -1
- package/lib/tokenize/is.js +35 -7
- package/lib/tokenize/statements/expression-statement/expression-statement.js +25 -58
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
2026.03.06, v18.1.1
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- ba26d34 @putout/printer: ExpressionStatement: isBreakline
|
|
5
|
+
|
|
6
|
+
2026.03.06, v18.1.0
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- 604e849 @putout/printer: ArrayExpression: long identifier: improve support
|
|
10
|
+
- bdd07e7 @putout/printer: isMultiLine: siplify
|
|
11
|
+
- 945a833 @putout/printer: ArrayExpression: newline: isCoupleLines
|
|
12
|
+
- 2ca2751 @putout/printer: ArrayExpression: newline: isStringsInsideArray
|
|
13
|
+
- 488eb71 @putout/printer: ArrayExpression: newline: isSimpleAndObject
|
|
14
|
+
- 246f9df @putout/printer: ArrayExpression: newline: isIdentifierAndString
|
|
15
|
+
- 56637d1 @putout/printer: ArrayExpression: newline: isStringAndIdentifier
|
|
16
|
+
- caa4682 @putout/printer: ArrayExpression: newline: isStringAndIdentifier
|
|
17
|
+
- 0e9dc1d @putout/printer: ArrayExpression: newline: isMultiline: simplify
|
|
18
|
+
- 103ad2e @putout/printer: ExpressionStatement: isBreakline
|
|
19
|
+
- 04c19c3 @putout/printer: ExpressionStatement: isNextToAssignmentCall: simplify
|
|
20
|
+
- 4bcbd61 @putout/printer: ExpressionStatement: isNextStatementWithBlockComment: simplify
|
|
21
|
+
|
|
1
22
|
2026.03.06, v18.0.16
|
|
2
23
|
|
|
3
24
|
feature:
|
|
@@ -77,7 +77,6 @@ const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.lengt
|
|
|
77
77
|
export const ArrayExpression = {
|
|
78
78
|
beforeIf(path) {
|
|
79
79
|
const {parentPath} = path;
|
|
80
|
-
const {elements} = path.node;
|
|
81
80
|
|
|
82
81
|
if (!parentPath.isArrayExpression())
|
|
83
82
|
return false;
|
|
@@ -85,10 +84,10 @@ export const ArrayExpression = {
|
|
|
85
84
|
if (isCoupleLines(parentPath))
|
|
86
85
|
return false;
|
|
87
86
|
|
|
88
|
-
if (isStringAndIdentifier(
|
|
87
|
+
if (isStringAndIdentifier(path) && isInsideOneElementArray(path))
|
|
89
88
|
return true;
|
|
90
89
|
|
|
91
|
-
return isIdentifierAndIdentifier(
|
|
90
|
+
return isIdentifierAndIdentifier(path);
|
|
92
91
|
},
|
|
93
92
|
before(path, {print}) {
|
|
94
93
|
print.breakline();
|
|
@@ -110,7 +109,6 @@ export const ArrayExpression = {
|
|
|
110
109
|
maybe.indent.inc(indented && shouldIncreaseIndent);
|
|
111
110
|
|
|
112
111
|
const isNewLine = isMultiLine(path, {
|
|
113
|
-
elements,
|
|
114
112
|
maxElementsInOneLine,
|
|
115
113
|
maxElementLengthInOneLine,
|
|
116
114
|
});
|
|
@@ -148,7 +146,7 @@ export const ArrayExpression = {
|
|
|
148
146
|
|
|
149
147
|
const parentElements = path.parentPath.get('elements');
|
|
150
148
|
|
|
151
|
-
if (isInsideArray(path) && isStringAndArray(
|
|
149
|
+
if (isInsideArray(path) && isStringAndArray(path.parentPath)) {
|
|
152
150
|
const parentCountTwo = parentElements.length === 2;
|
|
153
151
|
const isHideIdent = !isAroundStrings(path) || parentCountTwo;
|
|
154
152
|
|
|
@@ -159,7 +157,7 @@ export const ArrayExpression = {
|
|
|
159
157
|
maybe.indent(elements.length && isNewLine);
|
|
160
158
|
}
|
|
161
159
|
|
|
162
|
-
if (isSimpleAndNotEmptyObject(
|
|
160
|
+
if (isSimpleAndNotEmptyObject(path) && !isSpreadElement(elements.at(-1)) && !isCallExpression(elements.at(-1))) {
|
|
163
161
|
print(',');
|
|
164
162
|
print.breakline();
|
|
165
163
|
}
|
|
@@ -168,7 +166,6 @@ export const ArrayExpression = {
|
|
|
168
166
|
},
|
|
169
167
|
afterIf(path) {
|
|
170
168
|
const {parentPath} = path;
|
|
171
|
-
const {elements} = path.node;
|
|
172
169
|
|
|
173
170
|
if (!parentPath.isArrayExpression())
|
|
174
171
|
return false;
|
|
@@ -176,10 +173,10 @@ export const ArrayExpression = {
|
|
|
176
173
|
if (isCoupleLines(parentPath))
|
|
177
174
|
return false;
|
|
178
175
|
|
|
179
|
-
if (isStringAndIdentifier(
|
|
176
|
+
if (isStringAndIdentifier(path) && isInsideOneElementArray(path))
|
|
180
177
|
return true;
|
|
181
178
|
|
|
182
|
-
return isIdentifierAndIdentifier(
|
|
179
|
+
return isIdentifierAndIdentifier(path);
|
|
183
180
|
},
|
|
184
181
|
after(path, {print, indent}) {
|
|
185
182
|
indent.dec();
|
|
@@ -11,10 +11,69 @@ import {
|
|
|
11
11
|
isSimpleAndNotEmptyObject,
|
|
12
12
|
isInsideCall,
|
|
13
13
|
} from '#is';
|
|
14
|
+
import {isInsideArray} from './indent.js';
|
|
15
|
+
import {isInsideForOf} from '../object-pattern/is.js';
|
|
16
|
+
|
|
17
|
+
const isLastArg = ({parentPath}) => !parentPath.isCallExpression();
|
|
18
|
+
const isParentProperty = (path) => path.find(isObjectProperty);
|
|
19
|
+
|
|
20
|
+
const isNumbersArray = createTypeChecker([
|
|
21
|
+
['-: node.elements', isNumbers],
|
|
22
|
+
['-', isInsideForOf],
|
|
23
|
+
['-: -> !', isLastArg],
|
|
24
|
+
['+: -> !', isParentProperty],
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
const isStringAndString = (path) => {
|
|
28
|
+
const {elements} = path.node;
|
|
29
|
+
const [a, b] = elements;
|
|
30
|
+
|
|
31
|
+
return isStringLiteral(a) && isStringLiteral(b);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const isStringsInsideArray = createTypeChecker([
|
|
35
|
+
['-: -> !', isStringAndString],
|
|
36
|
+
['-: -> !', isInsideArray],
|
|
37
|
+
'+: parentPath.node.elements.0 -> ArrayExpression',
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
const isTwoSimplesInsideObjectProperty = (path) => {
|
|
41
|
+
const {node, parentPath} = path;
|
|
42
|
+
|
|
43
|
+
const {elements} = node;
|
|
44
|
+
const {length} = elements;
|
|
45
|
+
const [a, b] = elements;
|
|
46
|
+
|
|
47
|
+
if (length > 2)
|
|
48
|
+
return false;
|
|
49
|
+
|
|
50
|
+
if (!parentPath.isObjectProperty())
|
|
51
|
+
return false;
|
|
52
|
+
|
|
53
|
+
if (!isStringLiteral(a) || !isStringLiteral(b))
|
|
54
|
+
return false;
|
|
55
|
+
|
|
56
|
+
return !isCoupleLines(path);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const isShortTwoSimplesInsideCall = (path, {maxElementsInOneLine}) => {
|
|
60
|
+
const {node, parentPath} = path;
|
|
61
|
+
|
|
62
|
+
const {elements} = node;
|
|
63
|
+
const {length} = elements;
|
|
64
|
+
const [a, b] = elements;
|
|
65
|
+
|
|
66
|
+
if (!parentPath.isCallExpression())
|
|
67
|
+
return false;
|
|
68
|
+
|
|
69
|
+
if (!isStringLiteral(a) || !isStringLiteral(b))
|
|
70
|
+
return false;
|
|
71
|
+
|
|
72
|
+
return length < maxElementsInOneLine;
|
|
73
|
+
};
|
|
14
74
|
|
|
15
75
|
const {
|
|
16
76
|
isObjectExpression,
|
|
17
|
-
isArrayExpression,
|
|
18
77
|
isObjectProperty,
|
|
19
78
|
isCallExpression,
|
|
20
79
|
isAwaitExpression,
|
|
@@ -23,11 +82,14 @@ const {
|
|
|
23
82
|
isStringLiteral,
|
|
24
83
|
isSpreadElement,
|
|
25
84
|
isIdentifier,
|
|
85
|
+
isNumericLiteral,
|
|
26
86
|
} = types;
|
|
27
87
|
|
|
28
88
|
const {round} = Math;
|
|
29
89
|
|
|
30
|
-
const isOneSpread = (
|
|
90
|
+
const isOneSpread = (path) => {
|
|
91
|
+
const {elements} = path.node;
|
|
92
|
+
|
|
31
93
|
if (elements.length > 1)
|
|
32
94
|
return false;
|
|
33
95
|
|
|
@@ -36,19 +98,38 @@ const isOneSpread = (elements) => {
|
|
|
36
98
|
return isSpreadElement(first);
|
|
37
99
|
};
|
|
38
100
|
|
|
39
|
-
const isSimpleAndCall = (
|
|
101
|
+
const isSimpleAndCall = (path) => {
|
|
102
|
+
const {elements} = path.node;
|
|
103
|
+
const [a, b] = elements;
|
|
104
|
+
|
|
40
105
|
if (!isSimple(a))
|
|
41
106
|
return;
|
|
42
107
|
|
|
43
108
|
return isCallExpression(b) || isAwaitExpression(b);
|
|
44
109
|
};
|
|
45
110
|
|
|
46
|
-
const isBooleanAndSimple = (
|
|
111
|
+
const isBooleanAndSimple = (path) => {
|
|
112
|
+
const {elements} = path.node;
|
|
113
|
+
const [a, b] = elements;
|
|
114
|
+
|
|
115
|
+
return isBooleanLiteral(a) && isSimple(b);
|
|
116
|
+
};
|
|
117
|
+
|
|
47
118
|
const isBooleanAndObject = ([a, b]) => isBooleanLiteral(a) && isObjectExpression(b);
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const
|
|
119
|
+
|
|
120
|
+
const isNullAndSimple = (path) => {
|
|
121
|
+
const {elements} = path.node;
|
|
122
|
+
const [a, b] = elements;
|
|
123
|
+
|
|
124
|
+
return isNullLiteral(a) && isSimple(b);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const isSimpleAndObject = (path) => {
|
|
128
|
+
const {elements} = path.node;
|
|
129
|
+
const [a, b] = elements;
|
|
130
|
+
|
|
131
|
+
return isSimple(a) && isObjectExpression(b);
|
|
132
|
+
};
|
|
52
133
|
|
|
53
134
|
const isSiblingIsArray = (path) => {
|
|
54
135
|
if (path.getNextSibling().isArrayExpression())
|
|
@@ -70,13 +151,22 @@ const isMoreThenMaxLiteralLength = (path, {maxElementLengthInOneLine}) => {
|
|
|
70
151
|
return first.value.length > maxElementLengthInOneLine;
|
|
71
152
|
};
|
|
72
153
|
|
|
154
|
+
const isMoreThenMaxIdentifierLength = (path) => {
|
|
155
|
+
const [first] = path.node.elements;
|
|
156
|
+
|
|
157
|
+
return isIdentifier(first);
|
|
158
|
+
};
|
|
159
|
+
|
|
73
160
|
const isMoreThenMaxElementLengthInOneLine = createTypeChecker([
|
|
74
161
|
['-', isEmptyArray],
|
|
75
162
|
['-: -> !', isInsideCall],
|
|
76
163
|
['+', isMoreThenMaxLiteralLength],
|
|
164
|
+
['+', isMoreThenMaxIdentifierLength],
|
|
77
165
|
]);
|
|
78
166
|
|
|
79
|
-
function isMaxElementLengthInOneLine(
|
|
167
|
+
function isMaxElementLengthInOneLine(path, {maxElementLengthInOneLine}) {
|
|
168
|
+
const elements = path.get('elements');
|
|
169
|
+
|
|
80
170
|
if (elements.length > 1)
|
|
81
171
|
return false;
|
|
82
172
|
|
|
@@ -88,125 +178,55 @@ function isMaxElementLengthInOneLine(elements, maxElementLengthInOneLine) {
|
|
|
88
178
|
return first.node.name.length < maxElementLengthInOneLine;
|
|
89
179
|
}
|
|
90
180
|
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
if (isMaxElementLengthInOneLine(elements, maxElementLengthInOneLine))
|
|
95
|
-
return ONE_LINE;
|
|
96
|
-
|
|
97
|
-
if (isMoreThenMaxElementLengthInOneLine(path, {elements, maxElementLengthInOneLine}))
|
|
98
|
-
return MULTI_LINE;
|
|
99
|
-
|
|
100
|
-
if (elements.length > maxElementsInOneLine && isStringLiteral(first))
|
|
101
|
-
return MULTI_LINE;
|
|
102
|
-
|
|
103
|
-
if (elements.length > 3 && !isObjectExpression(first))
|
|
104
|
-
return MULTI_LINE;
|
|
105
|
-
|
|
106
|
-
if (isSimpleAndNotEmptyObject(elements))
|
|
107
|
-
return MULTI_LINE;
|
|
108
|
-
|
|
109
|
-
if (isOneSimple(path))
|
|
110
|
-
return ONE_LINE;
|
|
111
|
-
|
|
112
|
-
if (isOneSpread(elements))
|
|
113
|
-
return ONE_LINE;
|
|
114
|
-
|
|
115
|
-
if (elements.length === 2 && isIdentifierAndIdentifier(elements))
|
|
116
|
-
return ONE_LINE;
|
|
117
|
-
|
|
118
|
-
if (isCallInsideArrow(path))
|
|
119
|
-
return ONE_LINE;
|
|
120
|
-
|
|
121
|
-
if (isIncreaseIndent(path))
|
|
122
|
-
return ONE_LINE;
|
|
123
|
-
|
|
124
|
-
if (isInsideLoop(path))
|
|
125
|
-
return ONE_LINE;
|
|
126
|
-
|
|
127
|
-
if (isBooleanAndSimple(elements))
|
|
128
|
-
return ONE_LINE;
|
|
129
|
-
|
|
130
|
-
if (isNullAndSimple(elements))
|
|
131
|
-
return ONE_LINE;
|
|
132
|
-
|
|
133
|
-
if (isSimpleAndCall(elements))
|
|
134
|
-
return ONE_LINE;
|
|
135
|
-
|
|
136
|
-
if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
|
|
137
|
-
return ONE_LINE;
|
|
138
|
-
|
|
139
|
-
if (isTwoStringsDifferentLength(elements))
|
|
140
|
-
return ONE_LINE;
|
|
141
|
-
|
|
142
|
-
if (isTwoSimplesInsideObjectProperty(path))
|
|
143
|
-
return ONE_LINE;
|
|
144
|
-
|
|
145
|
-
if (isStringAndArray(elements) && elements.length < 3)
|
|
146
|
-
return ONE_LINE;
|
|
147
|
-
|
|
148
|
-
if (isStringAndMember(elements))
|
|
149
|
-
return ONE_LINE;
|
|
150
|
-
|
|
151
|
-
if (isStringAndIdentifier(elements))
|
|
152
|
-
return ONE_LINE;
|
|
153
|
-
|
|
154
|
-
if (isIdentifierAndString(elements))
|
|
155
|
-
return ONE_LINE;
|
|
156
|
-
|
|
157
|
-
if (isSimpleAndObject(elements))
|
|
158
|
-
return ONE_LINE;
|
|
159
|
-
|
|
160
|
-
if (isStringAndString(elements) && path.parentPath.isArrayExpression() && isArrayExpression(path.parentPath.node.elements[0]))
|
|
161
|
-
return ONE_LINE;
|
|
162
|
-
|
|
163
|
-
if (isSiblingIsArray(path))
|
|
164
|
-
return ONE_LINE;
|
|
165
|
-
|
|
166
|
-
if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
|
|
167
|
-
return MULTI_LINE;
|
|
168
|
-
|
|
169
|
-
return ONE_LINE;
|
|
181
|
+
const isElementsMoreThenMax = (path, {maxElementsInOneLine}) => {
|
|
182
|
+
const {elements} = path.node;
|
|
183
|
+
return elements.length > maxElementsInOneLine;
|
|
170
184
|
};
|
|
171
185
|
|
|
172
|
-
const
|
|
186
|
+
const isElementsMoreThenThree = (path) => {
|
|
187
|
+
const {elements} = path.node;
|
|
188
|
+
return elements.length > 3;
|
|
189
|
+
};
|
|
173
190
|
|
|
174
|
-
const
|
|
191
|
+
const isElementsMoreThenThreeWithNotFirstObject = createTypeChecker([
|
|
192
|
+
'-: node.elements.0 -> ObjectExpression',
|
|
193
|
+
isElementsMoreThenThree,
|
|
194
|
+
]);
|
|
175
195
|
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const {length} = elements;
|
|
181
|
-
const [a, b] = elements;
|
|
182
|
-
|
|
183
|
-
if (!parentPath.isCallExpression())
|
|
184
|
-
return false;
|
|
185
|
-
|
|
186
|
-
if (!isStringLiteral(a) || !isStringLiteral(b))
|
|
187
|
-
return false;
|
|
188
|
-
|
|
189
|
-
return length < maxElementsInOneLine;
|
|
190
|
-
};
|
|
196
|
+
const isElementsMoreThenMaxWithFirstString = createTypeChecker([
|
|
197
|
+
'-: node.elements.0 -> !StringLiteral',
|
|
198
|
+
isElementsMoreThenMax,
|
|
199
|
+
]);
|
|
191
200
|
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
201
|
+
export const isMultiLine = createTypeChecker([
|
|
202
|
+
['-', isMaxElementLengthInOneLine],
|
|
203
|
+
isMoreThenMaxElementLengthInOneLine,
|
|
204
|
+
isElementsMoreThenMaxWithFirstString,
|
|
205
|
+
isElementsMoreThenThreeWithNotFirstObject,
|
|
206
|
+
isSimpleAndNotEmptyObject,
|
|
207
|
+
['-', isOneSimple],
|
|
208
|
+
['-', isOneSpread],
|
|
209
|
+
['-', isIdentifierAndIdentifier],
|
|
210
|
+
['-', isCallInsideArrow],
|
|
211
|
+
['-', isIncreaseIndent],
|
|
212
|
+
['-', isInsideLoop],
|
|
213
|
+
['-', isBooleanAndSimple],
|
|
214
|
+
['-', isNullAndSimple],
|
|
215
|
+
['-', isSimpleAndCall],
|
|
216
|
+
['-', isShortTwoSimplesInsideCall],
|
|
217
|
+
['-', isTwoStringsDifferentLength],
|
|
218
|
+
['-', isTwoSimplesInsideObjectProperty],
|
|
219
|
+
['-', isStringAndArray],
|
|
220
|
+
['-', isStringAndMember],
|
|
221
|
+
['-', isStringAndIdentifier],
|
|
222
|
+
['-', isIdentifierAndString],
|
|
223
|
+
['-', isSimpleAndObject],
|
|
224
|
+
['-', isSiblingIsArray],
|
|
225
|
+
['-', isStringsInsideArray],
|
|
226
|
+
tooLong,
|
|
227
|
+
isCoupleLines,
|
|
228
|
+
isNumbersArray,
|
|
229
|
+
]);
|
|
210
230
|
|
|
211
231
|
function isOneSimple(path) {
|
|
212
232
|
const elements = path.get('elements');
|
|
@@ -228,17 +248,18 @@ function isOneSimple(path) {
|
|
|
228
248
|
return first.isMemberExpression();
|
|
229
249
|
}
|
|
230
250
|
|
|
231
|
-
function isTwoStringsDifferentLength(
|
|
232
|
-
const
|
|
251
|
+
function isTwoStringsDifferentLength(path) {
|
|
252
|
+
const {elements} = path.node;
|
|
253
|
+
const [a, b] = elements;
|
|
233
254
|
|
|
234
|
-
if (
|
|
255
|
+
if (elements.length > 2)
|
|
235
256
|
return false;
|
|
236
257
|
|
|
237
|
-
if (!
|
|
258
|
+
if (!isStringLiteral(a) || !isStringLiteral(b))
|
|
238
259
|
return false;
|
|
239
260
|
|
|
240
|
-
const aLength = a.
|
|
241
|
-
const bLength = b.
|
|
261
|
+
const aLength = a.value.length;
|
|
262
|
+
const bLength = b.value.length;
|
|
242
263
|
|
|
243
264
|
return round(bLength / aLength) > 2;
|
|
244
265
|
}
|
|
@@ -275,17 +296,13 @@ function isCallInsideArrow(path) {
|
|
|
275
296
|
|
|
276
297
|
function isNumbers(elements) {
|
|
277
298
|
for (const element of elements) {
|
|
278
|
-
if (
|
|
299
|
+
if (isNumericLiteral(element))
|
|
279
300
|
return true;
|
|
280
301
|
}
|
|
281
302
|
|
|
282
303
|
return false;
|
|
283
304
|
}
|
|
284
305
|
|
|
285
|
-
const isLastArg = ({parentPath}) => !parentPath.isCallExpression();
|
|
286
|
-
|
|
287
|
-
const isParentProperty = (path) => path.find(isObjectProperty);
|
|
288
|
-
|
|
289
306
|
export function isIncreaseIndent(path) {
|
|
290
307
|
const elements = path.get('elements');
|
|
291
308
|
|
|
@@ -12,7 +12,10 @@ const {
|
|
|
12
12
|
const isArgOfCall = (path) => path.parentPath?.isCallExpression() && path.parentPath.get('arguments.0') === path;
|
|
13
13
|
const isCall = (a) => a.type === 'CallExpression';
|
|
14
14
|
|
|
15
|
-
const isExcludedFromChain = satisfy([
|
|
15
|
+
const isExcludedFromChain = satisfy([
|
|
16
|
+
isUnaryExpression,
|
|
17
|
+
isIfStatement,
|
|
18
|
+
]);
|
|
16
19
|
const hasComment = ({type}) => type === 'CommentLine';
|
|
17
20
|
|
|
18
21
|
const isInsideMemberCall = (path) => {
|
|
@@ -38,7 +38,10 @@ const {
|
|
|
38
38
|
} = types;
|
|
39
39
|
|
|
40
40
|
const isLogicalArgument = (path) => isLogicalExpression(path.node.argument);
|
|
41
|
-
const isParens = createTypeChecker([
|
|
41
|
+
const isParens = createTypeChecker([
|
|
42
|
+
isInsideBody,
|
|
43
|
+
isInsideExpression,
|
|
44
|
+
]);
|
|
42
45
|
const getCallee = (fn) => (a) => fn(a.get('callee'));
|
|
43
46
|
|
|
44
47
|
const isMemberExpressionCallee = createTypeChecker([
|
package/lib/tokenize/is.js
CHANGED
|
@@ -91,7 +91,10 @@ export function isCoupleLines(path) {
|
|
|
91
91
|
|
|
92
92
|
export const exists = (a) => a.node;
|
|
93
93
|
|
|
94
|
-
export function isStringAndIdentifier(
|
|
94
|
+
export function isStringAndIdentifier(path) {
|
|
95
|
+
const {elements} = path.node;
|
|
96
|
+
const [a, b] = elements;
|
|
97
|
+
|
|
95
98
|
return isStringLiteral(a) && isIdentifier(b);
|
|
96
99
|
}
|
|
97
100
|
|
|
@@ -107,7 +110,8 @@ const checkObject = (elements) => {
|
|
|
107
110
|
return a.node.properties.length;
|
|
108
111
|
};
|
|
109
112
|
|
|
110
|
-
export const isSimpleAndNotEmptyObject = (
|
|
113
|
+
export const isSimpleAndNotEmptyObject = (path) => {
|
|
114
|
+
const elements = path.get('elements');
|
|
111
115
|
const [a] = elements;
|
|
112
116
|
|
|
113
117
|
const simpleTypes = [
|
|
@@ -124,20 +128,44 @@ export const isSimpleAndNotEmptyObject = (elements) => {
|
|
|
124
128
|
return checkObject(elements);
|
|
125
129
|
};
|
|
126
130
|
|
|
127
|
-
export const isIdentifierAndIdentifier = (
|
|
131
|
+
export const isIdentifierAndIdentifier = (path) => {
|
|
132
|
+
const {elements} = path.node;
|
|
133
|
+
|
|
134
|
+
if (elements.length !== 2)
|
|
135
|
+
return;
|
|
136
|
+
|
|
137
|
+
const [a, b] = elements;
|
|
138
|
+
|
|
128
139
|
return isIdentifier(a) && isIdentifier(b);
|
|
129
140
|
};
|
|
130
141
|
|
|
131
|
-
export const isStringAndMember = (
|
|
132
|
-
|
|
133
|
-
|
|
142
|
+
export const isStringAndMember = (path) => {
|
|
143
|
+
const {elements} = path.node;
|
|
144
|
+
const [a, b] = elements;
|
|
145
|
+
|
|
146
|
+
return isStringLiteral(a) && isMemberExpression(b);
|
|
147
|
+
};
|
|
148
|
+
export const isIdentifierAndString = (path) => {
|
|
149
|
+
const {elements} = path.node;
|
|
150
|
+
const [a, b] = elements;
|
|
151
|
+
|
|
152
|
+
return isIdentifier(a) && isStringLiteral(b);
|
|
153
|
+
};
|
|
154
|
+
export const isStringAndArray = (path) => {
|
|
155
|
+
const elements = path.get('elements');
|
|
156
|
+
|
|
157
|
+
if (elements.length !== 2)
|
|
158
|
+
return false;
|
|
159
|
+
|
|
160
|
+
const [a, b] = elements;
|
|
161
|
+
|
|
134
162
|
if (!isStringLiteral(a))
|
|
135
163
|
return false;
|
|
136
164
|
|
|
137
165
|
if (!isArrayExpression(b))
|
|
138
166
|
return false;
|
|
139
167
|
|
|
140
|
-
return !isStringAndIdentifier(b
|
|
168
|
+
return !isStringAndIdentifier(b);
|
|
141
169
|
};
|
|
142
170
|
|
|
143
171
|
const isIfOrStatement = (a) => isIfStatement(a) || isStatement(a);
|
|
@@ -4,7 +4,6 @@ import {
|
|
|
4
4
|
isNext,
|
|
5
5
|
isLast,
|
|
6
6
|
isNewlineBetweenSiblings,
|
|
7
|
-
satisfy,
|
|
8
7
|
noTrailingComment,
|
|
9
8
|
hasTrailingComment,
|
|
10
9
|
isCoupleLines,
|
|
@@ -19,27 +18,33 @@ import {
|
|
|
19
18
|
import {afterIf} from './after-if.js';
|
|
20
19
|
import {beforeIf} from './before-if.js';
|
|
21
20
|
|
|
22
|
-
const
|
|
23
|
-
|
|
21
|
+
const {isIfStatement} = types;
|
|
22
|
+
|
|
23
|
+
const isCallInsideExpression = createTypeChecker([
|
|
24
|
+
'-: -> !ExpressionStatement',
|
|
25
|
+
'+: node.expression -> CallExpression',
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
const isNextToAssignmentCall = createTypeChecker([
|
|
29
|
+
'-: node.expression -> AssignmentExpression',
|
|
30
|
+
['+', getNext(isCallInsideExpression)],
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
const isBreaklineAfter = createTypeChecker([
|
|
24
34
|
['-: -> !', hasTrailingComment],
|
|
25
35
|
['-: -> !', isLast],
|
|
26
36
|
['+', isCoupleLines],
|
|
27
37
|
]);
|
|
28
38
|
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const isNextIf = (path) => path
|
|
36
|
-
.getNextSibling()
|
|
37
|
-
.isIfStatement();
|
|
39
|
+
const isNextStatementWithBlockComment = createTypeChecker([
|
|
40
|
+
'-: node.expression -> !CallExpression',
|
|
41
|
+
'-: node.expression.arguments.0 -> !CallExpression',
|
|
42
|
+
'+: node.trailingComments.0 -> CommentBlock',
|
|
43
|
+
]);
|
|
38
44
|
|
|
39
|
-
const
|
|
45
|
+
const isBreakline = createTypeChecker([
|
|
40
46
|
isNewlineBetweenSiblings,
|
|
41
|
-
|
|
42
|
-
isNextIf,
|
|
47
|
+
getNext(isIfStatement),
|
|
43
48
|
]);
|
|
44
49
|
|
|
45
50
|
const isIndent = createTypeChecker([
|
|
@@ -54,20 +59,20 @@ export const isIndentAfter = createTypeChecker([
|
|
|
54
59
|
]);
|
|
55
60
|
|
|
56
61
|
export const ExpressionStatement = {
|
|
62
|
+
printLeadingCommentLine,
|
|
63
|
+
printLeadingCommentBlock,
|
|
57
64
|
beforeIf,
|
|
58
65
|
before(path, {indent}) {
|
|
59
66
|
indent();
|
|
60
67
|
},
|
|
61
68
|
print(path, {print, maybe, store}) {
|
|
62
|
-
const insideReturn = isInsideReturn(path);
|
|
63
|
-
|
|
64
69
|
print('__expression');
|
|
65
|
-
maybe.print(!
|
|
70
|
+
maybe.print(!isInsideReturn(path), ';');
|
|
66
71
|
|
|
67
72
|
if (!isNext(path))
|
|
68
73
|
return;
|
|
69
74
|
|
|
70
|
-
if (
|
|
75
|
+
if (isBreakline(path)) {
|
|
71
76
|
print.newline();
|
|
72
77
|
maybe.indent(isIndent(path));
|
|
73
78
|
store(true);
|
|
@@ -75,7 +80,7 @@ export const ExpressionStatement = {
|
|
|
75
80
|
},
|
|
76
81
|
afterIf,
|
|
77
82
|
after(path, {print, maybe, store}) {
|
|
78
|
-
maybe.print.breakline(
|
|
83
|
+
maybe.print.breakline(isBreaklineAfter(path));
|
|
79
84
|
|
|
80
85
|
if (isInsideReturn(path))
|
|
81
86
|
return;
|
|
@@ -85,42 +90,4 @@ export const ExpressionStatement = {
|
|
|
85
90
|
maybe.markAfter(store(), path);
|
|
86
91
|
},
|
|
87
92
|
};
|
|
88
|
-
ExpressionStatement.printLeadingCommentLine = printLeadingCommentLine;
|
|
89
|
-
ExpressionStatement.printLeadingCommentBlock = printLeadingCommentBlock;
|
|
90
|
-
|
|
91
|
-
function isNotLastBody(path) {
|
|
92
|
-
return path.parentPath.get('body') === path;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function isNextToAssignmentCall(path) {
|
|
96
|
-
if (isAssignmentExpression(path.node.expression))
|
|
97
|
-
return false;
|
|
98
|
-
|
|
99
|
-
const nextPath = path.getNextSibling();
|
|
100
|
-
|
|
101
|
-
if (!isExpressionStatement(nextPath))
|
|
102
|
-
return false;
|
|
103
|
-
|
|
104
|
-
const {expression} = nextPath.node;
|
|
105
|
-
|
|
106
|
-
return isCallExpression(expression);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function isNextStatementWithBlockComment(path) {
|
|
110
|
-
const {expression} = path.node;
|
|
111
|
-
|
|
112
|
-
if (!isCallExpression(expression))
|
|
113
|
-
return false;
|
|
114
|
-
|
|
115
|
-
if (!isCallExpression(expression.arguments[0]))
|
|
116
|
-
return false;
|
|
117
|
-
|
|
118
|
-
return hasTrailingBlock(path);
|
|
119
|
-
}
|
|
120
93
|
|
|
121
|
-
function hasTrailingBlock(path) {
|
|
122
|
-
const {trailingComments} = path.node;
|
|
123
|
-
const [first] = trailingComments;
|
|
124
|
-
|
|
125
|
-
return isCommentBlock(first);
|
|
126
|
-
}
|
package/package.json
CHANGED