Skip to content

Commit

Permalink
feat(semantic): report parameter related errors for set accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Feb 5, 2024
1 parent a66e71a commit 393e5eb
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,9 @@ impl MethodDefinitionKind {
pub fn is_method(&self) -> bool {
matches!(self, Self::Method)
}
pub fn is_set(&self) -> bool {
matches!(self, Self::Set)
}
}

#[derive(Debug, Clone, Hash)]
Expand Down
28 changes: 28 additions & 0 deletions crates/oxc_semantic/src/checker/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,39 @@ impl EarlyErrorTypeScript {
AstKind::TSTypeParameterDeclaration(declaration) => {
check_ts_type_parameter_declaration(declaration, ctx);
}
AstKind::MethodDefinition(method) => {
check_method_definition(method, ctx);
}
_ => {}
}
}
}

fn check_method_definition(method: &MethodDefinition<'_>, ctx: &SemanticBuilder<'_>) {
#[derive(Debug, Error, Diagnostic)]
#[error("A 'set' accessor must have exactly one parameter.")]
#[diagnostic()]
struct SetAccessorWithParameters(#[label] Span);

#[derive(Debug, Error, Diagnostic)]
#[error("A 'set' accessor cannot have rest parameter.")]
#[diagnostic()]
struct SetAccessorWithRestParameter(#[label] Span);

if method.kind.is_set() {
method.value.params.rest.as_ref().map_or_else(
|| {
if method.value.params.items.len() != 1 {
ctx.error(SetAccessorWithParameters(method.value.params.span));
}
},
|rest| {
ctx.error(SetAccessorWithRestParameter(rest.span));
},
);
}
}

fn check_ts_type_parameter_declaration(
declaration: &TSTypeParameterDeclaration<'_>,
ctx: &SemanticBuilder<'_>,
Expand Down
12 changes: 12 additions & 0 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4015,6 +4015,18 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
· ──
╰────
× A 'set' accessor must have exactly one parameter.
╭─[es2015/uncategorised/346/input.js:1:19]
1class A { set prop() {} }
· ──
╰────
× A 'set' accessor must have exactly one parameter.
╭─[es2015/uncategorised/347/input.js:1:19]
1class A { set prop(x, y) {} }
· ──────
╰────
× A 'set' accessor must have exactly one parameter.
╭─[es2015/uncategorised/347/input.js:1:19]
1class A { set prop(x, y) {} }
Expand Down
52 changes: 49 additions & 3 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parser_typescript Summary:
AST Parsed : 5239/5243 (99.92%)
Positive Passed: 5232/5243 (99.79%)
Negative Passed: 1038/4879 (21.27%)
Negative Passed: 1040/4879 (21.32%)
Expect Syntax Error: "compiler/ClassDeclaration10.ts"
Expect Syntax Error: "compiler/ClassDeclaration11.ts"
Expect Syntax Error: "compiler/ClassDeclaration13.ts"
Expand Down Expand Up @@ -33,7 +33,6 @@ Expect Syntax Error: "compiler/accessorAccidentalCallDiagnostic.ts"
Expect Syntax Error: "compiler/accessorDeclarationEmitVisibilityErrors.ts"
Expect Syntax Error: "compiler/accessorInferredReturnTypeErrorInReturnStatement.ts"
Expect Syntax Error: "compiler/accessorWithInitializer.ts"
Expect Syntax Error: "compiler/accessorWithRestParam.ts"
Expect Syntax Error: "compiler/accessorWithoutBody1.ts"
Expect Syntax Error: "compiler/accessorWithoutBody2.ts"
Expect Syntax Error: "compiler/accessorsInAmbientContext.ts"
Expand Down Expand Up @@ -3173,7 +3172,6 @@ Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/
Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration11.ts"
Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration16.ts"
Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration17.ts"
Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration18.ts"
Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration2.ts"
Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration3.ts"
Expect Syntax Error: "conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration7.ts"
Expand Down Expand Up @@ -4099,6 +4097,22 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
6 │ }
╰────

× A 'set' accessor cannot have rest parameter.
╭─[compiler/accessorWithRestParam.ts:4:11]
3 │ class C {
4 │ set X(...v) { }
· ────
5 │ static set X(...v2) { }
╰────

× A 'set' accessor cannot have rest parameter.
╭─[compiler/accessorWithRestParam.ts:5:18]
4 │ set X(...v) { }
5 │ static set X(...v2) { }
· ─────
6 │ }
╰────

× Unexpected token
╭─[compiler/aliasErrors.ts:13:12]
12 │ import m2 = no.mod;
Expand Down Expand Up @@ -16774,6 +16788,14 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
× A 'set' accessor must have exactly one parameter.
╭─[conformance/parser/ecmascript5/ErrorRecovery/parserErrantSemicolonInClass1.ts:15:17]
14 │ }
15 │ public set d() {
· ──
16 │ }
╰────

× A 'set' accessor must have exactly one parameter.
╭─[conformance/parser/ecmascript5/ErrorRecovery/parserErrantSemicolonInClass1.ts:15:17]
14 │ }
15 │ public set d() {
· ──
16 │ }
Expand Down Expand Up @@ -17135,6 +17157,22 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
4 │ }
╰────

× A 'set' accessor must have exactly one parameter.
╭─[conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration13.ts:3:11]
2 │ class C {
3 │ set Foo() { }
· ──
4 │ }
╰────

× A 'set' accessor must have exactly one parameter.
╭─[conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration14.ts:3:11]
2 │ class C {
3 │ set Foo(a: number, b: number) { }
· ──────────────────────
4 │ }
╰────

× A 'set' accessor must have exactly one parameter.
╭─[conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration14.ts:3:11]
2 │ class C {
Expand All @@ -17151,6 +17189,14 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts"
4 │ }
╰────

× A 'set' accessor cannot have rest parameter.
╭─[conformance/parser/ecmascript5/MemberAccessorDeclarations/parserMemberAccessorDeclaration18.ts:3:12]
2 │ class C {
3 │ set Foo(...a) { }
· ────
4 │ }
╰────

× Expected a semicolon or an implicit semicolon after a statement, but found none
╭─[conformance/parser/ecmascript5/MemberFunctionDeclarations/parserMemberFunctionDeclaration4.ts:2:11]
1 │ class C {
Expand Down

0 comments on commit 393e5eb

Please sign in to comment.