Skip to content

Commit

Permalink
feat(parser): report class properties that are both definite and opti…
Browse files Browse the repository at this point in the history
…onal (#5181)
  • Loading branch information
DonIsaac committed Aug 25, 2024
1 parent a563968 commit 7dfd51a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
8 changes: 8 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ pub fn optional_accessor_property(span: Span) -> OxcDiagnostic {
ts_error("1276", "An 'accessor' property cannot be declared optional.").with_label(span)
}

#[cold]
pub fn optional_definite_property(span: Span) -> OxcDiagnostic {
// NOTE: could not find an error code when tsc parses this; its parser panics.
OxcDiagnostic::error("A property cannot be both optional and definite.")
.with_label(span)
.with_help("Remove either the `?` or the `!`")
}

#[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
4 changes: 4 additions & 0 deletions crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ impl<'a> ParserImpl<'a> {
};
let definite = self.eat(Kind::Bang);

if optional && definite {
self.error(diagnostics::optional_definite_property(optional_span.expand_right(1)));
}

if let PropertyKey::PrivateIdentifier(private_ident) = &key {
// `private #foo`, etc. is illegal
if self.ts_enabled() {
Expand Down
12 changes: 10 additions & 2 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: 12619ffe
parser_babel Summary:
AST Parsed : 2092/2100 (99.62%)
Positive Passed: 2082/2100 (99.14%)
Negative Passed: 1379/1492 (92.43%)
Negative Passed: 1380/1492 (92.49%)
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 @@ -53,7 +53,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/modifiers-override-errors/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/parameter-properties-binding-patterns/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/private-fields-modifier-abstract/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/class/property-optional-definite-assignment-not-allowed/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/function/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/module-class/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/declare/module-function/input.ts
Expand Down Expand Up @@ -10264,6 +10263,15 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
3 │ }
╰────

× A property cannot be both optional and definite.
╭─[babel/packages/babel-parser/test/fixtures/typescript/class/property-optional-definite-assignment-not-allowed/input.ts:2:4]
1 │ class C {
2 │ x?!: number;
· ──
3 │ }
╰────
help: Remove either the `?` or the `!`

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/typescript/const/invalid-initializer-ambient-context/input.ts:2:35]
1 │ declare module N {
Expand Down

0 comments on commit 7dfd51a

Please sign in to comment.