diff --git a/crates/oxc_parser/src/diagnostics.rs b/crates/oxc_parser/src/diagnostics.rs index 1f0dac1e8ff16..ec17389f55867 100644 --- a/crates/oxc_parser/src/diagnostics.rs +++ b/crates/oxc_parser/src/diagnostics.rs @@ -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) diff --git a/crates/oxc_parser/src/js/class.rs b/crates/oxc_parser/src/js/class.rs index 913fccc7ab4dd..cdc33ce60954c 100644 --- a/crates/oxc_parser/src/js/class.rs +++ b/crates/oxc_parser/src/js/class.rs @@ -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(param: T ) {} } + self.error(diagnostics::ts_constructor_type_parameter(type_sig.span)); + } + if r#static { self.error(diagnostics::static_constructor(key.span())); } diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index e5349ac6bbc8f..2da1b3cee5173 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -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() {} }\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();