TypeScript: Fix missing comments in MethodDefinition (#9872)

This commit is contained in:
fisker Cheung authored and GitHub committed 2020-12-15 12:22:56 +08:00
1 parent ce6f07efe9
commit 099ba04327
7 files changed
+81 -7

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
#### Fix missing comments in `MethodDefinition` (#9872 by @fisker)
`typescript` parser only, `babel-ts` don't have this issue.
<!-- prettier-ignore -->
```typescript
// Input
class Foo {
bar() /* bat */;
}
// Prettier stable
Error: Comment "bat" was not printed. Please report this error!
// Prettier master
class Foo {
bar /* bat */();
}
```
+2 -1
View File
@@ -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);
+12 -4
View File
@@ -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,
};
+3 -1
View File
@@ -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":
+3 -1
View File
@@ -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"
);
}
@@ -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"]
@@ -0,0 +1,10 @@
// #9324
class foo1 {
bar() /* bat */;
}
// #9367
class Test {
foo (/* 2 */) /* 3 */;
}