From 748dcbf70d340a350e60de1f3f15c6623076cbb9 Mon Sep 17 00:00:00 2001 From: Davy Duperron Date: Fri, 17 Mar 2017 16:11:43 +0100 Subject: [PATCH] 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. --- src/comments.js | 15 +++- tests/import/__snapshots__/jsfmt.spec.js.snap | 80 +++++++++++++++++++ tests/import/comments.js | 10 +++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/comments.js b/src/comments.js index e01499db..2873caaf 100644 --- a/src/comments.js +++ b/src/comments.js @@ -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; diff --git a/tests/import/__snapshots__/jsfmt.spec.js.snap b/tests/import/__snapshots__/jsfmt.spec.js.snap index 25ae675a..d35e1dc2 100644 --- a/tests/import/__snapshots__/jsfmt.spec.js.snap +++ b/tests/import/__snapshots__/jsfmt.spec.js.snap @@ -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\\"; " `; diff --git a/tests/import/comments.js b/tests/import/comments.js index b5f2a1ce..3ab2c495 100644 --- a/tests/import/comments.js +++ b/tests/import/comments.js @@ -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";