@putout/printer 2.11.0 → 2.13.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,14 @@
1
+ 2023.06.13, v2.13.0
2
+
3
+ feature:
4
+ - 1e77dca @putout/printer: ArrayExpression: one string literal
5
+
6
+ 2023.06.13, v2.12.0
7
+
8
+ feature:
9
+ - a264001 @putout/printer: ArrayExpression: newline
10
+ - cbe69f6 @putout/printer: BlockStatement
11
+
1
12
  2023.06.13, v2.11.0
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -35,9 +35,7 @@ And update `.putout.json`:
35
35
  ```json
36
36
  {
37
37
  "printer": "putout",
38
- "plugins": [
39
- "printer"
40
- ]
38
+ "plugins": ["printer"]
41
39
  }
42
40
  ```
43
41
 
@@ -268,9 +266,7 @@ function speed(printer) {
268
266
  for (let i = 0; i < 1000; i++) {
269
267
  putout(code, {
270
268
  printer,
271
- plugins: [
272
- 'remove-unused-variables',
273
- ],
269
+ plugins: ['remove-unused-variables'],
274
270
  });
275
271
  }
276
272
 
@@ -104,7 +104,7 @@ module.exports.parseComments = (path, {write}, semantics) => {
104
104
  };
105
105
 
106
106
  function isSameLine(path, loc) {
107
- return path.node.loc.start.line === loc.start.line || path.node.loc.end.line === loc.end.line;
107
+ return path.node.loc?.start.line === loc.start.line || path.node.loc?.end.line === loc.end.line;
108
108
  }
109
109
 
110
110
  function maybeInsideFn(insideFn, {print, indent}) {
@@ -1,84 +1,20 @@
1
1
  'use strict';
2
2
 
3
- const {round} = Math;
4
-
5
- const {
6
- isObjectProperty,
7
- isStringLiteral,
8
- isArrayExpression,
9
- isIdentifier,
10
- isCallExpression,
11
- isBooleanLiteral,
12
- isNullLiteral,
13
- isObjectExpression,
14
- isAwaitExpression,
15
- } = require('@babel/types');
16
-
17
- const {isSimple} = require('@putout/operate');
18
-
19
3
  const {
20
4
  isCoupleLines,
21
5
  isStringAndIdentifier,
22
- isIdentifierAndString,
23
- isStringAndMember,
6
+ isIdentifierAndIdentifier,
7
+ isStringAndArray,
24
8
  } = require('../../is');
25
9
 
26
- const isForOf = ({parentPath}) => parentPath.isForOfStatement();
10
+ const {
11
+ isNewlineBetweenElements,
12
+ isIncreaseIndent,
13
+ } = require('./new-line');
27
14
 
28
15
  const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
29
-
30
- const isStringAndArray = ([a, b]) => {
31
- if (!isStringLiteral(a))
32
- return false;
33
-
34
- if (!isArrayExpression(b))
35
- return false;
36
-
37
- return !isStringAndIdentifier(b.node.elements);
38
- };
39
-
40
- const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
41
-
42
- const isShortTwoSimplesInsideCall = (path, short) => {
43
- const {
44
- node,
45
- parentPath,
46
- } = path;
47
-
48
- const {elements} = node;
49
- const {length} = elements;
50
- const [a, b] = elements;
51
-
52
- if (!parentPath.isCallExpression())
53
- return false;
54
-
55
- if (!isStringLiteral(a) || !isStringLiteral(b))
56
- return false;
57
-
58
- return length < short;
59
- };
60
-
61
- const isIdentifierAndIdentifier = ([a, b]) => isIdentifier(a) && isIdentifier(b);
62
16
  const isInsideArray = (path) => path.parentPath.isArrayExpression();
63
17
 
64
- const isSimpleAndCall = ([a, b]) => {
65
- if (!isSimple(a))
66
- return;
67
-
68
- return isCallExpression(b) || isAwaitExpression(b);
69
- };
70
-
71
- const isBooleanAndSimple = ([a, b]) => isBooleanLiteral(a) && isSimple(b);
72
- const isNullAndSimple = ([a, b]) => isNullLiteral(a) && isSimple(b);
73
- const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
74
-
75
- const isStringAndObject = (elements) => {
76
- const first = elements.at(0);
77
- const last = elements.at(-1);
78
-
79
- return isStringLiteral(first) && isObjectExpression(last);
80
- };
81
-
82
18
  const isTwoLongStrings = ([a, b]) => {
83
19
  const LONG_STRING = 20;
84
20
 
@@ -177,184 +113,3 @@ module.exports.ArrayExpression = {
177
113
  indent.inc();
178
114
  },
179
115
  };
180
-
181
- function isNumbers(elements) {
182
- for (const element of elements) {
183
- if (element.isNumericLiteral())
184
- return true;
185
- }
186
-
187
- return false;
188
- }
189
-
190
- function isLastArg({parentPath}) {
191
- return !parentPath.isCallExpression();
192
- }
193
-
194
- function isParentProperty(path) {
195
- return path.find(isObjectProperty);
196
- }
197
-
198
- function isIncreaseIndent(path) {
199
- const elements = path.get('elements');
200
-
201
- if (!elements.length)
202
- return false;
203
-
204
- if (isInsideCallLoop(path))
205
- return false;
206
-
207
- if (elements[0].isObjectExpression())
208
- return true;
209
-
210
- if (isStringAndObject(elements))
211
- return true;
212
-
213
- return false;
214
- }
215
-
216
- function tooLong(path) {
217
- const elements = path.get('elements');
218
-
219
- if (elements.length < 2)
220
- return false;
221
-
222
- for (const el of path.get('elements')) {
223
- if (el.isStringLiteral() && el.node.value.length > 4)
224
- return true;
225
- }
226
-
227
- return false;
228
- }
229
-
230
- function isCallInsideArrow(path) {
231
- const {parentPath} = path;
232
-
233
- if (!parentPath.isCallExpression())
234
- return false;
235
-
236
- if (!parentPath.parentPath.isFunction())
237
- return false;
238
-
239
- return path.node.elements.length < 4;
240
- }
241
-
242
- function isOneSimple(path) {
243
- const elements = path.get('elements');
244
-
245
- if (elements.length !== 1)
246
- return false;
247
-
248
- const [first] = elements;
249
-
250
- if (first.isIdentifier() && first.node.name.length < 5)
251
- return true;
252
-
253
- if (first.isCallExpression())
254
- return false;
255
-
256
- return first.isMemberExpression();
257
- }
258
-
259
- function isNewlineBetweenElements(path, {elements, maxElementsInOneLine}) {
260
- if (elements.length > 3 && !isObjectExpression(elements[0]))
261
- return true;
262
-
263
- if (isOneSimple(path))
264
- return false;
265
-
266
- if (elements.length === 2 && isIdentifierAndIdentifier(elements))
267
- return false;
268
-
269
- if (isCallInsideArrow(path))
270
- return false;
271
-
272
- if (isOneElementCall(path, {elements}))
273
- return false;
274
-
275
- if (isIncreaseIndent(path))
276
- return false;
277
-
278
- if (isInsideLoop(path))
279
- return false;
280
-
281
- if (isBooleanAndSimple(elements))
282
- return false;
283
-
284
- if (isNullAndSimple(elements))
285
- return false;
286
-
287
- if (isSimpleAndCall(elements))
288
- return false;
289
-
290
- if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
291
- return false;
292
-
293
- if (isTwoStringsDifferentLength(elements))
294
- return false;
295
-
296
- if (isStringAndArray(elements))
297
- return false;
298
-
299
- if (isStringAndMember(elements))
300
- return false;
301
-
302
- if (isStringAndIdentifier(elements))
303
- return false;
304
-
305
- if (isIdentifierAndString(elements))
306
- return false;
307
-
308
- if (isSimpleAndObject(elements))
309
- return false;
310
-
311
- if (isStringAndString(elements) && path.parentPath.isArrayExpression() && isArrayExpression(path.parentPath.node.elements[0]))
312
- return false;
313
-
314
- if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
315
- return true;
316
-
317
- return false;
318
- }
319
-
320
- function isTwoStringsDifferentLength(strings) {
321
- const [a, b] = strings;
322
-
323
- if (strings.length > 2)
324
- return false;
325
-
326
- if (!a?.isStringLiteral() || !b?.isStringLiteral())
327
- return false;
328
-
329
- const aLength = a.node.value.length;
330
- const bLength = b.node.value.length;
331
-
332
- return round(bLength / aLength) > 2;
333
- }
334
-
335
- function isInsideCallLoop(path) {
336
- if (!path.parentPath.isCallExpression())
337
- return false;
338
-
339
- if (!path.parentPath.parentPath.isForOfStatement())
340
- return false;
341
-
342
- return true;
343
- }
344
-
345
- function isInsideLoop(path) {
346
- if (!path.parentPath.isForOfStatement())
347
- return false;
348
-
349
- return true;
350
- }
351
-
352
- const isOneElementCall = (path, {elements}) => {
353
- if (!path.parentPath.isCallExpression())
354
- return false;
355
-
356
- if (elements.length !== 1)
357
- return false;
358
-
359
- return !isCallExpression(elements[0]);
360
- };
@@ -0,0 +1,243 @@
1
+ 'use strict';
2
+
3
+ const {isSimple} = require('@putout/operate');
4
+
5
+ const {
6
+ isObjectExpression,
7
+ isArrayExpression,
8
+ isObjectProperty,
9
+ isCallExpression,
10
+ isAwaitExpression,
11
+ isBooleanLiteral,
12
+ isNullLiteral,
13
+ isStringLiteral,
14
+ } = require('@babel/types');
15
+
16
+ const {
17
+ isStringAndMember,
18
+ isStringAndIdentifier,
19
+ isIdentifierAndString,
20
+ isCoupleLines,
21
+ isStringAndArray,
22
+ isIdentifierAndIdentifier,
23
+ } = require('../../is');
24
+
25
+ const {round} = Math;
26
+
27
+ const isSimpleAndCall = ([a, b]) => {
28
+ if (!isSimple(a))
29
+ return;
30
+
31
+ return isCallExpression(b) || isAwaitExpression(b);
32
+ };
33
+
34
+ const isBooleanAndSimple = ([a, b]) => isBooleanLiteral(a) && isSimple(b);
35
+ const isNullAndSimple = ([a, b]) => isNullLiteral(a) && isSimple(b);
36
+ const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
37
+ const ONE_LINE = false;
38
+ const MULTI_LINE = true;
39
+
40
+ module.exports.isNewlineBetweenElements = (path, {elements, maxElementsInOneLine}) => {
41
+ if (elements.length > 3 && !isObjectExpression(elements[0]))
42
+ return MULTI_LINE;
43
+
44
+ if (isOneSimple(path))
45
+ return ONE_LINE;
46
+
47
+ if (elements.length === 2 && isIdentifierAndIdentifier(elements))
48
+ return ONE_LINE;
49
+
50
+ if (isCallInsideArrow(path))
51
+ return ONE_LINE;
52
+
53
+ if (isIncreaseIndent(path))
54
+ return ONE_LINE;
55
+
56
+ if (isInsideLoop(path))
57
+ return ONE_LINE;
58
+
59
+ if (isBooleanAndSimple(elements))
60
+ return ONE_LINE;
61
+
62
+ if (isNullAndSimple(elements))
63
+ return ONE_LINE;
64
+
65
+ if (isSimpleAndCall(elements))
66
+ return ONE_LINE;
67
+
68
+ if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
69
+ return ONE_LINE;
70
+
71
+ if (isTwoStringsDifferentLength(elements))
72
+ return ONE_LINE;
73
+
74
+ if (isStringAndArray(elements))
75
+ return ONE_LINE;
76
+
77
+ if (isStringAndMember(elements))
78
+ return ONE_LINE;
79
+
80
+ if (isStringAndIdentifier(elements))
81
+ return ONE_LINE;
82
+
83
+ if (isIdentifierAndString(elements))
84
+ return ONE_LINE;
85
+
86
+ if (isSimpleAndObject(elements))
87
+ return ONE_LINE;
88
+
89
+ if (isStringAndString(elements) && path.parentPath.isArrayExpression() && isArrayExpression(path.parentPath.node.elements[0]))
90
+ return ONE_LINE;
91
+
92
+ if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
93
+ return MULTI_LINE;
94
+
95
+ return ONE_LINE;
96
+ };
97
+
98
+ const isForOf = ({parentPath}) => parentPath.isForOfStatement();
99
+
100
+ const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
101
+
102
+ const isShortTwoSimplesInsideCall = (path, short) => {
103
+ const {
104
+ node,
105
+ parentPath,
106
+ } = path;
107
+
108
+ const {elements} = node;
109
+ const {length} = elements;
110
+ const [a, b] = elements;
111
+
112
+ if (!parentPath.isCallExpression())
113
+ return false;
114
+
115
+ if (!isStringLiteral(a) || !isStringLiteral(b))
116
+ return false;
117
+
118
+ return length < short;
119
+ };
120
+
121
+ function isOneSimple(path) {
122
+ const elements = path.get('elements');
123
+
124
+ if (elements.length !== 1)
125
+ return false;
126
+
127
+ const [first] = elements;
128
+
129
+ if (first.isIdentifier() && first.node.name.length < 5)
130
+ return true;
131
+
132
+ if (first.isStringLiteral() && first.node.value.length > 10)
133
+ return false;
134
+
135
+ if (!first.isIdentifier() && isSimple(first))
136
+ return true;
137
+
138
+ if (first.isCallExpression())
139
+ return false;
140
+
141
+ return first.isMemberExpression();
142
+ }
143
+
144
+ function isTwoStringsDifferentLength(strings) {
145
+ const [a, b] = strings;
146
+
147
+ if (strings.length > 2)
148
+ return false;
149
+
150
+ if (!a?.isStringLiteral() || !b?.isStringLiteral())
151
+ return false;
152
+
153
+ const aLength = a.node.value.length;
154
+ const bLength = b.node.value.length;
155
+
156
+ return round(bLength / aLength) > 2;
157
+ }
158
+
159
+ function isInsideLoop(path) {
160
+ if (!path.parentPath.isForOfStatement())
161
+ return false;
162
+
163
+ return true;
164
+ }
165
+
166
+ function tooLong(path) {
167
+ const elements = path.get('elements');
168
+
169
+ if (elements.length < 2)
170
+ return false;
171
+
172
+ for (const el of path.get('elements')) {
173
+ if (el.isStringLiteral() && el.node.value.length > 4)
174
+ return true;
175
+ }
176
+
177
+ return false;
178
+ }
179
+
180
+ function isCallInsideArrow(path) {
181
+ const {parentPath} = path;
182
+
183
+ if (!parentPath.isCallExpression())
184
+ return false;
185
+
186
+ if (!parentPath.parentPath.isFunction())
187
+ return false;
188
+
189
+ return path.node.elements.length < 4;
190
+ }
191
+
192
+ function isNumbers(elements) {
193
+ for (const element of elements) {
194
+ if (element.isNumericLiteral())
195
+ return true;
196
+ }
197
+
198
+ return false;
199
+ }
200
+
201
+ function isLastArg({parentPath}) {
202
+ return !parentPath.isCallExpression();
203
+ }
204
+
205
+ function isParentProperty(path) {
206
+ return path.find(isObjectProperty);
207
+ }
208
+
209
+ module.exports.isIncreaseIndent = isIncreaseIndent;
210
+ function isIncreaseIndent(path) {
211
+ const elements = path.get('elements');
212
+
213
+ if (!elements.length)
214
+ return false;
215
+
216
+ if (isInsideCallLoop(path))
217
+ return false;
218
+
219
+ if (elements[0].isObjectExpression())
220
+ return true;
221
+
222
+ if (isStringAndObject(elements))
223
+ return true;
224
+
225
+ return false;
226
+ }
227
+
228
+ function isInsideCallLoop(path) {
229
+ if (!path.parentPath.isCallExpression())
230
+ return false;
231
+
232
+ if (!path.parentPath.parentPath.isForOfStatement())
233
+ return false;
234
+
235
+ return true;
236
+ }
237
+
238
+ const isStringAndObject = (elements) => {
239
+ const first = elements.at(0);
240
+ const last = elements.at(-1);
241
+
242
+ return isStringLiteral(first) && isObjectExpression(last);
243
+ };
@@ -8,6 +8,7 @@ const {
8
8
  isForOfStatement,
9
9
  isVariableDeclaration,
10
10
  isMemberExpression,
11
+ isArrayExpression,
11
12
  } = require('@babel/types');
12
13
 
13
14
  const isParentProgram = (path) => path.parentPath?.isProgram();
@@ -43,9 +44,23 @@ function isCoupleLines(path) {
43
44
  }
44
45
 
45
46
  module.exports.exists = (a) => a.node;
46
- module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
47
+ module.exports.isStringAndIdentifier = isStringAndIdentifier;
48
+ function isStringAndIdentifier([a, b]) {
49
+ return isStringLiteral(a) && isIdentifier(b);
50
+ }
51
+
52
+ module.exports.isIdentifierAndIdentifier = ([a, b]) => isIdentifier(a) && isIdentifier(b);
47
53
  module.exports.isStringAndMember = ([a, b]) => isStringLiteral(a) && isMemberExpression(b);
48
54
  module.exports.isIdentifierAndString = ([a, b]) => isIdentifier(a) && isStringLiteral(b);
55
+ module.exports.isStringAndArray = ([a, b]) => {
56
+ if (!isStringLiteral(a))
57
+ return false;
58
+
59
+ if (!isArrayExpression(b))
60
+ return false;
61
+
62
+ return !isStringAndIdentifier(b.node.elements);
63
+ };
49
64
 
50
65
  const isIfOrStatement = (a) => isIfStatement(a) || isStatement(a);
51
66
  const isForOfOrStatement = (a) => isForOfStatement(a) || isStatement(a);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "2.11.0",
3
+ "version": "2.13.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",