diff --git a/changelog_unreleased/javascript/12349.md b/changelog_unreleased/javascript/12349.md new file mode 100644 index 000000000..35bf9cba2 --- /dev/null +++ b/changelog_unreleased/javascript/12349.md @@ -0,0 +1,23 @@ +#### Fix idempotence issues caused by line comments after `=` in assignments (#12349 by @thorn0) + + +```jsx +// Input +const kochabCooieGameOnOboleUnweave = // !!! + rhubarbRhubarb(annularCooeedSplicesWalksWayWay); + +// Prettier stable, first format +const kochabCooieGameOnOboleUnweave = rhubarbRhubarb( // !!! + annularCooeedSplicesWalksWayWay +); + +// Prettier stable, second format +const kochabCooieGameOnOboleUnweave = rhubarbRhubarb( + // !!! + annularCooeedSplicesWalksWayWay +); + +// Prettier main +const kochabCooieGameOnOboleUnweave = // !!! + rhubarbRhubarb(annularCooeedSplicesWalksWayWay); +``` diff --git a/src/language-js/print/assignment.js b/src/language-js/print/assignment.js index 1cb8bc81c..972c2f58a 100644 --- a/src/language-js/print/assignment.js +++ b/src/language-js/print/assignment.js @@ -2,7 +2,7 @@ const { isNonEmptyArray, getStringWidth } = require("../../common/util.js"); const { - builders: { line, group, indent, indentIfBreak }, + builders: { line, group, indent, indentIfBreak, lineSuffixBoundary }, utils: { cleanDoc, willBreak, canBreak }, } = require("../../document/index.js"); const { @@ -50,6 +50,7 @@ function printAssignment( group(leftDoc), operator, group(indent(line), { id: groupId }), + lineSuffixBoundary, indentIfBreak(rightDoc, { groupId }), ]); } diff --git a/tests/format/js/assignment-comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/assignment-comments/__snapshots__/jsfmt.spec.js.snap index 5bac54654..84351ac84 100644 --- a/tests/format/js/assignment-comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/assignment-comments/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,47 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`call.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +if (true) + if (true) + if (true) + if (true) + if (true) + longVariableName1 = // @ts-ignore + (variable01 + veryLongVariableNameNumber2).method(); + +=====================================output===================================== +if (true) + if (true) + if (true) + if (true) + if (true) + longVariableName1 = // @ts-ignore + (variable01 + veryLongVariableNameNumber2).method(); + +================================================================================ +`; + +exports[`call2.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const kochabCooieGameOnOboleUnweave = // ??? + rhubarbRhubarb(annularCooeedSplicesWalksWayWay); + +=====================================output===================================== +const kochabCooieGameOnOboleUnweave = // ??? + rhubarbRhubarb(annularCooeedSplicesWalksWayWay); + +================================================================================ +`; + exports[`function.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] @@ -82,7 +124,8 @@ f3 = ( a = b //comment ) => {}; -f4 = () => {}; // Comment +f4 = // Comment + () => {}; f5 = // Comment @@ -109,7 +152,8 @@ let f3 = ( a = b //comment ) => {}; -let f4 = () => {}; // Comment +let f4 = // Comment + () => {}; let f5 = // Comment @@ -141,7 +185,8 @@ const bifornCringerMoshedPerplexSawder = // !!! anodyneCondosMalateOverateRetinol; =====================================output===================================== -const kochabCooieGameOnOboleUnweave = annularCooeedSplicesWalksWayWay; // ??? +const kochabCooieGameOnOboleUnweave = // ??? + annularCooeedSplicesWalksWayWay; const bifornCringerMoshedPerplexSawder = // !!! glimseGlyphsHazardNoopsTieTie + diff --git a/tests/format/js/assignment-comments/call.js b/tests/format/js/assignment-comments/call.js new file mode 100644 index 000000000..e78303905 --- /dev/null +++ b/tests/format/js/assignment-comments/call.js @@ -0,0 +1,7 @@ +if (true) + if (true) + if (true) + if (true) + if (true) + longVariableName1 = // @ts-ignore + (variable01 + veryLongVariableNameNumber2).method(); diff --git a/tests/format/js/assignment-comments/call2.js b/tests/format/js/assignment-comments/call2.js new file mode 100644 index 000000000..e0956821e --- /dev/null +++ b/tests/format/js/assignment-comments/call2.js @@ -0,0 +1,2 @@ +const kochabCooieGameOnOboleUnweave = // ??? + rhubarbRhubarb(annularCooeedSplicesWalksWayWay);