@putout/printer 1.16.2 → 1.17.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,9 @@
1
+ 2023.04.01, v1.17.0
2
+
3
+ feature:
4
+ - 1378835 @putout/printer: improve support of round brackets around LogicalExpressions
5
+ - 3fc47f2 @putout/printer: improve support of BinaryExpression inside UnaryExpression
6
+
1
7
  2023.04.01, v1.16.2
2
8
 
3
9
  feature:
@@ -3,13 +3,20 @@
3
3
  module.exports.BinaryExpression = BinaryExpression;
4
4
  module.exports.LogicalExpression = BinaryExpression;
5
5
 
6
- const isLogical = (path) => path.isLogicalExpression();
6
+ const isLogical = (main, path) => {
7
+ const is = path.isLogicalExpression();
8
+
9
+ if (!is)
10
+ return false;
11
+
12
+ return main.node.operator !== path.node.operator;
13
+ };
7
14
 
8
15
  function BinaryExpression(path, {write, traverse, maybe}) {
9
16
  const left = path.get('left');
10
17
  const right = path.get('right');
11
- const isLeft = isLogical(left);
12
- const isRight = isLogical(right);
18
+ const isLeft = isLogical(path, left);
19
+ const isRight = isLogical(path, right);
13
20
 
14
21
  maybe.write(isLeft, '(');
15
22
  traverse(left);
@@ -32,21 +32,21 @@ function printUnary(path, name, {print}) {
32
32
  }
33
33
  const isWord = (a) => /delete|typeof/.test(a);
34
34
 
35
- function unaryExpression(path, {print, maybe}) {
35
+ function unaryExpression(path, {maybe, traverse}) {
36
36
  const {
37
37
  prefix,
38
38
  operator,
39
39
  } = path.node;
40
40
 
41
- if (prefix)
42
- print(operator);
41
+ const argPath = path.get('argument');
42
+ const round = argPath.isBinaryExpression();
43
43
 
44
- maybe.print(isWord(
45
- operator,
46
- ), ' ');
44
+ maybe.print(prefix, operator);
47
45
 
48
- print('__argument');
46
+ maybe.print(isWord(operator), ' ');
49
47
 
50
- if (!prefix)
51
- print(operator);
48
+ maybe.print(round, '(');
49
+ traverse(argPath);
50
+ maybe.print(round, ')');
51
+ maybe.print(!prefix, operator);
52
52
  }
@@ -15,7 +15,7 @@ const isNext = (path) => {
15
15
  const isNextParent = (path) => isNext(path.parentPath);
16
16
  const isLast = (path) => isParentProgram(path) && !isNext(path);
17
17
 
18
- module.exports.isFirst = (path) => path.node === path.parentPath.node.body[0];
18
+ module.exports.isFirst = (path) => path.node === path.parentPath.node.body?.[0];
19
19
  module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
20
20
  module.exports.isNext = isNext;
21
21
  module.exports.isNextParent = isNextParent;
@@ -59,5 +59,8 @@ module.exports.IfStatement = {
59
59
  };
60
60
 
61
61
  function shouldAddNewlineBefore(path) {
62
+ if (path.parentPath.isIfStatement())
63
+ return false;
64
+
62
65
  return !isFirst(path) && !hasPrevNewline(path);
63
66
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.16.2",
3
+ "version": "1.17.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",