@putout/printer 1.98.3 → 1.100.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.05.17, v1.100.0
2
+
3
+ feature:
4
+ - 1b2c36b @putout/printer: IfStatement: improve newline support when nested
5
+
6
+ 2023.05.16, v1.99.0
7
+
8
+ feature:
9
+ - a7f4de3 @putout/printer: BinaryExpression: parens (coderaiser/putout#146)
10
+
1
11
  2023.05.15, v1.98.3
2
12
 
3
13
  feature:
@@ -2,26 +2,25 @@
2
2
 
3
3
  const BinaryExpression = {
4
4
  condition(path) {
5
+ const parens = path.node.extra?.parenthesized;
6
+
7
+ if (parens)
8
+ return true;
9
+
5
10
  return path.parentPath.isAwaitExpression();
6
11
  },
7
12
  before(path, {print}) {
8
13
  print('(');
9
14
  },
10
- print(path, {write, traverse, maybe}) {
15
+ print(path, {write, traverse}) {
11
16
  const left = path.get('left');
12
17
  const right = path.get('right');
13
- const isLeft = isLogical(path, left);
14
- const isRight = isLogical(path, right);
15
18
 
16
- maybe.write(isLeft, '(');
17
19
  traverse(left);
18
- maybe.write(isLeft, ')');
19
20
  write.space();
20
21
  write(path.node.operator);
21
22
  write.space();
22
- maybe.write(isRight, '(');
23
23
  traverse(right);
24
- maybe.write(isRight, ')');
25
24
  },
26
25
  after(path, {print}) {
27
26
  print(')');
@@ -30,12 +29,3 @@ const BinaryExpression = {
30
29
 
31
30
  module.exports.BinaryExpression = BinaryExpression;
32
31
  module.exports.LogicalExpression = BinaryExpression;
33
-
34
- const isLogical = (main, path) => {
35
- const is = path.isLogicalExpression();
36
-
37
- if (!is)
38
- return false;
39
-
40
- return main.node.operator !== path.node.operator;
41
- };
@@ -46,14 +46,10 @@ function unaryExpression(path, {maybe, traverse}) {
46
46
  } = path.node;
47
47
 
48
48
  const argPath = path.get('argument');
49
- const round = argPath.isBinaryExpression() || argPath.isLogicalExpression();
50
49
 
51
50
  maybe.print(prefix, operator);
52
51
  maybe.print(isWord(operator), ' ');
53
52
 
54
- maybe.print(round, '(');
55
53
  traverse(argPath);
56
- maybe.print(round, ')');
57
-
58
54
  maybe.print(!prefix, operator);
59
55
  }
@@ -5,6 +5,9 @@ const {exists} = require('../is');
5
5
 
6
6
  const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
7
7
 
8
+ const isInsideIf = (path) => path.parentPath.parentPath?.isIfStatement();
9
+ const isEmptyBody = (path) => !path.node.body.length;
10
+
8
11
  module.exports.IfStatement = {
9
12
  print: (path, {indent, print, maybe, write, traverse}) => {
10
13
  indent();
@@ -21,6 +24,7 @@ module.exports.IfStatement = {
21
24
  if (isConsequentBlock) {
22
25
  print.space();
23
26
  print(consequent);
27
+ maybe.print.newline(isEmptyBody(consequent) && isInsideIf(path));
24
28
  } else {
25
29
  const is = !isEmptyConsequent(path);
26
30
  maybe.print.newline(is);
@@ -49,7 +53,7 @@ module.exports.IfStatement = {
49
53
  afterIf: (path) => {
50
54
  const next = path.getNextSibling();
51
55
 
52
- if (!next.node)
56
+ if (!exists(next))
53
57
  return false;
54
58
 
55
59
  if (next.isReturnStatement())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.98.3",
3
+ "version": "1.100.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",