@putout/printer 1.57.0 → 1.59.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 +10 -0
- package/lib/tokenize/comments.js +9 -2
- package/lib/tokenize/expressions/class.js +7 -1
- package/lib/tokenize/expressions/functions/arrow-function-expression.js +8 -1
- package/lib/tokenize/expressions/functions/class-method.js +32 -0
- package/lib/tokenize/expressions/functions/function-declaration.js +12 -0
- package/lib/tokenize/expressions/functions/functions.js +2 -23
- package/lib/tokenize/jsx/jsx-element.js +16 -1
- package/lib/tokenize/jsx/jsx-opening-element.js +25 -23
- package/lib/tokenize/literals/index.js +4 -0
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/lib/tokenize/comments.js
CHANGED
|
@@ -10,12 +10,19 @@ module.exports.parseLeadingComments = (path, {print, maybe}) => {
|
|
|
10
10
|
if (!leadingComments || !leadingComments.length)
|
|
11
11
|
return;
|
|
12
12
|
|
|
13
|
-
const
|
|
13
|
+
const insideFn = path.parentPath.isFunction();
|
|
14
|
+
const isIndent = !path.isClassMethod() && !insideFn;
|
|
14
15
|
|
|
15
16
|
for (const {type, value} of leadingComments) {
|
|
16
|
-
maybe.indent(
|
|
17
|
+
maybe.indent(isIndent);
|
|
17
18
|
|
|
18
19
|
if (type === 'CommentLine') {
|
|
20
|
+
maybe.indent.inc(insideFn);
|
|
21
|
+
maybe.indent.inc(insideFn);
|
|
22
|
+
maybe.print.breakline(insideFn);
|
|
23
|
+
maybe.indent.dec(insideFn);
|
|
24
|
+
maybe.indent.dec(insideFn);
|
|
25
|
+
|
|
19
26
|
print(`//${value}`);
|
|
20
27
|
print.newline();
|
|
21
28
|
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const {isNext} = require('../is');
|
|
4
|
+
|
|
3
5
|
module.exports.ClassExpression = classVisitor;
|
|
4
6
|
module.exports.ClassDeclaration = classVisitor;
|
|
5
7
|
|
|
6
|
-
module.exports.ClassDeclaration = (path, {print, indent, maybe}) => {
|
|
8
|
+
module.exports.ClassDeclaration = (path, {print, indent, maybe, write}) => {
|
|
7
9
|
indent();
|
|
8
10
|
|
|
9
11
|
classVisitor(path, {
|
|
@@ -11,6 +13,10 @@ module.exports.ClassDeclaration = (path, {print, indent, maybe}) => {
|
|
|
11
13
|
indent,
|
|
12
14
|
maybe,
|
|
13
15
|
});
|
|
16
|
+
|
|
17
|
+
if (!path.parentPath.isExportDeclaration() && isNext(path)) {
|
|
18
|
+
write.newline();
|
|
19
|
+
}
|
|
14
20
|
};
|
|
15
21
|
|
|
16
22
|
function classVisitor(path, {print, indent, maybe}) {
|
|
@@ -37,7 +37,14 @@ module.exports.ArrowFunctionExpression = {
|
|
|
37
37
|
|
|
38
38
|
print.space();
|
|
39
39
|
print('=>');
|
|
40
|
-
|
|
40
|
+
|
|
41
|
+
const body = path.get('body');
|
|
42
|
+
|
|
43
|
+
const insideCall = path.parentPath.isCallExpression();
|
|
44
|
+
const isJSX = body.isJSXElement();
|
|
45
|
+
|
|
46
|
+
maybe.print.space(!insideCall && isJSX);
|
|
47
|
+
maybe.print.space(!isJSX);
|
|
41
48
|
print('__body');
|
|
42
49
|
},
|
|
43
50
|
after(path, {write}) {
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.ClassMethod = (path, {print, maybe}) => {
|
|
4
|
+
const {kind} = path.node;
|
|
5
|
+
const isConstructor = kind === 'constructor';
|
|
6
|
+
const isMethod = kind === 'method';
|
|
7
|
+
const isGetter = /get|set/.test(kind);
|
|
8
|
+
|
|
9
|
+
maybe.print(isConstructor, kind);
|
|
10
|
+
maybe.print(isMethod, '__key');
|
|
11
|
+
|
|
12
|
+
if (isGetter) {
|
|
13
|
+
print(kind);
|
|
14
|
+
print(' ');
|
|
15
|
+
print('__key');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
print('(');
|
|
19
|
+
|
|
20
|
+
const params = path.get('params');
|
|
21
|
+
const n = params.length;
|
|
22
|
+
|
|
23
|
+
for (let i = 0; i < n; i++) {
|
|
24
|
+
print(params[i]);
|
|
25
|
+
|
|
26
|
+
if (i < n - 1)
|
|
27
|
+
print(', ');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
print(') ');
|
|
31
|
+
print('__body');
|
|
32
|
+
};
|
|
@@ -2,8 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
const {markAfter} = require('../../mark');
|
|
4
4
|
const {isNext} = require('../../is');
|
|
5
|
+
const isFirst = (path) => !path.getPrevSibling().node;
|
|
5
6
|
|
|
6
7
|
module.exports.FunctionDeclaration = {
|
|
8
|
+
beforeIf(path) {
|
|
9
|
+
if (isFirst(path))
|
|
10
|
+
return false;
|
|
11
|
+
|
|
12
|
+
const prev = path.getPrevSibling();
|
|
13
|
+
|
|
14
|
+
return prev.isClassDeclaration();
|
|
15
|
+
},
|
|
16
|
+
before(path, {write}) {
|
|
17
|
+
write.newline();
|
|
18
|
+
},
|
|
7
19
|
print(path, {print, maybe}) {
|
|
8
20
|
const {async} = path.node;
|
|
9
21
|
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
const {ArrowFunctionExpression} = require('./arrow-function-expression');
|
|
4
4
|
const {FunctionDeclaration} = require('./function-declaration');
|
|
5
|
+
const {ClassMethod} = require('./class-method');
|
|
5
6
|
|
|
6
7
|
module.exports.ArrowFunctionExpression = ArrowFunctionExpression;
|
|
7
8
|
module.exports.FunctionDeclaration = FunctionDeclaration;
|
|
9
|
+
module.exports.ClassMethod = ClassMethod;
|
|
8
10
|
|
|
9
11
|
module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
|
|
10
12
|
const {node} = path;
|
|
@@ -63,26 +65,3 @@ module.exports.ObjectMethod = (path, {print}) => {
|
|
|
63
65
|
print(') ');
|
|
64
66
|
print('__body');
|
|
65
67
|
};
|
|
66
|
-
|
|
67
|
-
module.exports.ClassMethod = (path, {print, maybe}) => {
|
|
68
|
-
const {kind} = path.node;
|
|
69
|
-
const notMethod = kind !== 'method';
|
|
70
|
-
const notConstructor = kind !== 'constructor';
|
|
71
|
-
|
|
72
|
-
maybe.print(notMethod, `${kind} `);
|
|
73
|
-
maybe.print(notConstructor, '__key');
|
|
74
|
-
print('(');
|
|
75
|
-
|
|
76
|
-
const params = path.get('params');
|
|
77
|
-
const n = params.length;
|
|
78
|
-
|
|
79
|
-
for (let i = 0; i < n; i++) {
|
|
80
|
-
print(params[i]);
|
|
81
|
-
|
|
82
|
-
if (i < n - 1)
|
|
83
|
-
print(', ');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
print(') ');
|
|
87
|
-
print('__body');
|
|
88
|
-
};
|
|
@@ -7,10 +7,25 @@ module.exports.JSXElement = {
|
|
|
7
7
|
indent.inc();
|
|
8
8
|
write.breakline();
|
|
9
9
|
},
|
|
10
|
-
print(path, {print, traverse}) {
|
|
10
|
+
print(path, {print, traverse, indent, maybe}) {
|
|
11
|
+
const insideFn = path.parentPath.isArrowFunctionExpression();
|
|
12
|
+
const insideCall = path.parentPath.parentPath.isCallExpression();
|
|
13
|
+
|
|
14
|
+
if (insideFn && insideCall) {
|
|
15
|
+
indent.inc();
|
|
16
|
+
indent.inc();
|
|
17
|
+
indent();
|
|
18
|
+
}
|
|
19
|
+
|
|
11
20
|
print('__openingElement');
|
|
12
21
|
path.get('children').map(traverse);
|
|
13
22
|
print('__closingElement');
|
|
23
|
+
|
|
24
|
+
if (insideFn && insideCall) {
|
|
25
|
+
indent.dec();
|
|
26
|
+
maybe.print.breakline(insideCall);
|
|
27
|
+
indent.dec();
|
|
28
|
+
}
|
|
14
29
|
},
|
|
15
30
|
after(path, {write, indent}) {
|
|
16
31
|
indent.dec();
|
|
@@ -2,27 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
const {isCoupleLines} = require('../is');
|
|
4
4
|
|
|
5
|
-
module.exports.JSXOpeningElement =
|
|
6
|
-
print(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
5
|
+
module.exports.JSXOpeningElement = {
|
|
6
|
+
print(path, {print, maybe}) {
|
|
7
|
+
print('<');
|
|
8
|
+
print('__name');
|
|
9
|
+
|
|
10
|
+
const coupleLines = isCoupleLines(path);
|
|
11
|
+
const noCoupleLines = !coupleLines;
|
|
12
|
+
const shouldIndent = coupleLines && path.parentPath.parentPath.isJSXElement();
|
|
13
|
+
|
|
14
|
+
maybe.indent.inc(shouldIndent);
|
|
15
|
+
|
|
16
|
+
for (const attr of path.get('attributes')) {
|
|
17
|
+
maybe.print.space(noCoupleLines);
|
|
18
|
+
print(attr);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (isCoupleLines(path))
|
|
22
|
+
print.breakline();
|
|
23
|
+
|
|
24
|
+
if (path.node.selfClosing)
|
|
25
|
+
print('/');
|
|
26
|
+
|
|
27
|
+
print('>');
|
|
28
|
+
maybe.indent.dec(shouldIndent);
|
|
29
|
+
},
|
|
28
30
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.59.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",
|