From 1d45bce62575cc4415225545a4d6941f9b91748a Mon Sep 17 00:00:00 2001 From: SUZUKI Sosuke Date: Sun, 29 Oct 2023 21:52:38 +0900 Subject: [PATCH] Consistently format strings containing escapes (#15525) --- changelog_unreleased/javascript/15525.md | 26 +++++++++++ src/language-js/utils/index.js | 7 ++- .../strings/__snapshots__/jsfmt.spec.js.snap | 46 +++++++++++++++++++ tests/format/js/strings/escaped.js | 5 ++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/javascript/15525.md create mode 100644 tests/format/js/strings/escaped.js diff --git a/changelog_unreleased/javascript/15525.md b/changelog_unreleased/javascript/15525.md new file mode 100644 index 000000000..de0f9b20d --- /dev/null +++ b/changelog_unreleased/javascript/15525.md @@ -0,0 +1,26 @@ +#### Consistently format strings containing escapes (#15525 by @sosukesuzuki) + + +```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"); +``` diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index 864fbd7a4..01cb8077b 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -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") { diff --git a/tests/format/js/strings/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/strings/__snapshots__/jsfmt.spec.js.snap index 9997bec12..70ea41b3b 100644 --- a/tests/format/js/strings/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/strings/__snapshots__/jsfmt.spec.js.snap @@ -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"] diff --git a/tests/format/js/strings/escaped.js b/tests/format/js/strings/escaped.js new file mode 100644 index 000000000..118095cc5 --- /dev/null +++ b/tests/format/js/strings/escaped.js @@ -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");