@putout/printer 8.3.0 → 8.5.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,13 @@
1
+ 2024.02.15, v8.5.0
2
+
3
+ feature:
4
+ - 0270569 @putout/printer: ArrayExpression: couple elements including nesting
5
+
6
+ 2024.02.15, v8.4.0
7
+
8
+ feature:
9
+ - 1b6c4d1 @putout/printer: ArrayExpression: improve support
10
+
1
11
  2024.02.14, v8.3.0
2
12
 
3
13
  feature:
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {types} = require('@putout/babel');
3
4
  const {
4
5
  isCoupleLines,
5
6
  isStringAndIdentifier,
@@ -19,12 +20,15 @@ const {
19
20
  isArrayIndented,
20
21
  } = require('./indent');
21
22
 
22
- const {types} = require('@putout/babel');
23
23
  const {
24
24
  isObjectExpression,
25
25
  isSpreadElement,
26
+ isStringLiteral,
26
27
  } = types;
27
28
 
29
+ const isNextString = (path) => isStringLiteral(path.getNextSibling());
30
+ const isPrevString = (path) => isStringLiteral(path.getPrevSibling());
31
+ const isAroundStrings = (path) => isNextString(path) || isPrevString(path);
28
32
  const isNextObject = (a) => a.getNextSibling().isObjectExpression();
29
33
  const isPrevObject = (a) => a.getPrevSibling().isObjectExpression();
30
34
  const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a);
@@ -98,9 +102,12 @@ module.exports.ArrayExpression = {
98
102
  const parentElements = path.parentPath.get('elements');
99
103
 
100
104
  if (isInsideArray(path) && isStringAndArray(parentElements)) {
101
- indent.dec();
105
+ const parentCountTwo = parentElements.length === 2;
106
+ const isHideIdent = !isAroundStrings(path) || parentCountTwo;
107
+
108
+ maybe.indent.dec(isHideIdent);
102
109
  maybe.indent(elements.length && isNewLine);
103
- indent.inc();
110
+ maybe.indent.inc(isHideIdent);
104
111
  } else if (!isArrayInsideArray(path) && !isObjectExpression(elements.at(-1))) {
105
112
  maybe.indent(elements.length && isNewLine);
106
113
  }
@@ -128,3 +135,4 @@ module.exports.ArrayExpression = {
128
135
  indent.inc();
129
136
  },
130
137
  };
138
+
@@ -1,10 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  const {types} = require('@putout/babel');
4
- const {isStringLiteral} = types;
5
-
6
4
  const {isIndented} = require('../../is');
7
5
 
6
+ const {
7
+ isStringLiteral,
8
+ isArrayExpression,
9
+ } = types;
10
+
8
11
  const isInsideArray = (path) => path.parentPath.isArrayExpression();
9
12
 
10
13
  module.exports.isInsideArray = isInsideArray;
@@ -26,7 +29,14 @@ function isArrayInsideArray(path) {
26
29
  if (!path.isArrayExpression() || !path.parentPath.isArrayExpression())
27
30
  return false;
28
31
 
29
- return path.parentPath.node.elements.length <= 3;
32
+ const parentElements = path.parentPath.node.elements;
33
+ const parentHasArrays = parentElements.filter(isArrayExpression).length;
34
+ const lastIsArray = !isArrayExpression(parentElements.at(-1));
35
+
36
+ if (parentHasArrays && lastIsArray)
37
+ return false;
38
+
39
+ return parentElements.length <= 3;
30
40
  }
31
41
 
32
42
  const isTwoLongStrings = ([a, b]) => {
@@ -96,7 +96,7 @@ module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
96
96
  if (isTwoSimplesInsideObjectProperty(path))
97
97
  return ONE_LINE;
98
98
 
99
- if (isStringAndArray(elements))
99
+ if (isStringAndArray(elements) && elements.length < 3)
100
100
  return ONE_LINE;
101
101
 
102
102
  if (isStringAndMember(elements))
@@ -65,12 +65,7 @@ module.exports.isIdentifierAndIdentifier = ([a, b]) => {
65
65
  };
66
66
  module.exports.isStringAndMember = ([a, b]) => isStringLiteral(a) && isMemberExpression(b);
67
67
  module.exports.isIdentifierAndString = ([a, b]) => isIdentifier(a) && isStringLiteral(b);
68
- module.exports.isStringAndArray = (array) => {
69
- if (array.length > 2)
70
- return false;
71
-
72
- const [a, b] = array;
73
-
68
+ module.exports.isStringAndArray = ([a, b]) => {
74
69
  if (!isStringLiteral(a))
75
70
  return false;
76
71
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.3.0",
3
+ "version": "8.5.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",