@putout/printer 1.6.1 → 1.6.3

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.20, v1.6.3
2
+
3
+ feature:
4
+ - 052b966 @putout/printer: write -> print
5
+
6
+ 2023.03.20, v1.6.2
7
+
8
+ feature:
9
+ - eb7ba9c @putout/printer: add format
10
+
1
11
  2023.03.20, v1.6.1
2
12
 
3
13
  fix:
package/README.md CHANGED
@@ -48,7 +48,9 @@ Here is how you can override `AssignmentPattern`:
48
48
  const ast = parse('const {a = 5} = b');
49
49
 
50
50
  print(ast, {
51
- indent: ' ',
51
+ format: {
52
+ indent: ' ',
53
+ },
52
54
  visitors: {
53
55
  AssignmentPattern(path, {print}) {
54
56
  print(' /* [hello world] */= ');
@@ -1,28 +1,25 @@
1
1
  'use strict';
2
2
 
3
- module.exports.MemberExpression = (path, {traverse, write}) => {
3
+ module.exports.MemberExpression = (path, {print}) => {
4
4
  const {computed} = path.node;
5
- const propertyPath = path.get('property');
6
5
 
7
- traverse(path.get('object'));
6
+ print('__object');
8
7
 
9
8
  if (computed) {
10
- write('[');
11
- traverse(propertyPath);
12
- write(']');
9
+ print('[');
10
+ print('__property');
11
+ print(']');
13
12
 
14
13
  return;
15
14
  }
16
15
 
17
- write('.');
18
- traverse(propertyPath);
16
+ print('.');
17
+ print('__property');
19
18
  };
20
19
 
21
- module.exports.OptionalMemberExpression = (path, {traverse, write}) => {
22
- const propertyPath = path.get('property');
23
-
24
- traverse(path.get('object'));
25
-
26
- write('?.');
27
- traverse(propertyPath);
20
+ module.exports.OptionalMemberExpression = (path, {print}) => {
21
+ print('__object');
22
+ print('?.');
23
+ print('__property');
28
24
  };
25
+
@@ -2,21 +2,21 @@
2
2
 
3
3
  const {entries} = Object;
4
4
 
5
- module.exports.NewExpression = (path, {write, maybeWrite, traverse, indent}) => {
5
+ module.exports.NewExpression = (path, {indent, print, maybe}) => {
6
6
  indent();
7
- write('new ');
8
- traverse(path.get('callee'));
7
+ print('new ');
8
+ print('__callee');
9
9
 
10
10
  const args = path.get('arguments');
11
- write('(');
11
+ print('(');
12
12
 
13
13
  const n = args.length - 1;
14
14
 
15
15
  for (const [i, arg] of entries(args)) {
16
- traverse(arg);
17
- maybeWrite(i < n, ', ');
16
+ print(arg);
17
+ maybe.print(i < n, ', ');
18
18
  }
19
19
 
20
- write(')');
20
+ print(')');
21
21
  };
22
22
 
@@ -9,7 +9,7 @@ const isForOf = (path) => {
9
9
  return path.parentPath?.parentPath?.isForOfStatement();
10
10
  };
11
11
 
12
- module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
12
+ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
13
13
  indent.inc();
14
14
 
15
15
  const properties = path.get('properties');
@@ -17,19 +17,19 @@ module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
17
17
  const parens = isBodyOfArrow(path);
18
18
  const manyLines = !isOneLine(path);
19
19
 
20
- maybe.write(parens, '(');
21
- write('{');
20
+ maybe.print(parens, '(');
21
+ print('{');
22
22
 
23
- maybe.write(manyLines, '\n');
23
+ maybe.print(manyLines, '\n');
24
24
 
25
25
  for (const property of properties) {
26
26
  if (property.isSpreadElement()) {
27
27
  maybe.indent(length > 1);
28
- traverse(property);
28
+ print(property);
29
29
 
30
30
  if (length > 1) {
31
- write(',');
32
- write.newline();
31
+ print(',');
32
+ print.newline();
33
33
  }
34
34
 
35
35
  continue;
@@ -40,27 +40,27 @@ module.exports.ObjectExpression = (path, {traverse, write, maybe, indent}) => {
40
40
  maybe.indent(manyLines);
41
41
 
42
42
  if (property.isObjectMethod()) {
43
- traverse(property);
43
+ print(property);
44
44
  continue;
45
45
  }
46
46
 
47
- maybe.write(computed, '[');
48
- traverse(property.get('key'));
49
- maybe.write(computed, ']');
47
+ maybe.print(computed, '[');
48
+ print(property.get('key'));
49
+ maybe.print(computed, ']');
50
50
 
51
51
  if (!shorthand) {
52
- write(': ');
53
- traverse(property.get('value'));
52
+ print(': ');
53
+ print(property.get('value'));
54
54
  }
55
55
 
56
- maybe.write(manyLines, ',\n');
56
+ maybe.print(manyLines, ',\n');
57
57
  }
58
58
 
59
59
  indent.dec();
60
60
 
61
61
  maybe.indent(manyLines);
62
- write('}');
63
- maybe.write(parens, ')');
62
+ print('}');
63
+ maybe.print(parens, ')');
64
64
  };
65
65
  function isOneLine(path) {
66
66
  const isCall = path.parentPath.isCallExpression();
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- module.exports.RestElement = (path, {write, traverse}) => {
4
- write('...');
5
- traverse(path.get('argument'));
3
+ module.exports.RestElement = (path, {print}) => {
4
+ print('...');
5
+ print('__argument');
6
6
  };
@@ -2,14 +2,14 @@
2
2
 
3
3
  const {entries} = Object;
4
4
 
5
- module.exports.SequenceExpression = (path, {maybe, traverse}) => {
5
+ module.exports.SequenceExpression = (path, {maybe, print}) => {
6
6
  const expressions = path.get('expressions');
7
7
  const n = expressions.length - 1;
8
8
 
9
9
  for (const [index, expression] of entries(expressions)) {
10
- traverse(expression);
11
- maybe.write(index < n, ',');
12
- maybe.write(index < n, ' ');
10
+ print(expression);
11
+ maybe.print(index < n, ',');
12
+ maybe.print(index < n, ' ');
13
13
  }
14
14
  };
15
15
 
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
- module.exports.SpreadElement = (path, {write, traverse}) => {
4
- write('...');
5
- traverse(path.get('argument'));
3
+ module.exports.SpreadElement = (path, {print}) => {
4
+ print('...');
5
+ print('__argument');
6
6
  };
7
+
@@ -1,49 +1,46 @@
1
1
  'use strict';
2
2
 
3
- module.exports.UnaryExpression = unaryExpressions;
4
- module.exports.UpdateExpression = unaryExpressions;
5
- module.exports.AwaitExpression = (path, {write, traverse}) => {
3
+ module.exports.UnaryExpression = unaryExpression;
4
+ module.exports.UpdateExpression = unaryExpression;
5
+ module.exports.AwaitExpression = (path, {print}) => {
6
6
  printUnary(path, 'await', {
7
- write,
8
- traverse,
7
+ print,
9
8
  });
10
9
  };
11
- module.exports.YieldExpression = (path, {write, traverse}) => {
10
+ module.exports.YieldExpression = (path, {print}) => {
12
11
  printUnary(path, 'yield', {
13
- write,
14
- traverse,
12
+ print,
15
13
  });
16
14
  };
17
15
 
18
- module.exports.ThrowStatement = (path, {write, indent, traverse}) => {
16
+ module.exports.ThrowStatement = (path, {print, indent}) => {
19
17
  indent();
20
18
 
21
19
  printUnary(path, 'throw', {
22
- write,
23
- traverse,
20
+ print,
24
21
  });
25
22
 
26
- write(';');
27
- write.newline();
23
+ print(';');
24
+ print.newline();
28
25
  };
29
26
 
30
- function printUnary(path, name, {write, traverse}) {
31
- write(`${name} `);
32
- traverse(path.get('argument'));
27
+ function printUnary(path, name, {print}) {
28
+ print(`${name} `);
29
+ print('__argument');
33
30
  }
34
31
 
35
32
  const isWord = (a) => /delete|typeof/.test(a);
36
33
 
37
- function unaryExpressions(path, {traverse, write, maybe}) {
34
+ function unaryExpression(path, {print, maybe}) {
38
35
  const {prefix, operator} = path.node;
39
36
 
40
37
  if (prefix)
41
- write(operator);
38
+ print(operator);
42
39
 
43
- maybe.write(isWord(operator), ' ');
44
- traverse(path.get('argument'));
40
+ maybe.print(isWord(operator), ' ');
41
+ print('__argument');
45
42
 
46
43
  if (!prefix)
47
- write(operator);
44
+ print(operator);
48
45
  }
49
46
 
@@ -4,17 +4,16 @@ const {TemplateLiteral} = require('./template-literal');
4
4
 
5
5
  module.exports = {
6
6
  TemplateLiteral,
7
- NumericLiteral(path, {write}) {
8
- const {raw} = path.node;
9
- write(raw);
10
- //write(raw || extra.raw);
7
+ NumericLiteral(path, {print}) {
8
+ const {raw, extra} = path.node;
9
+ print(raw || extra.raw);
11
10
  },
12
- BooleanLiteral(path, {write}) {
13
- write(path.node.value);
11
+ BooleanLiteral(path, {print}) {
12
+ print(path.node.value);
14
13
  },
15
- StringLiteral(path, {write}) {
14
+ StringLiteral(path, {print}) {
16
15
  const {value} = path.node;
17
- write(`'${value}'`);
16
+ print(`'${value}'`);
18
17
  },
19
18
  Identifier(path, {write}) {
20
19
  write(path.node.name);
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const isObject = (a) => a && typeof a === 'object';
4
+
3
5
  const babelTraverse = require('@babel/traverse').default;
4
6
  const expressions = require('./expressions');
5
7
  const statements = require('./statements');
@@ -25,7 +27,15 @@ const traversers = {
25
27
  const GET = '__';
26
28
  const get = (path, command) => path.get(command.replace(GET, ''));
27
29
 
30
+ function initFormat(format) {
31
+ return {
32
+ ...format,
33
+ indent: ' ',
34
+ };
35
+ }
36
+
28
37
  module.exports.tokenize = (ast, overrides = {}) => {
38
+ const format = initFormat(overrides.format);
29
39
  const tokens = [];
30
40
  const debug = createDebug(tokens);
31
41
  const write = (value) => {
@@ -35,7 +45,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
35
45
  });
36
46
  };
37
47
 
38
- const maybeWrite = (a, b) => a && write(b);
39
48
  const maybeIndent = (a) => a && indent();
40
49
  const maybeIndentInc = (a) => a && indent.inc();
41
50
  const maybeIndentDec = (a) => a && indent.dec();
@@ -48,7 +57,7 @@ module.exports.tokenize = (ast, overrides = {}) => {
48
57
  const indent = () => {
49
58
  tokens.push({
50
59
  type: TYPES.INDENT,
51
- value: printIndent(i),
60
+ value: printIndent(i, format.indent),
52
61
  });
53
62
  };
54
63
 
@@ -60,14 +69,14 @@ module.exports.tokenize = (ast, overrides = {}) => {
60
69
  const linebreak = () => {
61
70
  tokens.push({
62
71
  type: TYPES.NEWLINE,
63
- value: `${printIndent(i)}\n`,
72
+ value: `${printIndent(i, format.indent)}\n`,
64
73
  });
65
74
  };
66
75
 
67
76
  const breakline = () => {
68
77
  tokens.push({
69
78
  type: TYPES.NEWLINE,
70
- value: `\n${printIndent(i)}`,
79
+ value: `\n${printIndent(i, format.indent)}`,
71
80
  });
72
81
  };
73
82
 
@@ -86,7 +95,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
86
95
  });
87
96
 
88
97
  const maybe = {
89
- write: maybeWrite,
90
98
  indent: maybeIndent,
91
99
  markBefore: maybeMarkBefore,
92
100
  markAfter: maybeMarkAfter,
@@ -102,7 +110,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
102
110
  decIndent,
103
111
  indent,
104
112
  write,
105
- maybeWrite,
106
113
  debug,
107
114
  maybeIndent,
108
115
  traverse,
@@ -155,12 +162,12 @@ module.exports.tokenize = (ast, overrides = {}) => {
155
162
  return tokens;
156
163
  };
157
164
 
158
- function printIndent(i) {
165
+ function printIndent(i, indent) {
159
166
  let result = '';
160
167
  ++i;
161
168
 
162
169
  while (--i) {
163
- result += ' ';
170
+ result += indent;
164
171
  }
165
172
 
166
173
  return result;
@@ -170,11 +177,12 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
170
177
  if (maybeLine === path)
171
178
  return null;
172
179
 
173
- if (!isString(maybeLine))
174
- return traverse(maybeLine);
175
-
176
- if (maybeLine.startsWith(GET))
180
+ if (isString(maybeLine) && maybeLine.startsWith(GET))
177
181
  return traverse(get(path, maybeLine));
178
182
 
183
+ if (isObject(maybeLine))
184
+ return traverse(maybeLine);
185
+
179
186
  return write(maybeLine);
180
187
  };
188
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
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",