@putout/printer 1.1.0 → 1.1.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/lib/print-tokens/cook.js +29 -0
- package/lib/print-tokens/index.js +8 -0
- package/lib/printer.js +5 -82
- package/lib/tokenize/debug.js +17 -0
- package/lib/tokenize/expressions/arrays.js +68 -0
- package/lib/{expressions → tokenize/expressions}/call-expression.js +8 -5
- package/lib/{statements → tokenize/statements}/block-statement.js +5 -6
- package/lib/{statements → tokenize/statements}/expression-statement.js +4 -4
- package/lib/tokenize/statements/for-of-statement.js +44 -0
- package/lib/{statements → tokenize/statements}/index.js +5 -5
- package/lib/{statements → tokenize/statements}/variable-declaration.js +6 -6
- package/lib/tokenize/tokenize.js +119 -0
- package/lib/types.js +10 -0
- package/package.json +1 -1
- package/lib/expressions/arrays.js +0 -51
- package/lib/statements/for-of-statement.js +0 -31
- /package/lib/{expressions → tokenize/expressions}/assignment-expression.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/class-declaration.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/functions.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/index.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/member-expressions.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/new-expression.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/object-expression.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/object-pattern.js +0 -0
- /package/lib/{expressions → tokenize/expressions}/unary-expressions.js +0 -0
- /package/lib/{literals → tokenize/literals}/index.js +0 -0
- /package/lib/{literals → tokenize/literals}/template-literal.js +0 -0
- /package/lib/{statements → tokenize/statements}/if-statement.js +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.cook = (tokens) => {
|
|
4
|
+
const cookedTokens = [];
|
|
5
|
+
|
|
6
|
+
for (const [i, {
|
|
7
|
+
value,
|
|
8
|
+
}] of tokens.entries()) {
|
|
9
|
+
/*
|
|
10
|
+
if (type !== TYPES.TOKEN)
|
|
11
|
+
console.log(type);
|
|
12
|
+
|
|
13
|
+
if (type === TYPES.NEWLINE && next?.type === TYPES.LINEBREAK) {
|
|
14
|
+
console.log('[skipped]');
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (type === TYPES.LINEBREAK && next?.type === TYPES.LINEBREAK) {
|
|
19
|
+
console.log('[skipped]');
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
cookedTokens.push(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return cookedTokens;
|
|
28
|
+
};
|
|
29
|
+
|
package/lib/printer.js
CHANGED
|
@@ -1,86 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const statements = require('./statements');
|
|
6
|
-
const literals = require('./literals');
|
|
7
|
-
const toSnakeCase = require('just-snake-case');
|
|
3
|
+
const {tokenize} = require('./tokenize/tokenize');
|
|
4
|
+
const {printTokens} = require('./print-tokens');
|
|
8
5
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
...literals,
|
|
6
|
+
module.exports.print = (ast) => {
|
|
7
|
+
const tokens = tokenize(ast);
|
|
8
|
+
return printTokens(tokens);
|
|
13
9
|
};
|
|
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,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {TYPES} = require('../types');
|
|
4
|
+
const toSnakeCase = require('just-snake-case');
|
|
5
|
+
|
|
6
|
+
const {DEBUG} = process.env;
|
|
7
|
+
|
|
8
|
+
module.exports.createDebug = (tokens) => (a) => {
|
|
9
|
+
if (!DEBUG)
|
|
10
|
+
return;
|
|
11
|
+
|
|
12
|
+
tokens.push({
|
|
13
|
+
type: TYPES.DEBUG,
|
|
14
|
+
value: `/*__${toSnakeCase(a)}*/`,
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {entries} = Object;
|
|
4
|
+
const isForOf = ({parentPath}) => parentPath.parentPath.parentPath.isForOfStatement();
|
|
5
|
+
|
|
6
|
+
module.exports.ArrayPattern = (path, {write, indent, maybe, traverse}) => {
|
|
7
|
+
write('[');
|
|
8
|
+
|
|
9
|
+
const elements = path.get('elements');
|
|
10
|
+
|
|
11
|
+
indent.inc();
|
|
12
|
+
const isNewLine = !isForOf(path) && elements.length > 2;
|
|
13
|
+
const n = elements.length - 1;
|
|
14
|
+
|
|
15
|
+
maybe.write(isNewLine && elements.length, '\n');
|
|
16
|
+
|
|
17
|
+
for (const [index, element] of entries(elements)) {
|
|
18
|
+
maybe.indent(isNewLine);
|
|
19
|
+
|
|
20
|
+
traverse(element);
|
|
21
|
+
|
|
22
|
+
maybe.write(isNewLine, ',\n');
|
|
23
|
+
maybe.write(!isNewLine && index < n, ', ');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
indent.dec();
|
|
27
|
+
|
|
28
|
+
maybe.indent(elements.length && isNewLine);
|
|
29
|
+
|
|
30
|
+
write(']');
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
module.exports.ArrayExpression = (path, {write, indent, maybe, traverse}) => {
|
|
34
|
+
write('[');
|
|
35
|
+
|
|
36
|
+
const elements = path.get('elements');
|
|
37
|
+
|
|
38
|
+
indent.inc();
|
|
39
|
+
const isNewLine = !isNumbers(elements);
|
|
40
|
+
const n = elements.length - 1;
|
|
41
|
+
|
|
42
|
+
maybe.write(isNewLine && elements.length, '\n');
|
|
43
|
+
|
|
44
|
+
for (const [index, element] of entries(elements)) {
|
|
45
|
+
maybe.indent(isNewLine);
|
|
46
|
+
|
|
47
|
+
traverse(element);
|
|
48
|
+
|
|
49
|
+
maybe.write(isNewLine, ',\n');
|
|
50
|
+
maybe.write(!isNewLine && index < n, ', ');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
indent.dec();
|
|
54
|
+
|
|
55
|
+
maybe.indent(elements.length && isNewLine);
|
|
56
|
+
|
|
57
|
+
write(']');
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function isNumbers(elements) {
|
|
61
|
+
for (const element of elements) {
|
|
62
|
+
if (element.isNumericLiteral())
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const {entries} = Object;
|
|
4
4
|
|
|
5
|
-
module.exports.CallExpression = (path, {traverse, indent, write,
|
|
5
|
+
module.exports.CallExpression = (path, {traverse, indent, write, incIndent, decIndent, maybeWrite}) => {
|
|
6
6
|
const isParentCall = toLong(path) && path.parentPath.isCallExpression();
|
|
7
7
|
|
|
8
|
-
if (shouldAddNewLine(path))
|
|
9
|
-
|
|
8
|
+
if (shouldAddNewLine(path)) {
|
|
9
|
+
write.newline();
|
|
10
|
+
write.indent();
|
|
11
|
+
}
|
|
10
12
|
|
|
11
13
|
traverse(path.get('callee'));
|
|
12
14
|
write('(');
|
|
@@ -19,7 +21,7 @@ module.exports.CallExpression = (path, {traverse, indent, write, writeEmptyLine,
|
|
|
19
21
|
|
|
20
22
|
for (const [i, arg] of entries(args)) {
|
|
21
23
|
if (isParentCall) {
|
|
22
|
-
write(
|
|
24
|
+
write.newline();
|
|
23
25
|
indent();
|
|
24
26
|
}
|
|
25
27
|
|
|
@@ -35,7 +37,8 @@ module.exports.CallExpression = (path, {traverse, indent, write, writeEmptyLine,
|
|
|
35
37
|
|
|
36
38
|
if (isParentCall) {
|
|
37
39
|
decIndent();
|
|
38
|
-
|
|
40
|
+
write.newline();
|
|
41
|
+
write.indent();
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
write(')');
|
|
@@ -2,17 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
const isFirstStatement = (path) => path.get('body.0')?.isStatement();
|
|
4
4
|
|
|
5
|
-
module.exports.BlockStatement = (path, {write, indent,
|
|
5
|
+
module.exports.BlockStatement = (path, {write, indent, traverse}) => {
|
|
6
6
|
const body = path.get('body');
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
indent.inc();
|
|
9
9
|
write('{');
|
|
10
10
|
|
|
11
11
|
if (body.length > 1 || isFirstStatement(path))
|
|
12
|
-
write(
|
|
12
|
+
write.newline();
|
|
13
13
|
|
|
14
14
|
body.forEach(traverse);
|
|
15
|
-
|
|
15
|
+
indent.dec();
|
|
16
16
|
|
|
17
17
|
if (body.length)
|
|
18
18
|
indent();
|
|
@@ -24,6 +24,5 @@ module.exports.BlockStatement = (path, {write, indent, incIndent, decIndent, tra
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
if (!/FunctionExpression/.test(path.parentPath.type))
|
|
27
|
-
write(
|
|
27
|
+
write.newline();
|
|
28
28
|
};
|
|
29
|
-
|
|
@@ -2,18 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports.ExpressionStatement = (path, {write, indent, traverse}) => {
|
|
4
4
|
if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path)) {
|
|
5
|
-
write(
|
|
6
|
-
indent();
|
|
5
|
+
write.linebreak();
|
|
7
6
|
}
|
|
8
7
|
|
|
9
8
|
const expressionPath = path.get('expression');
|
|
10
9
|
|
|
11
10
|
indent();
|
|
12
11
|
traverse(expressionPath);
|
|
13
|
-
write('
|
|
12
|
+
write(';');
|
|
13
|
+
write.newline();
|
|
14
14
|
|
|
15
15
|
if (isStrictMode(path))
|
|
16
|
-
write(
|
|
16
|
+
write.newline();
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
function isCoupleLinesExpression(path) {
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const isPrevNewLine = (path) => {
|
|
4
|
+
const prev = path.getPrevSibling();
|
|
5
|
+
|
|
6
|
+
if (!prev.node)
|
|
7
|
+
return true;
|
|
8
|
+
|
|
9
|
+
return prev.__putout_newline;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports.ForOfStatement = (path, {write, indent, traverse}) => {
|
|
13
|
+
if (!isPrevNewLine(path)) {
|
|
14
|
+
write.indent();
|
|
15
|
+
write.newline();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
indent();
|
|
19
|
+
write('for (');
|
|
20
|
+
traverse(path.get('left'));
|
|
21
|
+
write(' of ');
|
|
22
|
+
traverse(path.get('right'));
|
|
23
|
+
write(')');
|
|
24
|
+
|
|
25
|
+
const bodyPath = path.get('body');
|
|
26
|
+
|
|
27
|
+
if (bodyPath.isExpressionStatement()) {
|
|
28
|
+
indent.inc();
|
|
29
|
+
write.newline();
|
|
30
|
+
traverse(bodyPath);
|
|
31
|
+
indent.dec();
|
|
32
|
+
write.newline();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (bodyPath.isBlockStatement()) {
|
|
36
|
+
write(' ');
|
|
37
|
+
traverse(bodyPath);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (path.getNextSibling().node) {
|
|
41
|
+
//write.newline();
|
|
42
|
+
//path.__putout_newline = true;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
@@ -20,14 +20,14 @@ module.exports = {
|
|
|
20
20
|
write('continue;\n');
|
|
21
21
|
},
|
|
22
22
|
ReturnStatement(path, {indent, write, traverse}) {
|
|
23
|
-
indent();
|
|
24
|
-
|
|
25
23
|
if (path?.parentPath?.node?.body?.length > 2) {
|
|
26
|
-
write(
|
|
27
|
-
|
|
24
|
+
write.indent();
|
|
25
|
+
write.newline();
|
|
28
26
|
}
|
|
29
27
|
|
|
28
|
+
indent();
|
|
30
29
|
write('return');
|
|
30
|
+
|
|
31
31
|
const argPath = path.get('argument');
|
|
32
32
|
|
|
33
33
|
if (argPath.node) {
|
|
@@ -36,7 +36,7 @@ module.exports = {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
write(';');
|
|
39
|
-
write(
|
|
39
|
+
write.newline();
|
|
40
40
|
},
|
|
41
41
|
};
|
|
42
42
|
|
|
@@ -9,9 +9,9 @@ const isNextAssign = (path) => {
|
|
|
9
9
|
return nextPath.get('expression').isAssignmentExpression();
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
module.exports.VariableDeclaration = (path, {write,
|
|
12
|
+
module.exports.VariableDeclaration = (path, {write, maybe, maybeIndent, traverse}) => {
|
|
13
13
|
if (!isFirst(path) && shouldAddNewLine(path)) {
|
|
14
|
-
write(
|
|
14
|
+
write.linebreak();
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
|
|
@@ -22,14 +22,14 @@ module.exports.VariableDeclaration = (path, {write, maybeWrite, maybeIndent, tra
|
|
|
22
22
|
|
|
23
23
|
const initPath = path.get('declarations.0.init');
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
maybe.write(initPath.node, ' = ');
|
|
26
26
|
traverse(initPath);
|
|
27
|
-
|
|
27
|
+
maybe.write(isParentBlock, ';\n');
|
|
28
28
|
|
|
29
29
|
const is = isCoupleLinesExpression(path) && !isNextAssign(path);
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
maybe.indent(is);
|
|
32
|
+
maybe.write(is, '\n');
|
|
33
33
|
};
|
|
34
34
|
function isCoupleLinesExpression(path) {
|
|
35
35
|
const start = path.node?.loc?.start.line;
|
|
@@ -0,0 +1,119 @@
|
|
|
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 {TYPES} = require('../types');
|
|
8
|
+
const {createDebug} = require('./debug');
|
|
9
|
+
|
|
10
|
+
const {assign} = Object;
|
|
11
|
+
|
|
12
|
+
const traversers = {
|
|
13
|
+
...expressions,
|
|
14
|
+
...statements,
|
|
15
|
+
...literals,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports.tokenize = (ast) => {
|
|
19
|
+
const tokens = [];
|
|
20
|
+
const debug = createDebug(tokens);
|
|
21
|
+
const write = (value) => {
|
|
22
|
+
tokens.push({
|
|
23
|
+
type: TYPES.TOKEN,
|
|
24
|
+
value,
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
|
|
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 indent = () => {
|
|
36
|
+
tokens.push({
|
|
37
|
+
type: TYPES.INDENT,
|
|
38
|
+
value: printIndent(i),
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
assign(indent, {
|
|
43
|
+
inc: incIndent,
|
|
44
|
+
dec: decIndent,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const writeEmptyLine = () => {
|
|
48
|
+
tokens.push({
|
|
49
|
+
type: TYPES.NEWLINE,
|
|
50
|
+
value: `\n${printIndent(i)}`,
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const newline = () => {
|
|
55
|
+
tokens.push({
|
|
56
|
+
type: TYPES.NEWLINE,
|
|
57
|
+
value: '\n',
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
assign(write, {
|
|
62
|
+
indent,
|
|
63
|
+
newline,
|
|
64
|
+
linebreak: writeEmptyLine,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const maybe = {
|
|
68
|
+
write: maybeWrite,
|
|
69
|
+
indent: maybeIndent,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const printer = {
|
|
73
|
+
incIndent,
|
|
74
|
+
decIndent,
|
|
75
|
+
indent,
|
|
76
|
+
write,
|
|
77
|
+
maybeWrite,
|
|
78
|
+
debug,
|
|
79
|
+
maybeIndent,
|
|
80
|
+
traverse,
|
|
81
|
+
writeEmptyLine,
|
|
82
|
+
maybe,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
babelTraverse(ast, {
|
|
86
|
+
Program(path) {
|
|
87
|
+
traverse(path);
|
|
88
|
+
path.stop();
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
function traverse(path) {
|
|
93
|
+
const {type} = path;
|
|
94
|
+
const currentTraverse = traversers[type];
|
|
95
|
+
|
|
96
|
+
if (!path.node)
|
|
97
|
+
return;
|
|
98
|
+
|
|
99
|
+
if (!currentTraverse)
|
|
100
|
+
throw Error(`Node type '${type}' is not supported yet: '${path}'`);
|
|
101
|
+
|
|
102
|
+
currentTraverse(path, printer);
|
|
103
|
+
debug(path.type);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return tokens;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
function printIndent(i) {
|
|
110
|
+
let result = '';
|
|
111
|
+
++i;
|
|
112
|
+
|
|
113
|
+
while (--i) {
|
|
114
|
+
result += ' ';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
|
package/lib/types.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.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",
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {entries} = Object;
|
|
4
|
-
|
|
5
|
-
module.exports.ArrayPattern = (path, {write, traverse}) => {
|
|
6
|
-
write('[');
|
|
7
|
-
|
|
8
|
-
for (const element of path.get('elements')) {
|
|
9
|
-
traverse(element);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
write(']');
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
module.exports.ArrayExpression = (path, {write, maybeWrite, indent, maybeIndent, incIndent, decIndent, traverse}) => {
|
|
16
|
-
write('[');
|
|
17
|
-
|
|
18
|
-
const elements = path.get('elements');
|
|
19
|
-
|
|
20
|
-
incIndent();
|
|
21
|
-
const isNewLine = !isNumbers(elements);
|
|
22
|
-
const n = elements.length - 1;
|
|
23
|
-
|
|
24
|
-
maybeWrite(isNewLine && elements.length, '\n');
|
|
25
|
-
|
|
26
|
-
for (const [index, element] of entries(elements)) {
|
|
27
|
-
maybeIndent(isNewLine);
|
|
28
|
-
|
|
29
|
-
traverse(element);
|
|
30
|
-
|
|
31
|
-
maybeWrite(isNewLine, ',\n');
|
|
32
|
-
maybeWrite(!isNewLine && index < n, ', ');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
decIndent();
|
|
36
|
-
|
|
37
|
-
if (elements.length)
|
|
38
|
-
indent();
|
|
39
|
-
|
|
40
|
-
write(']');
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
function isNumbers(elements) {
|
|
44
|
-
for (const element of elements) {
|
|
45
|
-
if (element.isNumericLiteral())
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
|
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|