refactor(parser): use PropName trait from oxc_ecmascript (#7543)

This commit is contained in:
Boshen 2024-11-29 09:18:46 +00:00
parent 9d6e14bb67
commit b24beeb0f5
3 changed files with 6 additions and 12 deletions

1
Cargo.lock generated
View file

@ -1831,6 +1831,7 @@ dependencies = [
"oxc_allocator", "oxc_allocator",
"oxc_ast", "oxc_ast",
"oxc_diagnostics", "oxc_diagnostics",
"oxc_ecmascript",
"oxc_regular_expression", "oxc_regular_expression",
"oxc_span", "oxc_span",
"oxc_syntax", "oxc_syntax",

View file

@ -22,6 +22,7 @@ doctest = false
oxc_allocator = { workspace = true } oxc_allocator = { workspace = true }
oxc_ast = { workspace = true } oxc_ast = { workspace = true }
oxc_diagnostics = { workspace = true } oxc_diagnostics = { workspace = true }
oxc_ecmascript = { workspace = true }
oxc_regular_expression = { workspace = true } oxc_regular_expression = { workspace = true }
oxc_span = { workspace = true } oxc_span = { workspace = true }
oxc_syntax = { workspace = true } oxc_syntax = { workspace = true }

View file

@ -1,6 +1,7 @@
use oxc_allocator::{Box, Vec}; use oxc_allocator::{Box, Vec};
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_diagnostics::Result; use oxc_diagnostics::Result;
use oxc_ecmascript::PropName;
use oxc_span::{GetSpan, Span}; use oxc_span::{GetSpan, Span};
use crate::{ use crate::{
@ -15,15 +16,6 @@ type Extends<'a> =
type Implements<'a> = Vec<'a, TSClassImplements<'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 /// Section 15.7 Class Definitions
impl<'a> ParserImpl<'a> { impl<'a> ParserImpl<'a> {
// `start_span` points at the start of all decoractors and `class` keyword. // `start_span` points at the start of all decoractors and `class` keyword.
@ -318,7 +310,7 @@ impl<'a> ParserImpl<'a> {
.map(Some) .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 {
if !computed { 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() { if r#static && name == "prototype" && !self.ctx.has_ambient() {
self.error(diagnostics::static_prototype(span)); self.error(diagnostics::static_prototype(span));
} }
@ -357,7 +349,7 @@ impl<'a> ParserImpl<'a> {
return Err(self.unexpected()); return Err(self.unexpected());
} }
if !computed { if !computed {
if let Some((name, span)) = prop_name(&key) { if let Some((name, span)) = key.prop_name() {
if name == "constructor" { if name == "constructor" {
self.error(diagnostics::field_constructor(span)); self.error(diagnostics::field_constructor(span));
} }
@ -410,7 +402,7 @@ impl<'a> ParserImpl<'a> {
) -> Result<ClassElement<'a>> { ) -> Result<ClassElement<'a>> {
let kind = if !r#static let kind = if !r#static
&& !computed && !computed
&& prop_name(&key).map_or(false, |(name, _)| name == "constructor") && key.prop_name().map_or(false, |(name, _)| name == "constructor")
{ {
MethodDefinitionKind::Constructor MethodDefinitionKind::Constructor
} else { } else {