@putout/printer 1.5.0 → 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,13 @@
1
+ 2023.03.19, v1.5.2
2
+
3
+ feature:
4
+ - 5354616 @putout/printer: add support of TryCatchStatement
5
+
6
+ 2023.03.18, v1.5.1
7
+
8
+ feature:
9
+ - a79e4f6 @putout/printer: print, indent, maybe
10
+
1
11
  2023.03.18, v1.5.0
2
12
 
3
13
  feature:
@@ -7,44 +7,43 @@ const {
7
7
 
8
8
  const {entries} = Object;
9
9
 
10
- module.exports.CallExpression = (path, {traverse, indent, write, incIndent, decIndent, maybeWrite}) => {
10
+ module.exports.CallExpression = (path, {indent, print, maybe}) => {
11
11
  const isParentCall = toLong(path) && path.parentPath.isCallExpression();
12
12
 
13
13
  if (shouldAddNewLine(path) && !isMarkedParentBefore(path) && !isMarkedPrevAfter(path.parentPath))
14
- write.linebreak();
14
+ print.linebreak();
15
15
 
16
- traverse(path.get('callee'));
17
- write('(');
16
+ print(path.get('callee'));
17
+ print('(');
18
18
 
19
19
  const args = path.get('arguments');
20
20
  const n = args.length - 1;
21
21
 
22
- if (isParentCall)
23
- incIndent();
22
+ maybe.indent.inc(isParentCall);
24
23
 
25
24
  for (const [i, arg] of entries(args)) {
26
25
  if (isParentCall) {
27
- write.newline();
26
+ print.newline();
28
27
  indent();
29
28
  }
30
29
 
31
- traverse(arg);
30
+ print(arg);
32
31
 
33
32
  if (isParentCall) {
34
- write(',');
33
+ print(',');
35
34
  continue;
36
35
  }
37
36
 
38
- maybeWrite(i < n, ', ');
37
+ maybe.print(i < n, ', ');
39
38
  }
40
39
 
41
40
  if (isParentCall) {
42
- decIndent();
43
- write.newline();
44
- write.indent();
41
+ indent.dec();
42
+ print.newline();
43
+ print.indent();
45
44
  }
46
45
 
47
- write(')');
46
+ print(')');
48
47
  };
49
48
 
50
49
  function shouldAddNewLine({parentPath}) {
@@ -4,27 +4,27 @@ const {entries} = Object;
4
4
 
5
5
  const isFirst = (path) => path.node === path.parentPath.node.body[0];
6
6
 
7
- module.exports.ClassDeclaration = (path, {write, maybeWrite, writeEmptyLine, indent, incIndent, decIndent, traverse}) => {
7
+ module.exports.ClassDeclaration = (path, {maybe, print, indent}) => {
8
8
  if (!isFirst(path)) {
9
- writeEmptyLine();
9
+ print.linebreak();
10
10
  }
11
11
 
12
12
  indent();
13
- write('class ');
14
- traverse(path.get('id'));
15
- write(' {\n');
16
- incIndent();
13
+ print('class ');
14
+ print(path.get('id'));
15
+ print(' {\n');
16
+ indent.inc();
17
17
 
18
18
  const body = path.get('body.body');
19
19
  const n = body.length - 1;
20
20
 
21
21
  for (const [i, item] of entries(body)) {
22
22
  indent();
23
- traverse(item);
24
- maybeWrite(i < n, '\n');
23
+ print(item);
24
+ maybe.print(i < n, '\n');
25
25
  }
26
26
 
27
- decIndent();
28
- write('}');
27
+ indent.dec();
28
+ print('}');
29
29
  };
30
30
 
@@ -3,20 +3,19 @@
3
3
  const {entries} = Object;
4
4
  const isForOf = (path) => path.parentPath?.parentPath?.parentPath?.isForOfStatement();
5
5
 
6
- module.exports.ObjectPattern = (path, {traverse, maybeIndent, write, maybeWrite, incIndent, decIndent}) => {
7
- incIndent();
8
-
9
- write('{');
6
+ module.exports.ObjectPattern = (path, {indent, print, maybe}) => {
7
+ indent.inc();
8
+ print('{');
10
9
 
11
10
  const properties = path.get('properties');
12
11
  const n = properties.length - 1;
13
12
 
14
13
  const is = !isForOf(path) && !path.parentPath.isFunction() && n && checkLength(properties);
15
- maybeWrite(is, '\n');
14
+ maybe.print(is, '\n');
16
15
 
17
16
  for (const [i, property] of entries(properties)) {
18
17
  if (property.isRestElement()) {
19
- traverse(property);
18
+ print(property);
20
19
  continue;
21
20
  }
22
21
 
@@ -24,29 +23,29 @@ module.exports.ObjectPattern = (path, {traverse, maybeIndent, write, maybeWrite,
24
23
  const keyPath = property.get('key');
25
24
  const {shorthand} = property.node;
26
25
 
27
- maybeIndent(is);
28
- traverse(keyPath);
26
+ maybe.indent(is);
27
+ print(keyPath);
29
28
 
30
29
  if (!shorthand) {
31
- write(': ');
32
- traverse(valuePath);
30
+ print(': ');
31
+ print(valuePath);
33
32
  }
34
33
 
35
34
  if (valuePath.isAssignmentPattern())
36
- traverse(valuePath);
35
+ print(valuePath);
37
36
 
38
37
  if (is) {
39
- write(',\n');
38
+ print(',');
39
+ print.newline();
40
40
  continue;
41
41
  }
42
42
 
43
- if (i < n)
44
- write(', ');
43
+ maybe.print(i < n, ', ');
45
44
  }
46
45
 
47
- decIndent();
48
- maybeIndent(is);
49
- write('}');
46
+ indent.dec();
47
+ maybe.indent(is);
48
+ print('}');
50
49
  };
51
50
 
52
51
  function checkLength(properties) {
@@ -57,3 +56,4 @@ function checkLength(properties) {
57
56
 
58
57
  return false;
59
58
  }
59
+
@@ -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
+
@@ -12,6 +12,8 @@ const {
12
12
  } = require('./mark');
13
13
  const {parseComments} = require('./comments');
14
14
 
15
+ const isString = (a) => typeof a === 'string';
16
+
15
17
  const {assign} = Object;
16
18
 
17
19
  const traversers = {
@@ -34,6 +36,8 @@ module.exports.tokenize = (ast) => {
34
36
  const maybeIndent = (a) => a && indent();
35
37
  const maybeIndentInc = (a) => a && indent.inc();
36
38
  const maybeIndentDec = (a) => a && indent.dec();
39
+ const maybeNewline = (a) => a && newline();
40
+ const maybeLinebreak = (a) => a && writeEmptyLine();
37
41
 
38
42
  let i = 0;
39
43
  const incIndent = () => ++i;
@@ -69,12 +73,21 @@ module.exports.tokenize = (ast) => {
69
73
  newline,
70
74
  linebreak: writeEmptyLine,
71
75
  });
76
+ const print = (maybeLine) => {
77
+ if (isString(maybeLine))
78
+ return write(maybeLine);
79
+
80
+ return traverse(maybeLine);
81
+ };
82
+
83
+ const maybePrint = (a, b) => a && print(b);
72
84
 
73
85
  const maybe = {
74
86
  write: maybeWrite,
75
87
  indent: maybeIndent,
76
88
  markBefore: maybeMarkBefore,
77
89
  markAfter: maybeMarkAfter,
90
+ print: maybePrint,
78
91
  };
79
92
 
80
93
  assign(maybe.indent, {
@@ -82,6 +95,12 @@ module.exports.tokenize = (ast) => {
82
95
  dec: maybeIndentDec,
83
96
  });
84
97
 
98
+ assign(print, write);
99
+ assign(maybePrint, {
100
+ newline: maybeNewline,
101
+ linebreak: maybeLinebreak,
102
+ });
103
+
85
104
  const printer = {
86
105
  incIndent,
87
106
  decIndent,
@@ -93,6 +112,7 @@ module.exports.tokenize = (ast) => {
93
112
  traverse,
94
113
  writeEmptyLine,
95
114
  maybe,
115
+ print,
96
116
  };
97
117
 
98
118
  babelTraverse(ast, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.5.0",
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",