@putout/printer 12.9.0 → 12.11.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
+ 2025.01.23, v12.11.0
2
+
3
+ feature:
4
+ - f854725 @putout/printer: BlockStatement: is-call-inside-chain: ReturnStatement
5
+
6
+ 2025.01.23, v12.10.0
7
+
8
+ feature:
9
+ - 262925b @putout/printer: BlockStatement: call inside chain
10
+
1
11
  2025.01.23, v12.9.0
2
12
 
3
13
  feature:
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('@putout/babel');
4
+ const {chain} = require('./chain');
5
+ const {satisfy} = require('../../is');
6
+
7
+ const {
8
+ isUnaryExpression,
9
+ isIfStatement,
10
+ isCallExpression,
11
+ } = types;
12
+
13
+ const isArgOfCall = (path) => path.parentPath.isCallExpression() && path.parentPath.get('arguments.0') === path;
14
+ const isCall = (a) => a.type === 'CallExpression';
15
+
16
+ const isExcludedFromChain = satisfy([isUnaryExpression, isIfStatement]);
17
+
18
+ module.exports.isLooksLikeChain = (path) => {
19
+ const [root, properties] = chain(path);
20
+
21
+ if (isExcludedFromChain(root))
22
+ return false;
23
+
24
+ if (properties.find(isPathGet))
25
+ return false;
26
+
27
+ if (path.find(isIfUp))
28
+ return false;
29
+
30
+ const calls = properties.filter(isCall);
31
+ const [firstCall] = calls;
32
+
33
+ if (calls.length === 2 && !firstCall.name)
34
+ return false;
35
+
36
+ if (isArgOfCall(path))
37
+ return false;
38
+
39
+ return calls.length > 1;
40
+ };
41
+
42
+ const isPathGet = (property) => {
43
+ return isCallExpression(property, {
44
+ name: 'get',
45
+ });
46
+ };
47
+
48
+ const isIfUp = (path) => {
49
+ const ifPath = path.find(isIfStatement);
50
+ let is = false;
51
+
52
+ if (!ifPath)
53
+ return is;
54
+
55
+ ifPath.get('test').traverse({
56
+ MemberExpression(currentPath) {
57
+ if (path === currentPath) {
58
+ is = true;
59
+ path.stop();
60
+ }
61
+ },
62
+ });
63
+
64
+ return is;
65
+ };
@@ -1,20 +1,8 @@
1
1
  'use strict';
2
2
 
3
- const {types} = require('@putout/babel');
4
-
5
- const {chain} = require('./chain');
6
- const {satisfy} = require('../../is');
7
-
8
3
  const {maybePrintComputed} = require('../object-expression/maybe-print-computed');
9
4
  const {maybeParens} = require('../../maybe/maybe-parens');
10
-
11
- const {
12
- isUnaryExpression,
13
- isIfStatement,
14
- isCallExpression,
15
- } = types;
16
-
17
- const isArgOfCall = (path) => path.parentPath.isCallExpression() && path.parentPath.get('arguments.0') === path;
5
+ const {isLooksLikeChain} = require('./is-looks-like-chain');
18
6
 
