@putout/printer 6.11.1 → 6.11.3

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.3
2
+
3
+ feature:
4
+ - f363754 ArrayExpression: isOneLine
5
+
6
+ 2023.12.01, v6.11.2
7
+
8
+ feature:
9
+ - 8bc537a ArrayExpression: indent
10
+
1
11
  2023.12.01, v6.11.1
2
12
 
3
13
  feature:
@@ -5,7 +5,6 @@ const {
5
5
  isStringAndIdentifier,
6
6
  isIdentifierAndIdentifier,
7
7
  isStringAndArray,
8
- isIndented,
9
8
  } = require('../../is');
10
9
 
11
10
  const {
@@ -15,31 +14,18 @@ const {
15
14
  } = require('./newline');
16
15
 
17
16
  const {types} = require('@putout/babel');
17
+
18
18
  const {
19
- isObjectExpression,
20
- isStringLiteral,
21
- } = types;
19
+ isInsideArray,
20
+ isArrayInsideArray,
21
+ isArrayIndented,
22
+ } = require('./indent');
22
23
 
23
- const isNextObject = (a) => a.getNextSibling().isObjectExpression();
24
+ const {isObjectExpression} = types;
24
25
 
25
- const isArrayInsideArray = (path) => {
26
- if (!path.isArrayExpression() || !path.parentPath.isArrayExpression())
27
- return false;
28
-
29
- return path.parentPath.node.elements.length <= 3;
30
- };
26
+ const isNextObject = (a) => a.getNextSibling().isObjectExpression();
31
27
 
32
28
  const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
33
- const isInsideArray = (path) => path.parentPath.isArrayExpression();
34
-
35
- const isTwoLongStrings = ([a, b]) => {
36
- const LONG_STRING = 20;
37
-
38
- if (!isStringLiteral(a) || !isStringLiteral(b))
39
- return false;
40
-
41
- return a.node.value.length > LONG_STRING;
42
- };
43
29
 
