Fix interleaved comments in class decorators (#2661)

I wrote this fix a while ago but it conflicted with the class heuristic, now that #2660 fixes it, we can ship this one as well!

Fixes #1460
Fixes #2507
master
Christopher Chedeau 2017-08-24 16:04:02 -07:00 committed by GitHub
parent 05b4422e4f
commit ab91f6f8be
3 changed files with 59 additions and 0 deletions

View File

@ -192,6 +192,12 @@ function attach(comments, ast, text) {
comment
) ||
handleTryStatementComments(enclosingNode, followingNode, comment) ||
handleClassComments(
enclosingNode,
precedingNode,
followingNode,
comment
) ||
handleImportSpecifierComments(enclosingNode, comment) ||
handleObjectPropertyComments(enclosingNode, comment) ||
handleForComments(enclosingNode, precedingNode, comment) ||
@ -246,6 +252,12 @@ function attach(comments, ast, text) {
followingNode,
comment
) ||
handleClassComments(
enclosingNode,
precedingNode,
followingNode,
comment
) ||
handleLabeledStatementComments(enclosingNode, comment) ||
handleCallExpressionComments(precedingNode, enclosingNode, comment) ||
handlePropertyComments(enclosingNode, comment) ||
@ -551,6 +563,32 @@ function handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) {
return false;
}
function handleClassComments(
enclosingNode,
precedingNode,
followingNode,
comment
) {
if (
enclosingNode &&
(enclosingNode.type === "ClassDeclaration" ||
enclosingNode.type === "ClassExpression") &&
(enclosingNode.decorators && enclosingNode.decorators.length > 0) &&
!(followingNode && followingNode.type === "Decorator")
) {
if (!enclosingNode.decorators || enclosingNode.decorators.length === 0) {
addLeadingComment(enclosingNode, comment);
} else {
addTrailingComment(
enclosingNode.decorators[enclosingNode.decorators.length - 1],
comment
);
}
return true;
}
return false;
}
function handleMethodNameComments(text, enclosingNode, precedingNode, comment) {
// This is only needed for estree parsers (flow, typescript) to attach
// after a method name:

View File

@ -25,6 +25,13 @@ class X {
],
})
export class AppModule {}
// A
@Foo()
// B
@Bar()
// C
export class Bar{}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var x = 100;
@ -48,6 +55,13 @@ class X {}
})
export class AppModule {}
// A
@Foo()
// B
@Bar()
// C
export class Bar {}
`;
exports[`methods.js 1`] = `

View File

@ -22,3 +22,10 @@ class X {
],
})
export class AppModule {}
// A
@Foo()
// B
@Bar()
// C
export class Bar{}