@putout/printer 1.20.0 → 1.22.0
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 +12 -0
- package/lib/tokenize/debug.js +4 -2
- package/lib/tokenize/expressions/array-expression.js +12 -4
- package/lib/tokenize/expressions/array-pattern.js +0 -1
- package/lib/tokenize/expressions/call-expression.js +0 -32
- package/lib/tokenize/expressions/class.js +1 -0
- package/lib/tokenize/expressions/functions.js +0 -1
- package/lib/tokenize/expressions/new-expression.js +0 -1
- package/lib/tokenize/expressions/object-expression.js +7 -1
- package/lib/tokenize/is.js +4 -0
- package/lib/tokenize/mark.js +0 -9
- package/lib/tokenize/statements/break-statement.js +6 -4
- package/lib/tokenize/statements/expression-statement.js +1 -1
- package/lib/tokenize/statements/for-of-statement.js +33 -29
- package/lib/tokenize/statements/if-statement.js +2 -8
- package/lib/tokenize/statements/switch-statement.js +0 -1
- package/lib/tokenize/statements/try-statements.js +1 -1
- package/lib/tokenize/statements/variable-declaration.js +5 -2
- package/lib/tokenize/tokenize.js +0 -1
- package/lib/tokenize/typescript/index.js +1 -1
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
2023.04.04, v1.22.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- c8fc7b7 @putout/printer: improve support of tuples
|
|
5
|
+
- 599bd93 @putout/printer: set @putout/printer as putout printer
|
|
6
|
+
|
|
7
|
+
2023.04.04, v1.21.0
|
|
8
|
+
|
|
9
|
+
feature:
|
|
10
|
+
- d5716be @putout/printer: ForOfStatement: convert
|
|
11
|
+
- 5bd0fe5 @putout/printer: improve newlines formatting
|
|
12
|
+
|
|
1
13
|
2023.04.03, v1.20.0
|
|
2
14
|
|
|
3
15
|
feature:
|
package/lib/tokenize/debug.js
CHANGED
|
@@ -52,8 +52,10 @@ module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({t
|
|
|
52
52
|
function createStore() {
|
|
53
53
|
let chunks = [];
|
|
54
54
|
|
|
55
|
-
return (
|
|
56
|
-
|
|
55
|
+
return (...args) => {
|
|
56
|
+
const [chunk] = args;
|
|
57
|
+
|
|
58
|
+
if (args.length) {
|
|
57
59
|
chunks.push(chunk);
|
|
58
60
|
return;
|
|
59
61
|
}
|
|
@@ -8,6 +8,7 @@ const isForOf = ({parentPath}) => parentPath.isForOfStatement();
|
|
|
8
8
|
const SECOND = 1;
|
|
9
9
|
|
|
10
10
|
const isStringAndArray = ([a, b]) => a?.isStringLiteral() && b?.isArrayExpression();
|
|
11
|
+
const isStringAndIdentifier = ([a, b]) => a?.isStringLiteral() && b?.isIdentifier();
|
|
11
12
|
const isArrayParent = (path) => path.parentPath.isArrayExpression();
|
|
12
13
|
const isTwoLongStrings = ([a, b]) => {
|
|
13
14
|
const LONG_STRING = 20;
|
|
@@ -27,9 +28,8 @@ module.exports.ArrayExpression = (path, {print, maybe, indent}) => {
|
|
|
27
28
|
|
|
28
29
|
print('[');
|
|
29
30
|
|
|
30
|
-
if (!isTwoLongStrings(elements)
|
|
31
|
-
indent.inc();
|
|
32
|
-
}
|
|
31
|
+
if (!isTwoLongStrings(elements))
|
|
32
|
+
maybe.indent.inc(!shouldIncreaseIndent);
|
|
33
33
|
|
|
34
34
|
const isNewLine = isNewlineBetweenElements(path, {elements});
|
|
35
35
|
const n = elements.length - 1;
|
|
@@ -133,13 +133,21 @@ function isNewlineBetweenElements(path, {elements}) {
|
|
|
133
133
|
if (isStringAndArray(elements))
|
|
134
134
|
return false;
|
|
135
135
|
|
|
136
|
+
if (isStringAndIdentifier(elements))
|
|
137
|
+
return false;
|
|
138
|
+
|
|
136
139
|
if (tooLong(path) || isCoupleLines(path) || !isNumbers(elements) && !isForOf(path) && isLastArg(path) && !isParentProperty(path))
|
|
137
140
|
return true;
|
|
138
141
|
|
|
139
142
|
return false;
|
|
140
143
|
}
|
|
141
144
|
|
|
142
|
-
function isTwoStringsDifferentLength(
|
|
145
|
+
function isTwoStringsDifferentLength(strings) {
|
|
146
|
+
const [a, b] = strings;
|
|
147
|
+
|
|
148
|
+
if (strings.length > 2)
|
|
149
|
+
return false;
|
|
150
|
+
|
|
143
151
|
if (!a?.isStringLiteral() || !b?.isStringLiteral())
|
|
144
152
|
return false;
|
|
145
153
|
|
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
isMarkedParentBefore,
|
|
5
|
-
hasPrevNewline,
|
|
6
|
-
} = require('../mark');
|
|
7
|
-
|
|
8
3
|
const CallExpression = {
|
|
9
|
-
beforeIf(path) {
|
|
10
|
-
return isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath);
|
|
11
|
-
},
|
|
12
|
-
before(path, {print}) {
|
|
13
|
-
print.breakline();
|
|
14
|
-
},
|
|
15
4
|
print(path, {indent, print, maybe}) {
|
|
16
5
|
const isParentCall = toLong(path) && path.parentPath.isCallExpression();
|
|
17
6
|
print('__callee');
|
|
@@ -57,27 +46,6 @@ const CallExpression = {
|
|
|
57
46
|
module.exports.OptionalCallExpression = CallExpression;
|
|
58
47
|
module.exports.CallExpression = CallExpression;
|
|
59
48
|
|
|
60
|
-
function isNewLineBefore({parentPath}) {
|
|
61
|
-
if (!parentPath.isExpressionStatement())
|
|
62
|
-
return false;
|
|
63
|
-
|
|
64
|
-
const prevPath = parentPath.getPrevSibling();
|
|
65
|
-
const prevPrevPath = prevPath.getPrevSibling();
|
|
66
|
-
|
|
67
|
-
if (prevPath.isStatement() && !prevPath.isExpressionStatement() && !prevPath.isVariableDeclaration())
|
|
68
|
-
return true;
|
|
69
|
-
|
|
70
|
-
const twoPrev = prevPath.node && prevPrevPath.node;
|
|
71
|
-
|
|
72
|
-
if (!twoPrev)
|
|
73
|
-
return false;
|
|
74
|
-
|
|
75
|
-
if (prevPath.isExpressionStatement() && prevPath.get('expression').isCallExpression())
|
|
76
|
-
return false;
|
|
77
|
-
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
49
|
function toLong(path) {
|
|
82
50
|
const args = path.get('arguments');
|
|
83
51
|
|
|
@@ -52,10 +52,16 @@ module.exports.ObjectExpression = (path, {print, maybe, indent}) => {
|
|
|
52
52
|
indent.dec();
|
|
53
53
|
maybe.indent(manyLines);
|
|
54
54
|
print('}');
|
|
55
|
-
maybe.print.newline(path
|
|
55
|
+
maybe.print.newline(shouldAddNewline(path));
|
|
56
56
|
maybe.print(parens, ')');
|
|
57
57
|
};
|
|
58
58
|
|
|
59
|
+
function shouldAddNewline(path) {
|
|
60
|
+
if (!path.parentPath.isLogicalExpression())
|
|
61
|
+
return false;
|
|
62
|
+
|
|
63
|
+
return path.parentPath.parentPath.isSpreadElement();
|
|
64
|
+
}
|
|
59
65
|
module.exports.ObjectProperty = (path, {print, maybe}) => {
|
|
60
66
|
const {
|
|
61
67
|
shorthand,
|
package/lib/tokenize/is.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {isMarkedAfter} = require('./mark');
|
|
3
4
|
const isParentProgram = (path) => path.parentPath?.isProgram();
|
|
4
5
|
const isParentBlock = (path) => path.parentPath.isBlockStatement();
|
|
5
6
|
|
|
@@ -9,6 +10,9 @@ const isNext = (path) => {
|
|
|
9
10
|
if (!next.node)
|
|
10
11
|
return false;
|
|
11
12
|
|
|
13
|
+
if (isMarkedAfter(path.get('body')))
|
|
14
|
+
return false;
|
|
15
|
+
|
|
12
16
|
return !next.isEmptyStatement();
|
|
13
17
|
};
|
|
14
18
|
|
package/lib/tokenize/mark.js
CHANGED
|
@@ -14,20 +14,11 @@ function markBefore(path) {
|
|
|
14
14
|
function markAfter(path) {
|
|
15
15
|
path[WATER_MARK_AFTER] = true;
|
|
16
16
|
}
|
|
17
|
-
module.exports.isMarkedBefore = isMarkedBefore;
|
|
18
17
|
module.exports.isMarkedAfter = isMarkedAfter;
|
|
19
18
|
|
|
20
|
-
function isMarkedBefore(path) {
|
|
21
|
-
return path[WATER_MARK_BEFORE];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
19
|
function isMarkedAfter(path) {
|
|
25
20
|
return path[WATER_MARK_AFTER];
|
|
26
21
|
}
|
|
27
22
|
module.exports.hasPrevNewline = (path) => {
|
|
28
23
|
return isMarkedAfter(path.getPrevSibling());
|
|
29
24
|
};
|
|
30
|
-
|
|
31
|
-
module.exports.isMarkedParentBefore = (path) => {
|
|
32
|
-
return isMarkedBefore(path.parentPath);
|
|
33
|
-
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
|
-
isNext,
|
|
5
4
|
isParentBlock,
|
|
6
5
|
isNextParent,
|
|
7
6
|
} = require('../is');
|
|
@@ -15,9 +14,12 @@ module.exports.BreakStatement = {
|
|
|
15
14
|
print('break;');
|
|
16
15
|
},
|
|
17
16
|
afterIf(path) {
|
|
18
|
-
if (
|
|
19
|
-
return
|
|
17
|
+
if (isParentBlock(path))
|
|
18
|
+
return true;
|
|
20
19
|
|
|
21
|
-
|
|
20
|
+
if (isNextParent(path))
|
|
21
|
+
return true;
|
|
22
|
+
|
|
23
|
+
return false;
|
|
22
24
|
},
|
|
23
25
|
};
|
|
@@ -11,40 +11,44 @@ const {
|
|
|
11
11
|
isNext,
|
|
12
12
|
} = require('../is');
|
|
13
13
|
|
|
14
|
-
module.exports.ForOfStatement =
|
|
15
|
-
|
|
14
|
+
module.exports.ForOfStatement = {
|
|
15
|
+
beforeIf(path) {
|
|
16
|
+
return !isFirst(path) && !hasPrevNewline(path);
|
|
17
|
+
},
|
|
18
|
+
before(path, {print}) {
|
|
16
19
|
print.indent();
|
|
17
20
|
print.newline();
|
|
18
21
|
markBefore(path);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
22
|
+
},
|
|
23
|
+
print(path, {indent, print, maybe}) {
|
|
24
|
+
indent();
|
|
25
|
+
print('for (');
|
|
26
|
+
print('__left');
|
|
27
|
+
print(' of ');
|
|
28
|
+
print('__right');
|
|
29
|
+
print(')');
|
|
30
|
+
|
|
31
|
+
const bodyPath = path.get('body');
|
|
32
|
+
|
|
33
|
+
if (bodyPath.isExpressionStatement()) {
|
|
34
|
+
indent.inc();
|
|
35
|
+
print.newline();
|
|
36
|
+
print('__body');
|
|
37
|
+
indent.dec();
|
|
38
|
+
maybe.print.newline(isNext(path));
|
|
39
|
+
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (isNext(path)) {
|
|
43
|
+
if (bodyPath.isBlockStatement()) {
|
|
44
|
+
print(' ');
|
|
45
|
+
print('__body');
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
afterIf: isNext,
|
|
49
|
+
after(path, {print}) {
|
|
46
50
|
print.indent();
|
|
47
51
|
print.newline();
|
|
48
52
|
markAfter(path);
|
|
49
|
-
}
|
|
53
|
+
},
|
|
50
54
|
};
|
|
@@ -37,23 +37,17 @@ module.exports.IfStatement = {
|
|
|
37
37
|
write(' else ');
|
|
38
38
|
traverse(alternate);
|
|
39
39
|
} else if (alternate.isIfStatement()) {
|
|
40
|
-
|
|
40
|
+
write.space();
|
|
41
41
|
write('else');
|
|
42
|
-
|
|
42
|
+
write.space();
|
|
43
43
|
traverse(alternate);
|
|
44
44
|
} else if (alternate.node) {
|
|
45
|
-
print.breakline();
|
|
46
45
|
write('else');
|
|
47
46
|
print.newline();
|
|
48
47
|
indent.inc();
|
|
49
48
|
traverse(alternate);
|
|
50
49
|
indent.dec();
|
|
51
50
|
}
|
|
52
|
-
|
|
53
|
-
const next = path.getNextSibling();
|
|
54
|
-
|
|
55
|
-
if (next.isExpressionStatement() && !next.get('expression').isCallExpression())
|
|
56
|
-
print.newline();
|
|
57
51
|
},
|
|
58
52
|
afterIf: (path) => {
|
|
59
53
|
const next = path.getNextSibling();
|
|
@@ -23,9 +23,8 @@ module.exports.VariableDeclaration = {
|
|
|
23
23
|
print('__declarations.0.id');
|
|
24
24
|
|
|
25
25
|
const initPath = path.get('declarations.0.init');
|
|
26
|
-
|
|
27
26
|
maybe.print(initPath.node, ' = ');
|
|
28
|
-
print(
|
|
27
|
+
print('__declarations.0.init');
|
|
29
28
|
maybe.print(isParentBlock(path), ';');
|
|
30
29
|
|
|
31
30
|
let wasNewline = false;
|
|
@@ -60,10 +59,14 @@ function shouldAddNewlineAfter(path) {
|
|
|
60
59
|
return true;
|
|
61
60
|
|
|
62
61
|
const next = path.getNextSibling();
|
|
62
|
+
const prev = path.getPrevSibling();
|
|
63
63
|
|
|
64
64
|
if (isCoupleLines(next))
|
|
65
65
|
return true;
|
|
66
66
|
|
|
67
|
+
if (prev.isVariableDeclaration() && !next.isVariableDeclaration())
|
|
68
|
+
return true;
|
|
69
|
+
|
|
67
70
|
return false;
|
|
68
71
|
}
|
|
69
72
|
const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
|
package/lib/tokenize/tokenize.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.22.0",
|
|
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",
|