From e52ee63cbb1f4f78bf287d1639d6334404e375e2 Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 16 May 2023 20:00:51 +0800 Subject: [PATCH] refactor(ast): clean up `BindingProperty` --- crates/oxc_ast/src/ast/js.rs | 2 -- crates/oxc_ast/src/ast_builder.rs | 39 ++++++++++++++++++----------- crates/oxc_ast_lower/src/lib.rs | 15 +---------- crates/oxc_hir/src/hir.rs | 2 -- crates/oxc_hir/src/hir_builder.rs | 4 +-- crates/oxc_parser/src/js/binding.rs | 17 ++++--------- crates/oxc_parser/src/js/list.rs | 2 +- crates/oxc_parser/src/js/object.rs | 12 ++++----- 8 files changed, 39 insertions(+), 54 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index c90c7587b..33a789fc6 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -1200,10 +1200,8 @@ pub struct ObjectPattern<'a> { pub struct BindingProperty<'a> { #[cfg_attr(feature = "serde", serde(flatten))] pub span: Span, - pub kind: PropertyKind, pub key: PropertyKey<'a>, pub value: BindingPattern<'a>, - pub method: bool, pub shorthand: bool, pub computed: bool, } diff --git a/crates/oxc_ast/src/ast_builder.rs b/crates/oxc_ast/src/ast_builder.rs index 1862755d8..2dcee96a8 100644 --- a/crates/oxc_ast/src/ast_builder.rs +++ b/crates/oxc_ast/src/ast_builder.rs @@ -475,6 +475,20 @@ impl<'a> AstBuilder<'a> { })) } + pub fn object_property( + &self, + span: Span, + kind: PropertyKind, + key: PropertyKey<'a>, + value: Expression<'a>, + init: Option>, + method: bool, + shorthand: bool, + computed: bool, + ) -> Box<'a, ObjectProperty<'a>> { + self.alloc(ObjectProperty { span, kind, key, value, init, method, shorthand, computed }) + } + pub fn parenthesized_expression( &self, span: Span, @@ -730,6 +744,17 @@ impl<'a> AstBuilder<'a> { BindingPatternKind::ObjectPattern(self.alloc(ObjectPattern { span, properties, rest })) } + pub fn binding_property( + &self, + span: Span, + key: PropertyKey<'a>, + value: BindingPattern<'a>, + shorthand: bool, + computed: bool, + ) -> BindingProperty<'a> { + BindingProperty { span, key, value, shorthand, computed } + } + pub fn spread_element( &self, span: Span, @@ -738,20 +763,6 @@ impl<'a> AstBuilder<'a> { self.alloc(SpreadElement { span, argument }) } - pub fn property( - &self, - span: Span, - kind: PropertyKind, - key: PropertyKey<'a>, - value: Expression<'a>, - init: Option>, - method: bool, - shorthand: bool, - computed: bool, - ) -> Box<'a, ObjectProperty<'a>> { - self.alloc(ObjectProperty { span, kind, key, value, init, method, shorthand, computed }) - } - pub fn array_pattern( &self, span: Span, diff --git a/crates/oxc_ast_lower/src/lib.rs b/crates/oxc_ast_lower/src/lib.rs index 1b1550c17..22dcdd6b0 100644 --- a/crates/oxc_ast_lower/src/lib.rs +++ b/crates/oxc_ast_lower/src/lib.rs @@ -919,22 +919,9 @@ impl<'a> AstLower<'a> { includes: SymbolFlags, excludes: SymbolFlags, ) -> hir::BindingProperty<'a> { - let kind = match prop.kind { - ast::PropertyKind::Init => hir::PropertyKind::Init, - ast::PropertyKind::Get => hir::PropertyKind::Get, - ast::PropertyKind::Set => hir::PropertyKind::Set, - }; let key = self.lower_property_key(&prop.key); let value = self.lower_binding_pattern(&prop.value, includes, excludes); - self.hir.binding_property( - prop.span, - kind, - key, - value, - prop.method, - prop.shorthand, - prop.computed, - ) + self.hir.binding_property(prop.span, key, value, prop.shorthand, prop.computed) } fn lower_array_pattern( diff --git a/crates/oxc_hir/src/hir.rs b/crates/oxc_hir/src/hir.rs index 0a68410e3..8715d09fc 100644 --- a/crates/oxc_hir/src/hir.rs +++ b/crates/oxc_hir/src/hir.rs @@ -1312,10 +1312,8 @@ pub struct ObjectPattern<'a> { pub struct BindingProperty<'a> { #[cfg_attr(feature = "serde", serde(flatten))] pub span: Span, - pub kind: PropertyKind, pub key: PropertyKey<'a>, pub value: BindingPattern<'a>, - pub method: bool, pub shorthand: bool, pub computed: bool, } diff --git a/crates/oxc_hir/src/hir_builder.rs b/crates/oxc_hir/src/hir_builder.rs index c582dd48e..5cbeec903 100644 --- a/crates/oxc_hir/src/hir_builder.rs +++ b/crates/oxc_hir/src/hir_builder.rs @@ -925,14 +925,12 @@ impl<'a> HirBuilder<'a> { pub fn binding_property( &mut self, span: Span, - kind: PropertyKind, key: PropertyKey<'a>, value: BindingPattern<'a>, - method: bool, shorthand: bool, computed: bool, ) -> BindingProperty<'a> { - BindingProperty { span, kind, key, value, method, shorthand, computed } + BindingProperty { span, key, value, shorthand, computed } } pub fn spread_element( diff --git a/crates/oxc_parser/src/js/binding.rs b/crates/oxc_parser/src/js/binding.rs index 7ddcb930a..957c89dbb 100644 --- a/crates/oxc_parser/src/js/binding.rs +++ b/crates/oxc_parser/src/js/binding.rs @@ -74,9 +74,10 @@ impl<'a> Parser<'a> { self.with_context(Context::In, |p| p.parse_initializer(span, pattern)) } - // object pattern property only has kind: init and method: false - // Result> { + /// `BindingProperty`[Yield, Await] : + /// `SingleNameBinding`[?Yield, ?Await] + /// `PropertyName`[?Yield, ?Await] : `BindingElement`[?Yield, ?Await] + pub(crate) fn parse_binding_property(&mut self) -> Result> { let span = self.start_span(); let mut shorthand = false; @@ -104,15 +105,7 @@ impl<'a> Parser<'a> { self.parse_binding_element()? }; - Ok(BindingProperty { - span: self.end_span(span), - key, - value, - kind: PropertyKind::Init, - method: false, - shorthand, - computed, - }) + Ok(self.ast.binding_property(self.end_span(span), key, value, shorthand, computed)) } /// Initializer[In, Yield, Await] : diff --git a/crates/oxc_parser/src/js/list.rs b/crates/oxc_parser/src/js/list.rs index 360aa2adc..1b588831e 100644 --- a/crates/oxc_parser/src/js/list.rs +++ b/crates/oxc_parser/src/js/list.rs @@ -85,7 +85,7 @@ impl<'a> SeparatedList<'a> for ObjectPatternProperties<'a> { p.error(diagnostics::RestElementLast(r.span)); } } else { - let prop = p.parse_object_pattern_property()?; + let prop = p.parse_binding_property()?; self.elements.push(prop); } Ok(()) diff --git a/crates/oxc_parser/src/js/object.rs b/crates/oxc_parser/src/js/object.rs index 606fdbbb3..db4e0df4d 100644 --- a/crates/oxc_parser/src/js/object.rs +++ b/crates/oxc_parser/src/js/object.rs @@ -68,7 +68,7 @@ impl<'a> Parser<'a> { if matches!(self.cur_kind(), Kind::LParen | Kind::LAngle | Kind::ShiftLeft) { let method = self.parse_method(false, false)?; - return Ok(self.ast.property( + return Ok(self.ast.object_property( self.end_span(span), PropertyKind::Init, key, @@ -119,7 +119,7 @@ impl<'a> Parser<'a> { } else { None }; - Ok(self.ast.property( + Ok(self.ast.object_property( self.end_span(span), PropertyKind::Init, PropertyKey::Identifier(key), @@ -141,7 +141,7 @@ impl<'a> Parser<'a> { ) -> Result>> { self.bump_any(); // bump `:` let value = self.parse_assignment_expression_base()?; - Ok(self.ast.property( + Ok(self.ast.object_property( self.end_span(span), PropertyKind::Init, key, @@ -198,7 +198,7 @@ impl<'a> Parser<'a> { let (key, computed) = self.parse_property_name()?; let method = self.parse_method(r#async, generator)?; let value = self.ast.function_expression(method); - Ok(self.ast.property( + Ok(self.ast.object_property( self.end_span(span), PropertyKind::Init, key, @@ -221,7 +221,7 @@ impl<'a> Parser<'a> { self.error(diagnostics::GetterParameters(method.params.span)); } let value = self.ast.function_expression(method); - Ok(self.ast.property( + Ok(self.ast.object_property( self.end_span(span), PropertyKind::Get, key, @@ -251,7 +251,7 @@ impl<'a> Parser<'a> { } } - Ok(self.ast.property( + Ok(self.ast.object_property( self.end_span(span), PropertyKind::Set, key,