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> {
#[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,
}

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(
&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<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(
&self,
span: Span,

View file

@ -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(

View file

@ -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,
}

View file

@ -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(

View file

@ -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
// <https://github.com/estree/estree/blob/master/es2015.md#objectpattern
pub(crate) fn parse_object_pattern_property(&mut self) -> Result<BindingProperty<'a>> {
/// `BindingProperty`[Yield, Await] :
/// `SingleNameBinding`[?Yield, ?Await]
/// `PropertyName`[?Yield, ?Await] : `BindingElement`[?Yield, ?Await]
pub(crate) fn parse_binding_property(&mut self) -> Result<BindingProperty<'a>> {
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] :

View file

@ -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(())

View file

@ -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<Box<'a, ObjectProperty<'a>>> {
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,