diff --git a/src/comments.js b/src/comments.js index 9305f4a3..e9c2bc08 100644 --- a/src/comments.js +++ b/src/comments.js @@ -147,6 +147,7 @@ function attach(comments, ast, text, options) { handleMemberExpressionComments(enclosingNode, followingNode, comment) || handleIfStatementComments(enclosingNode, followingNode, comment) || handleTryStatementComments(enclosingNode, followingNode, comment) || + handleImportSpecifierComments(enclosingNode, comment) || handleClassComments(enclosingNode, comment) ) { // We're good @@ -170,6 +171,7 @@ function attach(comments, ast, text, options) { comment, text ) || + handleImportSpecifierComments(enclosingNode, comment) || handleTemplateLiteralComments(enclosingNode, comment) || handleClassComments(enclosingNode, comment) ) { @@ -491,6 +493,14 @@ function handleClassComments(enclosingNode, comment) { return false; } +function handleImportSpecifierComments(enclosingNode, comment) { + if (enclosingNode && enclosingNode.type === "ImportSpecifier") { + addLeadingComment(enclosingNode, 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 193a2908..7adcfa0a 100644 --- a/tests/import/__snapshots__/jsfmt.spec.js.snap +++ b/tests/import/__snapshots__/jsfmt.spec.js.snap @@ -54,6 +54,108 @@ import {fitsIn, oneLine} from \\".\\"; " `; +exports[`comments.js 1`] = ` +"import { a //comment1 +//comment2 +//comment3 +as b} from \\"\\"; + +import { + a as //comment1 + //comment2 + //comment3 + b +} from \\"\\"; + +import { + a as //comment2 //comment1 + //comment3 + b +} from \\"\\"; + +import { + a as //comment3 //comment2 //comment1 + b +} from \\"\\"; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import { + //comment1 + //comment2 + //comment3 + a as b +} from \\"\\"; + +import { + //comment1 + //comment2 + //comment3 + a as b +} from \\"\\"; + +import { + //comment2 //comment1 + //comment3 + a as b +} from \\"\\"; + +import { + //comment3 //comment2 //comment1 + a as b +} from \\"\\"; +" +`; + +exports[`comments.js 2`] = ` +"import { a //comment1 +//comment2 +//comment3 +as b} from \\"\\"; + +import { + a as //comment1 + //comment2 + //comment3 + b +} from \\"\\"; + +import { + a as //comment2 //comment1 + //comment3 + b +} from \\"\\"; + +import { + a as //comment3 //comment2 //comment1 + b +} from \\"\\"; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import { + //comment1 + //comment2 + //comment3 + a as b +} from \\"\\"; + +import { + //comment1 + //comment2 + //comment3 + a as b +} from \\"\\"; + +import { + //comment2 //comment1 + //comment3 + a as b +} from \\"\\"; + +import { + //comment3 //comment2 //comment1 + a as b +} from \\"\\"; +" +`; + exports[`long-line.js 1`] = ` "import someCoolUtilWithARatherLongName from '../../../../utils/someCoolUtilWithARatherLongName'; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/import/comments.js b/tests/import/comments.js new file mode 100644 index 00000000..b5f2a1ce --- /dev/null +++ b/tests/import/comments.js @@ -0,0 +1,22 @@ +import { a //comment1 +//comment2 +//comment3 +as b} from ""; + +import { + a as //comment1 + //comment2 + //comment3 + b +} from ""; + +import { + a as //comment2 //comment1 + //comment3 + b +} from ""; + +import { + a as //comment3 //comment2 //comment1 + b +} from "";