@putout/printer 8.11.0 → 8.12.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 CHANGED
@@ -1,3 +1,13 @@
1
+ 2024.04.11, v8.12.1
2
+
3
+ feature:
4
+ - cfb862d @putout/printer: tokenize: ArrayExpression: Identifier, Object: Tuple
5
+
6
+ 2024.04.11, v8.12.0
7
+
8
+ feature:
9
+ - 426fccf @putout/printer: ArrayExpression, ObjectExpression: improve support of printing Identifier, Object inside Arrray
10
+
1
11
  2024.04.09, v8.11.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -183,7 +183,10 @@ When you want to improve support of existing visitor or extend **Printer** with
183
183
  When you need to override behavior of existing visitor use:
184
184
 
185
185
  ```js
186
- import {print, visitors as v} from '@putout/printer';
186
+ import {
187
+ print,
188
+ visitors as v,
189
+ } from '@putout/printer';
187
190
 
188
191
  print(ast, {
189
192
  visitors: {
@@ -6,6 +6,7 @@ const {
6
6
  isStringAndIdentifier,
7
7
  isIdentifierAndIdentifier,
8
8
  isStringAndArray,
9
+ isIdentifierAndObject,
9
10
  } = require('../../is');
10
11
 
11
12
  const {
@@ -24,6 +25,7 @@ const {
24
25
  isObjectExpression,
25
26
  isSpreadElement,
26
27
  isStringLiteral,
28
+ isIdentifier,
27
29
  } = types;
28
30
 
29
31
  const isNextString = (path) => isStringLiteral(path.getNextSibling());
@@ -32,6 +34,8 @@ const isAroundStrings = (path) => isNextString(path) || isPrevString(path);
32
34
  const isNextObject = (a) => a.getNextSibling().isObjectExpression();
33
35
  const isPrevObject = (a) => a.getPrevSibling().isObjectExpression();
34
36
  const isObjectAfterSpread = (a) => isSpreadElement(a) && isNextObject(a) && !isPrevObject(a);
37
+ const isObjectAfterIdentifier = (a) => isIdentifier(a) && isNextObject(a) && !isPrevObject(a);
38
+ const isObjectAfterSimple = (a) => isObjectAfterSpread(a) || isObjectAfterIdentifier(a);
35
39
 
36
40
  const isInsideOneElementArray = ({parentPath}) => parentPath.node.elements.length === 1;
37
41
 
@@ -88,7 +92,7 @@ module.exports.ArrayExpression = {
88
92
  maybe.print(is, ',');
89
93
 
90
94
  maybe.print.newline(is && !isNextObject(element));
91
- maybe.print.space(isObjectAfterSpread(element));
95
+ maybe.print.space(is && isObjectAfterSimple(element));
92
96
 
93
97
  if (!is && index < n) {
94
98
  print(',');
@@ -112,6 +116,11 @@ module.exports.ArrayExpression = {
112
116
  maybe.indent(elements.length && isNewLine);
113
117
  }
114
118
 
119
+ if (isIdentifierAndObject(elements)) {
120
+ print(',');
121
+ print.breakline();
122
+ }
123
+
115
124
  print(']');
116
125
  },
117
126
  afterIf(path) {
@@ -21,6 +21,7 @@ const {
21
21
  isCoupleLines,
22
22
  isStringAndArray,
23
23
  isIdentifierAndIdentifier,
24
+ isIdentifierAndObject,
24
25
  } = require('../../is');
25
26
 
26
27
  const {round} = Math;
@@ -60,6 +61,9 @@ module.exports.isMultiLine = (path, {elements, maxElementsInOneLine}) => {
60
61
  if (elements.length > 3 && !isObjectExpression(elements[0]))
61
62
  return MULTI_LINE;
62
63
 
64
+ if (isIdentifierAndObject(elements))
65
+ return MULTI_LINE;
66
+
63
67
  if (isOneSimple(path))
64
68
  return ONE_LINE;
65
69
 
@@ -1,5 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ const {types} = require('@putout/babel');
4
+ const {isIdentifier} = types;
5
+
3
6
  const {
4
7
  isCoupleLines,
5
8
  isForOf,
@@ -88,7 +91,9 @@ module.exports.ObjectExpression = (path, printer, semantics) => {
88
91
  }
89
92
 
90
93
  indent.dec();
94
+
91
95
  maybe.indent(manyLines);
96
+
92
97
  print('}');
93
98
  maybe.print(parens, ')');
94
99
 
@@ -9,6 +9,7 @@ const {
9
9
  isVariableDeclaration,
10
10
  isMemberExpression,
11
11
  isArrayExpression,
12
+ isObjectExpression,
12
13
  } = require('@putout/babel').types;
13
14
 
14
15
  const isParentProgram = (path) => path.parentPath?.isProgram();
@@ -60,6 +61,16 @@ function isStringAndIdentifier([a, b]) {
60
61
  return isStringLiteral(a) && isIdentifier(b);
61
62
  }
62
63
 
64
+ module.exports.isIdentifierAndObject = ([a, b]) => {
65
+ if (!isIdentifier(a))
66
+ return false;
67
+
68
+ if (!isObjectExpression(b))
69
+ return false;
70
+
71
+ return b.node.properties.length;
72
+ };
73
+
63
74
  module.exports.isIdentifierAndIdentifier = ([a, b]) => {
64
75
  return isIdentifier(a) && isIdentifier(b);
65
76
  };
@@ -83,7 +83,6 @@ module.exports.VariableDeclaration = {
83
83
  write.newline();
84
84
  wasNewline = true;
85
85
  } else if (isParentSwitchCase(path)) {
86
- //write.breakline();
87
86
  write.newline();
88
87
  }
89
88
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "8.11.0",
3
+ "version": "8.12.1",
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",