@putout/printer 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) coderaiser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Printer [![NPM version][NPMIMGURL]][NPMURL]
2
+
3
+ [NPMIMGURL]: https://img.shields.io/npm/v/@putout/printer.svg?style=flat&longCache=true
4
+ [NPMURL]: https://npmjs.org/package/@putout/printer "npm"
5
+
6
+ **EasyPrint** prints [**Babel AST**](https://github.com/coderaiser/estree-to-babel) to readable **JavaScript**.
7
+
8
+ - ☝️ Similar to **Recast**, but simpler and easier in maintenance, since it supports only **Babel**.
9
+ - ☝️ As opinionated as **Prettier**, but has more user-friendly output and works directly with **AST**.
10
+ - ☝️ Like **ESLint** but without any configuration and plugins 🤷‍, also works directly with **Babel AST** only.
11
+
12
+ ## Install
13
+
14
+ ```
15
+ npm i @putout/printer
16
+ ```
17
+
18
+ ## API
19
+
20
+ ```js
21
+ const {print} = require('@putout/printer');
22
+ const {parse} = require('@babel/parser');
23
+
24
+ const ast = parse('const a = (b, c) => {const d = 5; return a;}');
25
+ print(ast);
26
+ // returns
27
+ `
28
+ const a = (b, c) => {
29
+ const d = 5;
30
+ return a;
31
+ };
32
+ `;
33
+ ```
34
+
35
+ ## License
36
+
37
+ MIT
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ module.exports.ArrayPattern = (path, {write, traverse}) => {
4
+ write('[');
5
+
6
+ for (const element of path.get('elements')) {
7
+ traverse(element);
8
+ }
9
+
10
+ write(']');
11
+ };
12
+
13
+ module.exports.ArrayExpression = (path, {write, indent, incIndent, decIndent, traverse}) => {
14
+ write('[');
15
+ incIndent();
16
+
17
+ const elements = path.get('elements');
18
+
19
+ for (const element of elements) {
20
+ write('\n');
21
+ indent();
22
+ traverse(element);
23
+ write(',\n');
24
+ }
25
+
26
+ decIndent();
27
+
28
+ if (elements.length)
29
+ indent();
30
+
31
+ write(']');
32
+ };
33
+
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ module.exports.AssignmentExpression = (path, {traverse, write}) => {
4
+ if (shouldAddNewLine(path))
5
+ write('\n');
6
+
7
+ traverse(path.get('left'));
8
+ write(` ${path.node.operator} `);
9
+ traverse(path.get('right'));
10
+ };
11
+
12
+ function shouldAddNewLine({parentPath}) {
13
+ const prevPath = parentPath.getPrevSibling();
14
+ const prevPrevPath = prevPath.getPrevSibling();
15
+
16
+ const twoPrev = prevPath.node && prevPrevPath.node;
17
+
18
+ if (!twoPrev)
19
+ return false;
20
+
21
+ if (prevPath.isExpressionStatement() && prevPath.get('expression').isAssignmentExpression())
22
+ return false;
23
+
24
+ return true;
25
+ }
@@ -0,0 +1,74 @@
1
+ 'use strict';
2
+
3
+ const {entries} = Object;
4
+
5
+ module.exports.CallExpression = (path, {traverse, indent, write, writeEmptyLine, incIndent, decIndent, maybeWrite}) => {
6
+ const isParentCall = toLong(path) && path.parentPath.isCallExpression();
7
+
8
+ if (shouldAddNewLine(path))
9
+ writeEmptyLine();
10
+
11
+ traverse(path.get('callee'));
12
+ write('(');
13
+
14
+ const args = path.get('arguments');
15
+ const n = args.length - 1;
16
+
17
+ if (isParentCall)
18
+ incIndent();
19
+
20
+ for (const [i, arg] of entries(args)) {
21
+ if (isParentCall) {
22
+ write('\n');
23
+ indent();
24
+ }
25
+
26
+ traverse(arg);
27
+
28
+ if (isParentCall) {
29
+ write(',');
30
+ continue;
31
+ }
32
+
33
+ maybeWrite(i < n, ', ');
34
+ }
35
+
36
+ if (isParentCall) {
37
+ decIndent();
38
+ writeEmptyLine();
39
+ }
40
+
41
+ write(')');
42
+ };
43
+
44
+ function shouldAddNewLine({parentPath}) {
45
+ if (!parentPath.isExpressionStatement())
46
+ return false;
47
+
48
+ const prevPath = parentPath.getPrevSibling();
49
+ const prevPrevPath = prevPath.getPrevSibling();
50
+
51
+ if (prevPath.isStatement() && !prevPath.isExpressionStatement() && !prevPath.isVariableDeclaration())
52
+ return true;
53
+
54
+ const twoPrev = prevPath.node && prevPrevPath.node;
55
+
56
+ if (!twoPrev)
57
+ return false;
58
+
59
+ if (prevPath.isExpressionStatement() && prevPath.get('expression').isCallExpression())
60
+ return false;
61
+
62
+ return true;
63
+ }
64
+
65
+ function toLong(path) {
66
+ const args = path.get('arguments');
67
+
68
+ for (const arg of args) {
69
+ if (arg.isIdentifier() && arg.node.name.length > 5)
70
+ return true;
71
+ }
72
+
73
+ return false;
74
+ }
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ const {entries} = Object;
4
+
5
+ const isFirst = (path) => path.node === path.parentPath.node.body[0];
6
+
7
+ module.exports.ClassDeclaration = (path, {write, maybeWrite, writeEmptyLine, indent, incIndent, decIndent, traverse}) => {
8
+ if (!isFirst(path)) {
9
+ writeEmptyLine();
10
+ }
11
+
12
+ indent();
13
+ write('class ');
14
+ traverse(path.get('id'));
15
+ write(' {\n');
16
+ incIndent();
17
+
18
+ const body = path.get('body.body');
19
+ const n = body.length - 1;
20
+
21
+ for (const [i, item] of entries(body)) {
22
+ indent();
23
+ traverse(item);
24
+ maybeWrite(i < n, '\n');
25
+ }
26
+
27
+ decIndent();
28
+ write('}');
29
+ };
30
+
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const isFirst = (path) => path.node === path.parentPath.node.body[0];
4
+
5
+ module.exports.ArrowFunctionExpression = (path, {write, traverse}) => {
6
+ write('(');
7
+
8
+ const params = path.get('params');
9
+ const n = params.length;
10
+
11
+ for (let i = 0; i < n; i++) {
12
+ traverse(params[i]);
13
+
14
+ if (i < n - 1)
15
+ write(', ');
16
+ }
17
+
18
+ write(') => ');
19
+ traverse(path.get('body'));
20
+ };
21
+
22
+ module.exports.ObjectMethod = (path, {write, traverse}) => {
23
+ traverse(path.get('key'));
24
+ write('(');
25
+
26
+ const params = path.get('params');
27
+ const n = params.length - 1;
28
+
29
+ for (let i = 0; i <= n; i++) {
30
+ traverse(params[i]);
31
+
32
+ if (i < n)
33
+ write(', ');
34
+ }
35
+
36
+ write(') ');
37
+ traverse(path.get('body'));
38
+ };
39
+
40
+ module.exports.FunctionDeclaration = (path, {write, traverse}) => {
41
+ if (!isFirst(path))
42
+ write('\n');
43
+
44
+ write('function ');
45
+ traverse(path.get('id'));
46
+ write('(');
47
+
48
+ const params = path.get('params');
49
+ const n = params.length - 1;
50
+
51
+ for (let i = 0; i <= n; i++) {
52
+ traverse(params[i]);
53
+
54
+ if (i < n)
55
+ write(', ');
56
+ }
57
+
58
+ write(') ');
59
+ traverse(path.get('body'));
60
+ };
61
+
62
+ module.exports.ClassMethod = (path, {write, traverse}) => {
63
+ traverse(path.get('key'));
64
+ write('(');
65
+
66
+ const params = path.get('params');
67
+ const n = params.length;
68
+
69
+ for (let i = 0; i < n; i++) {
70
+ traverse(params[i]);
71
+
72
+ if (i < n - 1)
73
+ write(', ');
74
+ }
75
+
76
+ write(') ');
77
+ traverse(path.get('body'));
78
+ };
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const functions = require('./functions');
4
+ const arrays = require('./arrays');
5
+ const unaryExpressions = require('./unary-expressions');
6
+ const memberExpressions = require('./member-expressions');
7
+ const {ClassDeclaration} = require('./class-declaration');
8
+ const {CallExpression} = require('./call-expression');
9
+ const {NewExpression} = require('./new-expression');
10
+ const {ObjectExpression} = require('./object-expression');
11
+ const {ObjectPattern} = require('./object-pattern');
12
+ const {AssignmentExpression} = require('./assignment-expression');
13
+
14
+ module.exports = {
15
+ ...functions,
16
+ ...unaryExpressions,
17
+ ...arrays,
18
+ ...memberExpressions,
19
+ AssignmentExpression,
20
+ CallExpression,
21
+ ClassDeclaration,
22
+ NewExpression,
23
+ ObjectExpression,
24
+ ObjectPattern,
25
+ BinaryExpression(path, {traverse, write}) {
26
+ traverse(path.get('left'));
27
+ write(` ${path.node.operator} `);
28
+ traverse(path.get('right'));
29
+ },
30
+ LogicalExpression(path, {traverse, write}) {
31
+ traverse(path.get('left'));
32
+ write(` ${path.node.operator} `);
33
+ traverse(path.get('right'));
34
+ },
35
+ };
36
+
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ module.exports.MemberExpression = (path, {traverse, write}) => {
4
+ const {computed} = path.node;
5
+ const propertyPath = path.get('property');
6
+
7
+ traverse(path.get('object'));
8
+
9
+ if (computed) {
10
+ write('[');
11
+ traverse(propertyPath);
12
+ write(']');
13
+
14
+ return;
15
+ }
16
+
17
+ write('.');
18
+ traverse(propertyPath);
19
+ };
20
+
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);
28
+ };
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const {entries} = Object;
4
+
5
+ module.exports.NewExpression = (path, {write, maybeWrite, traverse, indent}) => {
6
+ indent();
7
+ write('new ');
8
+ traverse(path.get('callee'));
9
+
10
+ const args = path.get('arguments');
11
+ write('(');
12
+
13
+ const n = args.length - 1;
14
+
15
+ for (const [i, arg] of entries(args)) {
16
+ traverse(arg);
17
+ maybeWrite(i < n, ', ');
18
+ }
19
+
20
+ write(')');
21
+ };
22
+
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
4
+
5
+ module.exports.ObjectExpression = (path, {traverse, write, maybeWrite, maybeIndent, incIndent, decIndent}) => {
6
+ incIndent();
7
+
8
+ const properties = path.get('properties');
9
+ const parens = isBodyOfArrow(path);
10
+ const isCall = path.parentPath.isCallExpression();
11
+ const isOneLine = isCall && properties.length < 2;
12
+
13
+ maybeWrite(parens, '(');
14
+ write('{');
15
+
16
+ maybeWrite(!isOneLine, '\n');
17
+
18
+ for (const property of properties) {
19
+ const {shorthand, computed} = property.node;
20
+
21
+ maybeIndent(!isOneLine);
22
+
23
+ if (property.isObjectMethod()) {
24
+ traverse(property);
25
+ continue;
26
+ }
27
+
28
+ maybeWrite(computed, '[');
29
+ traverse(property.get('key'));
30
+ maybeWrite(computed, ']');
31
+
32
+ if (!shorthand) {
33
+ write(': ');
34
+ traverse(property.get('value'));
35
+ }
36
+
37
+ maybeWrite(!isOneLine, ',\n');
38
+ }
39
+
40
+ decIndent();
41
+ maybeIndent(!isOneLine);
42
+ write('}');
43
+ maybeWrite(parens, ')');
44
+ };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ const {entries} = Object;
4
+ const isForOf = (path) => path.parentPath?.parentPath?.parentPath?.isForOfStatement();
5
+
6
+ module.exports.ObjectPattern = (path, {traverse, maybeIndent, write, maybeWrite, incIndent, decIndent}) => {
7
+ incIndent();
8
+
9
+ write('{');
10
+
11
+ const properties = path.get('properties');
12
+ const n = properties.length - 1;
13
+
14
+ const is = !isForOf(path) && !path.parentPath.isFunction() && properties.length > 1 && checkLength(properties);
15
+ maybeWrite(is, '\n');
16
+
17
+ for (const [i, property] of entries(properties)) {
18
+ const {shorthand} = property.node;
19
+ maybeIndent(is);
20
+ traverse(property.get('key'));
21
+
22
+ if (!shorthand) {
23
+ write(': ');
24
+ traverse(property.get('value'));
25
+ }
26
+
27
+ if (is) {
28
+ write(',\n');
29
+ continue;
30
+ }
31
+
32
+ if (i < n)
33
+ write(', ');
34
+ }
35
+
36
+ decIndent();
37
+ maybeIndent(is);
38
+ write('}');
39
+ };
40
+
41
+ function checkLength(properties) {
42
+ for (const prop of properties) {
43
+ if (prop.node.value.name.length > 4)
44
+ return true;
45
+ }
46
+
47
+ return false;
48
+ }
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ module.exports.UnaryExpression = unaryExpressions;
4
+ module.exports.UpdateExpression = unaryExpressions;
5
+
6
+ function unaryExpressions(path, {traverse, write}) {
7
+ const {prefix, operator} = path.node;
8
+
9
+ if (prefix)
10
+ write(operator);
11
+
12
+ traverse(path.get('argument'));
13
+
14
+ if (!prefix)
15
+ write(operator);
16
+ }
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ const {TemplateLiteral} = require('./template-literal');
4
+
5
+ module.exports = {
6
+ TemplateLiteral,
7
+ NumericLiteral(path, {write}) {
8
+ write(path.node.value);
9
+ },
10
+ BooleanLiteral(path, {write}) {
11
+ write(path.node.value);
12
+ },
13
+ StringLiteral(path, {write}) {
14
+ const {value} = path.node;
15
+ write(`'${value}'`);
16
+ },
17
+ Identifier(path, {write}) {
18
+ write(path.node.name);
19
+ },
20
+ };
21
+
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ module.exports.TemplateLiteral = (path, {write, traverse}) => {
4
+ write('`');
5
+
6
+ let i = 0;
7
+ const expressions = path.get('expressions');
8
+
9
+ for (const element of path.node.quasis) {
10
+ write(element.value.raw);
11
+ const exp = expressions[i++];
12
+
13
+ if (exp) {
14
+ write('${');
15
+ traverse(exp);
16
+ write('}');
17
+ }
18
+ }
19
+
20
+ write('`');
21
+ };
22
+
package/lib/printer.js ADDED
@@ -0,0 +1,86 @@
1
+ 'use strict';
2
+
3
+ const babelTraverse = require('@babel/traverse').default;
4
+ const expressions = require('./expressions');
5
+ const statements = require('./statements');
6
+ const literals = require('./literals');
7
+ const toSnakeCase = require('just-snake-case');
8
+
9
+ const traversers = {
10
+ ...expressions,
11
+ ...statements,
12
+ ...literals,
13
+ };
14
+
15
+ const {DEBUG} = process.env;
16
+
17
+ module.exports.print = function print(ast) {
18
+ const tokens = [];
19
+ const write = (a) => {
20
+ tokens.push(a);
21
+ };
22
+
23
+ const indent = () => {
24
+ write(printIndent(i));
25
+ };
26
+
27
+ const debug = (a) => maybeWrite(DEBUG, `/*__${toSnakeCase(a)}*/`);
28
+ const maybeWrite = (a, b) => a && write(b);
29
+ const maybeIndent = (a) => a && indent();
30
+
31
+ let i = 0;
32
+ const incIndent = () => ++i;
33
+ const decIndent = () => --i;
34
+
35
+ const writeEmptyLine = () => {
36
+ write('\n');
37
+ indent();
38
+ };
39
+
40
+ const printer = {
41
+ incIndent,
42
+ decIndent,
43
+ indent,
44
+ write,
45
+ maybeWrite,
46
+ debug,
47
+ maybeIndent,
48
+ traverse,
49
+ writeEmptyLine,
50
+ };
51
+
52
+ babelTraverse(ast, {
53
+ Program(path) {
54
+ traverse(path);
55
+ path.stop();
56
+ },
57
+ });
58
+
59
+ function traverse(path) {
60
+ const {type} = path;
61
+ const currentTraverse = traversers[type];
62
+
63
+ if (!path.node)
64
+ return;
65
+
66
+ if (!currentTraverse)
67
+ throw Error(`Node type '${type}' is not supported yet: '${path}'`);
68
+
69
+ currentTraverse(path, printer);
70
+ debug(path.type);
71
+ }
72
+
73
+ return tokens.join('');
74
+ };
75
+
76
+ function printIndent(i) {
77
+ let result = '';
78
+ ++i;
79
+
80
+ while (--i) {
81
+ result += ' ';
82
+ }
83
+
84
+ return result;
85
+ }
86
+
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ const isFirstStatement = (path) => path.get('body.0')?.isStatement();
4
+
5
+ module.exports.BlockStatement = (path, {write, indent, incIndent, decIndent, traverse}) => {
6
+ const body = path.get('body');
7
+
8
+ incIndent();
9
+ write('{');
10
+
11
+ if (body.length > 1 || isFirstStatement(path))
12
+ write('\n');
13
+
14
+ body.forEach(traverse);
15
+ decIndent();
16
+
17
+ if (body.length)
18
+ indent();
19
+
20
+ write('}');
21
+
22
+ if (path.parentPath.isObjectMethod()) {
23
+ write(',');
24
+ }
25
+
26
+ if (!/FunctionExpression/.test(path.parentPath.type))
27
+ write('\n');
28
+ };
29
+
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ module.exports.ExpressionStatement = (path, {write, indent, traverse}) => {
4
+ if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path)) {
5
+ write('\n');
6
+ indent();
7
+ }
8
+
9
+ const expressionPath = path.get('expression');
10
+
11
+ indent();
12
+ traverse(expressionPath);
13
+ write(';\n');
14
+
15
+ if (isStrictMode(path))
16
+ write('\n');
17
+ };
18
+
19
+ function isCoupleLinesExpression(path) {
20
+ const start = path.node.loc?.start.line;
21
+ const end = path.node.loc?.end.line;
22
+
23
+ return end > start;
24
+ }
25
+
26
+ function isStrictMode(path) {
27
+ const expressionPath = path.get('expression');
28
+
29
+ if (!expressionPath.isStringLiteral())
30
+ return false;
31
+
32
+ const {value} = path.node.expression;
33
+
34
+ return value === 'use strict';
35
+ }
36
+
37
+ function isFirst(path) {
38
+ return path.node === path.parentPath.node.body[0];
39
+ }
40
+
41
+ function shouldAddNewLine(path) {
42
+ const prev = path.getPrevSibling();
43
+
44
+ if (prev.isVariableDeclaration())
45
+ return false;
46
+
47
+ if (prev.isIfStatement())
48
+ return false;
49
+
50
+ if (isStrictMode(prev))
51
+ return false;
52
+
53
+ return true;
54
+ }
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ module.exports.ForOfStatement = (path, {write, indent, incIndent, traverse}) => {
4
+ if (!isFirst(path)) {
5
+ indent();
6
+ write('\n');
7
+ }
8
+
9
+ indent();
10
+ write('for (');
11
+ traverse(path.get('left'));
12
+ write(' of ');
13
+ traverse(path.get('right'));
14
+ write(')');
15
+
16
+ const bodyPath = path.get('body');
17
+
18
+ if (bodyPath.isExpressionStatement()) {
19
+ incIndent();
20
+ write('\n');
21
+ traverse(bodyPath);
22
+
23
+ return;
24
+ }
25
+
26
+ write(' ');
27
+ traverse(bodyPath);
28
+ };
29
+ function isFirst(path) {
30
+ return path.node === path.parentPath.node.body[0];
31
+ }
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ module.exports.IfStatement = (path, {write, indent, traverse, incIndent, decIndent}) => {
4
+ if (!isFirst(path)) {
5
+ indent();
6
+ write('\n');
7
+ }
8
+
9
+ indent();
10
+ write('if (');
11
+ traverse(path.get('test'));
12
+ write(')');
13
+
14
+ const consequent = path.get('consequent');
15
+
16
+ if (consequent.isBlockStatement()) {
17
+ write(' ');
18
+ traverse(consequent);
19
+
20
+ return;
21
+ }
22
+
23
+ write('\n');
24
+ incIndent();
25
+ traverse(consequent);
26
+ decIndent();
27
+
28
+ const next = path.getNextSibling();
29
+
30
+ if (!next.node || next.isIfStatement() || next.isExpressionStatement() || next.isReturnStatement())
31
+ return;
32
+
33
+ indent();
34
+ write('\n');
35
+ };
36
+
37
+ function isFirst(path) {
38
+ return path.node === path.parentPath.node.body[0];
39
+ }
40
+
@@ -0,0 +1,42 @@
1
+ 'use strict';
2
+
3
+ const {ExpressionStatement} = require('./expression-statement');
4
+ const {VariableDeclaration} = require('./variable-declaration');
5
+ const {IfStatement} = require('./if-statement');
6
+ const {ForOfStatement} = require('./for-of-statement');
7
+ const {BlockStatement} = require('./block-statement');
8
+
9
+ module.exports = {
10
+ BlockStatement,
11
+ ExpressionStatement,
12
+ VariableDeclaration,
13
+ IfStatement,
14
+ ForOfStatement,
15
+ Program(path, {traverse}) {
16
+ path.get('body').forEach(traverse);
17
+ },
18
+ ContinueStatement(path, {write, indent}) {
19
+ indent();
20
+ write('continue;\n');
21
+ },
22
+ ReturnStatement(path, {indent, write, traverse}) {
23
+ indent();
24
+
25
+ if (path?.parentPath?.node?.body?.length > 2) {
26
+ write('\n');
27
+ indent();
28
+ }
29
+
30
+ write('return');
31
+ const argPath = path.get('argument');
32
+
33
+ if (argPath.node) {
34
+ write(' ');
35
+ traverse(argPath);
36
+ }
37
+
38
+ write(';');
39
+ write('\n');
40
+ },
41
+ };
42
+
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ const isNextAssign = (path) => {
4
+ const nextPath = path.getNextSibling();
5
+
6
+ if (!nextPath.isExpressionStatement())
7
+ return false;
8
+
9
+ return nextPath.get('expression').isAssignmentExpression();
10
+ };
11
+
12
+ module.exports.VariableDeclaration = (path, {write, maybeWrite, maybeIndent, traverse}) => {
13
+ if (!isFirst(path) && shouldAddNewLine(path)) {
14
+ write('\n');
15
+ }
16
+
17
+ const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
18
+
19
+ maybeIndent(isParentBlock);
20
+ write(`${path.node.kind} `);
21
+ traverse(path.get('declarations.0.id'));
22
+
23
+ const initPath = path.get('declarations.0.init');
24
+
25
+ maybeWrite(initPath.node, ' = ');
26
+ traverse(initPath);
27
+ maybeWrite(isParentBlock, ';\n');
28
+
29
+ const is = isCoupleLinesExpression(path) && !isNextAssign(path);
30
+
31
+ maybeIndent(is);
32
+ maybeWrite(is, '\n');
33
+ };
34
+ function isCoupleLinesExpression(path) {
35
+ const start = path.node?.loc?.start.line;
36
+ const end = path.node?.loc?.end.line;
37
+
38
+ return end > start;
39
+ }
40
+
41
+ function shouldAddNewLine(path) {
42
+ const prevPath = path.getPrevSibling();
43
+ const nextPath = path.getNextSibling();
44
+ const nextNextPath = nextPath.getNextSibling();
45
+
46
+ const twoNext = nextPath.node && nextNextPath.node;
47
+
48
+ if (!twoNext)
49
+ return false;
50
+
51
+ if (prevPath.isVariableDeclaration() || prevPath.isExpressionStatement() && prevPath.get('expression').isStringLiteral())
52
+ return false;
53
+
54
+ if (prevPath.isIfStatement())
55
+ return false;
56
+
57
+ return true;
58
+ }
59
+
60
+ function isFirst(path) {
61
+ return path.node === path.parentPath.node.body[0];
62
+ }
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@putout/printer",
3
+ "version": "1.0.0",
4
+ "type": "commonjs",
5
+ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
+ "description": "Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout",
7
+ "homepage": "https://github.com/putoutjs/printer#readme",
8
+ "main": "./lib/printer.js",
9
+ "release": false,
10
+ "tag": false,
11
+ "changelog": false,
12
+ "exports": {
13
+ ".": "./lib/printer.js"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git://github.com/putoutjs/printer.git"
18
+ },
19
+ "scripts": {
20
+ "test": "madrun test",
21
+ "watch:test": "madrun watch:test",
22
+ "lint": "madrun lint",
23
+ "fresh:lint": "madrun fresh:lint",
24
+ "lint:fresh": "madrun lint:fresh",
25
+ "fix:lint": "madrun fix:lint",
26
+ "coverage": "madrun coverage",
27
+ "report": "madrun report"
28
+ },
29
+ "dependencies": {
30
+ "@babel/parser": "^7.19.0",
31
+ "@babel/traverse": "^7.21.2",
32
+ "just-snake-case": "^3.2.0"
33
+ },
34
+ "keywords": [
35
+ "putout",
36
+ "printer",
37
+ "AST",
38
+ "babel",
39
+ "api",
40
+ "traverse",
41
+ "generate"
42
+ ],
43
+ "devDependencies": {
44
+ "c8": "^7.5.0",
45
+ "eslint": "^8.0.1",
46
+ "eslint-plugin-n": "^15.2.4",
47
+ "eslint-plugin-putout": "^17.0.0",
48
+ "just-kebab-case": "^4.2.0",
49
+ "lerna": "^6.0.1",
50
+ "madrun": "^9.0.0",
51
+ "mock-require": "^3.0.3",
52
+ "montag": "^1.0.0",
53
+ "nodemon": "^2.0.1",
54
+ "putout": "*",
55
+ "supertape": "^8.0.0",
56
+ "try-catch": "^3.0.0"
57
+ },
58
+ "license": "MIT",
59
+ "engines": {
60
+ "node": ">=16"
61
+ },
62
+ "publishConfig": {
63
+ "access": "public"
64
+ }
65
+ }