diff --git a/changelog_unreleased/typescript/9872.md b/changelog_unreleased/typescript/9872.md new file mode 100644 index 000000000..1ee6d2c9b --- /dev/null +++ b/changelog_unreleased/typescript/9872.md @@ -0,0 +1,19 @@ +#### Fix missing comments in `MethodDefinition` (#9872 by @fisker) + +`typescript` parser only, `babel-ts` don't have this issue. + + +```typescript +// Input +class Foo { + bar() /* bat */; +} + +// Prettier stable +Error: Comment "bat" was not printed. Please report this error! + +// Prettier master +class Foo { + bar /* bat */(); +} +``` diff --git a/src/language-js/comments.js b/src/language-js/comments.js index 91cd34ffa..98b1e760a 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -531,7 +531,8 @@ function handleCommentInEmptyParens({ comment, enclosingNode, text }) { } if ( enclosingNode && - enclosingNode.type === "MethodDefinition" && + (enclosingNode.type === "MethodDefinition" || + enclosingNode.type === "TSAbstractMethodDefinition") && getFunctionParameters(enclosingNode.value).length === 0 ) { addDanglingComment(enclosingNode.value, comment); diff --git a/src/language-js/print/function.js b/src/language-js/print/function.js index df4ee059b..5037c16ba 100644 --- a/src/language-js/print/function.js +++ b/src/language-js/print/function.js @@ -95,12 +95,19 @@ function printMethod(path, options, print) { parts.push( printPropertyKey(path, options, print), - node.optional || node.key.optional ? "?" : "", - node === value - ? printMethodInternal(path, options, print) - : path.call((path) => printMethodInternal(path, options, print), "value") + node.optional || node.key.optional ? "?" : "" ); + if (node === value) { + parts.push(printMethodInternal(path, options, print)); + } else if (value.type === "FunctionExpression") { + parts.push( + path.call((path) => printMethodInternal(path, options, print), "value") + ); + } else { + parts.push(path.call(print, "value")); + } + return concat(parts); } @@ -371,5 +378,6 @@ module.exports = { printMethod, printReturnStatement, printThrowStatement, + printMethodInternal, shouldPrintParamsWithoutParens, }; diff --git a/src/language-js/print/typescript.js b/src/language-js/print/typescript.js index 10724d858..77c67791f 100644 --- a/src/language-js/print/typescript.js +++ b/src/language-js/print/typescript.js @@ -34,7 +34,7 @@ const { printTypeParameters, } = require("./type-parameters"); const { printPropertyKey } = require("./property"); -const { printFunctionDeclaration } = require("./function"); +const { printFunctionDeclaration, printMethodInternal } = require("./function"); const { printInterface } = require("./interface"); const { printAssignmentRight } = require("./assignment"); const { printBlock } = require("./block"); @@ -546,6 +546,8 @@ function printTypescript(path, options, print) { ]); case "TSTypeAnnotation": return path.call(print, "typeAnnotation"); + case "TSEmptyBodyFunctionExpression": + return printMethodInternal(path, options, print); // These are not valid TypeScript. Printing them just for the sake of error recovery. case "TSJSDocAllType": diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 8e9020e7f..7dff1ad22 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -1276,7 +1276,9 @@ function canAttachComment(node) { !isLineComment(node) && node.type !== "EmptyStatement" && node.type !== "TemplateElement" && - node.type !== "Import" + node.type !== "Import" && + // `babel-ts` don't have similar node for `class Foo { bar() /* bat */; }` + node.type !== "TSEmptyBodyFunctionExpression" ); } diff --git a/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap index a13784235..4b4f2a713 100644 --- a/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap @@ -72,6 +72,38 @@ class F<__T> {} ================================================================================ `; +exports[`empty-method-body.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// #9324 + +class foo1 { + bar() /* bat */; +} + +// #9367 +class Test { + foo (/* 2 */) /* 3 */; +} + +=====================================output===================================== +// #9324 + +class foo1 { + bar /* bat */(); +} + +// #9367 +class Test { + foo /* 3 */(/* 2 */); +} + +================================================================================ +`; + exports[`extends_implements.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript/class/empty-method-body.ts b/tests/typescript/class/empty-method-body.ts new file mode 100644 index 000000000..426eab724 --- /dev/null +++ b/tests/typescript/class/empty-method-body.ts @@ -0,0 +1,10 @@ +// #9324 + +class foo1 { + bar() /* bat */; +} + +// #9367 +class Test { + foo (/* 2 */) /* 3 */; +}