@putout/printer 1.23.0 → 1.24.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.05, v1.24.0
2
+
3
+ feature:
4
+ - 47fb021 @putout/printer: add support of chaining
5
+
1
6
  2023.04.05, v1.23.0
2
7
 
3
8
  feature:
@@ -1,6 +1,10 @@
1
1
  'use strict';
2
2
 
3
- module.exports.MemberExpression = (path, {print}) => {
3
+ const {isIfStatement} = require('@babel/types');
4
+ const isInnerCall = (path) => path.get('object').isCallExpression();
5
+ const isOuterCall = (path) => path.parentPath.isCallExpression();
6
+
7
+ module.exports.MemberExpression = (path, {print, indent, maybe}) => {
4
8
  const {computed} = path.node;
5
9
  print('__object');
6
10
 
@@ -12,8 +16,17 @@ module.exports.MemberExpression = (path, {print}) => {
12
16
  return;
13
17
  }
14
18
 
19
+ const isChain = looksLikeChain(path);
20
+ maybe.indent.inc(isChain);
21
+
22
+ if (isChain) {
23
+ print.newline();
24
+ indent(isChain);
25
+ }
26
+
15
27
  print('.');
16
28
  print('__property');
29
+ maybe.indent.dec(isChain);
17
30
  };
18
31
 
19
32
  module.exports.OptionalMemberExpression = (path, {print}) => {
@@ -31,3 +44,18 @@ module.exports.OptionalMemberExpression = (path, {print}) => {
31
44
 
32
45
  print('__property');
33
46
  };
47
+
48
+ function looksLikeChain(path) {
49
+ const {parentPath} = path;
50
+
51
+ if (parentPath.parentPath.isStatement() && !parentPath.parentPath.isExpressionStatement())
52
+ return false;
53
+
54
+ if (path.find(isIfStatement))
55
+ return false;
56
+
57
+ if (isInnerCall(path) && isOuterCall(path))
58
+ return true;
59
+
60
+ return false;
61
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.23.0",
3
+ "version": "1.24.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",