@putout/printer 1.5.2 → 1.5.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 +10 -0
- package/lib/tokenize/expressions/call-expression.js +1 -1
- package/lib/tokenize/expressions/class-declaration.js +1 -1
- package/lib/tokenize/mark.js +0 -2
- package/lib/tokenize/statements/block-statement.js +21 -10
- package/lib/tokenize/statements/debugger-statement.js +8 -0
- package/lib/tokenize/statements/expression-statement.js +3 -2
- package/lib/tokenize/statements/if-statement.js +29 -18
- package/lib/tokenize/statements/index.js +2 -0
- package/lib/tokenize/statements/try-statements.js +1 -1
- package/lib/tokenize/statements/variable-declaration.js +16 -13
- package/lib/tokenize/tokenize.js +12 -5
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -11,7 +11,7 @@ module.exports.CallExpression = (path, {indent, print, maybe}) => {
|
|
|
11
11
|
const isParentCall = toLong(path) && path.parentPath.isCallExpression();
|
|
12
12
|
|
|
13
13
|
if (shouldAddNewLine(path) && !isMarkedParentBefore(path) && !isMarkedPrevAfter(path.parentPath))
|
|
14
|
-
print.
|
|
14
|
+
print.breakline();
|
|
15
15
|
|
|
16
16
|
print(path.get('callee'));
|
|
17
17
|
print('(');
|
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,35 +2,45 @@
|
|
|
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
|
-
const shouldAddNewLine = !
|
|
27
|
+
const shouldAddNewLine = !isNewLineAdded(path);
|
|
30
28
|
|
|
31
29
|
maybe.print.newline(shouldAddNewLine);
|
|
32
30
|
};
|
|
33
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
|
+
|
|
34
44
|
function isTry({parentPath}) {
|
|
35
45
|
if (parentPath.isTryStatement())
|
|
36
46
|
return true;
|
|
@@ -40,3 +50,4 @@ function isTry({parentPath}) {
|
|
|
40
50
|
|
|
41
51
|
return false;
|
|
42
52
|
}
|
|
53
|
+
|
|
@@ -7,9 +7,9 @@ const {
|
|
|
7
7
|
} = require('../mark');
|
|
8
8
|
const {isNext} = require('../is');
|
|
9
9
|
|
|
10
|
-
module.exports.ExpressionStatement = (path, {write, indent, traverse}) => {
|
|
10
|
+
module.exports.ExpressionStatement = (path, {write, indent, traverse, print}) => {
|
|
11
11
|
if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path) && !isMarkedPrevAfter(path)) {
|
|
12
|
-
|
|
12
|
+
print.breakline();
|
|
13
13
|
markBefore(path);
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -62,3 +62,4 @@ function shouldAddNewLine(path) {
|
|
|
62
62
|
|
|
63
63
|
return true;
|
|
64
64
|
}
|
|
65
|
+
|
|
@@ -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
|
|
|
@@ -7,6 +7,7 @@ const {ForOfStatement} = require('./for-of-statement');
|
|
|
7
7
|
const {BlockStatement} = require('./block-statement');
|
|
8
8
|
const {ReturnStatement} = require('./return-statement');
|
|
9
9
|
const TryStatements = require('./try-statements');
|
|
10
|
+
const {DebuggerStatement} = require('./debugger-statement');
|
|
10
11
|
|
|
11
12
|
module.exports = {
|
|
12
13
|
BlockStatement,
|
|
@@ -15,6 +16,7 @@ module.exports = {
|
|
|
15
16
|
IfStatement,
|
|
16
17
|
ForOfStatement,
|
|
17
18
|
ReturnStatement,
|
|
19
|
+
DebuggerStatement,
|
|
18
20
|
Program(path, {traverse}) {
|
|
19
21
|
path.get('body').forEach(traverse);
|
|
20
22
|
},
|
|
@@ -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,29 @@ 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.breakline();
|
|
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.linebreak();
|
|
35
|
+
markAfter(path);
|
|
36
|
+
}
|
|
35
37
|
};
|
|
36
38
|
|
|
37
39
|
function shouldAddNewLine(path) {
|
|
@@ -60,3 +62,4 @@ function shouldAddNewLine(path) {
|
|
|
60
62
|
function isFirst(path) {
|
|
61
63
|
return path.node === path.parentPath.node.body[0];
|
|
62
64
|
}
|
|
65
|
+
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -37,7 +37,7 @@ module.exports.tokenize = (ast) => {
|
|
|
37
37
|
const maybeIndentInc = (a) => a && indent.inc();
|
|
38
38
|
const maybeIndentDec = (a) => a && indent.dec();
|
|
39
39
|
const maybeNewline = (a) => a && newline();
|
|
40
|
-
const
|
|
40
|
+
const maybeBreakline = (a) => a && breakline();
|
|
41
41
|
|
|
42
42
|
let i = 0;
|
|
43
43
|
const incIndent = () => ++i;
|
|
@@ -54,7 +54,14 @@ module.exports.tokenize = (ast) => {
|
|
|
54
54
|
dec: decIndent,
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
-
const
|
|
57
|
+
const linebreak = () => {
|
|
58
|
+
tokens.push({
|
|
59
|
+
type: TYPES.NEWLINE,
|
|
60
|
+
value: `${printIndent(i)}\n`,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const breakline = () => {
|
|
58
65
|
tokens.push({
|
|
59
66
|
type: TYPES.NEWLINE,
|
|
60
67
|
value: `\n${printIndent(i)}`,
|
|
@@ -71,7 +78,8 @@ module.exports.tokenize = (ast) => {
|
|
|
71
78
|
assign(write, {
|
|
72
79
|
indent,
|
|
73
80
|
newline,
|
|
74
|
-
linebreak
|
|
81
|
+
linebreak,
|
|
82
|
+
breakline,
|
|
75
83
|
});
|
|
76
84
|
const print = (maybeLine) => {
|
|
77
85
|
if (isString(maybeLine))
|
|
@@ -98,7 +106,7 @@ module.exports.tokenize = (ast) => {
|
|
|
98
106
|
assign(print, write);
|
|
99
107
|
assign(maybePrint, {
|
|
100
108
|
newline: maybeNewline,
|
|
101
|
-
|
|
109
|
+
breakline: maybeBreakline,
|
|
102
110
|
});
|
|
103
111
|
|
|
104
112
|
const printer = {
|
|
@@ -110,7 +118,6 @@ module.exports.tokenize = (ast) => {
|
|
|
110
118
|
debug,
|
|
111
119
|
maybeIndent,
|
|
112
120
|
traverse,
|
|
113
|
-
writeEmptyLine,
|
|
114
121
|
maybe,
|
|
115
122
|
print,
|
|
116
123
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.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",
|