Stabilize import as comments (#855)

Since this is extremely rare, I just took the easy way out and if you are adding a comment inside the "as" node, I just move it outside. We could be more fancy but that works.

Fixes #620
master
Christopher Chedeau 2017-03-02 19:22:33 -08:00 committed by James Long
parent 705ac7d3cf
commit 516f5900d8
3 changed files with 134 additions and 0 deletions

View File

@ -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;

View File

@ -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';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

22
tests/import/comments.js Normal file
View File

@ -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 "";