Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ pub fn optional_accessor_property(span: Span) -> OxcDiagnostic {
ts_error("1276", "An 'accessor' property cannot be declared optional.").with_label(span)
}

#[cold]
pub fn constructor_accessor(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Classes may not have a field named 'constructor'").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.
Expand Down
5 changes: 4 additions & 1 deletion crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_allocator::{Box, Vec};
use oxc_ast::ast::*;
use oxc_ecmascript::PropName;
use oxc_span::Span;
use oxc_span::{GetSpan, Span};

use crate::{
Context, ParserImpl, StatementContext, diagnostics,
Expand Down Expand Up @@ -522,6 +522,9 @@ impl<'a> ParserImpl<'a> {
if let Some(optional_span) = optional_span {
self.error(diagnostics::optional_accessor_property(optional_span));
}
if name.is_specific_string_literal("constructor") && !computed {
self.error(diagnostics::constructor_accessor(name.span()));
}
return self.parse_class_accessor_property(
span, name, computed, definite, modifiers, decorators,
);
Expand Down
6 changes: 6 additions & 0 deletions tasks/coverage/misc/fail/oxc-14014.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Bar {
accessor 'constructor'
}
abstract class Baz {
accessor 'constructor'
}
18 changes: 17 additions & 1 deletion tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parser_misc Summary:
AST Parsed : 49/49 (100.00%)
Positive Passed: 49/49 (100.00%)
Negative Passed: 90/90 (100.00%)
Negative Passed: 91/91 (100.00%)

× Cannot assign to 'arguments' in strict mode
╭─[misc/fail/arguments-eval.ts:1:10]
Expand Down Expand Up @@ -2851,6 +2851,22 @@ Negative Passed: 90/90 (100.00%)
· ──
╰────

× Classes may not have a field named 'constructor'
╭─[misc/fail/oxc-14014.ts:2:12]
1 │ class Bar {
2 │ accessor 'constructor'
· ─────────────
3 │ }
╰────

× Classes may not have a field named 'constructor'
╭─[misc/fail/oxc-14014.ts:5:12]
4 │ abstract class Baz {
5 │ accessor 'constructor'
· ─────────────
6 │ }
╰────

× Unexpected token
╭─[misc/fail/oxc-169.js:2:1]
1 │ 1<(V=82<<t-j0<(V=$<LBI<(V=ut<I<(V=$<LBI<(V=uIV=82<<t-j0<(V=$<LBI<(V=ut<I<(V=$<LBI<(V<II>
Expand Down
Loading