@putout/printer 1.19.0 → 1.21.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/debug.js +4 -2
- package/lib/tokenize/expressions/call-expression.js +0 -32
- package/lib/tokenize/expressions/object-expression.js +8 -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 +2 -1
- package/lib/tokenize/statements/for-of-statement.js +34 -29
- package/lib/tokenize/statements/if-statement.js +4 -7
- package/lib/tokenize/statements/try-statements.js +2 -1
- package/lib/tokenize/statements/variable-declaration.js +5 -1
- package/lib/tokenize/tokenize.js +3 -1
- package/lib/tokenize/typescript/index.js +24 -1
- package/package.json +2 -2
- package/rules/putout-plugin-apply-computed-print/index.js +0 -21
package/ChangeLog
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
2023.04.04, v1.21.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- d5716be @putout/printer: ForOfStatement: convert
|
|
5
|
+
- 5bd0fe5 @putout/printer: improve newlines formatting
|
|
6
|
+
|
|
7
|
+
2023.04.03, v1.20.0
|
|
8
|
+
|
|
9
|
+
feature:
|
|
10
|
+
- 069b266 @putout/printer: add support of TSPropertySignature
|
|
11
|
+
|
|
1
12
|
2023.04.03, v1.19.0
|
|
2
13
|
|
|
3
14
|
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
|
}
|
|
@@ -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,17 @@ 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
|
+
}
|
|
65
|
+
|
|
59
66
|
module.exports.ObjectProperty = (path, {print, maybe}) => {
|
|
60
67
|
const {
|
|
61
68
|
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
|
};
|
|
@@ -72,7 +72,7 @@ function shouldAddNewLineAfter(path) {
|
|
|
72
72
|
if (isNext(path))
|
|
73
73
|
return true;
|
|
74
74
|
|
|
75
|
-
return
|
|
75
|
+
return path.findParent(isNext);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
function isStrictMode(path) {
|
|
@@ -85,3 +85,4 @@ function isStrictMode(path) {
|
|
|
85
85
|
|
|
86
86
|
return value === 'use strict';
|
|
87
87
|
}
|
|
88
|
+
|
|
@@ -11,40 +11,45 @@ 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
|
-
if (bodyPath.isExpressionStatement()) {
|
|
31
|
-
indent.inc();
|
|
32
|
-
print.newline();
|
|
33
|
-
print('__body');
|
|
34
|
-
indent.dec();
|
|
35
|
-
maybe.print.newline(isNext(path));
|
|
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');
|
|
36
32
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
+
}
|
|
42
|
+
|
|
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
|
};
|
|
55
|
+
|
|
@@ -37,12 +37,11 @@ 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();
|
|
@@ -50,10 +49,8 @@ module.exports.IfStatement = {
|
|
|
50
49
|
indent.dec();
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (next.isExpressionStatement() && !next.get('expression').isCallExpression())
|
|
56
|
-
print.newline();
|
|
52
|
+
//if (shouldAddNewline(path))
|
|
53
|
+
//print.newline();
|
|
57
54
|
},
|
|
58
55
|
afterIf: (path) => {
|
|
59
56
|
const next = path.getNextSibling();
|
|
@@ -25,7 +25,7 @@ module.exports.VariableDeclaration = {
|
|
|
25
25
|
const initPath = path.get('declarations.0.init');
|
|
26
26
|
|
|
27
27
|
maybe.print(initPath.node, ' = ');
|
|
28
|
-
print(
|
|
28
|
+
print('__declarations.0.init');
|
|
29
29
|
maybe.print(isParentBlock(path), ';');
|
|
30
30
|
|
|
31
31
|
let wasNewline = false;
|
|
@@ -60,10 +60,14 @@ function shouldAddNewlineAfter(path) {
|
|
|
60
60
|
return true;
|
|
61
61
|
|
|
62
62
|
const next = path.getNextSibling();
|
|
63
|
+
const prev = path.getPrevSibling();
|
|
63
64
|
|
|
64
65
|
if (isCoupleLines(next))
|
|
65
66
|
return true;
|
|
66
67
|
|
|
68
|
+
if (prev.isVariableDeclaration() && !next.isVariableDeclaration())
|
|
69
|
+
return true;
|
|
70
|
+
|
|
67
71
|
return false;
|
|
68
72
|
}
|
|
69
73
|
const isLast = (path) => path.parentPath.isProgram() && !isNext(path);
|
package/lib/tokenize/tokenize.js
CHANGED
|
@@ -234,11 +234,13 @@ const createPrint = (path, {traverse, write}) => (maybeLine) => {
|
|
|
234
234
|
};
|
|
235
235
|
|
|
236
236
|
const computePath = (path, maybeLine) => {
|
|
237
|
-
if (isString(maybeLine) && maybeLine.startsWith(GET))
|
|
237
|
+
if (isString(maybeLine) && maybeLine.startsWith(GET)) {
|
|
238
238
|
return get(path, maybeLine);
|
|
239
|
+
}
|
|
239
240
|
|
|
240
241
|
if (isObject(maybeLine))
|
|
241
242
|
return maybeLine;
|
|
242
243
|
|
|
243
244
|
return maybeLine;
|
|
244
245
|
};
|
|
246
|
+
|
|
@@ -35,7 +35,7 @@ module.exports = {
|
|
|
35
35
|
print('__typeAnnotation');
|
|
36
36
|
},
|
|
37
37
|
TSTypeAnnotation(path, {print}) {
|
|
38
|
-
if (
|
|
38
|
+
if (path.parentPath.isIdentifier()) {
|
|
39
39
|
print(':');
|
|
40
40
|
print.space();
|
|
41
41
|
}
|
|
@@ -55,4 +55,27 @@ module.exports = {
|
|
|
55
55
|
print('__typeAnnotation');
|
|
56
56
|
print(';');
|
|
57
57
|
},
|
|
58
|
+
TSTypeLiteral(path, {indent, traverse, write}) {
|
|
59
|
+
write('{');
|
|
60
|
+
write.newline();
|
|
61
|
+
indent.inc();
|
|
62
|
+
for (const member of path.get('members')) {
|
|
63
|
+
indent();
|
|
64
|
+
traverse(member);
|
|
65
|
+
write(';');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
write.newline();
|
|
69
|
+
indent.dec();
|
|
70
|
+
write('}');
|
|
71
|
+
},
|
|
72
|
+
TSPropertySignature(path, {print, maybe}) {
|
|
73
|
+
const {optional} = path.node;
|
|
74
|
+
print('__key');
|
|
75
|
+
maybe.print(optional, '?');
|
|
76
|
+
print(':');
|
|
77
|
+
print.space();
|
|
78
|
+
print('__typeAnnotation');
|
|
79
|
+
},
|
|
58
80
|
};
|
|
81
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.21.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",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"montag": "^1.0.0",
|
|
57
57
|
"nodemon": "^2.0.1",
|
|
58
58
|
"putout": "*",
|
|
59
|
-
"putout-plugin-
|
|
59
|
+
"putout-plugin-printer": "./rules",
|
|
60
60
|
"supertape": "^8.0.0",
|
|
61
61
|
"try-catch": "^3.0.0"
|
|
62
62
|
},
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {assign} = Object;
|
|
4
|
-
|
|
5
|
-
module.exports.report = () => `Use print('__path') instead of path.get(__path)`;
|
|
6
|
-
|
|
7
|
-
module.exports.replace = () => ({
|
|
8
|
-
'print(path.get(__a))': ({__a}) => {
|
|
9
|
-
const {
|
|
10
|
-
raw,
|
|
11
|
-
value,
|
|
12
|
-
} = __a;
|
|
13
|
-
|
|
14
|
-
assign(__a, {
|
|
15
|
-
value: `__${value}`,
|
|
16
|
-
raw: raw.replace(value, `__${value}`),
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
return 'print(__a)';
|
|
20
|
-
},
|
|
21
|
-
});
|