Skip to content

Commit 3576b80

Browse files
committed
feature: @putout/printer: ImportDeclaration: comments before ExpressionStatement
1 parent 5c9cc18 commit 3576b80

7 files changed

Lines changed: 117 additions & 12 deletions

File tree

lib/tokenize/comment/comments-printer/comments-printer.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,27 @@ module.exports.printLeadingComments = (path, printer, semantics, {currentTravers
2121
leadingComments = [],
2222
} = path.node;
2323

24-
const {printLeadingCommentLine} = currentTraverse;
24+
const {
25+
printLeadingCommentLine,
26+
printLeadingCommentBlock,
27+
} = currentTraverse;
28+
29+
const n = leadingComments.length - 1;
2530

2631
for (const [index, {type, value}] of leadingComments.entries()) {
27-
if (type === 'CommentLine')
32+
if (type === 'CommentLine') {
2833
printLeadingCommentLine?.(path, printer, semantics, {
2934
index,
3035
printComment: createPrintCommentLine(print, value),
36+
isLast: index === n,
37+
});
38+
continue;
39+
}
40+
41+
if (type === 'CommentBlock')
42+
printLeadingCommentBlock?.(path, printer, semantics, {
43+
index,
44+
printComment: createPrintCommentBlock(print, value),
3145
});
3246
}
3347
};
@@ -38,9 +52,23 @@ module.exports.printTrailingComments = (path, printer, semantics, {currentTraver
3852
trailingComments = [],
3953
} = path.node;
4054

41-
const {printTrailingCommentBlock} = currentTraverse;
55+
const {
56+
printTrailingCommentLine,
57+
printTrailingCommentBlock,
58+
} = currentTraverse;
59+
60+
const n = trailingComments.length - 1;
4261

4362
for (const [index, {type, value}] of trailingComments.entries()) {
63+
if (type === 'CommentLine') {
64+
printTrailingCommentLine?.(path, printer, semantics, {
65+
index,
66+
printComment: createPrintCommentLine(print, value),
67+
isLast: index === n,
68+
});
69+
continue;
70+
}
71+
4472
if (type === 'CommentBlock')
4573
printTrailingCommentBlock?.(path, printer, semantics, {
4674
index,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const {types} = require('@putout/babel');
4+
const {hasTrailingComment} = require('#is');
5+
6+
const {
7+
isBlockStatement,
8+
isProgram,
9+
isIfStatement,
10+
isClassMethod,
11+
} = types;
12+
13+
module.exports.printLeadingCommentLine = (path, printer, semantics, {index, isLast, printComment}) => {
14+
const {print, indent} = printer;
15+
const prev = path.getPrevSibling();
16+
const {parentPath} = path;
17+
const parentParentPath = parentPath.parentPath;
18+
19+
if (hasTrailingComment(prev))
20+
return;
21+
22+
if (!index && !prev.node && (isIfStatement(parentPath) || isClassMethod(parentParentPath)))
23+
indent();
24+
25+
printComment();
26+
27+
print.newline();
28+
29+
if (!isLast && !path.parentPath.isIfStatement())
30+
print.indent();
31+
};
32+
33+
module.exports.printLeadingCommentBlock = (path, printer, semantics, {printComment}) => {
34+
const {indent} = printer;
35+
const prev = path.getPrevSibling();
36+
37+
if (hasTrailingComment(prev))
38+
return;
39+
40+
if (isBlockStatement(path.parentPath) && !isProgram(path.parentPath.parentPath))
41+
indent();
42+
43+
printComment();
44+
};

lib/tokenize/statements/expression-statement/expression-statement.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const {
1515
} = require('../../is');
1616

1717
const {isInsideAssignNextAssignFunction} = require('./is-inside-assign-next-assign-function');
18+
19+
const {
20+
printLeadingCommentLine,
21+
printLeadingCommentBlock,
22+
} = require('./comments');
23+
1824
const isCommentBlock = (a) => a?.type === 'CommentBlock';
1925

2026
const {
@@ -117,6 +123,8 @@ module.exports.ExpressionStatement = {
117123
}
118124
},
119125
};
126+
module.exports.ExpressionStatement.printLeadingCommentLine = printLeadingCommentLine;
127+
module.exports.ExpressionStatement.printLeadingCommentBlock = printLeadingCommentBlock;
120128

121129
function isTopParentLast({parentPath}) {
122130
if (!parentPath.isIfStatement())

lib/tokenize/statements/expression-statement/expression-statement.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ test('printer: tokenizer: statement: expression: after call: indent', (t) => {
5656
t.end();
5757
});
5858

59+
test('printer: tokenizer: statement: expression: leading comment', (t) => {
60+
t.print(fixture.expressionLeadingComment);
61+
t.end();
62+
});
63+
5964
test('printer: tokenizer: statement: expression: before if', (t) => {
6065
const source = fixture.expressionBeforeIf;
6166
const ast = parse(source);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import currify from 'currify';
2+
3+
// hello
4+
transpile('');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const {types} = require('@putout/babel');
4+
const {isExportDeclaration} = types;
5+
6+
module.exports.printTrailingCommentLine = (path, printer, semantics, {printComment}) => {
7+
const {print} = printer;
8+
printComment();
9+
print.breakline();
10+
};
11+
12+
module.exports.printTrailingCommentBlock = (path, printer, semantics, {printComment}) => {
13+
const {maybe} = printer;
14+
const next = path.getNextSibling();
15+
16+
maybe.print.breakline(!isExportDeclaration(next));
17+
printComment();
18+
};

lib/tokenize/statements/import-declaration/import-declaration.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const {parseImportSpecifiers} = require('parse-import-specifiers');
4-
const {types} = require('@putout/babel');
4+
55
const {markAfter} = require('../../mark');
66
const {isLast, isNext} = require('../../is');
77

@@ -10,7 +10,10 @@ const {
1010
ImportAttribute,
1111
} = require('./import-attribute');
1212

13-
const {isExportDeclaration} = types;
13+
const {
14+
printTrailingCommentBlock,
15+
printTrailingCommentLine,
16+
} = require('./comments');
1417

1518
module.exports.ImportAttribute = ImportAttribute;
1619
module.exports.ImportDeclaration = {
@@ -137,10 +140,5 @@ function parseMaxSpecifiers(imports, semantics) {
137140
return maxSpecifiersInOneLine;
138141
}
139142

140-
module.exports.ImportDeclaration.printTrailingCommentBlock = (path, printer, semantics, {printComment}) => {
141-
const {maybe} = printer;
142-
const next = path.getNextSibling();
143-
144-
maybe.print.breakline(!isExportDeclaration(next));
145-
printComment();
146-
};
143+
module.exports.ImportDeclaration.printTrailingCommentBlock = printTrailingCommentBlock;
144+
module.exports.ImportDeclaration.printTrailingCommentLine = printTrailingCommentLine;

0 commit comments

Comments
 (0)