Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ use crate::{
impl<'a> IsolatedDeclarations<'a> {
pub(crate) fn is_literal_key(key: &PropertyKey<'a>) -> bool {
match key {
PropertyKey::StringLiteral(_)
| PropertyKey::NumericLiteral(_)
| PropertyKey::BigIntLiteral(_) => true,
PropertyKey::StringLiteral(_) | PropertyKey::NumericLiteral(_) => true,
PropertyKey::TemplateLiteral(l) => l.expressions.is_empty(),
PropertyKey::UnaryExpression(expr) => {
expr.operator.is_arithmetic()
Expand Down
33 changes: 29 additions & 4 deletions crates/oxc_isolated_declarations/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use oxc_ast::{
},
};
use oxc_span::{ContentEq, GetSpan, SPAN, Span};
use oxc_syntax::number::ToJsString;

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

/// Convert a a computed property key to a static property key when possible
fn transform_property_key(&self, key: &PropertyKey<'a>) -> PropertyKey<'a> {
match key {
// [100] -> 100
PropertyKey::NumericLiteral(literal) => self.ast.property_key_static_identifier(
literal.span,
self.ast.atom(&literal.value.to_js_string()),
),
// `["string"] -> string`
PropertyKey::StringLiteral(literal) => {
self.ast.property_key_static_identifier(literal.span, literal.value.as_str())
}
// `[`string`] -> string
PropertyKey::TemplateLiteral(literal) => {
self.ast.property_key_static_identifier(literal.span, literal.quasis[0].value.raw)
}
_ => key.clone_in(self.ast.allocator),
}
}

/// Transform object expression to TypeScript type
/// ```ts
/// export const obj = {
Expand Down Expand Up @@ -114,10 +135,13 @@ impl<'a> IsolatedDeclarations<'a> {
self.error(method_must_have_explicit_return_type(object.key.span()));
}
let params = self.transform_formal_parameters(&function.params);
let key = self.transform_property_key(key);
let computed = key.is_expression();

return Some(self.ast.ts_signature_method_signature(
object.span,
key.clone_in(self.ast.allocator),
object.computed,
key,
computed,
false,
TSMethodSignatureKind::Method,
function.type_parameters.clone_in(self.ast.allocator),
Expand Down Expand Up @@ -187,12 +211,13 @@ impl<'a> IsolatedDeclarations<'a> {
}
};

let key = self.transform_property_key(key);
let property_signature = self.ast.ts_signature_property_signature(
object.span,
false,
key.is_expression(),
false,
is_const,
key.clone_in(self.ast.allocator),
key,
type_annotation,
);
Some(property_signature)
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_isolated_declarations/tests/fixtures/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ const ObjectMethods = {
b(): number {},
c() {},
};

const ObjectKeys = {
a: 0,
["b"]: 1,
[`c`]: 2,
[3]: 3,
[-3]: 4,
[4n]: 5,
};
16 changes: 16 additions & 0 deletions crates/oxc_isolated_declarations/tests/snapshots/object.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ declare const ObjectMethods: {
b(): number
c()
};
declare const ObjectKeys: {
a: number
b: number
c: number
3: number
[-3]: number
};


==================== Errors ====================
Expand Down Expand Up @@ -52,5 +59,14 @@ declare const ObjectMethods: {
46 | };
`----

x TS9038: Computed property names on class or object literals cannot be
| inferred with --isolatedDeclarations.
,-[54:3]
53 | [-3]: 4,
54 | [4n]: 5,
: ^^
55 | };
`----


```
6 changes: 3 additions & 3 deletions tasks/coverage/snapshots/transpile.snap
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ export declare class C {
["2"]: number;
}
export declare const D: {
Symbol.iterator: number
globalThis.Symbol.toStringTag: number
[Symbol.iterator]: number
[globalThis.Symbol.toStringTag]: number
1: number
"2": number
2: number
};
export {};

Expand Down
Loading