Skip to content

Commit

Permalink
feat(parser): report errors on optional accessor properties
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Aug 24, 2024
1 parent 8fe0ab9 commit ceea503
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 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 @@ -249,6 +249,11 @@ pub fn constructor_async(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Constructor can't be an async method").with_label(span0)
}

#[cold]
pub fn optional_accessor_property(span: Span) -> OxcDiagnostic {
ts_error("1276", "An 'accessor' property cannot be declared optional.").with_label(span)
}

#[cold]
pub fn identifier_async(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Cannot use `{x0}` as an identifier in an async context"))
Expand Down
11 changes: 10 additions & 1 deletion crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,13 @@ impl<'a> ParserImpl<'a> {
let (key, computed) =
if let Some(result) = key_name { result } else { self.parse_class_element_name()? };

let optional = self.eat(Kind::Question);
let (optional, optional_span) = if self.at(Kind::Question) {
let span = self.start_span();
self.bump_any();
(true, self.end_span(span))
} else {
(false, oxc_span::SPAN)
};
let definite = self.eat(Kind::Bang);

if let PropertyKey::PrivateIdentifier(private_ident) = &key {
Expand All @@ -281,6 +287,9 @@ impl<'a> ParserImpl<'a> {
}

if accessor {
if optional {
self.error(diagnostics::optional_accessor_property(optional_span));
}
self.parse_class_accessor_property(span, key, computed, r#static, r#abstract).map(Some)
} else if self.at(Kind::LParen) || self.at(Kind::LAngle) || r#async || generator {
// LAngle for start of type parameters `foo<T>`
Expand Down
8 changes: 8 additions & 0 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10067,6 +10067,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
4 │
╰────

× TS(1276): An 'accessor' property cannot be declared optional.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/accessor-invalid/input.ts:7:14]
6 │ abstract accessor #s;
7 │ accessor #d?;
· ─
8 │ abstract accessor f = 1;
╰────

× Expected a semicolon or an implicit semicolon after a statement, but found none
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts:1:8]
1 │ declare abstract
Expand Down
11 changes: 9 additions & 2 deletions tasks/coverage/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
parser_misc Summary:
AST Parsed : 27/27 (100.00%)
Positive Passed: 27/27 (100.00%)
Negative Passed: 15/16 (93.75%)
Expect Syntax Error: tasks/coverage/misc/fail/oxc-5177.ts
Negative Passed: 16/16 (100.00%)

× Unexpected token
╭─[misc/fail/oxc-169.js:2:1]
Expand Down Expand Up @@ -231,6 +230,14 @@ Expect Syntax Error: tasks/coverage/misc/fail/oxc-5177.ts
3 │ }
╰────

× TS(1276): An 'accessor' property cannot be declared optional.
╭─[misc/fail/oxc-5177.ts:4:15]
3export class Bang {
4accessor x?: Foo
· ─
5 │ }
╰────

× The keyword 'let' is reserved
╭─[misc/fail/oxc.js:1:1]
1let.a = 1;
Expand Down

0 comments on commit ceea503

Please sign in to comment.