Migrate class comments to the beginning (#831)

Instead of trying to figure out a complicated way to preserve their correct position, it's easier to just migrate those comments before the class.

Fixes #693
Fixes #694
master
Christopher Chedeau 2017-02-28 08:23:15 -08:00 committed by James Long
parent 2ea1dbcb6a
commit 61482662cc
4 changed files with 98 additions and 2 deletions

View File

@ -145,7 +145,8 @@ function attach(comments, ast, text, options) {
if (
handleMemberExpressionComments(enclosingNode, followingNode, comment) ||
handleIfStatementComments(enclosingNode, followingNode, comment) ||
handleTryStatementComments(enclosingNode, followingNode, comment)
handleTryStatementComments(enclosingNode, followingNode, comment) ||
handleClassComments(enclosingNode, comment)
) {
// We're good
} else if (followingNode) {
@ -168,7 +169,8 @@ function attach(comments, ast, text, options) {
comment,
text
) ||
handleTemplateLiteralComments(enclosingNode, comment)
handleTemplateLiteralComments(enclosingNode, comment) ||
handleClassComments(enclosingNode, comment)
) {
// We're good
} else if (precedingNode) {
@ -454,6 +456,16 @@ function handleFunctionDeclarationComments(enclosingNode, comment) {
return false;
}
function handleClassComments(enclosingNode, comment) {
if (enclosingNode &&
(enclosingNode.type === "ClassDeclaration" ||
enclosingNode.type === "ClassExpression")) {
addLeadingComment(enclosingNode, comment);
return true;
}
return false;
}
function printComment(commentPath) {
const comment = commentPath.getValue();
comment.printed = true;

View File

@ -0,0 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`comments.js 1`] = `
"class A // comment 1
// comment 2
extends B {}
class A extends B // comment1
// comment2
// comment3
{}
class A /* a */ extends B {}
class A extends B /* a */ {}
class A extends /* a */ B {}
(class A // comment 1
// comment 2
extends B {});
(class A extends B // comment1
// comment2
// comment3
{});
(class A /* a */ extends B {});
(class A extends B /* a */ {});
(class A extends /* a */ B {});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// comment 1
// comment 2
class A extends B {}
// comment1
// comment2
// comment3
class A extends B {}
class A /* a */ extends B {}
class A extends B /* a */ {
}
class A extends /* a */ B {}
// comment 1
// comment 2
(class A extends B {});
// comment1
// comment2
// comment3
(class A extends B {});
(class A /* a */ extends B {});
(class A extends B /* a */ {
});
(class A extends /* a */ B {});
"
`;

View File

@ -0,0 +1,25 @@
class A // comment 1
// comment 2
extends B {}
class A extends B // comment1
// comment2
// comment3
{}
class A /* a */ extends B {}
class A extends B /* a */ {}
class A extends /* a */ B {}
(class A // comment 1
// comment 2
extends B {});
(class A extends B // comment1
// comment2
// comment3
{});
(class A /* a */ extends B {});
(class A extends B /* a */ {});
(class A extends /* a */ B {});

View File

@ -0,0 +1 @@
run_spec(__dirname);