@putout/printer 18.37.0 → 18.37.2

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
+ 2026.07.14, v18.37.2
2
+
3
+ feature:
4
+ - 47afbbf6 @putou/printer: member-expression: chain: simplify
5
+
6
+ 2026.07.14, v18.37.1
7
+
8
+ feature:
9
+ - 696133f1 @putout/printer: MemberExpression: chain: __a.__b().__c: newline
10
+
1
11
  2026.07.12, v18.37.0
2
12
 
3
13
  feature:
@@ -1,4 +1,5 @@
1
1
  import {types} from '@putout/babel';
2
+ import {createTypeChecker} from '#type-checker';
2
3
  import {hasTrailingComment} from '#is';
3
4
 
4
5
  const {
@@ -9,13 +10,12 @@ const {
9
10
  const {assign} = Object;
10
11
 
11
12
  export const chain = (path) => {
12
- const all = [
13
+ const properties = [
13
14
  ...down(path),
14
15
  ...up(path),
15
16
  ];
16
17
 
17
- const properties = all.slice(0, -1);
18
- const root = all.at(-1);
18
+ const root = properties.pop();
19
19
 
20
20
  return [root, properties];
21
21
  };
@@ -26,13 +26,8 @@ function down(path) {
26
26
  let current = path.get('object');
27
27
 
28
28
  while (!current.isIdentifier()) {
29
- const isFn = current.isCallExpression();
30
- const prop = build(current);
31
-
32
- if (isFn)
33
- current = current.get('callee');
34
-
35
- properties.unshift(prop);
29
+ properties.unshift(buildProperty(current));
30
+ current = stepIntoCallee(current);
36
31
 
37
32
  if (!current.isMemberExpression())
38
33
  break;
@@ -43,43 +38,76 @@ function down(path) {
43
38
  return properties;
44
39
  }
45
40
 
41
+ function stepIntoCallee(current) {
42
+ if (current.isCallExpression())
43
+ return current.get('callee');
44
+
45
+ return current;
46
+ }
47
+
46
48
  function up(current) {
47
49
  const properties = [];
48
50
 
49
51
  while (current.isMemberExpression()) {
50
52
  if (hasTrailingComment(current.get('object')))
51
- properties.push({
52
- type: 'CommentLine',
53
- });
53
+ properties.push(commentProperty());
54
54
 
55
- current = current.parentPath;
56
-
57
- if (isCallExpression(current)) {
58
- properties.push(build(current));
59
- current = current.parentPath;
60
- }
55
+ current = stepOutOfChain(current, properties);
61
56
 
62
57
  if (!isMemberExpression(current))
63
58
  break;
64
59
  }
65
60
 
66
- properties.push({
67
- type: current.type,
68
- });
61
+ properties.push(rootProperty(current));
69
62
 
70
63
  return properties;
71
64
  }
72
65
 
73
- function build(path) {
66
+ function stepOutOfChain(member, properties) {
67
+ const parent = member.parentPath;
68
+
69
+ if (!isMemberOwnCallee(member))
70
+ return parent;
71
+
72
+ properties.push(buildProperty(parent));
73
+
74
+ return parent.parentPath;
75
+ }
76
+
77
+ function isPathOwnCallee({node, parentPath}) {
78
+ return node === parentPath.node.callee;
79
+ }
80
+
81
+ const isMemberOwnCallee = createTypeChecker([
82
+ ['-: parentPath -> !CallExpression'],
83
+ ['+', isPathOwnCallee],
84
+ ]);
85
+
86
+ function buildProperty(path) {
74
87
  const prop = {
75
88
  type: path.type,
76
89
  };
77
90
 
78
- if (isCallExpression(path))
91
+ if (isCallExpression(path)) {
92
+ const {callee} = path.node;
93
+
79
94
  assign(prop, {
80
95
  args: path.node.arguments.length,
81
- name: path.node.callee.property?.name || '',
82
96
  });
97
+
98
+ if (isMemberExpression(callee))
99
+ assign(prop, {
100
+ name: callee.property.name,
101
+ });
102
+ }
83
103
 
84
104
  return prop;
85
105
  }
106
+
107
+ const commentProperty = () => ({
108
+ type: 'CommentLine',
109
+ });
110
+
111
+ const rootProperty = (path) => ({
112
+ type: path.type,
113
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "18.37.0",
3
+ "version": "18.37.2",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",