@putout/printer 1.119.0 → 1.121.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,14 @@
1
+ 2023.05.30, v1.121.0
2
+
3
+ feature:
4
+ - 26071a8 @putout/printer: add ability to traverse existing path
5
+
6
+ 2023.05.29, v1.120.0
7
+
8
+ feature:
9
+ - da33669 @putout/printer: ObjectProperty: improve support when one line inside LogicalExpression
10
+ - db06c37 @putout/printer: ObjectPattern: improve RestElement support
11
+
1
12
  2023.05.29, v1.119.0
2
13
 
3
14
  feature:
@@ -92,6 +92,8 @@ module.exports.ObjectProperty = (path, {print, maybe}) => {
92
92
  computed,
93
93
  } = path.node;
94
94
 
95
+ const properties = path.parentPath.get('properties');
96
+ const isLast = path === properties.at(-1);
95
97
  const manyLines = !isOneLine(path.parentPath);
96
98
 
97
99
  maybe.print(computed, '[');
@@ -104,7 +106,10 @@ module.exports.ObjectProperty = (path, {print, maybe}) => {
104
106
  print('__value');
105
107
  }
106
108
 
107
- maybe.print(manyLines, ',');
109
+ if (manyLines)
110
+ print(',');
111
+ else if (!isLast && properties.length)
112
+ print(', ');
108
113
  };
109
114
 
110
115
  const ONE_LINE = true;
@@ -1,6 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const {isIdentifier} = require('@babel/types');
3
+ const {
4
+ isIdentifier,
5
+ isObjectPattern,
6
+ } = require('@babel/types');
4
7
 
5
8
  const {
6
9
  isForOf,
@@ -22,14 +25,15 @@ module.exports.ObjectPattern = {
22
25
  const properties = path.get('properties');
23
26
  const n = properties.length - 1;
24
27
  const is = shouldAddNewline(path);
28
+ const hasObject = n && hasObjectPattern(properties);
25
29
 
26
30
  maybe.print(is, '\n');
27
31
 
28
32
  for (const [i, property] of properties.entries()) {
29
33
  if (property.isRestElement()) {
30
- maybe.indent(n > 1);
34
+ maybe.indent(hasObject);
31
35
  print(property);
32
- maybe.print.newline(n > 1);
36
+ maybe.print.newline(hasObject);
33
37
  continue;
34
38
  }
35
39
 
@@ -62,7 +66,7 @@ module.exports.ObjectPattern = {
62
66
  maybe.print.newline(couple);
63
67
  }
64
68
 
65
- if (is) {
69
+ if (is || hasObject) {
66
70
  print(',');
67
71
  print.newline();
68
72
 
@@ -83,7 +87,10 @@ module.exports.ObjectPattern = {
83
87
  if (!path.parentPath.isObjectProperty())
84
88
  return false;
85
89
 
86
- return isOneParentProperty(path);
90
+ if (isOneParentProperty(path))
91
+ return true;
92
+
93
+ return false;
87
94
  },
88
95
  after(path, {print}) {
89
96
  print.newline();
@@ -104,6 +111,15 @@ function checkLength(properties) {
104
111
  return false;
105
112
  }
106
113
 
114
+ function hasObjectPattern(properties) {
115
+ for (const property of properties) {
116
+ if (isObjectPattern(property.node.value))
117
+ return true;
118
+ }
119
+
120
+ return false;
121
+ }
122
+
107
123
  function shouldAddNewline(path) {
108
124
  const properties = path.get('properties');
109
125
  const n = properties.length - 1;
@@ -116,4 +132,3 @@ function shouldAddNewline(path) {
116
132
 
117
133
  return false;
118
134
  }
119
-
@@ -23,7 +23,9 @@ module.exports.VariableDeclaration = {
23
23
  print(path, {maybe, store, write, traverse}) {
24
24
  maybe.indent(isInsideBlock(path));
25
25
  write(String(path.node.kind));
26
- maybeSpaceAfterKeyword(path, {write});
26
+ maybeSpaceAfterKeyword(path, {
27
+ write,
28
+ });
27
29
 
28
30
  const declarations = path.get('declarations');
29
31
  const n = declarations.length - 1;
@@ -150,4 +152,3 @@ const isNextAssign = (path) => {
150
152
  .get('expression')
151
153
  .isAssignmentExpression();
152
154
  };
153
-
@@ -177,12 +177,15 @@ module.exports.tokenize = (ast, overrides = {}) => {
177
177
  ...overrides.visitors,
178
178
  };
179
179
 
180
- babelTraverse(maybeFile(ast), {
181
- Program(path) {
182
- traverse(path);
183
- path.stop();
184
- },
185
- });
180
+ if (ast.parentPath)
181
+ pathTraverse(ast, traverse);
182
+ else
183
+ babelTraverse(maybeFile(ast), {
184
+ Program(path) {
185
+ traverse(path);
186
+ path.stop();
187
+ },
188
+ });
186
189
 
187
190
  function traverse(path) {
188
191
  const {type} = path;
@@ -266,3 +269,14 @@ const computePath = (path, maybeLine) => {
266
269
 
267
270
  return maybeLine;
268
271
  };
272
+
273
+ function pathTraverse(ast, traverse) {
274
+ ast.parentPath.traverse({
275
+ enter(path) {
276
+ if (path === ast) {
277
+ traverse(path);
278
+ path.stop();
279
+ }
280
+ },
281
+ });
282
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.119.0",
3
+ "version": "1.121.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",