Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions crates/oxc_isolated_declarations/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oxc_ast::{
},
};
use oxc_span::{ContentEq, GetSpan, SPAN, Span};
use oxc_syntax::number::ToJsString;
use oxc_syntax::{identifier::is_identifier_name, number::ToJsString};

use crate::{
IsolatedDeclarations,
Expand Down Expand Up @@ -66,6 +66,10 @@ impl<'a> IsolatedDeclarations<'a> {
})
}

fn is_static_property_key(key: &str) -> bool {
is_identifier_name(key) || key.chars().all(|c| c.is_ascii_digit())
Comment thread
Dunqing marked this conversation as resolved.
Outdated
}

/// Convert a a computed property key to a static property key when possible
fn transform_property_key(&self, key: &PropertyKey<'a>) -> PropertyKey<'a> {
Comment thread
Dunqing marked this conversation as resolved.
match key {
Expand All @@ -75,11 +79,13 @@ impl<'a> IsolatedDeclarations<'a> {
self.ast.atom(&literal.value.to_js_string()),
),
// `["string"] -> string`
PropertyKey::StringLiteral(literal) => {
PropertyKey::StringLiteral(literal) if Self::is_static_property_key(&literal.value) => {
self.ast.property_key_static_identifier(literal.span, literal.value.as_str())
Comment thread
Dunqing marked this conversation as resolved.
}
// `[`string`] -> string
PropertyKey::TemplateLiteral(literal) => {
PropertyKey::TemplateLiteral(literal)
if Self::is_static_property_key(&literal.quasis[0].value.raw) =>
{
self.ast.property_key_static_identifier(literal.span, literal.quasis[0].value.raw)
}
_ => key.clone_in(self.ast.allocator),
Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_isolated_declarations/tests/fixtures/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ const ObjectKeys = {
[-3]: 4,
[4n]: 5,
};

type family =
'-apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif'
const family: family =
'-apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif'
const theme = {
'font-family': family as family
} as const

const X = {
'async': 1,
'await': 2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ declare const ObjectKeys: {
3: number
[-3]: number
};
type family = "-apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif";
declare const family: family;
declare const theme: {
readonly ["font-family"]: family
};
declare const X: {
async: number
await: number
};


==================== Errors ====================
Expand Down
Loading