@putout/printer 1.5.1 → 1.5.2

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,8 @@
1
+ 2023.03.19, v1.5.2
2
+
3
+ feature:
4
+ - 5354616 @putout/printer: add support of TryCatchStatement
5
+
1
6
  2023.03.18, v1.5.1
2
7
 
3
8
  feature:
@@ -2,7 +2,7 @@
2
2
 
3
3
  const isFirstStatement = (path) => path.get('body.0')?.isStatement();
4
4
 
5
- module.exports.BlockStatement = (path, {write, indent, traverse}) => {
5
+ module.exports.BlockStatement = (path, {write, indent, traverse, maybe}) => {
6
6
  const body = path.get('body');
7
7
 
8
8
  if (path.parentPath.isBlockStatement())
@@ -26,6 +26,17 @@ module.exports.BlockStatement = (path, {write, indent, traverse}) => {
26
26
  write(',');
27
27
  }
28
28
 
29
- if (!/FunctionExpression/.test(path.parentPath.type))
30
- write.newline();
29
+ const shouldAddNewLine = !isTry(path) && !/FunctionExpression/.test(path.parentPath.type);
30
+
31
+ maybe.print.newline(shouldAddNewLine);
31
32
  };
33
+
34
+ function isTry({parentPath}) {
35
+ if (parentPath.isTryStatement())
36
+ return true;
37
+
38
+ if (parentPath.parentPath?.isTryStatement())
39
+ return true;
40
+
41
+ return false;
42
+ }
@@ -6,6 +6,7 @@ const {IfStatement} = require('./if-statement');
6
6
  const {ForOfStatement} = require('./for-of-statement');
7
7
  const {BlockStatement} = require('./block-statement');
8
8
  const {ReturnStatement} = require('./return-statement');
9
+ const TryStatements = require('./try-statements');
9
10
 
10
11
  module.exports = {
11
12
  BlockStatement,
@@ -17,6 +18,7 @@ module.exports = {
17
18
  Program(path, {traverse}) {
18
19
  path.get('body').forEach(traverse);
19
20
  },
21
+ ...TryStatements,
20
22
  ContinueStatement(path, {write, indent}) {
21
23
  indent();
22
24
  write('continue;\n');
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ const {isNext} = require('../is');
4
+ module.exports.TryStatement = (path, {print, maybe}) => {
5
+ const finalizer = path.get('finalizer');
6
+
7
+ print('try ');
8
+ print(path.get('block'));
9
+ print(path.get('handler'));
10
+
11
+ if (finalizer.node) {
12
+ print(' ');
13
+ print('finally');
14
+ print(' ');
15
+ print(finalizer);
16
+ print.newline();
17
+ }
18
+
19
+ maybe.print.linebreak(isNext(path));
20
+ };
21
+
22
+ module.exports.CatchClause = (path, {print}) => {
23
+ const param = path.get('param');
24
+ const body = path.get('body');
25
+
26
+ print(' ');
27
+ print('catch ');
28
+
29
+ if (param.node) {
30
+ print('(');
31
+ print(param);
32
+ print(') ');
33
+ }
34
+
35
+ print(body);
36
+ };
37
+
@@ -36,6 +36,8 @@ module.exports.tokenize = (ast) => {
36
36
  const maybeIndent = (a) => a && indent();
37
37
  const maybeIndentInc = (a) => a && indent.inc();
38
38
  const maybeIndentDec = (a) => a && indent.dec();
39
+ const maybeNewline = (a) => a && newline();
40
+ const maybeLinebreak = (a) => a && writeEmptyLine();
39
41
 
40
42
  let i = 0;
41
43
  const incIndent = () => ++i;
@@ -94,6 +96,10 @@ module.exports.tokenize = (ast) => {
94
96
  });
95
97
 
96
98
  assign(print, write);
99
+ assign(maybePrint, {
100
+ newline: maybeNewline,
101
+ linebreak: maybeLinebreak,
102
+ });
97
103
 
98
104
  const printer = {
99
105
  incIndent,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
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",