19
7
  module.exports.MemberExpression = (path, printer) => {
20
8
  const {
@@ -35,12 +23,10 @@ module.exports.MemberExpression = (path, printer) => {
35
23
  if (computed)
36
24
  return maybePrintComputed(path, property, printer);
37
25
 
38
- const isChain = likeChain(path);
26
+ const isChain = isLooksLikeChain(path);
39
27
 
40
28
  maybe.indent.inc(isChain);
41
-
42
- if (isChain)
43
- print.breakline();
29
+ maybe.print.breakline(isChain);
44
30
 
45
31
  print('.');
46
32
  print('__property');
@@ -65,57 +51,3 @@ module.exports.OptionalMemberExpression = maybeParens((path, {print, maybe}) =>
65
51
 
66
52
  print('__property');
67
53
  });
68
-
69
- const isCall = (a) => a.type === 'CallExpression';
70
-
71
- const isPathGet = (property) => {
72
- return isCallExpression(property, {
73
- name: 'get',
74
- });
75
- };
76
-
77
- const isExcludedFromChain = satisfy([isUnaryExpression, isIfStatement]);
78
-
79
- const isIfUp = (path) => {
80
- const ifPath = path.find(isIfStatement);
81
- let is = false;
82
-
83
- if (!ifPath)
84
- return is;
85
-
86
- ifPath.get('test').traverse({
87
- MemberExpression(currentPath) {
88
- if (path === currentPath) {
89
- is = true;
90
- path.stop();
91
- }
92
- },
93
- });
94
-
95
- return is;
96
- };
97
-
98
- module.exports.likeChain = likeChain;
99
- function likeChain(path) {
100
- const [root, properties] = chain(path);
101
-
102
- if (isExcludedFromChain(root))
103
- return false;
104
-
105
- if (properties.find(isPathGet))
106
- return false;
107
-
108
- if (path.find(isIfUp))
109
- return false;
110
-
111
- const calls = properties.filter(isCall);
112
- const [firstCall] = calls;
113
-
114
- if (calls.length === 2 && !firstCall.name)
115
- return false;
116
-
117
- if (isArgOfCall(path))
118
- return false;
119
-
120
- return calls.length > 1;
121
- }
@@ -13,8 +13,8 @@ const {
13
13
  } = require('../../is');
14
14
 
15
15
  const {parseComments} = require('../../comment/comment');
16
- const {likeChain} = require('../member-expression/member-expressions');
17
16
  const {isInsideTuple} = require('./is-inside-tuple');
17
+ const {isLooksLikeChain} = require('../member-expression/is-looks-like-chain');
18
18
 
19
19
  const {
20
20
  isStringLiteral,
@@ -35,7 +35,7 @@ const isMemberExpressionCallee = ({parentPath}) => {
35
35
  if (!callee.isMemberExpression())
36
36
  return false;
37
37
 
38
- return likeChain(callee);
38
+ return isLooksLikeChain(callee);
39
39
  };
40
40
 
41
41
  const isInsideCall = ({parentPath}) => parentPath.isCallExpression();
@@ -14,6 +14,8 @@ const {parseComments} = require('../../comment/comment');
14
14
  const {insideIfWithNoBody} = require('./inside-if-with-no-body');
15
15
  const {getDirectives} = require('./get-directives');
16
16
 
17
+ const {isCallInsideChain} = require('./is-call-inside-chain');
18
+
17
19
  const {
18
20
  isArrowFunctionExpression,
19
21
  isObjectMethod,
@@ -60,6 +62,9 @@ module.exports.BlockStatement = {
60
62
  }
61
63
 
62
64
  maybe.write.linebreak(directives.length && body.length);
65
+ const callInsideChain = isCallInsideChain(path);
66
+
67
+ maybe.indent.inc(callInsideChain);
63
68
 
64
69
  for (const element of body) {
65
70
  traverse(element);
@@ -71,6 +76,8 @@ module.exports.BlockStatement = {
71
76
  maybe.indent(body.length);
72
77
  write('}');
73
78
 
79
+ maybe.indent.dec(callInsideChain);
80
+
74
81
  if (path.parentPath.isObjectMethod())
75
82
  write(',');
76
83
  },
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ const {types} = require('@putout/babel');
4
+ const {isLooksLikeChain} = require('../../expressions/member-expression/is-looks-like-chain');
5
+ const {
6
+ isReturnStatement,
7
+ isExpressionStatement,
8
+ isMemberExpression,
9
+ isCallExpression,
10
+ } = types;
11
+
12
+ module.exports.isCallInsideChain = (path) => {
13
+ if (!isCallExpression(path.parentPath.parentPath))
14
+ return false;
15
+
16
+ const member = path.find(isTopMemberInsideCall);
17
+
18
+ if (member)
19
+ return isLooksLikeChain(member);
20
+
21
+ const callPath = path.find(isTopCall);
22
+
23
+ if (!callPath)
24
+ return false;
25
+
26
+ const calleeMember = callPath.get('callee');
27
+
28
+ return isLooksLikeChain(calleeMember);
29
+ };
30
+
31
+ function isTopMemberInsideCall(path) {
32
+ if (!isMemberExpression(path))
33
+ return false;
34
+
35
+ return isExpressionStatement(path.parentPath.parentPath);
36
+ }
37
+
38
+ function isTopCall(path) {
39
+ if (!isCallExpression(path))
40
+ return false;
41
+
42
+ const {parentPath} = path;
43
+
44
+ if (isReturnStatement(parentPath))
45
+ return true;
46
+
47
+ return isExpressionStatement(parentPath);
48
+ }
@@ -14,6 +14,7 @@ const {
14
14
  } = require('../../is');
15
15
 
16
16
  const not = (fn) => (...a) => !fn(...a);
17
+
17
18
  const isBeforeElse = (path) => {
18
19
  if (!path.parentPath.isIfStatement())
19
20
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "12.9.0",
3
+ "version": "12.11.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "Simplest possible opinionated Babel AST printer for 🐊Putout",
@@ -56,6 +56,9 @@
56
56
  },
57
57
  "#test/fixture": {
58
58
  "default": "./test/fixture.js"
59
+ },
60
+ "#printer": {
61
+ "default": "./lib/printer.js"
59
62
  }
60
63
  },
61
64
  "devDependencies": {