@putout/printer 1.24.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,8 @@
1
+ 2023.04.06, v1.25.0
2
+
3
+ feature:
4
+ - db62133 @putout/printer: add support of chaining
5
+
1
6
  2023.04.05, v1.24.0
2
7
 
3
8
  feature:
@@ -1,8 +1,15 @@
1
1
  'use strict';
2
2
 
3
- const {isIfStatement} = require('@babel/types');
4
- const isInnerCall = (path) => path.get('object').isCallExpression();
5
- const isOuterCall = (path) => path.parentPath.isCallExpression();
3
+ const {
4
+ isIfStatement,
5
+ isIdentifier,
6
+ isThisExpression,
7
+ } = require('@babel/types');
8
+
9
+ const {
10
+ compare,
11
+ getTemplateValues,
12
+ } = require('@putout/compare');
6
13
 
7
14
  module.exports.MemberExpression = (path, {print, indent, maybe}) => {
8
15
  const {computed} = path.node;
@@ -54,8 +61,41 @@ function looksLikeChain(path) {
54
61
  if (path.find(isIfStatement))
55
62
  return false;
56
63
 
57
- if (isInnerCall(path) && isOuterCall(path))
58
- return true;
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;
59
99
 
60
- return false;
100
+ return !compare(parentPath, '__a.__b(__args)') || itMember || itExpression;
61
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.24.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
  },