Improve formatting for assignment its left can break (#15547)

This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2023-10-28 14:29:17 +09:00
1 parent f3dfbaf714
commit f852b8010e
5 files changed
+64 -8

No files matched your search

+18
View File
@@ -0,0 +1,18 @@
#### Improve formatting for assignment its left can break (#15547 by @sosukesuzuki)
<!-- prettier-ignore -->
```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}`;
```
+10 -7
View File
@@ -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";
}
@@ -1147,7 +1147,8 @@ printWidth: 1
(target)="
a[
b
] = 1
] =
1
"
(target)="
(a) // hello
@@ -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"]
@@ -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}`;