diff --git a/changelog_unreleased/javascript/15547.md b/changelog_unreleased/javascript/15547.md new file mode 100644 index 000000000..b14c82194 --- /dev/null +++ b/changelog_unreleased/javascript/15547.md @@ -0,0 +1,18 @@ +#### Improve formatting for assignment its left can break (#15547 by @sosukesuzuki) + + +```jsx +// Input +params["redirectTo"] = + `${window.location.pathname}${window.location.search}${window.location.hash}`; + +// Prettier stable +params[ + "redirectTo" +] = `${window.location.pathname}${window.location.search}${window.location.hash}`; + +// Prettier main +params["redirectTo"] = + `${window.location.pathname}${window.location.search}${window.location.hash}`; + +``` diff --git a/src/language-js/print/assignment.js b/src/language-js/print/assignment.js index a1f869866..231cfd00e 100644 --- a/src/language-js/print/assignment.js +++ b/src/language-js/print/assignment.js @@ -141,11 +141,13 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) { return "never-break-after-operator"; } + const canBreakLeftDoc = canBreak(leftDoc); + if ( isComplexDestructuring(node) || isComplexTypeAliasParams(node) || hasComplexTypeAnnotation(node) || - (isArrowFunctionVariableDeclarator(node) && canBreak(leftDoc)) + (isArrowFunctionVariableDeclarator(node) && canBreakLeftDoc) ) { return "break-lhs"; } @@ -163,12 +165,13 @@ function chooseLayout(path, options, print, leftDoc, rightPropertyName) { } if ( - hasShortKey || - rightNode.type === "TemplateLiteral" || - rightNode.type === "TaggedTemplateExpression" || - rightNode.type === "BooleanLiteral" || - isNumericLiteral(rightNode) || - rightNode.type === "ClassExpression" + !canBreakLeftDoc && + (hasShortKey || + rightNode.type === "TemplateLiteral" || + rightNode.type === "TaggedTemplateExpression" || + rightNode.type === "BooleanLiteral" || + isNumericLiteral(rightNode) || + rightNode.type === "ClassExpression") ) { return "never-break-after-operator"; } diff --git a/tests/format/angular/angular/__snapshots__/jsfmt.spec.js.snap b/tests/format/angular/angular/__snapshots__/jsfmt.spec.js.snap index 38236d5de..f6aa3e031 100644 --- a/tests/format/angular/angular/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/angular/angular/__snapshots__/jsfmt.spec.js.snap @@ -1147,7 +1147,8 @@ printWidth: 1 (target)=" a[ b - ] = 1 + ] = + 1 " (target)=" (a) // hello diff --git a/tests/format/js/assignment/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/assignment/__snapshots__/jsfmt.spec.js.snap index 90ce454a1..2e5ab3064 100644 --- a/tests/format/js/assignment/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/assignment/__snapshots__/jsfmt.spec.js.snap @@ -784,6 +784,33 @@ const { _id: id3 } = ================================================================================ `; +exports[`issue-15534.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +params["redirectTo"] = \`\${window.location.pathname}\${window.location.search}\${window.location.hash}\`; + +params["redirectTo"]["codePointAt"]["name"] = + \`\${window.location.pathname}\${window.location.search}\${window.location.hash}\`; + +params.redirectTo.bar.bar.ba.barab["foo"].abr = + \`\${window.location.pathname}\${window.location.search}\${window.location.hash}\`; + +=====================================output===================================== +params["redirectTo"] = + \`\${window.location.pathname}\${window.location.search}\${window.location.hash}\`; + +params["redirectTo"]["codePointAt"]["name"] = + \`\${window.location.pathname}\${window.location.search}\${window.location.hash}\`; + +params.redirectTo.bar.bar.ba.barab["foo"].abr = + \`\${window.location.pathname}\${window.location.search}\${window.location.hash}\`; + +================================================================================ +`; + exports[`lone-arg.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/tests/format/js/assignment/issue-15534.js b/tests/format/js/assignment/issue-15534.js new file mode 100644 index 000000000..f93b90a4d --- /dev/null +++ b/tests/format/js/assignment/issue-15534.js @@ -0,0 +1,7 @@ +params["redirectTo"] = `${window.location.pathname}${window.location.search}${window.location.hash}`; + +params["redirectTo"]["codePointAt"]["name"] = + `${window.location.pathname}${window.location.search}${window.location.hash}`; + +params.redirectTo.bar.bar.ba.barab["foo"].abr = + `${window.location.pathname}${window.location.search}${window.location.hash}`;