@putout/printer 1.5.1 → 1.5.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 +10 -0
- package/lib/tokenize/mark.js +0 -2
- package/lib/tokenize/statements/block-statement.js +33 -11
- package/lib/tokenize/statements/debugger-statement.js +8 -0
- package/lib/tokenize/statements/if-statement.js +29 -18
- package/lib/tokenize/statements/index.js +4 -0
- package/lib/tokenize/statements/try-statements.js +37 -0
- package/lib/tokenize/statements/variable-declaration.js +17 -13
- package/lib/tokenize/tokenize.js +8 -3
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/lib/tokenize/mark.js
CHANGED
|
@@ -29,8 +29,6 @@ module.exports.hasPrevNewline = (path) => {
|
|
|
29
29
|
return isMarkedAfter(path.getPrevSibling());
|
|
30
30
|
};
|
|
31
31
|
|
|
32
|
-
module.exports.maybeMarkAfter = (a, path) => a && markAfter(path);
|
|
33
|
-
|
|
34
32
|
module.exports.isMarkedParentBefore = (path) => {
|
|
35
33
|
return isMarkedBefore(path.parentPath);
|
|
36
34
|
};
|
|
@@ -2,30 +2,52 @@
|
|
|
2
2
|
|
|
3
3
|
const isFirstStatement = (path) => path.get('body.0')?.isStatement();
|
|
4
4
|
|
|
5
|
-
module.exports.BlockStatement = (path, {
|
|
5
|
+
module.exports.BlockStatement = (path, {indent, maybe, print}) => {
|
|
6
6
|
const body = path.get('body');
|
|
7
7
|
|
|
8
8
|
if (path.parentPath.isBlockStatement())
|
|
9
9
|
indent();
|
|
10
10
|
|
|
11
11
|
indent.inc();
|
|
12
|
-
|
|
12
|
+
print('{');
|
|
13
13
|
|
|
14
14
|
if (body.length > 1 || isFirstStatement(path))
|
|
15
|
-
|
|
15
|
+
print.newline();
|
|
16
16
|
|
|
17
|
-
body.forEach(
|
|
17
|
+
body.forEach(print);
|
|
18
18
|
indent.dec();
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
write('}');
|
|
20
|
+
maybe.indent(body.length);
|
|
21
|
+
print('}');
|
|
24
22
|
|
|
25
23
|
if (path.parentPath.isObjectMethod()) {
|
|
26
|
-
|
|
24
|
+
print(',');
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
const shouldAddNewLine = !isNewLineAdded(path);
|
|
28
|
+
|
|
29
|
+
maybe.print.newline(shouldAddNewLine);
|
|
31
30
|
};
|
|
31
|
+
|
|
32
|
+
function isNewLineAdded(path) {
|
|
33
|
+
const {parentPath} = path;
|
|
34
|
+
|
|
35
|
+
if (isTry(path) || /FunctionExpression/.test(path.parentPath.type))
|
|
36
|
+
return true;
|
|
37
|
+
|
|
38
|
+
if (parentPath.isIfStatement() && parentPath.get('consequent').node === path.node && parentPath.node.alternate)
|
|
39
|
+
return true;
|
|
40
|
+
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isTry({parentPath}) {
|
|
45
|
+
if (parentPath.isTryStatement())
|
|
46
|
+
return true;
|
|
47
|
+
|
|
48
|
+
if (parentPath.parentPath?.isTryStatement())
|
|
49
|
+
return true;
|
|
50
|
+
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
@@ -1,39 +1,50 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {hasPrevNewline} = require('../mark');
|
|
3
|
+
const {hasPrevNewline, markAfter} = require('../mark');
|
|
4
4
|
const {isFirst} = require('../is');
|
|
5
5
|
|
|
6
|
-
module.exports.IfStatement = (path, {
|
|
6
|
+
module.exports.IfStatement = (path, {indent, print}) => {
|
|
7
7
|
if (!isFirst(path) && !hasPrevNewline(path)) {
|
|
8
|
-
indent();
|
|
9
|
-
|
|
8
|
+
print.indent();
|
|
9
|
+
print.newline();
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
indent();
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
print('if (');
|
|
14
|
+
print(path.get('test'));
|
|
15
|
+
print(')');
|
|
16
16
|
|
|
17
17
|
const consequent = path.get('consequent');
|
|
18
|
+
const alternate = path.get('alternate');
|
|
18
19
|
|
|
19
20
|
if (consequent.isBlockStatement()) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
print(' ');
|
|
22
|
+
print(consequent);
|
|
23
|
+
} else {
|
|
24
|
+
print.newline();
|
|
25
|
+
indent.inc();
|
|
26
|
+
print(consequent);
|
|
27
|
+
indent.dec();
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
if (alternate.isBlockStatement()) {
|
|
31
|
+
print(' else ');
|
|
32
|
+
print(alternate);
|
|
33
|
+
} else if (alternate.node) {
|
|
34
|
+
print('else');
|
|
35
|
+
print.newline();
|
|
36
|
+
indent.inc();
|
|
37
|
+
print(alternate);
|
|
38
|
+
indent.dec();
|
|
39
|
+
}
|
|
30
40
|
|
|
31
41
|
const next = path.getNextSibling();
|
|
32
42
|
|
|
33
|
-
if (!next.node || next.
|
|
43
|
+
if (!next.node || next.isExpressionStatement() || next.isReturnStatement())
|
|
34
44
|
return;
|
|
35
45
|
|
|
36
|
-
indent();
|
|
37
|
-
|
|
46
|
+
print.indent();
|
|
47
|
+
print.newline();
|
|
48
|
+
markAfter(path);
|
|
38
49
|
};
|
|
39
50
|
|
|
@@ -6,6 +6,8 @@ const {IfStatement} = require('./if-statement');
|
|
|
6
6
|
const {ForOfStatement} = require('./for-of-statement');
|
|
7
7
|
const {BlockStatement} = require('./block-statement');
|
|
8
8
|
const {ReturnStatement} = require('./return-statement');
|
|
9
|
+
const TryStatements = require('./try-statements');
|
|
10
|
+
const {DebuggerStatement} = require('./debugger-statement');
|
|
9
11
|
|
|
10
12
|
module.exports = {
|
|
11
13
|
BlockStatement,
|
|
@@ -14,9 +16,11 @@ module.exports = {
|
|
|
14
16
|
IfStatement,
|
|
15
17
|
ForOfStatement,
|
|
16
18
|
ReturnStatement,
|
|
19
|
+
DebuggerStatement,
|
|
17
20
|
Program(path, {traverse}) {
|
|
18
21
|
path.get('body').forEach(traverse);
|
|
19
22
|
},
|
|
23
|
+
...TryStatements,
|
|
20
24
|
ContinueStatement(path, {write, indent}) {
|
|
21
25
|
indent();
|
|
22
26
|
write('continue;\n');
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {isNext} = require('../is');
|
|
4
|
+
module.exports.TryStatement = (path, {print, maybe}) => {
|
|
5
|
+
const finalizer = path.get('finalizer');
|
|
6
|
+
|
|
7
|
+
print('try ');
|
|
8
|
+
print(path.get('block'));
|
|
9
|
+
print(path.get('handler'));
|
|
10
|
+
|
|
11
|
+
if (finalizer.node) {
|
|
12
|
+
print(' ');
|
|
13
|
+
print('finally');
|
|
14
|
+
print(' ');
|
|
15
|
+
print(finalizer);
|
|
16
|
+
print.newline();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
maybe.print.linebreak(isNext(path));
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
module.exports.CatchClause = (path, {print}) => {
|
|
23
|
+
const param = path.get('param');
|
|
24
|
+
const body = path.get('body');
|
|
25
|
+
|
|
26
|
+
print(' ');
|
|
27
|
+
print('catch ');
|
|
28
|
+
|
|
29
|
+
if (param.node) {
|
|
30
|
+
print('(');
|
|
31
|
+
print(param);
|
|
32
|
+
print(') ');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
print(body);
|
|
36
|
+
};
|
|
37
|
+
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {isNext, isCoupleLines} = require('../is');
|
|
4
|
+
const {hasPrevNewline, markAfter} = require('../mark');
|
|
4
5
|
const isNextAssign = (path) => {
|
|
5
6
|
const nextPath = path.getNextSibling();
|
|
6
7
|
|
|
@@ -10,28 +11,30 @@ const isNextAssign = (path) => {
|
|
|
10
11
|
return nextPath.get('expression').isAssignmentExpression();
|
|
11
12
|
};
|
|
12
13
|
|
|
13
|
-
module.exports.VariableDeclaration = (path, {
|
|
14
|
-
if (!isFirst(path) && shouldAddNewLine(path))
|
|
15
|
-
|
|
16
|
-
}
|
|
14
|
+
module.exports.VariableDeclaration = (path, {maybe, print}) => {
|
|
15
|
+
if (!isFirst(path) && shouldAddNewLine(path) && !hasPrevNewline(path))
|
|
16
|
+
print.linebreak();
|
|
17
17
|
|
|
18
18
|
const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
maybe.indent(isParentBlock);
|
|
21
|
+
print(`${path.node.kind} `);
|
|
22
|
+
print(path.get('declarations.0.id'));
|
|
23
23
|
|
|
24
24
|
const initPath = path.get('declarations.0.init');
|
|
25
25
|
|
|
26
|
-
maybe.
|
|
27
|
-
|
|
28
|
-
maybe.
|
|
26
|
+
maybe.print(initPath.node, ' = ');
|
|
27
|
+
print(initPath);
|
|
28
|
+
maybe.print(isParentBlock, ';');
|
|
29
|
+
maybe.print.newline(isParentBlock);
|
|
29
30
|
|
|
30
31
|
const is = isNext(path) && isCoupleLines(path) && !isNextAssign(path);
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
if (is) {
|
|
34
|
+
print.indent();
|
|
35
|
+
print.newline();
|
|
36
|
+
markAfter(path);
|
|
37
|
+
}
|
|
35
38
|
};
|
|
36
39
|
|
|
37
40
|
function shouldAddNewLine(path) {
|
|
@@ -60,3 +63,4 @@ function shouldAddNewLine(path) {
|
|
|
60
63
|
function isFirst(path) {
|
|
61
64
|
return path.node === path.parentPath.node.body[0];
|
|
62
65
|
}
|
|
66
|
+
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -36,6 +36,8 @@ module.exports.tokenize = (ast) => {
|
|
|
36
36
|
const maybeIndent = (a) => a && indent();
|
|
37
37
|
const maybeIndentInc = (a) => a && indent.inc();
|
|
38
38
|
const maybeIndentDec = (a) => a && indent.dec();
|
|
39
|
+
const maybeNewline = (a) => a && newline();
|
|
40
|
+
const maybeLinebreak = (a) => a && linebreak();
|
|
39
41
|
|
|
40
42
|
let i = 0;
|
|
41
43
|
const incIndent = () => ++i;
|
|
@@ -52,7 +54,7 @@ module.exports.tokenize = (ast) => {
|
|
|
52
54
|
dec: decIndent,
|
|
53
55
|
});
|
|
54
56
|
|
|
55
|
-
const
|
|
57
|
+
const linebreak = () => {
|
|
56
58
|
tokens.push({
|
|
57
59
|
type: TYPES.NEWLINE,
|
|
58
60
|
value: `\n${printIndent(i)}`,
|
|
@@ -69,7 +71,7 @@ module.exports.tokenize = (ast) => {
|
|
|
69
71
|
assign(write, {
|
|
70
72
|
indent,
|
|
71
73
|
newline,
|
|
72
|
-
linebreak
|
|
74
|
+
linebreak,
|
|
73
75
|
});
|
|
74
76
|
const print = (maybeLine) => {
|
|
75
77
|
if (isString(maybeLine))
|
|
@@ -94,6 +96,10 @@ module.exports.tokenize = (ast) => {
|
|
|
94
96
|
});
|
|
95
97
|
|
|
96
98
|
assign(print, write);
|
|
99
|
+
assign(maybePrint, {
|
|
100
|
+
newline: maybeNewline,
|
|
101
|
+
linebreak: maybeLinebreak,
|
|
102
|
+
});
|
|
97
103
|
|
|
98
104
|
const printer = {
|
|
99
105
|
incIndent,
|
|
@@ -104,7 +110,6 @@ module.exports.tokenize = (ast) => {
|
|
|
104
110
|
debug,
|
|
105
111
|
maybeIndent,
|
|
106
112
|
traverse,
|
|
107
|
-
writeEmptyLine,
|
|
108
113
|
maybe,
|
|
109
114
|
print,
|
|
110
115
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.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",
|