Fix import declaration comments (#1030)

* Add handleImportDeclarationComments function.

* Fix indent.

* Update test cases accordingly.

* Implement better heuristic for adding comments.

* Make the test case more exhaustive.
master
Davy Duperron 2017-03-17 16:11:43 +01:00 committed by Christopher Chedeau
parent dde8463ad9
commit 748dcbf70d
3 changed files with 104 additions and 1 deletions

View File

@ -164,7 +164,8 @@ function attach(comments, ast, text, options) {
followingNode,
comment
) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment)
handleOnlyComments(enclosingNode, ast, comment, isLastComment) ||
handleImportDeclarationComments(enclosingNode, precedingNode, comment)
) {
// We're good
} else if (followingNode) {
@ -646,6 +647,18 @@ function handleForComments(enclosingNode, precedingNode, comment) {
return false;
}
function handleImportDeclarationComments(enclosingNode, precedingNode, comment) {
if (
precedingNode &&
enclosingNode && enclosingNode.type === "ImportDeclaration" &&
comment.type !== "CommentBlock" && comment.type !== "Block"
) {
addTrailingComment(precedingNode, comment);
return true;
}
return false;
}
function printComment(commentPath) {
const comment = commentPath.getValue();
comment.printed = true;

View File

@ -131,6 +131,16 @@ import {
a as //comment3 //comment2 //comment1
b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import {
//comment1
@ -156,6 +166,16 @@ import {
//comment3 //comment2 //comment1
a as b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
"
`;
@ -182,6 +202,16 @@ import {
a as //comment3 //comment2 //comment1
b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import {
//comment1
@ -207,6 +237,16 @@ import {
//comment3 //comment2 //comment1
a as b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
"
`;
@ -233,6 +273,16 @@ import {
a as //comment3 //comment2 //comment1
b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import {
//comment1
@ -258,6 +308,16 @@ import {
//comment3 //comment2 //comment1
a as b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
"
`;
@ -284,6 +344,16 @@ import {
a as //comment3 //comment2 //comment1
b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import {
//comment1
@ -309,6 +379,16 @@ import {
//comment3 //comment2 //comment1
a as b
} from \\"\\";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from \\"./module\\";
"
`;

View File

@ -20,3 +20,13 @@ import {
a as //comment3 //comment2 //comment1
b
} from "";
import {
// comment 1
FN1, // comment 2
/* comment 3 */ FN2,
// FN3,
FN4 /* comment 4 */
// FN4,
// FN5
} from "./module";