@putout/printer 1.6.11 → 1.6.13
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/array-expression.js +17 -2
- package/lib/tokenize/expressions/assignment-expression.js +2 -22
- package/lib/tokenize/expressions/call-expression.js +3 -3
- package/lib/tokenize/expressions/functions.js +4 -5
- package/lib/tokenize/expressions/index.js +2 -0
- package/lib/tokenize/expressions/tagged-template-expression.js +6 -0
- package/lib/tokenize/mark.js +0 -5
- package/lib/tokenize/statements/expression-statement.js +5 -5
- package/lib/tokenize/statements/for-of-statement.js +3 -3
- package/lib/tokenize/statements/variable-declaration.js +33 -10
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {isObjectProperty} = require('@babel/types');
|
|
4
|
+
const {isCoupleLines} = require('../is');
|
|
4
5
|
const {entries} = Object;
|
|
5
6
|
const isForOf = ({parentPath}) => parentPath.isForOfStatement();
|
|
7
|
+
const SECOND = 1;
|
|
6
8
|
|
|
7
9
|
module.exports.ArrayExpression = (path, {print, maybe}) => {
|
|
8
10
|
const elements = path.get('elements');
|
|
@@ -11,7 +13,7 @@ module.exports.ArrayExpression = (path, {print, maybe}) => {
|
|
|
11
13
|
print('[');
|
|
12
14
|
maybe.indent.inc(!elementIsObject);
|
|
13
15
|
|
|
14
|
-
const isNewLine =
|
|
16
|
+
const isNewLine = !elementIsObject && isNewLineBefore(path, {elements});
|
|
15
17
|
const n = elements.length - 1;
|
|
16
18
|
|
|
17
19
|
maybe.print(isNewLine && elements.length, '\n');
|
|
@@ -59,7 +61,13 @@ function isElementObject(path) {
|
|
|
59
61
|
if (!elements.length)
|
|
60
62
|
return false;
|
|
61
63
|
|
|
62
|
-
|
|
64
|
+
if (elements[0].isObjectExpression())
|
|
65
|
+
return true;
|
|
66
|
+
|
|
67
|
+
if (elements.length > 1 && elements[SECOND].isObjectExpression())
|
|
68
|
+
return true;
|
|
69
|
+
|
|
70
|
+
return false;
|
|
63
71
|
}
|
|
64
72
|
|
|
65
73
|
function tooLong(path) {
|
|
@@ -75,3 +83,10 @@ function tooLong(path) {
|
|
|
75
83
|
|
|
76
84
|
return false;
|
|
77
85
|
}
|
|
86
|
+
|
|
87
|
+
function isNewLineBefore(path, {elements}) {
|
|
88
|
+
if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
|
|
89
|
+
return true;
|
|
90
|
+
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
@@ -2,30 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports.AssignmentExpression = (path, {print}) => {
|
|
4
4
|
const {operator} = path.node;
|
|
5
|
-
const left = path.get('left');
|
|
6
|
-
const right = path.get('right');
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
print('\n');
|
|
10
|
-
|
|
11
|
-
print(left);
|
|
6
|
+
print('__left');
|
|
12
7
|
print(' ');
|
|
13
8
|
print(operator);
|
|
14
9
|
print(' ');
|
|
15
|
-
print(
|
|
10
|
+
print('__right');
|
|
16
11
|
};
|
|
17
|
-
|
|
18
|
-
function shouldAddNewLine({parentPath}) {
|
|
19
|
-
const prevPath = parentPath.getPrevSibling();
|
|
20
|
-
const prevPrevPath = prevPath.getPrevSibling();
|
|
21
|
-
|
|
22
|
-
const twoPrev = prevPath.node && prevPrevPath.node;
|
|
23
|
-
|
|
24
|
-
if (!twoPrev)
|
|
25
|
-
return false;
|
|
26
|
-
|
|
27
|
-
if (prevPath.isExpressionStatement() && prevPath.get('expression').isAssignmentExpression())
|
|
28
|
-
return false;
|
|
29
|
-
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
4
|
isMarkedParentBefore,
|
|
5
|
-
|
|
5
|
+
hasPrevNewline,
|
|
6
6
|
} = require('../mark');
|
|
7
7
|
|
|
8
8
|
module.exports.OptionalCallExpression = (path, {indent, print, maybe}) => {
|
|
@@ -20,7 +20,7 @@ module.exports.CallExpression = CallExpression;
|
|
|
20
20
|
function CallExpression(path, {indent, print, maybe}) {
|
|
21
21
|
const isParentCall = toLong(path) && path.parentPath.isCallExpression();
|
|
22
22
|
|
|
23
|
-
if (
|
|
23
|
+
if (isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath))
|
|
24
24
|
print.breakline();
|
|
25
25
|
|
|
26
26
|
print(path.get('callee'));
|
|
@@ -56,7 +56,7 @@ function CallExpression(path, {indent, print, maybe}) {
|
|
|
56
56
|
print(')');
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
function
|
|
59
|
+
function isNewLineBefore({parentPath}) {
|
|
60
60
|
if (!parentPath.isExpressionStatement())
|
|
61
61
|
return false;
|
|
62
62
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {hasPrevNewline} = require('../mark');
|
|
4
|
+
|
|
4
5
|
const isFirst = (path) => !path.getPrevSibling().node;
|
|
5
6
|
|
|
6
7
|
module.exports.FunctionExpression = (path, {print, maybe}) => {
|
|
@@ -31,10 +32,10 @@ module.exports.FunctionExpression = (path, {print, maybe}) => {
|
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
|
|
35
|
+
|
|
34
36
|
function ArrowFunctionExpression(path, {print, maybe}) {
|
|
35
37
|
const {async} = path.node;
|
|
36
38
|
maybe.print(async, 'async ');
|
|
37
|
-
|
|
38
39
|
print('(');
|
|
39
40
|
|
|
40
41
|
const params = path.get('params');
|
|
@@ -72,11 +73,10 @@ module.exports.ObjectMethod = (path, {print}) => {
|
|
|
72
73
|
module.exports.FunctionDeclaration = (path, {print, maybe}) => {
|
|
73
74
|
const {async} = path.node;
|
|
74
75
|
|
|
75
|
-
if (!isFirst(path) && !
|
|
76
|
+
if (!isFirst(path) && !hasPrevNewline(path) && !path.parentPath.isExportDeclaration())
|
|
76
77
|
print('\n');
|
|
77
78
|
|
|
78
79
|
maybe.print(async, 'async ');
|
|
79
|
-
|
|
80
80
|
print('function ');
|
|
81
81
|
print(path.get('id'));
|
|
82
82
|
print('(');
|
|
@@ -112,4 +112,3 @@ module.exports.ClassMethod = (path, {print}) => {
|
|
|
112
112
|
print(') ');
|
|
113
113
|
print(path.get('body'));
|
|
114
114
|
};
|
|
115
|
-
|
|
@@ -19,6 +19,7 @@ const {AssignmentPattern} = require('./assignment-pattern');
|
|
|
19
19
|
const {RestElement} = require('./rest-element');
|
|
20
20
|
const {SpreadElement} = require('./spread-element');
|
|
21
21
|
const {SequenceExpression} = require('./sequence-expression');
|
|
22
|
+
const {TaggedTemplateExpression} = require('./tagged-template-expression');
|
|
22
23
|
|
|
23
24
|
module.exports = {
|
|
24
25
|
...functions,
|
|
@@ -37,6 +38,7 @@ module.exports = {
|
|
|
37
38
|
RestElement,
|
|
38
39
|
SpreadElement,
|
|
39
40
|
SequenceExpression,
|
|
41
|
+
TaggedTemplateExpression,
|
|
40
42
|
BinaryExpression(path, {traverse, write}) {
|
|
41
43
|
traverse(path.get('left'));
|
|
42
44
|
write(` ${path.node.operator} `);
|
package/lib/tokenize/mark.js
CHANGED
|
@@ -13,7 +13,6 @@ function markBefore(path) {
|
|
|
13
13
|
function markAfter(path) {
|
|
14
14
|
path[WATER_MARK_AFTER] = true;
|
|
15
15
|
}
|
|
16
|
-
|
|
17
16
|
module.exports.isMarkedBefore = isMarkedBefore;
|
|
18
17
|
module.exports.isMarkedAfter = isMarkedAfter;
|
|
19
18
|
|
|
@@ -32,7 +31,3 @@ module.exports.hasPrevNewline = (path) => {
|
|
|
32
31
|
module.exports.isMarkedParentBefore = (path) => {
|
|
33
32
|
return isMarkedBefore(path.parentPath);
|
|
34
33
|
};
|
|
35
|
-
|
|
36
|
-
module.exports.isMarkedPrevAfter = (path) => {
|
|
37
|
-
return isMarkedAfter(path.getPrevSibling());
|
|
38
|
-
};
|
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
const {
|
|
4
4
|
markBefore,
|
|
5
5
|
markAfter,
|
|
6
|
-
|
|
6
|
+
hasPrevNewline,
|
|
7
7
|
} = require('../mark');
|
|
8
|
+
|
|
8
9
|
const {isNext} = require('../is');
|
|
9
10
|
|
|
10
11
|
module.exports.ExpressionStatement = (path, {write, indent, traverse, print}) => {
|
|
11
|
-
if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path) && !
|
|
12
|
+
if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewLine(path) && !hasPrevNewline(path)) {
|
|
12
13
|
print.breakline();
|
|
13
14
|
markBefore(path);
|
|
14
15
|
}
|
|
@@ -27,8 +28,8 @@ module.exports.ExpressionStatement = (path, {write, indent, traverse, print}) =>
|
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
function isCoupleLinesExpression(path) {
|
|
30
|
-
const start = path.node.loc?.start
|
|
31
|
-
const end = path.node.loc?.end
|
|
31
|
+
const start = path.node.loc?.start?.line;
|
|
32
|
+
const end = path.node.loc?.end?.line;
|
|
32
33
|
|
|
33
34
|
return end > start;
|
|
34
35
|
}
|
|
@@ -62,4 +63,3 @@ function shouldAddNewLine(path) {
|
|
|
62
63
|
|
|
63
64
|
return true;
|
|
64
65
|
}
|
|
65
|
-
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {hasPrevNewline} = require('../mark');
|
|
4
|
+
|
|
4
5
|
const {isFirst} = require('../is');
|
|
5
6
|
|
|
6
7
|
module.exports.ForOfStatement = (path, {indent, print}) => {
|
|
7
|
-
if (!isFirst(path) && !
|
|
8
|
+
if (!isFirst(path) && !hasPrevNewline(path)) {
|
|
8
9
|
print.indent();
|
|
9
10
|
print.newline();
|
|
10
11
|
}
|
|
@@ -31,4 +32,3 @@ module.exports.ForOfStatement = (path, {indent, print}) => {
|
|
|
31
32
|
print(bodyPath);
|
|
32
33
|
}
|
|
33
34
|
};
|
|
34
|
-
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
|
|
3
|
+
const {
|
|
4
|
+
isNext,
|
|
5
|
+
isCoupleLines,
|
|
6
|
+
} = require('../is');
|
|
7
|
+
const {
|
|
8
|
+
hasPrevNewline,
|
|
9
|
+
markAfter,
|
|
10
|
+
} = require('../mark');
|
|
11
|
+
|
|
5
12
|
const isNextAssign = (path) => {
|
|
6
13
|
const nextPath = path.getNextSibling();
|
|
7
14
|
|
|
@@ -12,7 +19,7 @@ const isNextAssign = (path) => {
|
|
|
12
19
|
};
|
|
13
20
|
|
|
14
21
|
module.exports.VariableDeclaration = (path, {maybe, print}) => {
|
|
15
|
-
if (!isFirst(path) &&
|
|
22
|
+
if (!isFirst(path) && shouldAddNewLineBefore(path) && !hasPrevNewline(path))
|
|
16
23
|
print.breakline();
|
|
17
24
|
|
|
18
25
|
const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type);
|
|
@@ -28,18 +35,36 @@ module.exports.VariableDeclaration = (path, {maybe, print}) => {
|
|
|
28
35
|
maybe.print(isParentBlock, ';');
|
|
29
36
|
maybe.print.newline(isParentBlock);
|
|
30
37
|
|
|
31
|
-
const is =
|
|
38
|
+
const is = shouldAddNewLineAfter(path);
|
|
32
39
|
|
|
33
40
|
if (is) {
|
|
34
41
|
print.linebreak();
|
|
35
42
|
markAfter(path);
|
|
36
43
|
}
|
|
37
|
-
|
|
38
|
-
if (path.parentPath.isProgram() && !isNext(path))
|
|
39
|
-
print.linebreak();
|
|
40
44
|
};
|
|
41
45
|
|
|
42
|
-
function
|
|
46
|
+
function shouldAddNewLineAfter(path) {
|
|
47
|
+
if (isLast(path))
|
|
48
|
+
return true;
|
|
49
|
+
|
|
50
|
+
if (!isNext(path))
|
|
51
|
+
return false;
|
|
52
|
+
|
|
53
|
+
if (isCoupleLines(path))
|
|
54
|
+
return true;
|
|
55
|
+
|
|
56
|
+
if (isNextAssign(path))
|
|
57
|
+
return true;
|
|
58
|
+
|
|
59
|
+
if (isCoupleLines(path.getNextSibling()))
|
|
60
|
+
return true;
|
|
61
|
+
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
|
|
66
|
+
|
|
67
|
+
function shouldAddNewLineBefore(path) {
|
|
43
68
|
const prevPath = path.getPrevSibling();
|
|
44
69
|
|
|
45
70
|
if (prevPath.isStatement() && !prevPath.isExpressionStatement() && !prevPath.isBlockStatement())
|
|
@@ -53,7 +78,6 @@ function shouldAddNewLine(path) {
|
|
|
53
78
|
|
|
54
79
|
const nextPath = path.getNextSibling();
|
|
55
80
|
const nextNextPath = nextPath.getNextSibling();
|
|
56
|
-
|
|
57
81
|
const twoNext = nextPath.node && nextNextPath.node;
|
|
58
82
|
|
|
59
83
|
if (!twoNext)
|
|
@@ -65,4 +89,3 @@ function shouldAddNewLine(path) {
|
|
|
65
89
|
function isFirst(path) {
|
|
66
90
|
return path.node === path.parentPath.node.body[0];
|
|
67
91
|
}
|
|
68
|
-
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.13",
|
|
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",
|