@putout/printer 1.5.0 → 1.5.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,8 @@
1
+ 2023.03.18, v1.5.1
2
+
3
+ feature:
4
+ - a79e4f6 @putout/printer: print, indent, maybe
5
+
1
6
  2023.03.18, v1.5.0
2
7
 
3
8
  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
+
@@ -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 = {
@@ -69,12 +71,21 @@ module.exports.tokenize = (ast) => {
69
71
  newline,
70
72
  linebreak: writeEmptyLine,
71
73
  });
74
+ const print = (maybeLine) => {
75
+ if (isString(maybeLine))
76
+ return write(maybeLine);
77
+
78
+ return traverse(maybeLine);
79
+ };
80
+
81
+ const maybePrint = (a, b) => a && print(b);
72
82
 
73
83
  const maybe = {
74
84
  write: maybeWrite,
75
85
  indent: maybeIndent,
76
86
  markBefore: maybeMarkBefore,
77
87
  markAfter: maybeMarkAfter,
88
+ print: maybePrint,
78
89
  };
79
90
 
80
91
  assign(maybe.indent, {
@@ -82,6 +93,8 @@ module.exports.tokenize = (ast) => {
82
93
  dec: maybeIndentDec,
83
94
  });
84
95
 
96
+ assign(print, write);
97
+
85
98
  const printer = {
86
99
  incIndent,
87
100
  decIndent,
@@ -93,6 +106,7 @@ module.exports.tokenize = (ast) => {
93
106
  traverse,
94
107
  writeEmptyLine,
95
108
  maybe,
109
+ print,
96
110
  };
97
111
 
98
112
  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.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",