Consistently format strings containing escapes (#15525)

This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2023-10-29 21:52:38 +09:00
1 parent 3974359d32
commit 1d45bce625
4 files changed
+82 -2

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
#### Consistently format strings containing escapes (#15525 by @sosukesuzuki)
<!-- prettier-ignore -->
```jsx
// Input
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 =
goog.getMsg("That\'s all we know");
// Prettier stable
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 = goog.getMsg(
"That's all we know",
);
// Prettier main
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 =
goog.getMsg("That's all we know");
```
+5 -2
View File
@@ -6,6 +6,7 @@ import isNextLineEmptyAfterIndex from "../../utils/is-next-line-empty.js";
import getStringWidth from "../../utils/get-string-width.js";
import { locStart, locEnd, hasSameLocStart } from "../loc.js";
import getVisitorKeys from "../traverse/get-visitor-keys.js";
import printString from "../../utils/print-string.js";
import createTypeCheckFunction from "./create-type-check-function.js";
import isBlockComment from "./is-block-comment.js";
import isNodeMatches from "./is-node-matches.js";
@@ -544,7 +545,9 @@ function getExpressionInnerNodeCount(node, maxCount) {
}
const LONE_SHORT_ARGUMENT_THRESHOLD_RATE = 0.25;
function isLoneShortArgument(node, { printWidth }) {
function isLoneShortArgument(node, options) {
const { printWidth } = options;
if (hasComment(node)) {
return false;
}
@@ -568,7 +571,7 @@ function isLoneShortArgument(node, { printWidth }) {
}
if (isStringLiteral(node)) {
return rawText(node).length <= threshold;
return printString(rawText(node), options).length <= threshold;
}
if (node.type === "TemplateLiteral") {
@@ -1,5 +1,51 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`escaped.js - {"trailingComma":"all"} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow"]
printWidth: 80
trailingComma: "all"
| printWidth
=====================================input======================================
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 =
goog.getMsg("That\\'s all we know");
=====================================output=====================================
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 =
goog.getMsg("That's all we know");
================================================================================
`;
exports[`escaped.js - {"trailingComma":"es5"} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow"]
printWidth: 80
trailingComma: "es5"
| printWidth
=====================================input======================================
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 =
goog.getMsg("That\\'s all we know");
=====================================output=====================================
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 =
goog.getMsg("That's all we know");
================================================================================
`;
exports[`multiline-literal.js - {"trailingComma":"all"} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow"]
+5
View File
@@ -0,0 +1,5 @@
export const MSG_GENERIC_OPERATION_FAILURE_BODY_1 =
goog.getMsg("That's all we know");
export const MSG_GENERIC_OPERATION_FAILURE_BODY_2 =
goog.getMsg("That\'s all we know");