From af70a98a629fc4d26e0107c18994784a5f49c851 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 20 Jan 2024 00:18:32 +0900 Subject: [PATCH 1/4] Mark dangling comments --- src/language-js/comments/handle-comments.js | 7 ++++- src/language-js/print/ternary.js | 32 ++++++++++++++++----- src/language-js/utils/index.js | 5 ++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 8ce2336d3980..a5de47acb80b 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -21,6 +21,7 @@ import { isObjectProperty, isPrettierIgnoreComment, isUnionType, + experimentalTernaryDanglingCommentMarker, } from "../utils/index.js"; import isBlockComment from "../utils/is-block-comment.js"; import isTypeCastComment from "../utils/is-type-cast-comment.js"; @@ -395,7 +396,11 @@ function handleConditionalExpressionComments({ ) ) ) { - addDanglingComment(enclosingNode, comment); + addDanglingComment( + enclosingNode, + comment, + experimentalTernaryDanglingCommentMarker, + ); return true; } addLeadingComment(followingNode, comment); diff --git a/src/language-js/print/ternary.js b/src/language-js/print/ternary.js index 6e2ee6737ac3..21b1a6dba57b 100644 --- a/src/language-js/print/ternary.js +++ b/src/language-js/print/ternary.js @@ -14,6 +14,7 @@ import { locEnd, locStart } from "../loc.js"; import pathNeedsParens from "../needs-parens.js"; import { CommentCheckFlags, + experimentalTernaryDanglingCommentMarker, getComments, hasComment, isBinaryCastExpression, @@ -284,16 +285,23 @@ function printTernary(path, options, print, args) { hasComment(consequentNode, CommentCheckFlags.Dangling) ) { path.call((childPath) => { - consequentComments.push( - printDanglingComments(childPath, options), - hardline, - ); + const danglingComments = printDanglingComments(childPath, options, { + marker: experimentalTernaryDanglingCommentMarker, + }); + if (danglingComments) { + consequentComments.push(danglingComments, hardline); + } }, "consequent"); } const alternateComments = []; if (hasComment(node.test, CommentCheckFlags.Dangling)) { path.call((childPath) => { - alternateComments.push(printDanglingComments(childPath, options)); + const danglingComments = printDanglingComments(childPath, options, { + marker: experimentalTernaryDanglingCommentMarker, + }); + if (danglingComments) { + alternateComments.push(danglingComments, hardline); + } }, "test"); } if ( @@ -301,11 +309,21 @@ function printTernary(path, options, print, args) { hasComment(alternateNode, CommentCheckFlags.Dangling) ) { path.call((childPath) => { - alternateComments.push(printDanglingComments(childPath, options)); + const danglingComments = printDanglingComments(childPath, options, { + marker: experimentalTernaryDanglingCommentMarker, + }); + if (danglingComments) { + alternateComments.push(danglingComments, hardline); + } }, "alternate"); } if (hasComment(node, CommentCheckFlags.Dangling)) { - alternateComments.push(printDanglingComments(path, options)); + const danglingComments = printDanglingComments(path, options, { + marker: experimentalTernaryDanglingCommentMarker, + }); + if (danglingComments) { + alternateComments.push(danglingComments); + } } const testId = Symbol("test"); diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index 821b40f4a363..4c0a5208d417 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -1217,6 +1217,10 @@ const isIntersectionType = createTypeCheckFunction([ "TSIntersectionType", ]); +const experimentalTernaryDanglingCommentMarker = Symbol( + "ternaryDanglingCommentMarker", +); + export { CommentCheckFlags, createTypeCheckFunction, @@ -1282,4 +1286,5 @@ export { shouldFlatten, shouldPrintComma, startsWithNoLookaheadToken, + experimentalTernaryDanglingCommentMarker, }; From c666f268ff264acde1cbde5d38fbfbef0e939363 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 20 Jan 2024 00:18:38 +0900 Subject: [PATCH 2/4] Add tests --- tests/format/js/conditional/15875.js | 16 +++ .../__snapshots__/jsfmt.spec.js.snap | 99 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/format/js/conditional/15875.js diff --git a/tests/format/js/conditional/15875.js b/tests/format/js/conditional/15875.js new file mode 100644 index 000000000000..0f9ad82336c2 --- /dev/null +++ b/tests/format/js/conditional/15875.js @@ -0,0 +1,16 @@ +([ + /* comment1 */ +]) ? [ + /* comment2 */ +] : [ + /* comment3 */ +]; + +({ + /* comment4 */ +}) ? ({ + /* comment5 */ +}) : ({ + /* comment6 */ +}); + diff --git a/tests/format/js/conditional/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/conditional/__snapshots__/jsfmt.spec.js.snap index 90675cc6f84d..f492a136813f 100644 --- a/tests/format/js/conditional/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/conditional/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,104 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`15875.js - {"experimentalTernaries":true} format 1`] = ` +====================================options===================================== +experimentalTernaries: true +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +([ + /* comment1 */ +]) ? [ + /* comment2 */ +] : [ + /* comment3 */ +]; + +({ + /* comment4 */ +}) ? ({ + /* comment5 */ +}) : ({ + /* comment6 */ +}); + + +=====================================output===================================== +( + [ + /* comment1 */ + ] +) ? + [ + /* comment2 */ + ] +: [ + /* comment3 */ + ]; + +( + ({ + /* comment4 */ + }) +) ? + { + /* comment5 */ + } +: { + /* comment6 */ + }; + +================================================================================ +`; + +exports[`15875.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +([ + /* comment1 */ +]) ? [ + /* comment2 */ +] : [ + /* comment3 */ +]; + +({ + /* comment4 */ +}) ? ({ + /* comment5 */ +}) : ({ + /* comment6 */ +}); + + +=====================================output===================================== +[ + /* comment1 */ +] + ? [ + /* comment2 */ + ] + : [ + /* comment3 */ + ]; + +({ + /* comment4 */ +}) + ? { + /* comment5 */ + } + : { + /* comment6 */ + }; + +================================================================================ +`; + exports[`comments.js - {"experimentalTernaries":true} format 1`] = ` ====================================options===================================== experimentalTernaries: true From dded76cfeeca7be51d1abd37479f5f4a285d6421 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 20 Jan 2024 00:22:19 +0900 Subject: [PATCH 3/4] Add changelog --- changelog_unreleased/javascript/15965.md | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 changelog_unreleased/javascript/15965.md diff --git a/changelog_unreleased/javascript/15965.md b/changelog_unreleased/javascript/15965.md new file mode 100644 index 000000000000..d0f01a9783f9 --- /dev/null +++ b/changelog_unreleased/javascript/15965.md @@ -0,0 +1,30 @@ +#### Fix comment duplication for experimental ternaries (#15965 by @sosukesuzuki) + + +```jsx +// Input +x = + IS_AUTOTEST || (IS_DEV && !IS_TEST) ? + [ + /*FooBlock*/ + ] + : []; + +// Prettier stable +x = + IS_AUTOTEST || (IS_DEV && !IS_TEST) ? + /*FooBlock*/ + [ + /*FooBlock*/ + ] + : []; + +// Prettier main +x = + IS_AUTOTEST || (IS_DEV && !IS_TEST) ? + [ + /*FooBlock*/ + ] + : []; + +``` From 044343210e9654da7e81f6da280affa2b6fae18d Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 20 Jan 2024 00:27:10 +0900 Subject: [PATCH 4/4] Run `fix:eslint` --- src/language-js/comments/handle-comments.js | 2 +- src/language-js/utils/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index a5de47acb80b..9083cacf3f9d 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -11,6 +11,7 @@ import isNonEmptyArray from "../../utils/is-non-empty-array.js"; import { locEnd, locStart } from "../loc.js"; import { createTypeCheckFunction, + experimentalTernaryDanglingCommentMarker, getCallArguments, getFunctionParameters, isCallExpression, @@ -21,7 +22,6 @@ import { isObjectProperty, isPrettierIgnoreComment, isUnionType, - experimentalTernaryDanglingCommentMarker, } from "../utils/index.js"; import isBlockComment from "../utils/is-block-comment.js"; import isTypeCastComment from "../utils/is-type-cast-comment.js"; diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index 4c0a5208d417..30206d01bbb2 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -1224,6 +1224,7 @@ const experimentalTernaryDanglingCommentMarker = Symbol( export { CommentCheckFlags, createTypeCheckFunction, + experimentalTernaryDanglingCommentMarker, getCallArguments, getCallArgumentSelector, getComments, @@ -1286,5 +1287,4 @@ export { shouldFlatten, shouldPrintComma, startsWithNoLookaheadToken, - experimentalTernaryDanglingCommentMarker, };