@putout/printer 8.2.0 → 8.4.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.4.0
2
+
3
+ feature:
4
+ - 1b6c4d1 @putout/printer: ArrayExpression: improve support
5
+
6
+ 2024.02.14, v8.3.0
7
+
8
+ feature:
9
+ - 788739f @putout/printer: SwitchStatement: improve indent
10
+
1
11
  2024.02.12, v8.2.0
2
12
 
3
13
  feature:
@@ -88,9 +88,8 @@ module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics)
88
88
 
89
89
  print(`//${value}`);
90
90
 
91
- const isParentSwitch = path.parentPath.isSwitchCase();
92
- maybe.print.breakline(propIs || isParentSwitch);
93
- maybe.print.newline(!propIs && !isParentSwitch);
91
+ maybe.print.breakline(propIs);
92
+ maybe.print.newline(!propIs);
94
93
  continue;
95
94
  }
96
95
 
@@ -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,13 @@ 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());
28
30
  const isNextObject = (a) => a.getNextSibling().isObjectExpression();
29
31
  const isPrevObject = (a) => a.getPrevSibling().isObjectExpression();
30
32
  const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a);
@@ -98,9 +100,9 @@ module.exports.ArrayExpression = {
98
100
  const parentElements = path.parentPath.get('elements');
99
101
 
100
102
  if (isInsideArray(path) && isStringAndArray(parentElements)) {
101
- indent.dec();
103
+ maybe.indent.dec(!isNextString(path));
102
104
  maybe.indent(elements.length && isNewLine);
103
- indent.inc();
105
+ maybe.indent.inc(!isNextString(path));
104
106
  } else if (!isArrayInsideArray(path) && !isObjectExpression(elements.at(-1))) {
105
107
  maybe.indent(elements.length && isNewLine);
106
108
  }
@@ -128,3 +130,4 @@ module.exports.ArrayExpression = {
128
130
  indent.inc();
129
131
  },
130
132
  };
133
+
@@ -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
 
@@ -45,7 +45,6 @@ module.exports.SwitchStatement = {
45
45
  print(':');
46
46
 
47
47
  const consequents = switchCase.get('consequent');
48
-
49
48
  const isBlock = switchCase.get('consequent.0')?.isBlockStatement();
50
49
 
51
50
  maybe.indent.inc(!isBlock);
@@ -18,7 +18,7 @@ const {maybeDeclare} = require('../../maybe/maybe-declare');
18
18
 
19
19
  const isParentTSModuleBlock = (path) => path.parentPath.isTSModuleBlock();
20
20
  const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
21
- const isInsideBlock = (path) => /^(Program|BlockStatement|TSModuleBlock)$/.test(path.parentPath.type);
21
+ const isInsideBlock = (path) => /^(Program|BlockStatement|TSModuleBlock|SwitchCase)$/.test(path.parentPath.type);
22
22
  const isParentSwitchCase = (path) => path.parentPath.isSwitchCase();
23
23
  const isFirstInSwitch = (path) => path.parentPath.get('consequent.0') === path;
24
24
  const isParentIf = (path) => path.parentPath.isIfStatement();
@@ -83,7 +83,8 @@ module.exports.VariableDeclaration = {
83
83
  write.newline();
84
84
  wasNewline = true;
85
85
  } else if (isParentSwitchCase(path)) {
86
- write.breakline();
86
+ //write.breakline();
87
+ write.newline();
87
88
  }
88
89
 
89
90
  if (isParentBlock(path) && isNext(path) && (noTrailingComment(path) || isNewlineBetweenSiblings(path))) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.2.0",
3
+ "version": "8.4.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",