Skip to content

Commit f4d6790

Browse files
authored
fix(parser): forbid accessors named constructor (#14017)
Re-creation of #14015. Closes #14014.
1 parent abd8c8a commit f4d6790

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

crates/oxc_parser/src/diagnostics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ pub fn optional_accessor_property(span: Span) -> OxcDiagnostic {
306306
ts_error("1276", "An 'accessor' property cannot be declared optional.").with_label(span)
307307
}
308308

309+
#[cold]
310+
pub fn constructor_accessor(span: Span) -> OxcDiagnostic {
311+
OxcDiagnostic::error("Classes may not have a field named 'constructor'").with_label(span)
312+
}
313+
309314
#[cold]
310315
pub fn optional_definite_property(span: Span) -> OxcDiagnostic {
311316
// NOTE: could not find an error code when tsc parses this; its parser panics.

crates/oxc_parser/src/js/class.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use oxc_allocator::{Box, Vec};
22
use oxc_ast::ast::*;
33
use oxc_ecmascript::PropName;
4-
use oxc_span::Span;
4+
use oxc_span::{GetSpan, Span};
55

66
use crate::{
77
Context, ParserImpl, StatementContext, diagnostics,
@@ -522,6 +522,9 @@ impl<'a> ParserImpl<'a> {
522522
if let Some(optional_span) = optional_span {
523523
self.error(diagnostics::optional_accessor_property(optional_span));
524524
}
525+
if name.is_specific_string_literal("constructor") && !computed {
526+
self.error(diagnostics::constructor_accessor(name.span()));
527+
}
525528
return self.parse_class_accessor_property(
526529
span, name, computed, definite, modifiers, decorators,
527530
);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Bar {
2+
accessor 'constructor'
3+
}
4+
abstract class Baz {
5+
accessor 'constructor'
6+
}

tasks/coverage/snapshots/parser_misc.snap

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
parser_misc Summary:
22
AST Parsed : 49/49 (100.00%)
33
Positive Passed: 49/49 (100.00%)
4-
Negative Passed: 90/90 (100.00%)
4+
Negative Passed: 91/91 (100.00%)
55

66
× Cannot assign to 'arguments' in strict mode
77
╭─[misc/fail/arguments-eval.ts:1:10]
@@ -2851,6 +2851,22 @@ Negative Passed: 90/90 (100.00%)
28512851
· ──
28522852
╰────
28532853
2854+
× Classes may not have a field named 'constructor'
2855+
╭─[misc/fail/oxc-14014.ts:2:12]
2856+
1 │ class Bar {
2857+
2accessor 'constructor'
2858+
· ─────────────
2859+
3}
2860+
╰────
2861+
2862+
× Classes may not have a field named 'constructor'
2863+
╭─[misc/fail/oxc-14014.ts:5:12]
2864+
4 │ abstract class Baz {
2865+
5accessor 'constructor'
2866+
· ─────────────
2867+
6}
2868+
╰────
2869+
28542870
× Unexpected token
28552871
╭─[misc/fail/oxc-169.js:2:1]
28562872
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>

0 commit comments

Comments
 (0)