Skip to content

Commit

Permalink
fix(oxc_parser): disallow type parameters on class constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
srijan-paul committed Dec 23, 2024
1 parent e632a7b commit d34bcfb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ pub fn ts_constructor_this_parameter(span: Span) -> OxcDiagnostic {
ts_error("2681", "A constructor cannot have a `this` parameter.").with_label(span)
}

#[cold]
pub fn ts_constructor_type_parameter(span: Span) -> OxcDiagnostic {
ts_error("1092", "Type parameters cannot appear on a constructor declaration").with_label(span)
}

#[cold]
pub fn ts_arrow_function_this_parameter(span: Span) -> OxcDiagnostic {
ts_error("2730", "An arrow function cannot have a `this` parameter.").with_label(span)
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ impl<'a> ParserImpl<'a> {
self.error(diagnostics::ts_constructor_this_parameter(this_param.span));
}

if let Some(type_sig) = &value.type_parameters {
// class Foo { constructor<T>(param: T ) {} }
self.error(diagnostics::ts_constructor_type_parameter(type_sig.span));
}

if r#static {
self.error(diagnostics::static_constructor(key.span()));
}
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,16 @@ mod test {
assert_eq!(ret.errors.len(), 0);
}

#[test]
fn ts_constructor_type_param() {
let allocator = Allocator::default();
let source_type = SourceType::from_path(Path::new("class.ts")).unwrap();
let source = "class A { constructor<T>() {} }\n";
let ret = Parser::new(&allocator, source, source_type).parse();
assert_eq!(ret.errors.len(), 1);
assert_eq!(ret.errors.first().unwrap().to_string(), "Type parameters cannot appear on a constructor declaration");
}

#[test]
fn directives() {
let allocator = Allocator::default();
Expand Down

0 comments on commit d34bcfb

Please sign in to comment.