diff --git a/changelog_unreleased/javascript/18963.md b/changelog_unreleased/javascript/18963.md new file mode 100644 index 000000000..e14f548e3 --- /dev/null +++ b/changelog_unreleased/javascript/18963.md @@ -0,0 +1,26 @@ +#### Fix duplicated comments in empty branches with `experimentalTernaries` (#18963 by @kovsu) + + +```jsx +// Input +condition ? ifTrue +: [ + // Hello, world! + ]; + +// Prettier stable (--experimental-ternaries) +condition ? ifTrue + // Hello, world! +: ( + [ + // Hello, world! + ] +); + +// Prettier main (--experimental-ternaries) +condition ? ifTrue : ( + [ + // Hello, world! + ] +); +``` diff --git a/src/language-js/print/ternary.js b/src/language-js/print/ternary.js index 98a5b46f4..bf10d0fae 100644 --- a/src/language-js/print/ternary.js +++ b/src/language-js/print/ternary.js @@ -281,29 +281,12 @@ function printTernary(path, options, print, args) { isSimpleExpressionByNodeCount(node.test, 1)) || tryToParenthesizeAlternate; - const consequentComments = []; - if ( - !isConsequentTernary && - hasComment(consequentNode, CommentCheckFlags.Dangling) - ) { - path.call(() => { - consequentComments.push(printDanglingComments(path, options), hardline); - }, "consequent"); - } const alternateComments = []; if (hasComment(node.test, CommentCheckFlags.Dangling)) { path.call(() => { alternateComments.push(printDanglingComments(path, options)); }, "test"); } - if ( - !isAlternateTernary && - hasComment(alternateNode, CommentCheckFlags.Dangling) - ) { - path.call(() => { - alternateComments.push(printDanglingComments(path, options)); - }, "alternate"); - } if (hasComment(node, CommentCheckFlags.Dangling)) { alternateComments.push(printDanglingComments(path, options)); } @@ -337,7 +320,6 @@ function printTernary(path, options, print, args) { (isInJsx && (isJsxElement(consequentNode) || isParentTernary || isInChain)) ? hardline : line, - consequentComments, printedConsequent, ]); diff --git a/tests/format/js/conditional/__snapshots__/format.test.js.snap b/tests/format/js/conditional/__snapshots__/format.test.js.snap index a5b74afcb..606f4dfeb 100644 --- a/tests/format/js/conditional/__snapshots__/format.test.js.snap +++ b/tests/format/js/conditional/__snapshots__/format.test.js.snap @@ -516,6 +516,139 @@ c */ ================================================================================ `; +exports[`issue-18944.js - {"experimentalTernaries":true} format 1`] = ` +====================================options===================================== +experimentalTernaries: true +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +condition ? ifTrue +: [ + // Hello, world! + ]; + +condition ? [ + // Hello, world! + ] +: ifFalse; + +condition ? ifTrue +: { + // Hello, world! + }; + +condition ? { + // Hello, world! + } +: ifFalse; + +condition ? ifTrue +: [ + // Hello, world! + 1, + ]; + +=====================================output===================================== +condition ? ifTrue : ( + [ + // Hello, world! + ] +); + +condition ? + [ + // Hello, world! + ] +: ifFalse; + +condition ? ifTrue : ( + { + // Hello, world! + } +); + +condition ? + { + // Hello, world! + } +: ifFalse; + +condition ? ifTrue : ( + [ + // Hello, world! + 1, + ] +); + +================================================================================ +`; + +exports[`issue-18944.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +condition ? ifTrue +: [ + // Hello, world! + ]; + +condition ? [ + // Hello, world! + ] +: ifFalse; + +condition ? ifTrue +: { + // Hello, world! + }; + +condition ? { + // Hello, world! + } +: ifFalse; + +condition ? ifTrue +: [ + // Hello, world! + 1, + ]; + +=====================================output===================================== +condition + ? ifTrue + : [ + // Hello, world! + ]; + +condition + ? [ + // Hello, world! + ] + : ifFalse; + +condition + ? ifTrue + : { + // Hello, world! + }; + +condition + ? { + // Hello, world! + } + : ifFalse; + +condition + ? ifTrue + : [ + // Hello, world! + 1, + ]; + +================================================================================ +`; + exports[`new-expression.js - {"experimentalTernaries":true} format 1`] = ` ====================================options===================================== experimentalTernaries: true diff --git a/tests/format/js/conditional/issue-18944.js b/tests/format/js/conditional/issue-18944.js new file mode 100644 index 000000000..fd368e99e --- /dev/null +++ b/tests/format/js/conditional/issue-18944.js @@ -0,0 +1,25 @@ +condition ? ifTrue +: [ + // Hello, world! + ]; + +condition ? [ + // Hello, world! + ] +: ifFalse; + +condition ? ifTrue +: { + // Hello, world! + }; + +condition ? { + // Hello, world! + } +: ifFalse; + +condition ? ifTrue +: [ + // Hello, world! + 1, + ];