Skip to content

Commit

Permalink
refactor(parser): use PropName trait from oxc_ecmascript (#7543)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 29, 2024
1 parent 9d6e14b commit b24beeb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ doctest = false
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_ecmascript = { workspace = true }
oxc_regular_expression = { workspace = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
Expand Down
16 changes: 4 additions & 12 deletions crates/oxc_parser/src/js/class.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use oxc_allocator::{Box, Vec};
use oxc_ast::ast::*;
use oxc_diagnostics::Result;
use oxc_ecmascript::PropName;
use oxc_span::{GetSpan, Span};

use crate::{
Expand All @@ -15,15 +16,6 @@ type Extends<'a> =

type Implements<'a> = Vec<'a, TSClassImplements<'a>>;

fn prop_name<'a>(key: &'a PropertyKey<'a>) -> Option<(&'a str, Span)> {
match key {
PropertyKey::StaticIdentifier(ident) => Some((&ident.name, ident.span)),
PropertyKey::Identifier(ident) => Some((&ident.name, ident.span)),
PropertyKey::StringLiteral(lit) => Some((&lit.value, lit.span)),
_ => None,
}
}

/// Section 15.7 Class Definitions
impl<'a> ParserImpl<'a> {
// `start_span` points at the start of all decoractors and `class` keyword.
Expand Down Expand Up @@ -318,7 +310,7 @@ impl<'a> ParserImpl<'a> {
.map(Some)
} else if self.at(Kind::LParen) || self.at(Kind::LAngle) || r#async || generator {
if !computed {
if let Some((name, span)) = prop_name(&key) {
if let Some((name, span)) = key.prop_name() {
if r#static && name == "prototype" && !self.ctx.has_ambient() {
self.error(diagnostics::static_prototype(span));
}
Expand Down Expand Up @@ -357,7 +349,7 @@ impl<'a> ParserImpl<'a> {
return Err(self.unexpected());
}
if !computed {
if let Some((name, span)) = prop_name(&key) {
if let Some((name, span)) = key.prop_name() {
if name == "constructor" {
self.error(diagnostics::field_constructor(span));
}
Expand Down Expand Up @@ -410,7 +402,7 @@ impl<'a> ParserImpl<'a> {
) -> Result<ClassElement<'a>> {
let kind = if !r#static
&& !computed
&& prop_name(&key).map_or(false, |(name, _)| name == "constructor")
&& key.prop_name().map_or(false, |(name, _)| name == "constructor")
{
MethodDefinitionKind::Constructor
} else {
Expand Down

0 comments on commit b24beeb

Please sign in to comment.