@putout/printer 1.22.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,13 @@
1
+ 2023.04.05, v1.24.0
2
+
3
+ feature:
4
+ - 47fb021 @putout/printer: add support of chaining
5
+
6
+ 2023.04.05, v1.23.0
7
+
8
+ feature:
9
+ - 316c0f6 @putout/printer: add support of ExpressionStatement parent of ObjectExpression
10
+
1
11
  2023.04.04, v1.22.0
2
12
 
3
13
  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
+ }
@@ -5,6 +5,7 @@ const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
5
5
  const isLogical = (path) => path.get('argument').isLogicalExpression();
6
6
  const isValue = (path) => path.get('properties.0.value').node;
7
7
  const isIf = (path) => path.parentPath.parentPath.isIfStatement();
8
+ const isParentExpression = (path) => path.parentPath.isExpressionStatement();
8
9
 
9
10
  const isForOf = (path) => {
10
11
  if (path.parentPath.isForOfStatement())
@@ -18,7 +19,7 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
18
19
 
19
20
  const properties = path.get('properties');
20
21
  const {length} = properties;
21
- const parens = isBodyOfArrow(path);
22
+ const parens = isParens(path);
22
23
  const manyLines = !isOneLine(path);
23
24
 
24
25
  maybe.print(parens, '(');
@@ -102,3 +103,13 @@ function isOneLine(path) {
102
103
 
103
104
  return !isValue(path);
104
105
  }
106
+
107
+ function isParens(path) {
108
+ if (isBodyOfArrow(path))
109
+ return true;
110
+
111
+ if (isParentExpression(path))
112
+ return true;
113
+
114
+ return false;
115
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.22.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",