feat(prettier): transform expression to identifier in PropertyKey (#1398)

This commit is contained in:
Dunqing 2023-11-18 11:20:43 +08:00 committed by GitHub
parent fb7cdc6687
commit 75eb708526
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View file

@ -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),
},
}
}
}

View file

@ -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