@putout/printer 1.16.2 → 1.17.1

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,15 @@
1
+ 2023.04.01, v1.17.1
2
+
3
+ feature:
4
+ - 794186f @putout/printer: improve support of IfElse
5
+ - a35e751 @putout/printer: improve support of round braces
6
+
7
+ 2023.04.01, v1.17.0
8
+
9
+ feature:
10
+ - 1378835 @putout/printer: improve support of round brackets around LogicalExpressions
11
+ - 3fc47f2 @putout/printer: improve support of BinaryExpression inside UnaryExpression
12
+
1
13
  2023.04.01, v1.16.2
2
14
 
3
15
  feature:
package/README.md CHANGED
@@ -64,6 +64,23 @@ print(ast, {
64
64
  'const {a /* [hello world] */= 5} = b;\n';
65
65
  ```
66
66
 
67
+ ## Maybe
68
+
69
+ When you need some condition use `maybe`. For example, to add newline only when parent node is `CallExpression` you
70
+ can use `maybe.print.newline(condition)`:
71
+
72
+ ```js
73
+ print(ast, {
74
+ visitors: {
75
+ AssignmentPattern(path, {print, maybe}) {
76
+ maybe.print.newline(path.parentPath.isCallExpression());
77
+ print(' /* [hello world] */= ');
78
+ print('__right');
79
+ },
80
+ },
81
+ });
82
+ ```
83
+
67
84
  ## License
68
85
 
69
86
  MIT
@@ -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,20 @@ 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() || argPath.isLogicalExpression();
43
43
 
44
- maybe.print(isWord(
45
- operator,
46
- ), ' ');
47
-
48
- print('__argument');
44
+ maybe.print(prefix, operator);
45
+ maybe.print(isWord(operator), ' ');
49
46
 
50
- if (!prefix)
51
- print(operator);
47
+ maybe.print(round, '(');
48
+ traverse(argPath);
49
+ maybe.print(round, ')');
50
+ maybe.print(!prefix, operator);
52
51
  }
@@ -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;
@@ -1,8 +1,11 @@
1
1
  'use strict';
2
2
 
3
- const {hasPrevNewline, markAfter} = require('../mark');
4
- const {isFirst} = require('../is');
3
+ const {
4
+ hasPrevNewline,
5
+ markAfter,
6
+ } = require('../mark');
5
7
 
8
+ const {isFirst} = require('../is');
6
9
  const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
7
10
 
8
11
  module.exports.IfStatement = {
@@ -10,7 +13,7 @@ module.exports.IfStatement = {
10
13
  print.linebreak();
11
14
  },
12
15
  beforeIf: shouldAddNewlineBefore,
13
- print: (path, {indent, print, maybe}) => {
16
+ print: (path, {indent, print, maybe, write, traverse}) => {
14
17
  indent();
15
18
  print('if (');
16
19
  print('__test');
@@ -24,7 +27,6 @@ module.exports.IfStatement = {
24
27
  print(consequent);
25
28
  } else {
26
29
  const is = !isEmptyConsequent(path);
27
-
28
30
  maybe.print.newline(is);
29
31
  maybe.indent.inc(is);
30
32
  print(consequent);
@@ -32,21 +34,34 @@ module.exports.IfStatement = {
32
34
  }
33
35
 
34
36
  if (alternate.isBlockStatement()) {
35
- print(' else ');
36
- print(alternate);
37
+ write(' else ');
38
+ traverse(alternate);
39
+ } else if (alternate.isIfStatement()) {
40
+ print.space();
41
+ write('else');
42
+ print.space();
43
+ traverse(alternate);
37
44
  } else if (alternate.node) {
38
45
  print.breakline();
39
- print('else');
46
+ write('else');
40
47
  print.newline();
41
48
  indent.inc();
42
- print(alternate);
49
+ traverse(alternate);
43
50
  indent.dec();
44
51
  }
52
+
53
+ const next = path.getNextSibling();
54
+
55
+ if (next.isExpressionStatement() && !next.get('expression').isCallExpression())
56
+ print.newline();
45
57
  },
46
58
  afterIf: (path) => {
47
59
  const next = path.getNextSibling();
48
60
 
49
- if (!next.node || next.isExpressionStatement() || next.isReturnStatement())
61
+ if (!next.node)
62
+ return false;
63
+
64
+ if (next.isReturnStatement())
50
65
  return false;
51
66
 
52
67
  return true;
@@ -59,5 +74,8 @@ module.exports.IfStatement = {
59
74
  };
60
75
 
61
76
  function shouldAddNewlineBefore(path) {
77
+ if (path.parentPath.isIfStatement())
78
+ return false;
79
+
62
80
  return !isFirst(path) && !hasPrevNewline(path);
63
81
  }
@@ -78,6 +78,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
78
78
  const maybeBreakline = (a) => a && breakline();
79
79
  const maybeLinebreak = (a) => a && linebreak();
80
80
  const maybeWrite = (a, b) => a && write(b);
81
+ const maybeSpace = (a) => a && space();
81
82
  let i = 0;
82
83
  const incIndent = () => ++i;
83
84
  const decIndent = () => --i;
@@ -135,6 +136,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
135
136
  markBefore: maybeMarkBefore,
136
137
  markAfter: maybeMarkAfter,
137
138
  write: maybeWrite,
139
+ space: maybeSpace,
138
140
  };
139
141
 
140
142
  assign(maybe.indent, {
@@ -190,6 +192,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
190
192
  newline: maybeNewline,
191
193
  breakline: maybeBreakline,
192
194
  linebreak: maybeLinebreak,
195
+ space: maybeSpace,
193
196
  });
194
197
 
195
198
  assign(printer.maybe, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.16.2",
3
+ "version": "1.17.1",
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",