44
30
  module.exports.ArrayExpression = {
45
31
  beforeIf(path) {
@@ -71,7 +57,7 @@ module.exports.ArrayExpression = {
71
57
 
72
58
  print('[');
73
59
 
74
- const indented = !isArrayInsideArray(path) && (!isTwoLongStrings(elements) || !isInsideArray(path) && isIndented(elements[0]));
60
+ const indented = isArrayIndented(path);
75
61
 
76
62
  if (indented)
77
63
  maybe.indent.inc(shouldIncreaseIndent);
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('@putout/babel');
4
+ const {isStringLiteral} = types;
5
+
6
+ const {isIndented} = require('../../is');
7
+
8
+ const isInsideArray = (path) => path.parentPath.isArrayExpression();
9
+
10
+ module.exports.isInsideArray = isInsideArray;
11
+
12
+ module.exports.isArrayIndented = (path) => {
13
+ const elements = path.get('elements');
14
+
15
+ if (isArrayInsideArray(path))
16
+ return false;
17
+
18
+ const [first] = elements;
19
+
20
+ return !isTwoLongStrings(elements) || !isInsideArray(path) && isIndented(first);
21
+ };
22
+
23
+ module.exports.isArrayInsideArray = isArrayInsideArray;
24
+
25
+ function isArrayInsideArray(path) {
26
+ if (!path.isArrayExpression() || !path.parentPath.isArrayExpression())
27
+ return false;
28
+
29
+ return path.parentPath.node.elements.length <= 3;
30
+ }
31
+
32
+ const isTwoLongStrings = ([a, b]) => {
33
+ const LONG_STRING = 20;
34
+
35
+ if (!isStringLiteral(a) || !isStringLiteral(b))
36
+ return false;
37
+
38
+ return a.node.value.length > LONG_STRING;
39
+ };
@@ -21,6 +21,8 @@ const {
21
21
  isCoupleLines,
22
22
  isStringAndArray,
23
23
  isIdentifierAndIdentifier,
24
+ satisfy,
25
+
24
26
  } = require('../../is');
25
27
 
26
28
  const {round} = Math;
@@ -47,17 +49,26 @@ const isSimpleAndObject = ([a, b]) => isSimple(a) && isObjectExpression(b);
47
49
  const ONE_LINE = false;
48
50
  const MULTI_LINE = true;
49
51
 
52
+ const isOneLine = satisfy([
53
+ isOneSimple,
54
+ isOneSpread,
55
+ isIdentifierAndIdentifier,
56
+ isBooleanAndSimple,
57
+ isNullAndSimple,
58
+ isSimpleAndCall,
59
+ isTwoStringsDifferentLength,
60
+ isStringAndArray,
61
+ isStringAndMember,
62
+ isStringAndIdentifier,
63
+ isIdentifierAndString,
64
+ isSimpleAndObject,
65
+ ]);
66
+
50
67
  module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
51
68
  if (elements.length > 3 && !isObjectExpression(elements[0]))
52
69
  return MULTI_LINE;
53
70
 
54
- if (isOneSimple(path))
55
- return ONE_LINE;
56
-
57
- if (isOneSpread(elements))
58
- return ONE_LINE;
59
-
60
- if (elements.length === 2 && isIdentifierAndIdentifier(elements))
71
+ if (isOneLine(elements))
61
72
  return ONE_LINE;
62
73
 
63
74
  if (isCallInsideArrow(path))
@@ -69,39 +80,12 @@ module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
69
80
  if (isInsideLoop(path))
70
81
  return ONE_LINE;
71
82
 
72
- if (isBooleanAndSimple(elements))
73
- return ONE_LINE;
74
-
75
- if (isNullAndSimple(elements))
76
- return ONE_LINE;
77
-
78
- if (isSimpleAndCall(elements))
79
- return ONE_LINE;
80
-
81
83
  if (isShortTwoSimplesInsideCall(path, maxElementsInOneLine))
82
84
  return ONE_LINE;
83
85
 
84
- if (isTwoStringsDifferentLength(elements))
85
- return ONE_LINE;
86
-
87
86
  if (isTwoSimplesInsideObjectProperty(path))
88
87
  return ONE_LINE;
89
88
 
90
- if (isStringAndArray(elements))
91
- return ONE_LINE;
92
-
93
- if (isStringAndMember(elements))
94
- return ONE_LINE;
95
-
96
- if (isStringAndIdentifier(elements))
97
- return ONE_LINE;
98
-
99
- if (isIdentifierAndString(elements))
100
- return ONE_LINE;
101
-
102
- if (isSimpleAndObject(elements))
103
- return ONE_LINE;
104
-
105
89
  if (isStringAndString(elements) && isParentIsArrayWithFirstArrayElement(path))
106
90
  return ONE_LINE;
107
91
 
@@ -159,9 +143,7 @@ const isTwoSimplesInsideObjectProperty = (path) => {
159
143
  return !isCoupleLines(path);
160
144
  };
161
145
 
162
- function isOneSimple(path) {
163
- const elements = path.get('elements');
164
-
146
+ function isOneSimple(elements) {
165
147
  if (elements.length !== 1)
166
148
  return false;
167
149
 
@@ -60,7 +60,14 @@ function isStringAndIdentifier([a, b]) {
60
60
  return isStringLiteral(a) && isIdentifier(b);
61
61
  }
62
62
 
63
- module.exports.isIdentifierAndIdentifier = ([a, b]) => isIdentifier(a) && isIdentifier(b);
63
+ module.exports.isIdentifierAndIdentifier = (elements) => {
64
+ if (elements.length !== 2)
65
+ return false;
66
+
67
+ const [a, b] = elements;
68
+
69
+ return isIdentifier(a) && isIdentifier(b);
70
+ };
64
71
  module.exports.isStringAndMember = ([a, b]) => isStringLiteral(a) && isMemberExpression(b);
65
72
  module.exports.isIdentifierAndString = ([a, b]) => isIdentifier(a) && isStringLiteral(b);
66
73
  module.exports.isStringAndArray = ([a, b]) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "6.11.1",
3
+ "version": "6.11.3",
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",