@putout/printer 1.6.2 → 1.6.4

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.21, v1.6.4
2
+
3
+ feature:
4
+ - aa1590c @putout/printer: add support of ForStatement
5
+
6
+ 2023.03.20, v1.6.3
7
+
8
+ feature:
9
+ - 052b966 @putout/printer: write -> print
10
+
1
11
  2023.03.20, v1.6.2
2
12
 
3
13
  feature:
@@ -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);
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ module.exports.ForStatement = (path, {print, maybe, indent}) => {
4
+ const {
5
+ test,
6
+ update,
7
+ body,
8
+ } = path.node;
9
+
10
+ print('for (');
11
+ print('__init');
12
+ print(';');
13
+ maybe.print(test, ' ');
14
+ print('__test');
15
+ print(';');
16
+ maybe.print(update, ' ');
17
+ print('__update');
18
+ print(')');
19
+
20
+ if (body.body) {
21
+ print(' ');
22
+ print('__body');
23
+ print.newline();
24
+
25
+ return;
26
+ }
27
+
28
+ if (!body.body) {
29
+ print.newline();
30
+ indent.inc();
31
+ print('__body');
32
+ indent.dec();
33
+ print.newline();
34
+ }
35
+ };
36
+
@@ -8,12 +8,14 @@ const {BlockStatement} = require('./block-statement');
8
8
  const {ReturnStatement} = require('./return-statement');
9
9
  const TryStatements = require('./try-statements');
10
10
  const {DebuggerStatement} = require('./debugger-statement');
11
+ const {ForStatement} = require('./for-statement');
11
12
 
12
13
  module.exports = {
13
14
  BlockStatement,
14
15
  ExpressionStatement,
15
16
  VariableDeclaration,
16
17
  IfStatement,
18
+ ForStatement,
17
19
  ForOfStatement,
18
20
  ReturnStatement,
19
21
  DebuggerStatement,
@@ -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');
@@ -43,7 +45,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
43
45
  });
44
46
  };
45
47
 
46
- const maybeWrite = (a, b) => a && write(b);
47
48
  const maybeIndent = (a) => a && indent();
48
49
  const maybeIndentInc = (a) => a && indent.inc();
49
50
  const maybeIndentDec = (a) => a && indent.dec();
@@ -94,7 +95,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
94
95
  });
95
96
 
96
97
  const maybe = {
97
- write: maybeWrite,
98
98
  indent: maybeIndent,
99
99
  markBefore: maybeMarkBefore,
100
100
  markAfter: maybeMarkAfter,
@@ -110,7 +110,6 @@ module.exports.tokenize = (ast, overrides = {}) => {
110
110
  decIndent,
111
111
  indent,
112
112
  write,
113
- maybeWrite,
114
113
  debug,
115
114
  maybeIndent,
116
115
  traverse,
@@ -178,11 +177,12 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
178
177
  if (maybeLine === path)
179
178
  return null;
180
179
 
181
- if (!isString(maybeLine))
182
- return traverse(maybeLine);
183
-
184
- if (maybeLine.startsWith(GET))
180
+ if (isString(maybeLine) && maybeLine.startsWith(GET))
185
181
  return traverse(get(path, maybeLine));
186
182
 
183
+ if (isObject(maybeLine))
184
+ return traverse(maybeLine);
185
+
187
186
  return write(maybeLine);
188
187
  };
188
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/printer",
3
- "version": "1.6.2",
3
+ "version": "1.6.4",
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",