Comments: attach method name comments with estree parsers, fixes #2141 (#2170)

master
Lucas Azzola 2017-06-17 22:22:39 +10:00 committed by GitHub
parent c669a1db72
commit bff28023b3
4 changed files with 54 additions and 1 deletions

View File

@ -279,6 +279,7 @@ function attach(comments, ast, text) {
) ||
handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) ||
handleCommentInEmptyParens(text, enclosingNode, comment) ||
handleMethodNameComments(enclosingNode, precedingNode, comment) ||
handleOnlyComments(enclosingNode, ast, comment, isLastComment)
) {
// We're good
@ -548,6 +549,24 @@ function handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) {
return false;
}
function handleMethodNameComments(enclosingNode, precedingNode, comment) {
// This is only needed for estree parsers (flow, typescript) to attach
// after a method name:
// obj = { fn /*comment*/() {} };
if (
enclosingNode &&
precedingNode &&
(enclosingNode.type === "Property" ||
enclosingNode.type === "MethodDefinition") &&
precedingNode.type === "Identifier" &&
enclosingNode.key === precedingNode
) {
addTrailingComment(precedingNode, comment);
return true;
}
return false;
}
function handleCommentInEmptyParens(text, enclosingNode, comment) {
if (getNextNonSpaceNonCommentCharacter(text, comment) !== ")") {
return false;

View File

@ -138,6 +138,29 @@ exports[`member.js 1`] = `
`;
exports[`method.js 1`] = `
class C {
name/*comment*/() {
}
};
({
name/*comment*/() {
}
});
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class C {
name /*comment*/() {}
}
({
name /*comment*/() {}
});
`;
exports[`ternary.js 1`] = `
if (1) (class {}) ? 1 : 2;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -1 +1 @@
run_spec(__dirname, null, ["typescript"]);
run_spec(__dirname, null, ["babylon", "typescript"]);

11
tests/classes/method.js Normal file
View File

@ -0,0 +1,11 @@
class C {
name/*comment*/() {
}
};
({
name/*comment*/() {
}
});