@putout/printer 1.49.1 → 1.51.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 +15 -0
- package/lib/tokenize/comments.js +2 -15
- package/lib/tokenize/expressions/array-expression.js +8 -2
- package/lib/tokenize/expressions/call-expression.js +43 -46
- package/lib/tokenize/is.js +10 -5
- package/lib/tokenize/statements/expression-statement.js +4 -17
- package/lib/tokenize/statements/if-statement.js +1 -16
- package/lib/tokenize/statements/variable-declaration.js +4 -0
- package/lib/tokenize/typescript/ts-type-alias-declaration.js +2 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
2023.04.17, v1.51.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 0fc4fb0 @putout/printer: add ability to preserve newline between ExpressionStatements
|
|
5
|
+
- 8073d64 @putout/printer: CallExpression: simplify
|
|
6
|
+
- b94009b @putout/printer: add ability to preserve newline
|
|
7
|
+
|
|
8
|
+
2023.04.14, v1.50.0
|
|
9
|
+
|
|
10
|
+
fix:
|
|
11
|
+
- 6203f72 @putout/printer: ArrayExpression: one element CallExpression
|
|
12
|
+
|
|
13
|
+
feature:
|
|
14
|
+
- 491126f @putout/printer: ArrayExpression: two elements ReturnStatement: no newline
|
|
15
|
+
|
|
1
16
|
2023.04.14, v1.49.1
|
|
2
17
|
|
|
3
18
|
fix:
|
package/lib/tokenize/comments.js
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
isFirst,
|
|
5
|
-
isNext,
|
|
6
|
-
} = require('./is');
|
|
3
|
+
const {isNext} = require('./is');
|
|
7
4
|
|
|
8
|
-
const {
|
|
9
|
-
markBefore,
|
|
10
|
-
hasPrevNewline,
|
|
11
|
-
} = require('./mark');
|
|
5
|
+
const {markBefore} = require('./mark');
|
|
12
6
|
|
|
13
7
|
module.exports.parseLeadingComments = (path, {print, maybe}) => {
|
|
14
8
|
const {leadingComments} = path.node;
|
|
@@ -16,9 +10,6 @@ module.exports.parseLeadingComments = (path, {print, maybe}) => {
|
|
|
16
10
|
if (!leadingComments || !leadingComments.length)
|
|
17
11
|
return;
|
|
18
12
|
|
|
19
|
-
if (shouldAddNewlineBefore(path))
|
|
20
|
-
print.linebreak();
|
|
21
|
-
|
|
22
13
|
const isClass = !path.isClassMethod();
|
|
23
14
|
|
|
24
15
|
for (const {type, value} of leadingComments) {
|
|
@@ -65,10 +56,6 @@ module.exports.parseTrailingComments = (path, {write, maybe}) => {
|
|
|
65
56
|
}
|
|
66
57
|
};
|
|
67
58
|
|
|
68
|
-
function shouldAddNewlineBefore(path) {
|
|
69
|
-
return path.isStatement() && !isFirst(path) && !hasPrevNewline(path);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
59
|
function isSameLine(path, loc) {
|
|
73
60
|
return path.node.loc.start.line === loc.start.line;
|
|
74
61
|
}
|
|
@@ -21,6 +21,9 @@ const isStringAndString = ([a, b]) => isStringLiteral(a) && isStringLiteral(b);
|
|
|
21
21
|
const isIdentifierAndIdentifier = ([a, b]) => isIdentifier(a) && isIdentifier(b);
|
|
22
22
|
const isArrayParent = (path) => path.parentPath.isArrayExpression();
|
|
23
23
|
|
|
24
|
+
const isOneElementCall = (path, {elements}) => path.parentPath.isCallExpression() && elements.length === 1;
|
|
25
|
+
const isTwoElementReturn = (path, {elements}) => path.parentPath.isReturnStatement() && elements.length === 2;
|
|
26
|
+
|
|
24
27
|
const isTwoLongStrings = ([a, b]) => {
|
|
25
28
|
const LONG_STRING = 20;
|
|
26
29
|
|
|
@@ -172,10 +175,13 @@ function tooLong(path) {
|
|
|
172
175
|
}
|
|
173
176
|
|
|
174
177
|
function isNewlineBetweenElements(path, {elements}) {
|
|
175
|
-
if (
|
|
178
|
+
if (isTwoElementReturn(path, {elements}))
|
|
176
179
|
return false;
|
|
177
180
|
|
|
178
|
-
if (
|
|
181
|
+
if (isOneElementCall(path, {elements}))
|
|
182
|
+
return false;
|
|
183
|
+
|
|
184
|
+
if (isIncreaseIndent(path))
|
|
179
185
|
return false;
|
|
180
186
|
|
|
181
187
|
if (isInsideLoop(path))
|
|
@@ -2,58 +2,55 @@
|
|
|
2
2
|
|
|
3
3
|
const {exists} = require('../is');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
5
|
+
function CallExpression(path, {indent, print, maybe, traverse}) {
|
|
6
|
+
const isParentCall = toLong(path) && path.parentPath.isCallExpression();
|
|
7
|
+
const callee = path.get('callee');
|
|
8
|
+
const typeParameters = path.get('typeParameters');
|
|
9
|
+
const isFn = callee.isFunction();
|
|
10
|
+
|
|
11
|
+
maybe.write(isFn, '(');
|
|
12
|
+
traverse(callee);
|
|
13
|
+
|
|
14
|
+
if (exists(typeParameters))
|
|
15
|
+
traverse(typeParameters);
|
|
16
|
+
|
|
17
|
+
maybe.write(isFn, ')');
|
|
18
|
+
|
|
19
|
+
if (path.node.optional)
|
|
20
|
+
print('?.');
|
|
21
|
+
|
|
22
|
+
print('(');
|
|
23
|
+
|
|
24
|
+
const args = path.get('arguments');
|
|
25
|
+
const n = args.length - 1;
|
|
26
|
+
|
|
27
|
+
maybe.indent.inc(isParentCall);
|
|
28
|
+
|
|
29
|
+
for (const [i, arg] of args.entries()) {
|
|
30
|
+
const isObject = arg.isObjectExpression();
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (isParentCall && !isObject) {
|
|
34
|
-
print.newline();
|
|
35
|
-
indent();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
print(arg);
|
|
39
|
-
|
|
40
|
-
if (isParentCall) {
|
|
41
|
-
print(',');
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
maybe.print(i < n, ', ');
|
|
32
|
+
if (isParentCall && !isObject) {
|
|
33
|
+
print.newline();
|
|
34
|
+
indent();
|
|
46
35
|
}
|
|
47
36
|
|
|
37
|
+
print(arg);
|
|
38
|
+
|
|
48
39
|
if (isParentCall) {
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
print(',');
|
|
41
|
+
continue;
|
|
51
42
|
}
|
|
52
43
|
|
|
53
|
-
print('
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
44
|
+
maybe.print(i < n, ', ');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (isParentCall) {
|
|
48
|
+
indent.dec();
|
|
49
|
+
print.breakline();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
print(')');
|
|
53
|
+
}
|
|
57
54
|
module.exports.OptionalCallExpression = CallExpression;
|
|
58
55
|
module.exports.CallExpression = CallExpression;
|
|
59
56
|
|
package/lib/tokenize/is.js
CHANGED
|
@@ -38,7 +38,6 @@ module.exports.isParentBlock = isParentBlock;
|
|
|
38
38
|
module.exports.isLast = isLast;
|
|
39
39
|
module.exports.isParentLast = (path) => isLast(path.parentPath);
|
|
40
40
|
module.exports.isCoupleLines = isCoupleLines;
|
|
41
|
-
module.exports.isNextCoupleLines = isNextCoupleLines;
|
|
42
41
|
|
|
43
42
|
function isCoupleLines(path) {
|
|
44
43
|
const start = path.node?.loc?.start?.line;
|
|
@@ -47,10 +46,6 @@ function isCoupleLines(path) {
|
|
|
47
46
|
return end > start;
|
|
48
47
|
}
|
|
49
48
|
module.exports.exists = (a) => a.node;
|
|
50
|
-
|
|
51
|
-
function isNextCoupleLines(path) {
|
|
52
|
-
return isCoupleLines(path.getNextSibling());
|
|
53
|
-
}
|
|
54
49
|
module.exports.isStringAndIdentifier = ([a, b]) => isStringLiteral(a) && isIdentifier(b);
|
|
55
50
|
|
|
56
51
|
const isIfOrStatement = (a) => isIfStatement(a) || isStatement(a);
|
|
@@ -71,3 +66,13 @@ module.exports.isForOf = (path) => {
|
|
|
71
66
|
|
|
72
67
|
return false;
|
|
73
68
|
};
|
|
69
|
+
|
|
70
|
+
module.exports.isNewlineBetweenStatements = (path) => {
|
|
71
|
+
const endCurrent = path.node?.loc?.end?.line;
|
|
72
|
+
const startNext = path.getNextSibling().node?.loc?.start?.line;
|
|
73
|
+
|
|
74
|
+
if (!startNext)
|
|
75
|
+
return false;
|
|
76
|
+
|
|
77
|
+
return startNext - endCurrent > 1;
|
|
78
|
+
};
|
|
@@ -5,20 +5,10 @@ const {
|
|
|
5
5
|
isParentProgram,
|
|
6
6
|
isLast,
|
|
7
7
|
isParentBlock,
|
|
8
|
-
isCoupleLines,
|
|
9
|
-
isNextCoupleLines,
|
|
10
8
|
isParentLast,
|
|
9
|
+
isNewlineBetweenStatements,
|
|
11
10
|
} = require('../is');
|
|
12
11
|
|
|
13
|
-
const isNextDifferent = (path) => {
|
|
14
|
-
const next = path.getNextSibling();
|
|
15
|
-
|
|
16
|
-
if (!next.isExpressionStatement())
|
|
17
|
-
return true;
|
|
18
|
-
|
|
19
|
-
return next.node.expression.type !== path.node.expression.type;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
12
|
module.exports.ExpressionStatement = {
|
|
23
13
|
print(path, {indent, print, maybe, store}) {
|
|
24
14
|
indent();
|
|
@@ -41,6 +31,9 @@ module.exports.ExpressionStatement = {
|
|
|
41
31
|
};
|
|
42
32
|
|
|
43
33
|
function shouldBreakline(path) {
|
|
34
|
+
if (isNewlineBetweenStatements(path))
|
|
35
|
+
return true;
|
|
36
|
+
|
|
44
37
|
if (isLast(path) || isParentLast(path))
|
|
45
38
|
return false;
|
|
46
39
|
|
|
@@ -53,12 +46,6 @@ function shouldBreakline(path) {
|
|
|
53
46
|
if (isStrictMode(path))
|
|
54
47
|
return true;
|
|
55
48
|
|
|
56
|
-
if (isNext(path) && isNextDifferent(path) && path.parentPath.node.body?.length > 2)
|
|
57
|
-
return true;
|
|
58
|
-
|
|
59
|
-
if (isCoupleLines(path) || isNextCoupleLines(path))
|
|
60
|
-
return true;
|
|
61
|
-
|
|
62
49
|
return false;
|
|
63
50
|
}
|
|
64
51
|
|
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
hasPrevNewline,
|
|
5
|
-
markAfter,
|
|
6
|
-
} = require('../mark');
|
|
3
|
+
const {markAfter} = require('../mark');
|
|
7
4
|
|
|
8
|
-
const {isFirst} = require('../is');
|
|
9
5
|
const isEmptyConsequent = (path) => path.get('consequent').isEmptyStatement();
|
|
10
6
|
|
|
11
7
|
module.exports.IfStatement = {
|
|
12
|
-
before: (path, {print}) => {
|
|
13
|
-
print.linebreak();
|
|
14
|
-
},
|
|
15
|
-
beforeIf: shouldAddNewlineBefore,
|
|
16
8
|
print: (path, {indent, print, maybe, write, traverse}) => {
|
|
17
9
|
indent();
|
|
18
10
|
print('if (');
|
|
@@ -66,10 +58,3 @@ module.exports.IfStatement = {
|
|
|
66
58
|
markAfter(path);
|
|
67
59
|
},
|
|
68
60
|
};
|
|
69
|
-
|
|
70
|
-
function shouldAddNewlineBefore(path) {
|
|
71
|
-
if (path.parentPath.isIfStatement())
|
|
72
|
-
return false;
|
|
73
|
-
|
|
74
|
-
return !isFirst(path) && !hasPrevNewline(path);
|
|
75
|
-
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const {
|
|
4
4
|
isNext,
|
|
5
5
|
isCoupleLines,
|
|
6
|
+
isNewlineBetweenStatements,
|
|
6
7
|
} = require('../is');
|
|
7
8
|
|
|
8
9
|
const {
|
|
@@ -67,6 +68,9 @@ function shouldAddNewlineAfter(path) {
|
|
|
67
68
|
if (prev.isVariableDeclaration() && !next.isVariableDeclaration())
|
|
68
69
|
return true;
|
|
69
70
|
|
|
71
|
+
if (isNewlineBetweenStatements(path))
|
|
72
|
+
return true;
|
|
73
|
+
|
|
70
74
|
return false;
|
|
71
75
|
}
|
|
72
76
|
const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.51.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",
|