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
67 changes: 31 additions & 36 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::borrow::Cow;

use oxc_allocator::{Box as ArenaBox, CloneIn, Vec as ArenaVec};
use oxc_ast::{NONE, ast::*};
use oxc_span::{GetSpan, SPAN};
use rustc_hash::FxHashMap;
use oxc_span::{ContentEq, GetSpan, SPAN};

use crate::{
IsolatedDeclarations,
Expand Down Expand Up @@ -51,12 +48,16 @@ impl<'a> IsolatedDeclarations<'a> {
}
}

pub(crate) fn is_valid_property_key(key: &PropertyKey<'a>) -> bool {
Self::is_literal_key(key) || Self::is_global_symbol(key)
}

pub(crate) fn report_property_key(&self, key: &PropertyKey<'a>) -> bool {
if !Self::is_literal_key(key) && !Self::is_global_symbol(key) {
if Self::is_valid_property_key(key) {
false
} else {
self.error(computed_property_name(key.span()));
true
} else {
false
}
}

Expand Down Expand Up @@ -291,24 +292,20 @@ impl<'a> IsolatedDeclarations<'a> {
/// ### Setter
///
/// 1. If it has no parameter type, infer it from the getter method's return type
fn collect_getter_or_setter_annotations(
fn collect_accessor_annotations(
&self,
decl: &Class<'a>,
) -> FxHashMap<Cow<str>, ArenaBox<'a, TSTypeAnnotation<'a>>> {
let mut method_annotations = FxHashMap::default();
) -> Vec<(PropertyKey<'a>, ArenaBox<'a, TSTypeAnnotation<'a>>)> {
let mut method_annotations = Vec::new();
for element in &decl.body.body {
if let ClassElement::MethodDefinition(method) = element {
if (method.key.is_private_identifier()
|| method.accessibility.is_some_and(TSAccessibility::is_private))
|| (method.computed && !Self::is_literal_key(&method.key))
|| (method.computed && !Self::is_valid_property_key(&method.key))
{
continue;
}

let Some(name) = method.key.static_name() else {
continue;
};

match method.kind {
MethodDefinitionKind::Set => {
let Some(first_param) = method.value.params.items.first() else {
Expand All @@ -317,17 +314,15 @@ impl<'a> IsolatedDeclarations<'a> {
if let Some(annotation) =
first_param.pattern.type_annotation.clone_in(self.ast.allocator)
{
method_annotations.insert(name, annotation);
method_annotations
.push((method.key.clone_in(self.ast.allocator), annotation));
}
}
MethodDefinitionKind::Get => {
let function = &method.value;
if let Some(annotation) = function
.return_type
.clone_in(self.ast.allocator)
.or_else(|| self.infer_function_return_type(function))
{
method_annotations.insert(name, annotation);
if let Some(annotation) = self.infer_function_return_type(function) {
method_annotations
.push((method.key.clone_in(self.ast.allocator), annotation));
}
}
_ => {}
Expand Down Expand Up @@ -356,7 +351,7 @@ impl<'a> IsolatedDeclarations<'a> {
}
}

let setter_getter_annotations = self.collect_getter_or_setter_annotations(decl);
let accessor_annotations = self.collect_accessor_annotations(decl);
let mut has_private_key = false;
let mut elements = self.ast.vec();
let mut is_function_overloads = false;
Expand Down Expand Up @@ -400,10 +395,12 @@ impl<'a> IsolatedDeclarations<'a> {
let mut params = params.clone_in(self.ast.allocator);
if let Some(param) = params.items.first_mut() {
if let Some(annotation) =
method.key.static_name().and_then(|name| {
setter_getter_annotations
.get(&name)
.map(|a| a.clone_in(self.ast.allocator))
accessor_annotations.iter().find_map(|(key, annotation)| {
if method.key.content_eq(key) {
Some(annotation.clone_in(self.ast.allocator))
} else {
None
}
})
{
param.pattern.type_annotation = Some(annotation);
Expand Down Expand Up @@ -448,15 +445,13 @@ impl<'a> IsolatedDeclarations<'a> {
rt
}
MethodDefinitionKind::Get => {
let rt = method.value.return_type.clone_in(self.ast.allocator).or_else(
|| {
method
.key
.static_name()
.and_then(|name| setter_getter_annotations.get(&name))
.map(|a| a.clone_in(self.ast.allocator))
},
);
let rt = accessor_annotations.iter().find_map(|(key, annotation)| {
if method.key.content_eq(key) {
Some(annotation.clone_in(self.ast.allocator))
} else {
None
}
});
if rt.is_none() {
self.error(accessor_must_have_explicit_return_type(
method.key.span(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,42 @@ class ClsBad {
set a(v) {
}
}


/// Infer type from the function body of the getter
class GlobalSymbol1 {
set [Symbol.toStringTag](v) {
}
get [Symbol.toStringTag]() {
return "string";
}
}

// Infer type from the parameter type of the setter
class GlobalSymbol2 {
set [Symbol.toStringTag](v: number) {
}
get [Symbol.toStringTag]() {
return GlobalSymbol2
}
}

// Infer type from the parameter type of the setter
class GlobalSymbol3 {
set [Symbol.toStringTag](v: number) {
}
get [Symbol.toStringTag](): string {
return "string";
}
}

// Cannot infer type from the function body of the getter
class GlobalSymbol4 {
set [Symbol.toStringTag](v) {
}
get [Symbol.toStringTag]() {
return GlobalSymbol4;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ declare class ClsBad {
get a();
set a(v);
}
/// Infer type from the function body of the getter
declare class GlobalSymbol1 {
set [Symbol.toStringTag](v: string);
get [Symbol.toStringTag](): string;
}
// Infer type from the parameter type of the setter
declare class GlobalSymbol2 {
set [Symbol.toStringTag](v: number);
get [Symbol.toStringTag](): number;
}
// Infer type from the parameter type of the setter
declare class GlobalSymbol3 {
set [Symbol.toStringTag](v: number);
get [Symbol.toStringTag](): number;
}
// Cannot infer type from the function body of the getter
declare class GlobalSymbol4 {
set [Symbol.toStringTag](v);
get [Symbol.toStringTag]();
}


==================== Errors ====================
Expand All @@ -35,5 +55,14 @@ declare class ClsBad {
26 | return;
`----

x TS9009: At least one accessor must have an explicit return type annotation
| with --isolatedDeclarations.
,-[64:8]
63 | }
64 | get [Symbol.toStringTag]() {
: ^^^^^^^^^^^^^^^^^^
65 | return GlobalSymbol4;
`----


```
Loading