Skip to content

Commit feca94e

Browse files
authored
refactor(parser): split check_method_definition by method kind (#14364)
Split `check_method_definition` by method kind into `check_method_definition_accessor`, `check_method_definition_constructor` and `check_method_definition_method` and keep common logic in `check_method_definition`. This avoids unnecessary checks and makes the logic easier to read.
1 parent 1489376 commit feca94e

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

crates/oxc_parser/src/js/class.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'a> ParserImpl<'a> {
416416
false,
417417
modifiers.accessibility(),
418418
);
419-
self.check_method_definition(&method_definition);
419+
self.check_method_definition_accessor(&method_definition);
420420
self.verify_modifiers(
421421
modifiers,
422422
ModifierFlags::all() - ModifierFlags::ASYNC - ModifierFlags::DECLARE,
@@ -451,7 +451,7 @@ impl<'a> ParserImpl<'a> {
451451
false,
452452
modifiers.accessibility(),
453453
);
454-
self.check_method_definition(&method_definition);
454+
self.check_method_definition_constructor(&method_definition);
455455
ClassElement::MethodDefinition(method_definition)
456456
}
457457

@@ -570,7 +570,7 @@ impl<'a> ParserImpl<'a> {
570570
optional,
571571
modifiers.accessibility(),
572572
);
573-
self.check_method_definition(&method_definition);
573+
self.check_method_definition_method(&method_definition);
574574
ClassElement::MethodDefinition(method_definition)
575575
}
576576

@@ -660,22 +660,15 @@ impl<'a> ParserImpl<'a> {
660660
}
661661

662662
fn check_method_definition(&mut self, method: &MethodDefinition<'a>) {
663-
let function = &method.value;
664-
match method.kind {
665-
MethodDefinitionKind::Get => self.check_getter(function),
666-
MethodDefinitionKind::Set => self.check_setter(function),
667-
_ => {}
668-
}
669663
if !method.computed
670664
&& let Some((name, span)) = method.key.prop_name()
671665
{
672-
if method.r#static && name == "prototype" && !self.ctx.has_ambient() {
673-
self.error(diagnostics::static_prototype(span));
674-
}
675-
if !method.r#static && name == "constructor" {
676-
if method.kind == MethodDefinitionKind::Get
677-
|| method.kind == MethodDefinitionKind::Set
678-
{
666+
if method.r#static {
667+
if name == "prototype" && !self.ctx.has_ambient() {
668+
self.error(diagnostics::static_prototype(span));
669+
}
670+
} else if name == "constructor" {
671+
if matches!(method.kind, MethodDefinitionKind::Get | MethodDefinitionKind::Set) {
679672
self.error(diagnostics::constructor_getter_setter(span));
680673
}
681674
if method.value.r#async {
@@ -686,33 +679,47 @@ impl<'a> ParserImpl<'a> {
686679
}
687680
}
688681
}
689-
if method.kind == MethodDefinitionKind::Constructor {
690-
if let Some(this_param) = &method.value.this_param {
691-
// class Foo { constructor(this: number) {} }
692-
self.error(diagnostics::ts_constructor_this_parameter(this_param.span));
693-
}
682+
}
694683

695-
if let Some(type_sig) = &method.value.type_parameters {
696-
// class Foo { constructor<T>(param: T ) {} }
697-
self.error(diagnostics::ts_constructor_type_parameter(type_sig.span));
698-
}
684+
fn check_method_definition_accessor(&mut self, method: &MethodDefinition<'a>) {
685+
self.check_method_definition(method);
686+
687+
match method.kind {
688+
MethodDefinitionKind::Get => self.check_getter(&method.value),
689+
MethodDefinitionKind::Set => self.check_setter(&method.value),
690+
_ => {}
699691
}
700692
if method.r#type.is_abstract() && method.value.body.is_some() {
701693
let (name, span) = method.key.prop_name().unwrap_or_else(|| {
702694
let span = method.key.span();
703695
(&self.source_text[span], span)
704696
});
705-
match method.kind {
706-
MethodDefinitionKind::Method => {
707-
self.error(diagnostics::abstract_method_cannot_have_implementation(name, span));
708-
}
709-
MethodDefinitionKind::Get | MethodDefinitionKind::Set => {
710-
self.error(diagnostics::abstract_accessor_cannot_have_implementation(
711-
name, span,
712-
));
713-
}
714-
MethodDefinitionKind::Constructor => {}
715-
}
697+
self.error(diagnostics::abstract_accessor_cannot_have_implementation(name, span));
698+
}
699+
}
700+
701+
fn check_method_definition_method(&mut self, method: &MethodDefinition<'a>) {
702+
self.check_method_definition(method);
703+
704+
if method.r#type.is_abstract() && method.value.body.is_some() {
705+
let (name, span) = method.key.prop_name().unwrap_or_else(|| {
706+
let span = method.key.span();
707+
(&self.source_text[span], span)
708+
});
709+
self.error(diagnostics::abstract_method_cannot_have_implementation(name, span));
710+
}
711+
}
712+
713+
fn check_method_definition_constructor(&mut self, method: &MethodDefinition<'a>) {
714+
self.check_method_definition(method);
715+
716+
if let Some(this_param) = &method.value.this_param {
717+
// class Foo { constructor(this: number) {} }
718+
self.error(diagnostics::ts_constructor_this_parameter(this_param.span));
719+
}
720+
if let Some(type_sig) = &method.value.type_parameters {
721+
// class Foo { constructor<T>(param: T ) {} }
722+
self.error(diagnostics::ts_constructor_type_parameter(type_sig.span));
716723
}
717724
}
718725
}

0 commit comments

Comments
 (0)