Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): disallow type parameters on class constructors #8071

Merged
merged 4 commits into from
Dec 23, 2024
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
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
20 changes: 17 additions & 3 deletions tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: 54a8389f
parser_babel Summary:
AST Parsed : 2205/2218 (99.41%)
Positive Passed: 2190/2218 (98.74%)
Negative Passed: 1520/1634 (93.02%)
Negative Passed: 1522/1634 (93.15%)
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.1-sloppy-labeled-functions-if-body/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-fn-decl-labeled-inside-if/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-fn-decl-labeled-inside-loop/input.js
Expand Down Expand Up @@ -44,8 +44,6 @@ Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/ty
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/constructor-with-invalid-order-modifiers-1/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/constructor-with-invalid-order-modifiers-2/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/constructor-with-invalid-order-modifiers-3/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters-babel-7/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/declare-field-initializer/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/declare-initializer/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/declare-method/input.ts
Expand Down Expand Up @@ -11575,6 +11573,22 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
8 │ abstract accessor f = 1;
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/input.ts:2:14]
1 │ class C {
2 │ constructor<T>(foo: T) {}
· ───
3 │ }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters-babel-7/input.ts:2:14]
1 │ class C {
2 │ constructor<T>(foo: T) {}
· ───
3 │ }
╰────

× Getters and setters must have an implementation.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/declare-accessor/input.ts:2:15]
1 │ class Foo {
Expand Down
83 changes: 81 additions & 2 deletions tasks/coverage/snapshots/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: d85767ab
parser_typescript Summary:
AST Parsed : 6494/6503 (99.86%)
Positive Passed: 6483/6503 (99.69%)
Negative Passed: 1240/5747 (21.58%)
Negative Passed: 1241/5747 (21.59%)
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration10.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration11.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration13.ts
Expand Down Expand Up @@ -3756,7 +3756,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ec
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration2.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration4.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration7.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration9.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration2.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration3.d.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/parser/ecmascript5/ErrorRecovery/AccessibilityAfterStatic/parserAccessibilityAfterStatic1.ts
Expand Down Expand Up @@ -10602,6 +10601,70 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
· ─
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:2:14]
1 │ class C {
2 │ constructor<>() { }
· ──
3 │ constructor<> () { }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:3:14]
2 │ constructor<>() { }
3 │ constructor<> () { }
· ──
4 │ constructor <>() { }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:4:15]
3 │ constructor<> () { }
4 │ constructor <>() { }
· ──
5 │ constructor <> () { }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:5:15]
4 │ constructor <>() { }
5 │ constructor <> () { }
· ──
6 │ constructor< >() { }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:6:14]
5 │ constructor <> () { }
6 │ constructor< >() { }
· ───
7 │ constructor< > () { }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:7:14]
6 │ constructor< >() { }
7 │ constructor< > () { }
· ───
8 │ constructor < >() { }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:8:15]
7 │ constructor< > () { }
8 │ constructor < >() { }
· ───
9 │ constructor < > () { }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:9:15]
8 │ constructor < >() { }
9 │ constructor < > () { }
· ───
10 │ }
╰────

× Type parameter list cannot be empty.
╭─[typescript/tests/cases/compiler/parserConstructorDeclaration12.ts:2:14]
1 │ class C {
Expand Down Expand Up @@ -20194,6 +20257,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
3 │ }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts:2:14]
1 │ class C {
2 │ constructor<>() { }
· ──
3 │ }
╰────

× Type parameter list cannot be empty.
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts:2:14]
1 │ class C {
Expand Down Expand Up @@ -20228,6 +20299,14 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
4 │ }
╰────

× TS(1092): Type parameters cannot appear on a constructor declaration
╭─[typescript/tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration9.ts:2:14]
1 │ class C {
2 │ constructor<T>() { }
· ───
3 │ }
╰────

× Unexpected token
╭─[typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum4.ts:2:9]
1 │ export enum SignatureFlags {
Expand Down
Loading