Fix duplicated comments on JSX super classes (#20211)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-10-03 10:26:53 +08:00
1 parent e5b6b2e1b8
commit d62653b04d
4 files changed
+50 -8

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
#### Fix duplicated comments on JSX super classes (#20211 by @fisker)
<!-- prettier-ignore -->
```jsx
// Input
class A extends (/* c */ <div />) {}
class B extends (<div /> /* c */) {}
// Prettier stable
class A extends /* c */ (/* c */ <div />) {}
class B extends (<div /> /* c */) /* c */ {}
// Prettier main
class A extends (/* c */ <div />) {}
class B extends (<div /> /* c */) {}
```
+11 -8
View File
@@ -19,7 +19,8 @@ import needsParentheses from "../parentheses/needs-parentheses.js";
import { isNonEmptyClassBody } from "../utilities/class-members.js"; import { isNonEmptyClassBody } from "../utilities/class-members.js";
import { CommentCheckFlags, hasComment } from "../utilities/comments.js"; import { CommentCheckFlags, hasComment } from "../utilities/comments.js";
import { createTypeCheckFunction } from "../utilities/create-type-check-function.js"; import { createTypeCheckFunction } from "../utilities/create-type-check-function.js";
import { isMemberExpression } from "../utilities/node-types.js"; import { hasNodeIgnoreComment } from "../utilities/has-node-ignore-comment.js";
import { isJsxElement, isMemberExpression } from "../utilities/node-types.js";
import { stripChainElementWrappers } from "../utilities/strip-chain-element-wrappers.js"; import { stripChainElementWrappers } from "../utilities/strip-chain-element-wrappers.js";
import { printAssignment } from "./assignment.js"; import { printAssignment } from "./assignment.js";
import { printClassMemberDecorators, printDecorators } from "./decorators.js"; import { printClassMemberDecorators, printDecorators } from "./decorators.js";
@@ -111,18 +112,20 @@ function printClassWithoutDecorators(path, options, print) {
} }
if (node.superClass) { if (node.superClass) {
const printed = [ let printed = [
printSuperClass(path, options, print), printSuperClass(path, options, print),
print("superTypeArguments"), print("superTypeArguments"),
]; ];
const printedWithComments = path.call( printed = [
() => ["extends ", printComments(path, printed, options)], "extends ",
"superClass", isJsxElement(node.superClass) && !hasNodeIgnoreComment(node.superClass)
); ? printed
: path.call(() => printComments(path, printed, options), "superClass"),
];
if (groupMode) { if (groupMode) {
extendsParts.push(line, group(printedWithComments)); extendsParts.push(line, group(printed));
} else { } else {
extendsParts.push(" ", printedWithComments); extendsParts.push(" ", printed);
} }
} else { } else {
extendsParts.push(printHeritageClauses(path, options, print, "extends")); extendsParts.push(printHeritageClauses(path, options, print, "extends"));
@@ -147,3 +147,22 @@ class A10 extends /* a */ /* prettier-ignore */ B {}
================================================================================ ================================================================================
`; `;
exports[`superclass-2.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80 (default) |
=====================================input======================================
class A11 extends (/* c */ <div />) {}
class A12 extends (/* c *//* prettier-ignore */ <div />) {}
class A13 extends (/* c */ <></>) {}
class A14 extends (<div /> /* c */) {}
=====================================output=====================================
class A11 extends (/* c */ <div />) {}
class A12 extends /* c */ /* prettier-ignore */ (<div />) {}
class A13 extends (/* c */ <></>) {}
class A14 extends (<div /> /* c */) {}
================================================================================
`;
@@ -0,0 +1,4 @@
class A11 extends (/* c */ <div />) {}
class A12 extends (/* c *//* prettier-ignore */ <div />) {}
class A13 extends (/* c */ <></>) {}
class A14 extends (<div /> /* c */) {}