diff --git a/src/comments.js b/src/comments.js index 2d0eda4f..66ec0b92 100644 --- a/src/comments.js +++ b/src/comments.js @@ -298,7 +298,8 @@ function attach(comments, ast, text) { handleObjectPropertyAssignment(enclosingNode, precedingNode, comment) || handleCommentInEmptyParens(text, enclosingNode, comment) || handleMethodNameComments(text, enclosingNode, precedingNode, comment) || - handleOnlyComments(enclosingNode, ast, comment, isLastComment) + handleOnlyComments(enclosingNode, ast, comment, isLastComment) || + handleFunctionNameComments(text, enclosingNode, precedingNode, comment) ) { // We're good } else if (precedingNode && followingNode) { @@ -641,6 +642,31 @@ function handleMethodNameComments(text, enclosingNode, precedingNode, comment) { return false; } +function handleFunctionNameComments( + text, + enclosingNode, + precedingNode, + comment +) { + if (getNextNonSpaceNonCommentCharacter(text, comment) !== "(") { + return false; + } + + if ( + precedingNode && + enclosingNode && + (enclosingNode.type === "FunctionDeclaration" || + enclosingNode.type === "FunctionExpression" || + enclosingNode.type === "ClassMethod" || + enclosingNode.type === "MethodDefinition" || + enclosingNode.type === "ObjectMethod") + ) { + addTrailingComment(precedingNode, comment); + return true; + } + return false; +} + function handleCommentInEmptyParens(text, enclosingNode, comment) { if (getNextNonSpaceNonCommentCharacter(text, comment) !== ")") { return false; diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 7fab1166..7806c1a3 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -294,6 +294,40 @@ call((/*object*/ row) => {}); KEYPAD_NUMBERS.map(num => ( // Buttons 0-9
)); + +function f /* f */() {} +function f (/* args */) {} +function f () /* returns */ {} +function f /* f */(/* args */) /* returns */ {} + +function f /* f */(/* a */ a) {} +function f /* f */(a /* a */) {} +function f /* f */(/* a */ a) /* returns */ {} + +const obj = { + f1 /* f */() {}, + f2 (/* args */) {}, + f3 () /* returns */ {}, + f4 /* f */(/* args */) /* returns */ {}, +}; + +(function f /* f */() {})(); +(function f (/* args */) {})(); +(function f () /* returns */ {})(); +(function f /* f */(/* args */) /* returns */ {})(); + +class C { + f/* f */() {} +} +class C { + f(/* args */) {} +} +class C { + f() /* returns */ {} +} +class C { + f/* f */(/* args */) /* returns */ {} +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function a(/* comment */) {} // comment function b() {} // comment @@ -303,6 +337,49 @@ KEYPAD_NUMBERS.map(( num // Buttons 0-9 ) =>
); +function f /* f */() {} +function f(/* args */) {} +function f() /* returns */ { +} +function f /* f */(/* args */) /* returns */ { +} + +function f /* f */(/* a */ a) {} +function f /* f */(a /* a */) {} +function f /* f */(/* a */ a) /* returns */ { +} + +const obj = { + f1 /* f */() {}, + f2(/* args */) {}, + f3() /* returns */ { + }, + f4 /* f */(/* args */) /* returns */ { + } +}; + +(function f /* f */() {})(); +(function f(/* args */) {})(); +(function f() /* returns */ { +})(); +(function f /* f */(/* args */) /* returns */ { +})(); + +class C { + f /* f */() {} +} +class C { + f(/* args */) {} +} +class C { + f() /* returns */ { + } +} +class C { + f /* f */(/* args */) /* returns */ { + } +} + `; exports[`if.js 1`] = ` diff --git a/tests/comments/function-declaration.js b/tests/comments/function-declaration.js index a7dc40f7..ffb4fd76 100644 --- a/tests/comments/function-declaration.js +++ b/tests/comments/function-declaration.js @@ -5,3 +5,37 @@ call((/*object*/ row) => {}); KEYPAD_NUMBERS.map(num => ( // Buttons 0-9
)); + +function f /* f */() {} +function f (/* args */) {} +function f () /* returns */ {} +function f /* f */(/* args */) /* returns */ {} + +function f /* f */(/* a */ a) {} +function f /* f */(a /* a */) {} +function f /* f */(/* a */ a) /* returns */ {} + +const obj = { + f1 /* f */() {}, + f2 (/* args */) {}, + f3 () /* returns */ {}, + f4 /* f */(/* args */) /* returns */ {}, +}; + +(function f /* f */() {})(); +(function f (/* args */) {})(); +(function f () /* returns */ {})(); +(function f /* f */(/* args */) /* returns */ {})(); + +class C { + f/* f */() {} +} +class C { + f(/* args */) {} +} +class C { + f() /* returns */ {} +} +class C { + f/* f */(/* args */) /* returns */ {} +}