diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index 24d6975f9..7c412047f 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -1556,7 +1556,7 @@ impl<'a> Format<'a> for PropertyKey<'a> { wrap!(p, self, PropertyKey, { // Perf: Cache the result of `need_quote` to avoid checking it in each PropertyKey - let need_quote = p.options.quote_props.is_consistent() + let need_quote = p.options.quote_props.consistent() && match p.parent_parent_kind() { Some(AstKind::ObjectExpression(a)) => a.properties.iter().any(|x| match x { ObjectPropertyKind::ObjectProperty(p) => { @@ -1584,16 +1584,20 @@ impl<'a> Format<'a> for PropertyKey<'a> { PropertyKey::PrivateIdentifier(ident) => ident.format(p), PropertyKey::Expression(expr) => match expr { Expression::StringLiteral(literal) => { - let unquote = if need_quote { - false + // This does not pass quotes/objects.js + // because prettier uses the function `isEs5IdentifierName` based on unicode version 3, + // but `is_identifier_name` uses the latest unicode version. + if is_identifier_name(literal.value.as_str()) + && (p.options.quote_props.as_needed() + || (p.options.quote_props.consistent()/* && !needsQuoteProps.get(parent) */)) + { + string!(p, literal.value.as_str()) } else { - is_identifier_name(literal.value.as_str()) - }; - - if !unquote || p.options.quote_props.is_preserve() { - literal.format(p) - } else { - p.str(literal.value.as_str()) + Doc::Str(string::print_string( + p, + literal.value.as_str(), + p.options.single_quote, + )) } } Expression::NumberLiteral(literal) => { diff --git a/crates/oxc_prettier/src/options.rs b/crates/oxc_prettier/src/options.rs index fd8fdf17a..864f62941 100644 --- a/crates/oxc_prettier/src/options.rs +++ b/crates/oxc_prettier/src/options.rs @@ -126,10 +126,15 @@ pub enum QuoteProps { } impl QuoteProps { - pub fn is_preserve(self) -> bool { + pub fn as_needed(self) -> bool { + matches!(self, Self::AsNeeded) + } + + pub fn preserve(self) -> bool { matches!(self, Self::Preserve) } - pub fn is_consistent(self) -> bool { + + pub fn consistent(self) -> bool { matches!(self, Self::Consistent) } }