From 5a946f4c5203623947e21ff9b0e723d7d6b19b7b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 18 Feb 2024 17:59:36 +0800 Subject: [PATCH] Minor tweak to `printPropertyKey` (#16059) --- src/language-js/print/jsx.js | 6 +----- src/language-js/print/property.js | 8 +------- src/language-js/utils/index.js | 7 ++++--- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index 077dd8f5a..64d0f7b33 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -573,7 +573,6 @@ function printJsxOpeningElement(path, options, print) { // don't break up opening elements with a single long text attribute if ( node.attributes?.length === 1 && - node.attributes[0].value && isStringLiteral(node.attributes[0].value) && !node.attributes[0].value.value.includes("\n") && // We should break for the following cases: @@ -601,10 +600,7 @@ function printJsxOpeningElement(path, options, print) { // We should print the opening element expanded if any prop value is a // string literal with newlines const shouldBreak = node.attributes?.some( - (attr) => - attr.value && - isStringLiteral(attr.value) && - attr.value.value.includes("\n"), + (attr) => isStringLiteral(attr.value) && attr.value.value.includes("\n"), ); const attributeLine = diff --git a/src/language-js/print/property.js b/src/language-js/print/property.js index c62ad0a56..2ccb7102f 100644 --- a/src/language-js/print/property.js +++ b/src/language-js/print/property.js @@ -129,15 +129,9 @@ function printPropertyKey(path, options, print) { const { key } = node; if (options.quoteProps === "consistent" && !needsQuoteProps.has(parent)) { - const objectHasStringProp = ( - parent.properties || - parent.body || - parent.members || - parent.attributes - ).some( + const objectHasStringProp = path.siblings.some( (prop) => !prop.computed && - prop.key && isStringLiteral(prop.key) && !isStringKeySafeToUnquote(prop, options), ); diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index dea3fe890..369e0c381 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -164,9 +164,10 @@ function isSignedNumericLiteral(node) { * @returns {boolean} */ function isStringLiteral(node) { - return ( - node.type === "StringLiteral" || - (node.type === "Literal" && typeof node.value === "string") + return Boolean( + node && + (node.type === "StringLiteral" || + (node.type === "Literal" && typeof node.value === "string")), ); }