@putout/printer 2.56.0 → 2.58.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/comment/comment.js +9 -0
- package/lib/tokenize/comment/maybe-inside-fn.js +12 -0
- package/lib/tokenize/comment/parse-comments.js +27 -0
- package/lib/tokenize/comment/parse-leading-comments.js +60 -0
- package/lib/tokenize/comment/parse-trailing-comments.js +39 -0
- package/lib/tokenize/expressions/functions/arrow-function-expression.js +9 -5
- package/lib/tokenize/expressions/functions/class-method.js +4 -5
- package/lib/tokenize/expressions/functions/function-declaration.js +4 -5
- package/lib/tokenize/expressions/functions/function-expression.js +9 -6
- package/lib/tokenize/expressions/functions/object-method.js +4 -5
- package/lib/tokenize/expressions/functions/params.js +7 -1
- package/lib/tokenize/expressions/object-expression/object-expression.js +1 -1
- package/lib/tokenize/jsx/index.js +1 -1
- package/lib/tokenize/literals/index.js +0 -1
- package/lib/tokenize/statements/block-statement/block-statement.js +1 -1
- package/lib/tokenize/statements/block-statement/get-directives.js +0 -1
- package/lib/tokenize/statements/program/program.js +1 -2
- package/lib/tokenize/statements/variable-declaration/variable-declaration.js +1 -1
- package/lib/tokenize/tokenize.js +1 -1
- package/package.json +1 -2
- package/lib/tokenize/comments/comments.js +0 -129
package/ChangeLog
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {parseLeadingComments} = require('./parse-leading-comments');
|
|
4
|
+
const {parseTrailingComments} = require('./parse-trailing-comments');
|
|
5
|
+
const {parseComments} = require('./parse-comments');
|
|
6
|
+
|
|
7
|
+
module.exports.parseLeadingComments = parseLeadingComments;
|
|
8
|
+
module.exports.parseTrailingComments = parseTrailingComments;
|
|
9
|
+
module.exports.parseComments = parseComments;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports.parseComments = (path, {write}, semantics) => {
|
|
4
|
+
if (!semantics.comments)
|
|
5
|
+
return;
|
|
6
|
+
|
|
7
|
+
const comments = path.node.comments || path.node.innerComments;
|
|
8
|
+
|
|
9
|
+
if (!comments)
|
|
10
|
+
return;
|
|
11
|
+
|
|
12
|
+
for (const {type, value} of comments) {
|
|
13
|
+
if (type === 'CommentLine') {
|
|
14
|
+
write.breakline();
|
|
15
|
+
write('//');
|
|
16
|
+
write(value);
|
|
17
|
+
write.newline();
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (type === 'CommentBlock') {
|
|
22
|
+
write('/*');
|
|
23
|
+
write(value);
|
|
24
|
+
write('*/');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {hasTrailingComment} = require('../is');
|
|
4
|
+
const {isVariableDeclarator} = require('@babel/types');
|
|
5
|
+
const {markBefore} = require('../mark');
|
|
6
|
+
const {maybeInsideFn} = require('./maybe-inside-fn');
|
|
7
|
+
|
|
8
|
+
module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
|
|
9
|
+
if (!semantics.comments)
|
|
10
|
+
return;
|
|
11
|
+
|
|
12
|
+
const {leadingComments} = path.node;
|
|
13
|
+
|
|
14
|
+
if (!leadingComments?.length)
|
|
15
|
+
return;
|
|
16
|
+
|
|
17
|
+
if (hasTrailingComment(path.getPrevSibling()))
|
|
18
|
+
return;
|
|
19
|
+
|
|
20
|
+
const insideFn = path.parentPath.isFunction();
|
|
21
|
+
const isProperty = path.isObjectProperty() || isVariableDeclarator(path);
|
|
22
|
+
const isIndent = !path.isClassMethod() && !insideFn && !isProperty;
|
|
23
|
+
|
|
24
|
+
for (const {type, value} of leadingComments) {
|
|
25
|
+
maybe.indent(isIndent);
|
|
26
|
+
|
|
27
|
+
if (type === 'CommentLine') {
|
|
28
|
+
maybeInsideFn(insideFn, {
|
|
29
|
+
print,
|
|
30
|
+
indent,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
maybe.print.space(isProperty);
|
|
34
|
+
print(`//${value}`);
|
|
35
|
+
|
|
36
|
+
maybe.print.breakline(isProperty);
|
|
37
|
+
maybe.print.newline(!isProperty);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (type === 'CommentBlock') {
|
|
42
|
+
const looksLikeMethod = path.isClassMethod();
|
|
43
|
+
const looksLikeDirective = path.isDirective();
|
|
44
|
+
const looksLikeProp = path.isObjectProperty();
|
|
45
|
+
|
|
46
|
+
if (looksLikeProp)
|
|
47
|
+
print.breakline();
|
|
48
|
+
|
|
49
|
+
print(`/*${value}*/`);
|
|
50
|
+
|
|
51
|
+
if (path.isStatement() || looksLikeDirective || looksLikeMethod || looksLikeProp) {
|
|
52
|
+
print.newline();
|
|
53
|
+
markBefore(path);
|
|
54
|
+
maybe.indent(looksLikeMethod || looksLikeProp);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {isLast} = require('../is');
|
|
4
|
+
|
|
5
|
+
function isSameLine(path, loc) {
|
|
6
|
+
return path.node.loc?.start.line === loc.start.line || path.node.loc?.end.line === loc.end.line;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
|
|
10
|
+
if (!semantics.comments)
|
|
11
|
+
return;
|
|
12
|
+
|
|
13
|
+
const {trailingComments} = path.node;
|
|
14
|
+
|
|
15
|
+
if (!trailingComments?.length)
|
|
16
|
+
return;
|
|
17
|
+
|
|
18
|
+
if (path.isDirective())
|
|
19
|
+
return;
|
|
20
|
+
|
|
21
|
+
for (const {type, value, loc} of trailingComments) {
|
|
22
|
+
const sameLine = isSameLine(path, loc);
|
|
23
|
+
|
|
24
|
+
if (type === 'CommentLine') {
|
|
25
|
+
maybe.write.space(sameLine);
|
|
26
|
+
maybe.indent(!sameLine);
|
|
27
|
+
|
|
28
|
+
write(`//${value}`);
|
|
29
|
+
maybe.write.newline(!isLast(path));
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (type === 'CommentBlock') {
|
|
34
|
+
maybe.write.space(sameLine);
|
|
35
|
+
write(`/*${value}*/`);
|
|
36
|
+
maybe.write.newline(!sameLine);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
@@ -10,15 +10,19 @@ module.exports.ArrowFunctionExpression = {
|
|
|
10
10
|
before(path, {write}) {
|
|
11
11
|
write('(');
|
|
12
12
|
},
|
|
13
|
-
print(path,
|
|
13
|
+
print(path, printer, semantics) {
|
|
14
|
+
const {
|
|
15
|
+
print,
|
|
16
|
+
maybe,
|
|
17
|
+
write,
|
|
18
|
+
traverse,
|
|
19
|
+
} = printer;
|
|
20
|
+
|
|
14
21
|
const {async} = path.node;
|
|
15
22
|
|
|
16
23
|
maybe.print(async, 'async ');
|
|
17
24
|
|
|
18
|
-
printParams(path,
|
|
19
|
-
print,
|
|
20
|
-
traverse,
|
|
21
|
-
});
|
|
25
|
+
printParams(path, printer, semantics);
|
|
22
26
|
|
|
23
27
|
const returnType = path.get('returnType');
|
|
24
28
|
|
|
@@ -4,7 +4,9 @@ const {isNext} = require('../../is');
|
|
|
4
4
|
const {printParams} = require('./params');
|
|
5
5
|
|
|
6
6
|
const ClassMethod = {
|
|
7
|
-
print(path,
|
|
7
|
+
print(path, printer, semantics) {
|
|
8
|
+
const {print, maybe} = printer;
|
|
9
|
+
|
|
8
10
|
const {kind, computed} = path.node;
|
|
9
11
|
const isConstructor = kind === 'constructor';
|
|
10
12
|
const isMethod = kind === 'method';
|
|
@@ -24,10 +26,7 @@ const ClassMethod = {
|
|
|
24
26
|
print('__key');
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
printParams(path,
|
|
28
|
-
print,
|
|
29
|
-
traverse,
|
|
30
|
-
});
|
|
29
|
+
printParams(path, printer, semantics);
|
|
31
30
|
|
|
32
31
|
print.space();
|
|
33
32
|
print('__body');
|
|
@@ -7,7 +7,9 @@ const {isNext, isNextParent} = require('../../is');
|
|
|
7
7
|
const {printParams} = require('./params');
|
|
8
8
|
|
|
9
9
|
module.exports.FunctionDeclaration = {
|
|
10
|
-
print(path,
|
|
10
|
+
print(path, printer, semantics) {
|
|
11
|
+
const {print, maybe} = printer;
|
|
12
|
+
|
|
11
13
|
const {async, generator} = path.node;
|
|
12
14
|
|
|
13
15
|
maybe.print(async, 'async ');
|
|
@@ -18,10 +20,7 @@ module.exports.FunctionDeclaration = {
|
|
|
18
20
|
print('__id');
|
|
19
21
|
print('__typeParameters');
|
|
20
22
|
|
|
21
|
-
printParams(path,
|
|
22
|
-
print,
|
|
23
|
-
traverse,
|
|
24
|
-
});
|
|
23
|
+
printParams(path, printer, semantics);
|
|
25
24
|
|
|
26
25
|
print.space();
|
|
27
26
|
|
|
@@ -3,9 +3,15 @@
|
|
|
3
3
|
const {exists} = require('../../is');
|
|
4
4
|
const {printParams} = require('./params');
|
|
5
5
|
|
|
6
|
-
module.exports.FunctionExpression = (path,
|
|
7
|
-
const {
|
|
6
|
+
module.exports.FunctionExpression = (path, printer, semantics) => {
|
|
7
|
+
const {
|
|
8
|
+
print,
|
|
9
|
+
maybe,
|
|
10
|
+
write,
|
|
11
|
+
traverse,
|
|
12
|
+
} = printer;
|
|
8
13
|
|
|
14
|
+
const {node} = path;
|
|
9
15
|
const {generator, async} = node;
|
|
10
16
|
|
|
11
17
|
maybe.write(async, 'async ');
|
|
@@ -19,10 +25,7 @@ module.exports.FunctionExpression = (path, {print, maybe, write, traverse}) => {
|
|
|
19
25
|
traverse(id);
|
|
20
26
|
}
|
|
21
27
|
|
|
22
|
-
printParams(path,
|
|
23
|
-
print,
|
|
24
|
-
traverse,
|
|
25
|
-
});
|
|
28
|
+
printParams(path, printer, semantics);
|
|
26
29
|
|
|
27
30
|
print.space();
|
|
28
31
|
print('__body');
|
|
@@ -10,7 +10,9 @@ module.exports.ObjectMethod = {
|
|
|
10
10
|
before(path, {write}) {
|
|
11
11
|
write('async ');
|
|
12
12
|
},
|
|
13
|
-
print(path,
|
|
13
|
+
print(path, printer, semantics) {
|
|
14
|
+
const {print, maybe} = printer;
|
|
15
|
+
|
|
14
16
|
const {
|
|
15
17
|
kind,
|
|
16
18
|
generator,
|
|
@@ -26,10 +28,7 @@ module.exports.ObjectMethod = {
|
|
|
26
28
|
print('__key');
|
|
27
29
|
maybe.print(computed, ']');
|
|
28
30
|
|
|
29
|
-
printParams(path,
|
|
30
|
-
print,
|
|
31
|
-
traverse,
|
|
32
|
-
});
|
|
31
|
+
printParams(path, printer, semantics);
|
|
33
32
|
|
|
34
33
|
print.space();
|
|
35
34
|
print('__body');
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const {parseComments} = require('../../comment/comment');
|
|
4
|
+
|
|
5
|
+
module.exports.printParams = (path, printer, semantics) => {
|
|
6
|
+
const {print, traverse} = printer;
|
|
7
|
+
|
|
4
8
|
printBraceOpen(path, {
|
|
5
9
|
print,
|
|
6
10
|
});
|
|
7
11
|
|
|
12
|
+
parseComments(path, printer, semantics);
|
|
13
|
+
|
|
8
14
|
const params = path.get('params');
|
|
9
15
|
const n = params.length;
|
|
10
16
|
|
|
@@ -11,7 +11,7 @@ const {
|
|
|
11
11
|
exists,
|
|
12
12
|
} = require('../../is');
|
|
13
13
|
|
|
14
|
-
const {parseComments} = require('../../
|
|
14
|
+
const {parseComments} = require('../../comment/comment');
|
|
15
15
|
const {isSpreadElement} = require('@babel/types');
|
|
16
16
|
|
|
17
17
|
const isBodyOfArrow = (path) => path.parentPath.node.body === path.node;
|
|
@@ -6,7 +6,7 @@ const {isCoupleLines} = require('../is');
|
|
|
6
6
|
const {JSXOpeningElement} = require('./jsx-opening-element');
|
|
7
7
|
const fragments = require('./jsx-fragment');
|
|
8
8
|
const {JSXText} = require('./jsx-text');
|
|
9
|
-
const {parseComments} = require('../
|
|
9
|
+
const {parseComments} = require('../comment/comment');
|
|
10
10
|
|
|
11
11
|
module.exports = {
|
|
12
12
|
...fragments,
|
|
@@ -13,7 +13,7 @@ const {
|
|
|
13
13
|
} = require('@babel/types');
|
|
14
14
|
|
|
15
15
|
const {markAfter} = require('../../mark');
|
|
16
|
-
const {parseComments} = require('../../
|
|
16
|
+
const {parseComments} = require('../../comment/comment');
|
|
17
17
|
const {insideIfWithNoBody} = require('./inside-if-with-no-body');
|
|
18
18
|
const {getDirectives} = require('./get-directives');
|
|
19
19
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {parseComments} = require('../../
|
|
3
|
+
const {parseComments} = require('../../comment/comment');
|
|
4
4
|
const {getDirectives} = require('../block-statement/get-directives');
|
|
5
5
|
|
|
6
6
|
module.exports.Program = (path, {print, write, traverse, maybe}, semantics) => {
|
|
@@ -24,4 +24,3 @@ module.exports.Program = (path, {print, write, traverse, maybe}, semantics) => {
|
|
|
24
24
|
|
|
25
25
|
print.newline();
|
|
26
26
|
};
|
|
27
|
-
|
|
@@ -13,7 +13,7 @@ const {isExportDeclaration} = require('@babel/types');
|
|
|
13
13
|
const {maybeSpaceAfterKeyword} = require('./maybe-space-after-keyword');
|
|
14
14
|
|
|
15
15
|
const {isConcatenation} = require('../../expressions/binary-expression/concatanate');
|
|
16
|
-
const {parseLeadingComments} = require('../../
|
|
16
|
+
const {parseLeadingComments} = require('../../comment/comment');
|
|
17
17
|
|
|
18
18
|
const isParentBlock = (path) => /Program|BlockStatement|Export/.test(path.parentPath.type);
|
|
19
19
|
const isInsideBlock = (path) => /^(Program|BlockStatement)$/.test(path.parentPath.type);
|
package/lib/tokenize/tokenize.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/printer",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.58.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "Simplest possible opinionated Babel AST printer for 🐊Putout",
|
|
@@ -53,7 +53,6 @@
|
|
|
53
53
|
"@putout/plugin-promises": "^11.0.0",
|
|
54
54
|
"@putout/plugin-react-hook-form": "^3.4.1",
|
|
55
55
|
"@putout/plugin-react-hooks": "^5.0.0",
|
|
56
|
-
"@putout/test": "^6.0.1",
|
|
57
56
|
"acorn": "^8.8.2",
|
|
58
57
|
"c8": "^8.0.0",
|
|
59
58
|
"escover": "^3.0.0",
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const {
|
|
4
|
-
hasTrailingComment,
|
|
5
|
-
isLast,
|
|
6
|
-
} = require('../is');
|
|
7
|
-
|
|
8
|
-
const {markBefore} = require('../mark');
|
|
9
|
-
const {isVariableDeclarator} = require('@babel/types');
|
|
10
|
-
|
|
11
|
-
module.exports.parseLeadingComments = (path, {print, maybe, indent}, semantics) => {
|
|
12
|
-
if (!semantics.comments)
|
|
13
|
-
return;
|
|
14
|
-
|
|
15
|
-
const {leadingComments} = path.node;
|
|
16
|
-
|
|
17
|
-
if (!leadingComments?.length)
|
|
18
|
-
return;
|
|
19
|
-
|
|
20
|
-
if (hasTrailingComment(path.getPrevSibling()))
|
|
21
|
-
return;
|
|
22
|
-
|
|
23
|
-
const insideFn = path.parentPath.isFunction();
|
|
24
|
-
const isProperty = path.isObjectProperty() || isVariableDeclarator(path);
|
|
25
|
-
const isIndent = !path.isClassMethod() && !insideFn && !isProperty;
|
|
26
|
-
|
|
27
|
-
for (const {type, value} of leadingComments) {
|
|
28
|
-
maybe.indent(isIndent);
|
|
29
|
-
|
|
30
|
-
if (type === 'CommentLine') {
|
|
31
|
-
maybeInsideFn(insideFn, {
|
|
32
|
-
print,
|
|
33
|
-
indent,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
maybe.print.space(isProperty);
|
|
37
|
-
print(`//${value}`);
|
|
38
|
-
|
|
39
|
-
maybe.print.breakline(isProperty);
|
|
40
|
-
maybe.print.newline(!isProperty);
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (type === 'CommentBlock') {
|
|
45
|
-
print(`/*${value}*/`);
|
|
46
|
-
|
|
47
|
-
if (path.isStatement() || path.isClassMethod() || path.isDirective()) {
|
|
48
|
-
print.newline();
|
|
49
|
-
markBefore(path);
|
|
50
|
-
maybe.indent(path.isClassMethod());
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
module.exports.parseTrailingComments = (path, {write, maybe}, semantics) => {
|
|
59
|
-
if (!semantics.comments)
|
|
60
|
-
return;
|
|
61
|
-
|
|
62
|
-
const {trailingComments} = path.node;
|
|
63
|
-
|
|
64
|
-
if (!trailingComments?.length)
|
|
65
|
-
return;
|
|
66
|
-
|
|
67
|
-
if (path.isDirective())
|
|
68
|
-
return;
|
|
69
|
-
|
|
70
|
-
for (const {type, value, loc} of trailingComments) {
|
|
71
|
-
const sameLine = isSameLine(path, loc);
|
|
72
|
-
|
|
73
|
-
if (type === 'CommentLine') {
|
|
74
|
-
maybe.write.space(sameLine);
|
|
75
|
-
maybe.indent(!sameLine);
|
|
76
|
-
|
|
77
|
-
write(`//${value}`);
|
|
78
|
-
maybe.write.newline(!isLast(path));
|
|
79
|
-
continue;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (type === 'CommentBlock') {
|
|
83
|
-
maybe.write.space(sameLine);
|
|
84
|
-
write(`/*${value}*/`);
|
|
85
|
-
maybe.write.newline(!sameLine);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
module.exports.parseComments = (path, {write}, semantics) => {
|
|
91
|
-
if (!semantics.comments)
|
|
92
|
-
return;
|
|
93
|
-
|
|
94
|
-
const comments = path.node.comments || path.node.innerComments;
|
|
95
|
-
|
|
96
|
-
if (!comments)
|
|
97
|
-
return;
|
|
98
|
-
|
|
99
|
-
for (const {type, value} of comments) {
|
|
100
|
-
if (type === 'CommentLine') {
|
|
101
|
-
write.breakline();
|
|
102
|
-
write('//');
|
|
103
|
-
write(value);
|
|
104
|
-
write.newline();
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (type === 'CommentBlock') {
|
|
109
|
-
write('/*');
|
|
110
|
-
write(value);
|
|
111
|
-
write('*/');
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
function isSameLine(path, loc) {
|
|
117
|
-
return path.node.loc?.start.line === loc.start.line || path.node.loc?.end.line === loc.end.line;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function maybeInsideFn(insideFn, {print, indent}) {
|
|
121
|
-
if (!insideFn)
|
|
122
|
-
return;
|
|
123
|
-
|
|
124
|
-
indent.inc();
|
|
125
|
-
indent.inc();
|
|
126
|
-
print.breakline();
|
|
127
|
-
indent.dec();
|
|
128
|
-
indent.dec();
|
|
129
|
-
}
|