Minor tweak to printPropertyKey (#16059)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-02-18 17:59:36 +08:00
1 parent c4af355521
commit 5a946f4c52
3 files changed
+6 -15

No files matched your search

+1 -5
View File
@@ -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 =
+1 -7
View File
@@ -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),
);
+4 -3
View File
@@ -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")),
);
}