From 48100c323db22d5eea36aa38dbe0db289333f76c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 31 Oct 2025 09:33:19 +0800 Subject: [PATCH] Fix duplicated comments on `prettier-ignored` super class (#18168) --- src/language-js/comments/printer-methods.js | 24 +++++++------------ .../__snapshots__/format.test.js.snap | 6 +++++ tests/format/js/class-comment/superclass.js | 3 +++ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/language-js/comments/printer-methods.js b/src/language-js/comments/printer-methods.js index 94488069c..e3dacf575 100644 --- a/src/language-js/comments/printer-methods.js +++ b/src/language-js/comments/printer-methods.js @@ -151,32 +151,26 @@ function canAttachComment(node, ancestors) { * @returns {boolean} */ function willPrintOwnComments(path) { - const { node, parent, key } = path; + const { key, parent } = path; if ( (key === "types" && isUnionType(parent)) || (key === "argument" && parent.type === "JSXSpreadAttribute") || - (key === "expression" && parent.type === "JSXSpreadChild") + (key === "expression" && parent.type === "JSXSpreadChild") || + (key === "superClass" && + (parent.type === "ClassDeclaration" || + parent.type === "ClassExpression")) || + // Not tested, don't know how to + (key === "patterns" && parent.type === "MatchOrPattern") ) { return true; } + const { node } = path; if (hasNodeIgnoreComment(node)) { return false; } - if (isJsxElement(node) || isUnionType(node)) { - return true; - } - - if (!parent) { - return false; - } - - return ( - parent.type === "MatchOrPattern" || - (path.key === "superClass" && - (parent.type === "ClassDeclaration" || parent.type === "ClassExpression")) - ); + return isJsxElement(node) || isUnionType(node); } function isGap(text, { parser }) { diff --git a/tests/format/js/class-comment/__snapshots__/format.test.js.snap b/tests/format/js/class-comment/__snapshots__/format.test.js.snap index 89ece48df..56c10ff5c 100644 --- a/tests/format/js/class-comment/__snapshots__/format.test.js.snap +++ b/tests/format/js/class-comment/__snapshots__/format.test.js.snap @@ -97,6 +97,9 @@ class C2 // comment extends Base { foo(){} } +(class A10 extends /* a */ /* prettier-ignore */B {}); +class A10 extends /* a */ /* prettier-ignore */B {} + =====================================output===================================== class A // comment 1 // comment 2 @@ -142,5 +145,8 @@ class C2 // comment foo() {} } +(class A10 extends /* a */ /* prettier-ignore */ B {}); +class A10 extends /* a */ /* prettier-ignore */ B {} + ================================================================================ `; diff --git a/tests/format/js/class-comment/superclass.js b/tests/format/js/class-comment/superclass.js index f6492b8c3..143b2c32e 100644 --- a/tests/format/js/class-comment/superclass.js +++ b/tests/format/js/class-comment/superclass.js @@ -38,3 +38,6 @@ class c extends d class C2 // comment extends Base { foo(){} } + +(class A10 extends /* a */ /* prettier-ignore */B {}); +class A10 extends /* a */ /* prettier-ignore */B {}