@putout/printer 1.16.1 → 1.17.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 +11 -0
- package/lib/tokenize/expressions/binary-expression.js +10 -3
- package/lib/tokenize/expressions/call-expression.js +4 -2
- package/lib/tokenize/expressions/object-expression.js +13 -6
- package/lib/tokenize/expressions/unary-expressions.js +9 -9
- package/lib/tokenize/is.js +1 -1
- package/lib/tokenize/statements/if-statement.js +3 -0
- package/lib/tokenize/statements/variable-declaration.js +31 -23
- package/package.json +4 -3
package/ChangeLog
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
2023.04.01, v1.17.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- 1378835 @putout/printer: improve support of round brackets around LogicalExpressions
|
|
5
|
+
- 3fc47f2 @putout/printer: improve support of BinaryExpression inside UnaryExpression
|
|
6
|
+
|
|
7
|
+
2023.04.01, v1.16.2
|
|
8
|
+
|
|
9
|
+
feature:
|
|
10
|
+
- 6da9571 @putout/printer: improve support of ObjectExpression used as argument of CallExpression
|
|
11
|
+
|
|
1
12
|
2023.04.01, v1.16.1
|
|
2
13
|
|
|
3
14
|
feature:
|
|
@@ -3,13 +3,20 @@
|
|
|
3
3
|
module.exports.BinaryExpression = BinaryExpression;
|
|
4
4
|
module.exports.LogicalExpression = BinaryExpression;
|
|
5
5
|
|
|
6
|
-
const isLogical = (path) =>
|
|
6
|
+
const isLogical = (main, path) => {
|
|
7
|
+
const is = path.isLogicalExpression();
|
|
8
|
+
|
|
9
|
+
if (!is)
|
|
10
|
+
return false;
|
|
11
|
+
|
|
12
|
+
return main.node.operator !== path.node.operator;
|
|
13
|
+
};
|
|
7
14
|
|
|
8
15
|
function BinaryExpression(path, {write, traverse, maybe}) {
|
|
9
16
|
const left = path.get('left');
|
|
10
17
|
const right = path.get('right');
|
|
11
|
-
const isLeft = isLogical(left);
|
|
12
|
-
const isRight = isLogical(right);
|
|
18
|
+
const isLeft = isLogical(path, left);
|
|
19
|
+
const isRight = isLogical(path, right);
|
|
13
20
|
|
|
14
21
|
maybe.write(isLeft, '(');
|
|
15
22
|
traverse(left);
|
|
@@ -27,7 +27,9 @@ const CallExpression = {
|
|
|
27
27
|
maybe.indent.inc(isParentCall);
|
|
28
28
|
|
|
29
29
|
for (const [i, arg] of args.entries()) {
|
|
30
|
-
|
|
30
|
+
const isObject = arg.isObjectExpression();
|
|
31
|
+
|
|
32
|
+
if (isParentCall && !isObject) {
|
|
31
33
|
print.newline();
|
|
32
34
|
indent();
|
|
33
35
|
}
|
|
@@ -80,7 +82,7 @@ function toLong(path) {
|
|
|
80
82
|
const args = path.get('arguments');
|
|
81
83
|
|
|
82
84
|
for (const arg of args) {
|
|
83
|
-
if (arg.isIdentifier() && arg.node.name.length >
|
|
85
|
+
if (arg.isIdentifier() && arg.node.name.length > 10)
|
|
84
86
|
return true;
|
|
85
87
|
}
|
|
86
88
|
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
const {isCoupleLines} = require('../is');
|
|
4
4
|
const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
|
|
5
5
|
const isLogical = (path) => path.get('argument').isLogicalExpression();
|
|
6
|
+
const isValue = (path) => path.get('properties.0.value').node;
|
|
7
|
+
const isIf = (path) => path.parentPath.parentPath.isIfStatement();
|
|
6
8
|
|
|
7
9
|
const isForOf = (path) => {
|
|
8
10
|
if (path.parentPath.isForOfStatement())
|
|
@@ -74,18 +76,23 @@ module.exports.ObjectProperty = (path, {print, maybe}) => {
|
|
|
74
76
|
maybe.print(manyLines, ',');
|
|
75
77
|
};
|
|
76
78
|
|
|
79
|
+
const ONE_LINE = true;
|
|
80
|
+
const MANY_LINES = false;
|
|
81
|
+
|
|
77
82
|
function isOneLine(path) {
|
|
78
|
-
const isCall = path.parentPath.isCallExpression();
|
|
79
83
|
const {length} = path.get('properties');
|
|
80
84
|
|
|
81
85
|
if (!length)
|
|
82
86
|
return true;
|
|
83
87
|
|
|
84
|
-
if (
|
|
85
|
-
return
|
|
88
|
+
if (isForOf(path))
|
|
89
|
+
return ONE_LINE;
|
|
86
90
|
|
|
87
|
-
if (
|
|
88
|
-
return
|
|
91
|
+
if (isIf(path))
|
|
92
|
+
return ONE_LINE;
|
|
93
|
+
|
|
94
|
+
if (isCoupleLines(path))
|
|
95
|
+
return MANY_LINES;
|
|
89
96
|
|
|
90
|
-
return
|
|
97
|
+
return !isValue(path);
|
|
91
98
|
}
|
|
@@ -32,21 +32,21 @@ function printUnary(path, name, {print}) {
|
|
|
32
32
|
}
|
|
33
33
|
const isWord = (a) => /delete|typeof/.test(a);
|
|
34
34
|
|
|
35
|
-
function unaryExpression(path, {
|
|
35
|
+
function unaryExpression(path, {maybe, traverse}) {
|
|
36
36
|
const {
|
|
37
37
|
prefix,
|
|
38
38
|
operator,
|
|
39
39
|
} = path.node;
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
const argPath = path.get('argument');
|
|
42
|
+
const round = argPath.isBinaryExpression();
|
|
43
43
|
|
|
44
|
-
maybe.print(
|
|
45
|
-
operator,
|
|
46
|
-
), ' ');
|
|
44
|
+
maybe.print(prefix, operator);
|
|
47
45
|
|
|
48
|
-
print('
|
|
46
|
+
maybe.print(isWord(operator), ' ');
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
maybe.print(round, '(');
|
|
49
|
+
traverse(argPath);
|
|
50
|
+
maybe.print(round, ')');
|
|
51
|
+
maybe.print(!prefix, operator);
|
|
52
52
|
}
|
package/lib/tokenize/is.js
CHANGED
|
@@ -15,7 +15,7 @@ const isNext = (path) => {
|
|
|
15
15
|
const isNextParent = (path) => isNext(path.parentPath);
|
|
16
16
|
const isLast = (path) => isParentProgram(path) && !isNext(path);
|
|
17
17
|
|
|
18
|
-
module.exports.isFirst = (path) => path.node === path.parentPath.node.body[0];
|
|
18
|
+
module.exports.isFirst = (path) => path.node === path.parentPath.node.body?.[0];
|
|
19
19
|
module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement();
|
|
20
20
|
module.exports.isNext = isNext;
|
|
21
21
|
module.exports.isNextParent = isNextParent;
|
|
@@ -12,32 +12,38 @@ const {
|
|
|
12
12
|
|
|
13
13
|
const isParentBlock = (path) => /Program|BlockStatement/.test(path.parentPath.type);
|
|
14
14
|
|
|
15
|
-
module.exports.VariableDeclaration =
|
|
16
|
-
|
|
15
|
+
module.exports.VariableDeclaration = {
|
|
16
|
+
beforeIf: shouldAddNewlineBefore,
|
|
17
|
+
before(path, {print}) {
|
|
17
18
|
print.breakline();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
19
|
+
},
|
|
20
|
+
print(path, {maybe, print, store}) {
|
|
21
|
+
maybe.indent(isParentBlock(path));
|
|
22
|
+
print(`${path.node.kind} `);
|
|
23
|
+
print('__declarations.0.id');
|
|
24
|
+
|
|
25
|
+
const initPath = path.get('declarations.0.init');
|
|
26
|
+
|
|
27
|
+
maybe.print(initPath.node, ' = ');
|
|
28
|
+
print(initPath);
|
|
29
|
+
maybe.print(isParentBlock(path), ';');
|
|
30
|
+
|
|
31
|
+
let wasNewline = false;
|
|
32
|
+
|
|
33
|
+
if (isParentBlock(path) && isNext(path)) {
|
|
34
|
+
print.newline();
|
|
35
|
+
wasNewline = true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
store(wasNewline);
|
|
39
|
+
},
|
|
40
|
+
afterIf: shouldAddNewlineAfter,
|
|
41
|
+
after(path, {maybe, store}) {
|
|
42
|
+
const wasNewline = store();
|
|
37
43
|
maybe.print.linebreak(wasNewline);
|
|
38
44
|
maybe.print.newline(!wasNewline);
|
|
39
45
|
markAfter(path);
|
|
40
|
-
}
|
|
46
|
+
},
|
|
41
47
|
};
|
|
42
48
|
|
|
43
49
|
function shouldAddNewlineAfter(path) {
|
|
@@ -53,7 +59,9 @@ function shouldAddNewlineAfter(path) {
|
|
|
53
59
|
if (isNextAssign(path))
|
|
54
60
|
return true;
|
|
55
61
|
|
|
56
|
-
|
|
62
|
+
const next = path.getNextSibling();
|
|
63
|
+
|
|
64
|
+
if (isCoupleLines(next))
|
|
57
65
|
return true;
|
|
58
66
|
|
|
59
67
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.17.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",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
],
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@putout/test": "^6.0.1",
|
|
47
|
+
"acorn": "^8.8.2",
|
|
47
48
|
"c8": "^7.5.0",
|
|
48
49
|
"eslint": "^8.0.1",
|
|
49
50
|
"eslint-plugin-n": "^15.2.4",
|
|
@@ -55,9 +56,9 @@
|
|
|
55
56
|
"montag": "^1.0.0",
|
|
56
57
|
"nodemon": "^2.0.1",
|
|
57
58
|
"putout": "*",
|
|
59
|
+
"putout-plugin-apply-computed-print": "./rules/putout-plugin-apply-computed-print",
|
|
58
60
|
"supertape": "^8.0.0",
|
|
59
|
-
"try-catch": "^3.0.0"
|
|
60
|
-
"putout-plugin-apply-computed-print": "./rules/putout-plugin-apply-computed-print"
|
|
61
|
+
"try-catch": "^3.0.0"
|
|
61
62
|
},
|
|
62
63
|
"license": "MIT",
|
|
63
64
|
"engines": {
|