@putout/printer 1.5.2 → 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 +5 -0
- 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/if-statement.js +29 -18
- package/lib/tokenize/statements/index.js +2 -0
- package/lib/tokenize/statements/variable-declaration.js +17 -13
- package/lib/tokenize/tokenize.js +3 -4
- 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,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
|
+
|
|
@@ -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,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
|
@@ -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 maybeLinebreak = (a) => a &&
|
|
40
|
+
const maybeLinebreak = (a) => a && linebreak();
|
|
41
41
|
|
|
42
42
|
let i = 0;
|
|
43
43
|
const incIndent = () => ++i;
|
|
@@ -54,7 +54,7 @@ module.exports.tokenize = (ast) => {
|
|
|
54
54
|
dec: decIndent,
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
-
const
|
|
57
|
+
const linebreak = () => {
|
|
58
58
|
tokens.push({
|
|
59
59
|
type: TYPES.NEWLINE,
|
|
60
60
|
value: `\n${printIndent(i)}`,
|
|
@@ -71,7 +71,7 @@ module.exports.tokenize = (ast) => {
|
|
|
71
71
|
assign(write, {
|
|
72
72
|
indent,
|
|
73
73
|
newline,
|
|
74
|
-
linebreak
|
|
74
|
+
linebreak,
|
|
75
75
|
});
|
|
76
76
|
const print = (maybeLine) => {
|
|
77
77
|
if (isString(maybeLine))
|
|
@@ -110,7 +110,6 @@ module.exports.tokenize = (ast) => {
|
|
|
110
110
|
debug,
|
|
111
111
|
maybeIndent,
|
|
112
112
|
traverse,
|
|
113
|
-
writeEmptyLine,
|
|
114
113
|
maybe,
|
|
115
114
|
print,
|
|
116
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",
|