@putout/printer 1.17.0 → 1.18.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,14 @@
1
+ 2023.04.02, v1.18.0
2
+
3
+ feature:
4
+ - 317a7cf @putout/printer: improve support of VariableDeclaration inside of ExportDeclaration
5
+
6
+ 2023.04.01, v1.17.1
7
+
8
+ feature:
9
+ - 794186f @putout/printer: improve support of IfElse
10
+ - a35e751 @putout/printer: improve support of round braces
11
+
1
12
  2023.04.01, v1.17.0
2
13
 
3
14
  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
@@ -39,10 +39,9 @@ function unaryExpression(path, {maybe, traverse}) {
39
39
  } = path.node;
40
40
 
41
41
  const argPath = path.get('argument');
42
- const round = argPath.isBinaryExpression();
42
+ const round = argPath.isBinaryExpression() || argPath.isLogicalExpression();
43
43
 
44
44
  maybe.print(prefix, operator);
45
-
46
45
  maybe.print(isWord(operator), ' ');
47
46
 
48
47
  maybe.print(round, '(');
@@ -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;
@@ -10,7 +10,7 @@ const {
10
10
  markAfter,
11
11
  } = require('../mark');
12
12
 
13
- const isParentBlock = (path) => /Program|BlockStatement/.test(path.parentPath.type);
13
+ const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
14
14
 
15
15
  module.exports.VariableDeclaration = {
16
16
  beforeIf: shouldAddNewlineBefore,
@@ -88,7 +88,7 @@ function shouldAddNewlineBefore(path) {
88
88
  }
89
89
 
90
90
  function isFirst(path) {
91
- return path.node === path.parentPath.node.body[0];
91
+ return path.node === path.parentPath.node.body?.[0];
92
92
  }
93
93
  const isNextAssign = (path) => {
94
94
  const nextPath = path.getNextSibling();
@@ -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.17.0",
3
+ "version": "1.18.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",