From 75eb708526ed6b794cfcecaf1b390a649b147002 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 18 Nov 2023 11:20:43 +0800 Subject: [PATCH] feat(prettier): transform expression to identifier in PropertyKey (#1398) --- crates/oxc_prettier/src/format/mod.rs | 16 +++++++++++++++- tasks/prettier_conformance/prettier.snap.md | 4 +--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index d1c54ba36..badb912e6 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -1305,7 +1305,21 @@ impl<'a> Format<'a> for PropertyKey<'a> { match self { PropertyKey::Identifier(ident) => ident.format(p), PropertyKey::PrivateIdentifier(ident) => ident.format(p), - PropertyKey::Expression(expr) => expr.format(p), + PropertyKey::Expression(expr) => match expr { + Expression::StringLiteral(literal) => { + let expr = format!(p, literal); + let value = literal.value.as_bytes(); + if !&value[0].is_ascii_digit() && !value.contains(&b'_') { + p.str(&literal.value) + } else { + literal.format(p) + } + } + Expression::Identifier(ident) => { + array!(p, ss!("["), ident.format(p), ss!("]")) + } + _ => expr.format(p), + }, } } } diff --git a/tasks/prettier_conformance/prettier.snap.md b/tasks/prettier_conformance/prettier.snap.md index c500de884..034c3e99a 100644 --- a/tasks/prettier_conformance/prettier.snap.md +++ b/tasks/prettier_conformance/prettier.snap.md @@ -1,4 +1,4 @@ -Compatibility: 81/881 (9.19%) +Compatibility: 83/881 (9.42%) # Failed @@ -803,7 +803,6 @@ Compatibility: 81/881 (9.19%) ### object-prop-break-in * object-prop-break-in/comment.js -* object-prop-break-in/long-value.js * object-prop-break-in/short-keys.js * object-prop-break-in/test.js @@ -1076,7 +1075,6 @@ Compatibility: 81/881 (9.19%) ### unicode * unicode/combining-characters.js -* unicode/keys.js * unicode/nbsp-jsx.js ### update-expression