mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +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)
|
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]
|
#[cold]
|
||||||
pub fn identifier_async(x0: &str, span1: Span) -> OxcDiagnostic {
|
pub fn identifier_async(x0: &str, span1: Span) -> OxcDiagnostic {
|
||||||
OxcDiagnostic::error(format!("Cannot use `{x0}` as an identifier in an async context"))
|
OxcDiagnostic::error(format!("Cannot use `{x0}` as an identifier in an async context"))
|
||||||
|
|
|
||||||
|
|
@ -263,7 +263,13 @@ impl<'a> ParserImpl<'a> {
|
||||||
let (key, computed) =
|
let (key, computed) =
|
||||||
if let Some(result) = key_name { result } else { self.parse_class_element_name()? };
|
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);
|
let definite = self.eat(Kind::Bang);
|
||||||
|
|
||||||
if let PropertyKey::PrivateIdentifier(private_ident) = &key {
|
if let PropertyKey::PrivateIdentifier(private_ident) = &key {
|
||||||
|
|
@ -281,6 +287,9 @@ impl<'a> ParserImpl<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if accessor {
|
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)
|
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 {
|
} else if self.at(Kind::LParen) || self.at(Kind::LAngle) || r#async || generator {
|
||||||
// LAngle for start of type parameters `foo<T>`
|
// 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 │
|
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
|
× 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]
|
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/declare-new-line-abstract/input.ts:1:8]
|
||||||
1 │ declare abstract
|
1 │ declare abstract
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
parser_misc Summary:
|
parser_misc Summary:
|
||||||
AST Parsed : 27/27 (100.00%)
|
AST Parsed : 27/27 (100.00%)
|
||||||
Positive Passed: 27/27 (100.00%)
|
Positive Passed: 27/27 (100.00%)
|
||||||
Negative Passed: 15/16 (93.75%)
|
Negative Passed: 16/16 (100.00%)
|
||||||
Expect Syntax Error: tasks/coverage/misc/fail/oxc-5177.ts
|
|
||||||
|
|
||||||
× Unexpected token
|
× Unexpected token
|
||||||
╭─[misc/fail/oxc-169.js:2:1]
|
╭─[misc/fail/oxc-169.js:2:1]
|
||||||
|
|
@ -231,6 +230,14 @@ Expect Syntax Error: tasks/coverage/misc/fail/oxc-5177.ts
|
||||||
3 │ }
|
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
|
× The keyword 'let' is reserved
|
||||||
╭─[misc/fail/oxc.js:1:1]
|
╭─[misc/fail/oxc.js:1:1]
|
||||||
1 │ let.a = 1;
|
1 │ let.a = 1;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue