@putout/printer 1.92.4 → 1.93.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,8 @@
1
+ 2023.05.11, v1.93.0
2
+
3
+ feature:
4
+ - 9822d55 @putout/printer: improve support computed property of ObjectPattern
5
+
1
6
  2023.05.11, v1.92.4
2
7
 
3
8
  feature:
@@ -8,55 +8,71 @@ const {
8
8
  exists,
9
9
  } = require('../is');
10
10
 
11
- module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
12
- indent.inc();
13
- print('{');
14
-
15
- const properties = path.get('properties');
16
- const n = properties.length - 1;
17
- const is = shouldAddNewline(path);
18
-
19
- maybe.print(is, '\n');
20
-
21
- for (const [i, property] of properties.entries()) {
22
- if (property.isRestElement()) {
23
- print(property);
24
- continue;
25
- }
11
+ const isOneParentProperty = ({parentPath}) => parentPath.parentPath.node.properties.length === 1;
12
+
13
+ module.exports.ObjectPattern = {
14
+ print(path, {indent, print, maybe}) {
15
+ indent.inc();
16
+ print('{');
26
17
 
27
- const valuePath = property.get('value');
28
- const keyPath = property.get('key');
29
- const isAssign = valuePath.isAssignmentPattern();
30
- const {shorthand} = property.node;
31
- const couple = isCoupleLines(valuePath) && !exists(property.getPrevSibling());
18
+ const properties = path.get('properties');
19
+ const n = properties.length - 1;
20
+ const is = shouldAddNewline(path);
32
21
 
33
- maybe.indent(is);
34
- maybe.print.breakline(couple);
35
- print(keyPath);
22
+ maybe.print(is, '\n');
36
23
 
37
- if (!shorthand) {
38
- print(': ');
39
- print(valuePath);
40
- } else if (isAssign) {
41
- print(valuePath);
24
+ for (const [i, property] of properties.entries()) {
25
+ if (property.isRestElement()) {
26
+ print(property);
27
+ continue;
28
+ }
42
29
 
43
- maybe.print(couple, ',');
44
- maybe.print.newline(couple);
45
- }
46
-
47
- if (is) {
48
- print(',');
49
- print.newline();
30
+ const valuePath = property.get('value');
31
+ const keyPath = property.get('key');
32
+ const isAssign = valuePath.isAssignmentPattern();
33
+ const {shorthand, computed} = property.node;
34
+ const couple = isCoupleLines(valuePath) && !exists(property.getPrevSibling());
50
35
 
51
- continue;
36
+ maybe.indent(is);
37
+ maybe.print.breakline(couple);
38
+
39
+ maybe.print(computed, '[');
40
+ print(keyPath);
41
+ maybe.print(computed, ']');
42
+
43
+ if (!shorthand) {
44
+ print(': ');
45
+ print(valuePath);
46
+ } else if (isAssign) {
47
+ print(valuePath);
48
+
49
+ maybe.print(couple, ',');
50
+ maybe.print.newline(couple);
51
+ }
52
+
53
+ if (is) {
54
+ print(',');
55
+ print.newline();
56
+
57
+ continue;
58
+ }
59
+
60
+ maybe.print(i < n, ', ');
52
61
  }
53
62
 
54
- maybe.print(i < n, ', ');
55
- }
56
-
57
- indent.dec();
58
- maybe.indent(is);
59
- print('}');
63
+ indent.dec();
64
+ maybe.indent(is);
65
+ print('}');
66
+ },
67
+ afterIf(path) {
68
+ if (!path.parentPath.isObjectProperty())
69
+ return false;
70
+
71
+ return isOneParentProperty(path);
72
+ },
73
+ after(path, {print}) {
74
+ print.newline();
75
+ },
60
76
  };
61
77
 
62
78
  function checkLength(properties) {
@@ -85,3 +101,4 @@ function shouldAddNewline(path) {
85
101
 
86
102
  return false;
87
103
  }
104
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.92.4",
3
+ "version": "1.93.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",