refactor(ast): clean up BindingProperty

This commit is contained in:
Boshen 2023-05-16 20:00:51 +08:00
parent bd56bb7abe
commit e52ee63cbb
No known key found for this signature in database
GPG key ID: 9C7A8C8AB22BEBD1
8 changed files with 39 additions and 54 deletions

View file

@ -1200,10 +1200,8 @@ pub struct ObjectPattern<'a> {
pub struct BindingProperty<'a> { pub struct BindingProperty<'a> {
#[cfg_attr(feature = "serde", serde(flatten))] #[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span, pub span: Span,
pub kind: PropertyKind,
pub key: PropertyKey<'a>, pub key: PropertyKey<'a>,
pub value: BindingPattern<'a>, pub value: BindingPattern<'a>,
pub method: bool,
pub shorthand: bool, pub shorthand: bool,
pub computed: bool, pub computed: bool,
} }

View file

@ -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<Expression<'a>>,
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( pub fn parenthesized_expression(
&self, &self,
span: Span, span: Span,
@ -730,6 +744,17 @@ impl<'a> AstBuilder<'a> {
BindingPatternKind::ObjectPattern(self.alloc(ObjectPattern { span, properties, rest })) 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( pub fn spread_element(
&self, &self,
span: Span, span: Span,
@ -738,20 +763,6 @@ impl<'a> AstBuilder<'a> {
self.alloc(SpreadElement { span, argument }) self.alloc(SpreadElement { span, argument })
} }
pub fn property(
&self,
span: Span,
kind: PropertyKind,
key: PropertyKey<'a>,
value: Expression<'a>,
init: Option<Expression<'a>>,
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( pub fn array_pattern(
&self, &self,
span: Span, span: Span,

View file

@ -919,22 +919,9 @@ impl<'a> AstLower<'a> {
includes: SymbolFlags, includes: SymbolFlags,
excludes: SymbolFlags, excludes: SymbolFlags,
) -> hir::BindingProperty<'a> { ) -> 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 key = self.lower_property_key(&prop.key);
let value = self.lower_binding_pattern(&prop.value, includes, excludes); let value = self.lower_binding_pattern(&prop.value, includes, excludes);
self.hir.binding_property( self.hir.binding_property(prop.span, key, value, prop.shorthand, prop.computed)
prop.span,
kind,
key,
value,
prop.method,
prop.shorthand,
prop.computed,
)
} }
fn lower_array_pattern( fn lower_array_pattern(

View file

@ -1312,10 +1312,8 @@ pub struct ObjectPattern<'a> {
pub struct BindingProperty<'a> { pub struct BindingProperty<'a> {
#[cfg_attr(feature = "serde", serde(flatten))] #[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span, pub span: Span,
pub kind: PropertyKind,
pub key: PropertyKey<'a>, pub key: PropertyKey<'a>,
pub value: BindingPattern<'a>, pub value: BindingPattern<'a>,
pub method: bool,
pub shorthand: bool, pub shorthand: bool,
pub computed: bool, pub computed: bool,
} }

View file

@ -925,14 +925,12 @@ impl<'a> HirBuilder<'a> {
pub fn binding_property( pub fn binding_property(
&mut self, &mut self,
span: Span, span: Span,
kind: PropertyKind,
key: PropertyKey<'a>, key: PropertyKey<'a>,
value: BindingPattern<'a>, value: BindingPattern<'a>,
method: bool,
shorthand: bool, shorthand: bool,
computed: bool, computed: bool,
) -> BindingProperty<'a> { ) -> BindingProperty<'a> {
BindingProperty { span, kind, key, value, method, shorthand, computed } BindingProperty { span, key, value, shorthand, computed }
} }
pub fn spread_element( pub fn spread_element(

View file

@ -74,9 +74,10 @@ impl<'a> Parser<'a> {
self.with_context(Context::In, |p| p.parse_initializer(span, pattern)) self.with_context(Context::In, |p| p.parse_initializer(span, pattern))
} }
// object pattern property only has kind: init and method: false /// `BindingProperty`[Yield, Await] :
// <https://github.com/estree/estree/blob/master/es2015.md#objectpattern /// `SingleNameBinding`[?Yield, ?Await]
pub(crate) fn parse_object_pattern_property(&mut self) -> Result<BindingProperty<'a>> { /// `PropertyName`[?Yield, ?Await] : `BindingElement`[?Yield, ?Await]
pub(crate) fn parse_binding_property(&mut self) -> Result<BindingProperty<'a>> {
let span = self.start_span(); let span = self.start_span();
let mut shorthand = false; let mut shorthand = false;
@ -104,15 +105,7 @@ impl<'a> Parser<'a> {
self.parse_binding_element()? self.parse_binding_element()?
}; };
Ok(BindingProperty { Ok(self.ast.binding_property(self.end_span(span), key, value, shorthand, computed))
span: self.end_span(span),
key,
value,
kind: PropertyKind::Init,
method: false,
shorthand,
computed,
})
} }
/// Initializer[In, Yield, Await] : /// Initializer[In, Yield, Await] :

View file

@ -85,7 +85,7 @@ impl<'a> SeparatedList<'a> for ObjectPatternProperties<'a> {
p.error(diagnostics::RestElementLast(r.span)); p.error(diagnostics::RestElementLast(r.span));
} }
} else { } else {
let prop = p.parse_object_pattern_property()?; let prop = p.parse_binding_property()?;
self.elements.push(prop); self.elements.push(prop);
} }
Ok(()) Ok(())

View file

@ -68,7 +68,7 @@ impl<'a> Parser<'a> {
if matches!(self.cur_kind(), Kind::LParen | Kind::LAngle | Kind::ShiftLeft) { if matches!(self.cur_kind(), Kind::LParen | Kind::LAngle | Kind::ShiftLeft) {
let method = self.parse_method(false, false)?; let method = self.parse_method(false, false)?;
return Ok(self.ast.property( return Ok(self.ast.object_property(
self.end_span(span), self.end_span(span),
PropertyKind::Init, PropertyKind::Init,
key, key,
@ -119,7 +119,7 @@ impl<'a> Parser<'a> {
} else { } else {
None None
}; };
Ok(self.ast.property( Ok(self.ast.object_property(
self.end_span(span), self.end_span(span),
PropertyKind::Init, PropertyKind::Init,
PropertyKey::Identifier(key), PropertyKey::Identifier(key),
@ -141,7 +141,7 @@ impl<'a> Parser<'a> {
) -> Result<Box<'a, ObjectProperty<'a>>> { ) -> Result<Box<'a, ObjectProperty<'a>>> {
self.bump_any(); // bump `:` self.bump_any(); // bump `:`
let value = self.parse_assignment_expression_base()?; let value = self.parse_assignment_expression_base()?;
Ok(self.ast.property( Ok(self.ast.object_property(
self.end_span(span), self.end_span(span),
PropertyKind::Init, PropertyKind::Init,
key, key,
@ -198,7 +198,7 @@ impl<'a> Parser<'a> {
let (key, computed) = self.parse_property_name()?; let (key, computed) = self.parse_property_name()?;
let method = self.parse_method(r#async, generator)?; let method = self.parse_method(r#async, generator)?;
let value = self.ast.function_expression(method); let value = self.ast.function_expression(method);
Ok(self.ast.property( Ok(self.ast.object_property(
self.end_span(span), self.end_span(span),
PropertyKind::Init, PropertyKind::Init,
key, key,
@ -221,7 +221,7 @@ impl<'a> Parser<'a> {
self.error(diagnostics::GetterParameters(method.params.span)); self.error(diagnostics::GetterParameters(method.params.span));
} }
let value = self.ast.function_expression(method); let value = self.ast.function_expression(method);
Ok(self.ast.property( Ok(self.ast.object_property(
self.end_span(span), self.end_span(span),
PropertyKind::Get, PropertyKind::Get,
key, key,
@ -251,7 +251,7 @@ impl<'a> Parser<'a> {
} }
} }
Ok(self.ast.property( Ok(self.ast.object_property(
self.end_span(span), self.end_span(span),
PropertyKind::Set, PropertyKind::Set,
key, key,