mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(parser): report errors on optional accessor properties (#5180)
This commit is contained in:
parent
c2fa72571f
commit
a563968386
4 changed files with 32 additions and 3 deletions
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
@ -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>`
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
@ -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]
|
||||
3 │ export class Bang {
|
||||
4 │ accessor x?: Foo
|
||||
· ─
|
||||
5 │ }
|
||||
╰────
|
||||
|
||||
× The keyword 'let' is reserved
|
||||
╭─[misc/fail/oxc.js:1:1]
|
||||
1 │ let.a = 1;
|
||||
|
|
|
|||
Loading…
Reference in a new issue