@putout/printer 1.23.0 → 1.25.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
+ 2023.04.06, v1.25.0
2
+
3
+ feature:
4
+ - db62133 @putout/printer: add support of chaining
5
+
6
+ 2023.04.05, v1.24.0
7
+
8
+ feature:
9
+ - 47fb021 @putout/printer: add support of chaining
10
+
1
11
  2023.04.05, v1.23.0
2
12
 
3
13
  feature:
@@ -1,6 +1,17 @@
1
1
  'use strict';
2
2
 
3
- module.exports.MemberExpression = (path, {print}) => {
3
+ const {
4
+ isIfStatement,
5
+ isIdentifier,
6
+ isThisExpression,
7
+ } = require('@babel/types');
8
+
9
+ const {
10
+ compare,
11
+ getTemplateValues,
12
+ } = require('@putout/compare');
13
+
14
+ module.exports.MemberExpression = (path, {print, indent, maybe}) => {
4
15
  const {computed} = path.node;
5
16
  print('__object');
6
17
 
@@ -12,8 +23,17 @@ module.exports.MemberExpression = (path, {print}) => {
12
23
  return;
13
24
  }
14
25
 
26
+ const isChain = looksLikeChain(path);
27
+ maybe.indent.inc(isChain);
28
+
29
+ if (isChain) {
30
+ print.newline();
31
+ indent(isChain);
32
+ }
33
+
15
34
  print('.');
16
35
  print('__property');
36
+ maybe.indent.dec(isChain);
17
37
  };
18
38
 
19
39
  module.exports.OptionalMemberExpression = (path, {print}) => {
@@ -31,3 +51,51 @@ module.exports.OptionalMemberExpression = (path, {print}) => {
31
51
 
32
52
  print('__property');
33
53
  };
54
+
55
+ function looksLikeChain(path) {
56
+ const {parentPath} = path;
57
+
58
+ if (parentPath.parentPath.isStatement() && !parentPath.parentPath.isExpressionStatement())
59
+ return false;
60
+
61
+ if (path.find(isIfStatement))
62
+ return false;
63
+
64
+ const isMember = ({parentPath}) => parentPath.parentPath.isMemberExpression();
65
+ const isExpression = ({parentPath}) => parentPath.parentPath.isExpressionStatement();
66
+ const itMember = isMember(path);
67
+ const itExpression = isExpression(path);
68
+
69
+ if (parentPath.isLiteral())
70
+ return false;
71
+
72
+ if (parentPath.isUnaryExpression())
73
+ return false;
74
+
75
+ if (!itMember && !path.parentPath.isExpressionStatement() && !parentPath.isCallExpression())
76
+ return false;
77
+
78
+ if (parentPath.isCallExpression() && parentPath.get('callee') !== path)
79
+ return false;
80
+
81
+ if (compare(parentPath, '__a.__b(__args);') && !itMember && !itExpression)
82
+ return false;
83
+
84
+ if (compare(parentPath, '__a.__b.__c(__args)') && !itMember)
85
+ return false;
86
+
87
+ if (compare(parentPath, '__a.__b.__c = __d'))
88
+ return false;
89
+
90
+ const {__a, __b} = getTemplateValues(parentPath, '__a.__b(__args)');
91
+ const aType = __a?.type;
92
+ const bType = __b?.type;
93
+
94
+ if (aType === bType && isIdentifier(__a) && itExpression)
95
+ return false;
96
+
97
+ if (isThisExpression(__a) && isIdentifier(__b) && itExpression)
98
+ return false;
99
+
100
+ return !compare(parentPath, '__a.__b(__args)') || itMember || itExpression;
101
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.23.0",
3
+ "version": "1.25.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",
@@ -30,6 +30,7 @@
30
30
  "@babel/parser": "^7.19.0",
31
31
  "@babel/traverse": "^7.21.2",
32
32
  "@babel/types": "^7.21.3",
33
+ "@putout/compare": "^9.13.0",
33
34
  "fullstore": "^3.0.0",
34
35
  "just-snake-case": "^3.2.0"
35
36
  },