@putout/printer 1.4.2 → 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,13 @@
1
+ 2023.03.18, v1.5.1
2
+
3
+ feature:
4
+ - a79e4f6 @putout/printer: print, indent, maybe
5
+
6
+ 2023.03.18, v1.5.0
7
+
8
+ feature:
9
+ - db7c503 @putout/printer: add support of YieldExpression, SequenceExpressions, FunctionExpression + generator
10
+
1
11
  2023.03.18, v1.4.2
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,7 +3,35 @@
3
3
  const {isMarkedPrevAfter} = require('../mark');
4
4
  const isFirst = (path) => path.node === path.parentPath.node.body[0];
5
5
 
6
- module.exports.ArrowFunctionExpression = (path, {write, maybe, traverse}) => {
6
+ module.exports.FunctionExpression = (path, {write, maybe, traverse}) => {
7
+ const {node} = path;
8
+
9
+ const {
10
+ generator,
11
+ async,
12
+ } = node;
13
+
14
+ maybe.write(async, 'async ');
15
+ write('function');
16
+ maybe.write(generator, '*');
17
+ write(' (');
18
+
19
+ const params = path.get('params');
20
+ const n = params.length;
21
+
22
+ for (let i = 0; i < n; i++) {
23
+ traverse(params[i]);
24
+
25
+ if (i < n - 1)
26
+ write(', ');
27
+ }
28
+
29
+ write(') ');
30
+ traverse(path.get('body'));
31
+ };
32
+
33
+ module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
34
+ function ArrowFunctionExpression(path, {write, maybe, traverse}) {
7
35
  const {async} = path.node;
8
36
  maybe.write(async, 'async ');
9
37
 
@@ -21,7 +49,7 @@ module.exports.ArrowFunctionExpression = (path, {write, maybe, traverse}) => {
21
49
 
22
50
  write(') => ');
23
51
  traverse(path.get('body'));
24
- };
52
+ }
25
53
 
26
54
  module.exports.ObjectMethod = (path, {write, traverse}) => {
27
55
  traverse(path.get('key'));
@@ -84,3 +112,4 @@ module.exports.ClassMethod = (path, {write, traverse}) => {
84
112
  write(') ');
85
113
  traverse(path.get('body'));
86
114
  };
115
+
@@ -15,6 +15,7 @@ const {ArrayPattern} = require('./array-pattern');
15
15
  const {AssignmentPattern} = require('./assignment-pattern');
16
16
  const {RestElement} = require('./rest-element');
17
17
  const {SpreadElement} = require('./spread-element');
18
+ const {SequenceExpression} = require('./sequence-expression');
18
19
 
19
20
  module.exports = {
20
21
  ...functions,
@@ -31,6 +32,7 @@ module.exports = {
31
32
  ObjectPattern,
32
33
  RestElement,
33
34
  SpreadElement,
35
+ SequenceExpression,
34
36
  BinaryExpression(path, {traverse, write}) {
35
37
  traverse(path.get('left'));
36
38
  write(` ${path.node.operator} `);
@@ -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
+
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const {entries} = Object;
4
+
5
+ module.exports.SequenceExpression = (path, {maybe, traverse}) => {
6
+ const expressions = path.get('expressions');
7
+ const n = expressions.length - 1;
8
+
9
+ for (const [index, expression] of entries(expressions)) {
10
+ traverse(expression);
11
+ maybe.write(index < n, ',');
12
+ maybe.write(index < n, ' ');
13
+ }
14
+ };
15
+
@@ -3,10 +3,35 @@
3
3
  module.exports.UnaryExpression = unaryExpressions;
4
4
  module.exports.UpdateExpression = unaryExpressions;
5
5
  module.exports.AwaitExpression = (path, {write, traverse}) => {
6
- write('await ');
7
- traverse(path.get('argument'));
6
+ printUnary(path, 'await', {
7
+ write,
8
+ traverse,
9
+ });
10
+ };
11
+ module.exports.YieldExpression = (path, {write, traverse}) => {
12
+ printUnary(path, 'yield', {
13
+ write,
14
+ traverse,
15
+ });
16
+ };
17
+
18
+ module.exports.ThrowStatement = (path, {write, indent, traverse}) => {
19
+ indent();
20
+
21
+ printUnary(path, 'throw', {
22
+ write,
23
+ traverse,
24
+ });
25
+
26
+ write(';');
27
+ write.newline();
8
28
  };
9
29
 
30
+ function printUnary(path, name, {write, traverse}) {
31
+ write(`${name} `);
32
+ traverse(path.get('argument'));
33
+ }
34
+
10
35
  const isWord = (a) => /delete|typeof/.test(a);
11
36
 
12
37
  function unaryExpressions(path, {traverse, write, maybe}) {
@@ -21,3 +46,4 @@ function unaryExpressions(path, {traverse, write, maybe}) {
21
46
  if (!prefix)
22
47
  write(operator);
23
48
  }
49
+
@@ -5,8 +5,9 @@ const {TemplateLiteral} = require('./template-literal');
5
5
  module.exports = {
6
6
  TemplateLiteral,
7
7
  NumericLiteral(path, {write}) {
8
- const {extra, raw} = path.node;
9
- write(raw || extra.raw);
8
+ const {raw} = path.node;
9
+ write(raw);
10
+ //write(raw || extra.raw);
10
11
  },
11
12
  BooleanLiteral(path, {write}) {
12
13
  write(path.node.value);
@@ -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.4.2",
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",