From 3ae0ac452989f828e6d1f712206ac06a21fd7d05 Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Mon, 19 Jul 2021 00:50:23 +0200 Subject: [PATCH] Implement fmt for binding pattern --- boa/src/syntax/ast/node/declaration/mod.rs | 180 ++++++++++++++++----- 1 file changed, 143 insertions(+), 37 deletions(-) diff --git a/boa/src/syntax/ast/node/declaration/mod.rs b/boa/src/syntax/ast/node/declaration/mod.rs index c7c31358128..06b6e79ae55 100644 --- a/boa/src/syntax/ast/node/declaration/mod.rs +++ b/boa/src/syntax/ast/node/declaration/mod.rs @@ -240,9 +240,8 @@ impl fmt::Display for Declaration { write!(f, " = {}", init)?; } } - // TODO: fmt for binding pattern - Self::Pattern(_) => { - fmt::Display::fmt("binding pattern fmt", f)?; + Self::Pattern(pattern) => { + fmt::Display::fmt(&pattern, f)?; } } Ok(()) @@ -296,6 +295,54 @@ impl Declaration { } } +#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] +#[derive(Clone, Debug, Trace, Finalize, PartialEq)] +pub enum DeclarationPattern { + Object(DeclarationPatternObject), + Array(DeclarationPatternArray), +} + +impl fmt::Display for DeclarationPattern { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match &self { + DeclarationPattern::Object(o) => { + write!(f, "{{ {} }}", o)?; + } + DeclarationPattern::Array(a) => { + write!(f, "[ {} ]", a)?; + } + } + Ok(()) + } +} + +impl DeclarationPattern { + pub(in crate::syntax) fn run( + &self, + init: Option, + context: &mut Context, + ) -> Result, Value)>> { + match &self { + DeclarationPattern::Object(pattern) => pattern.run(init, context), + DeclarationPattern::Array(pattern) => pattern.run(init, context), + } + } + + pub fn idents(&self) -> Vec<&str> { + match &self { + DeclarationPattern::Object(pattern) => pattern.idents(), + DeclarationPattern::Array(pattern) => pattern.idents(), + } + } + + pub fn init(&self) -> Option<&Node> { + match &self { + DeclarationPattern::Object(pattern) => pattern.init(), + DeclarationPattern::Array(pattern) => pattern.init(), + } + } +} + #[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] #[derive(Clone, Debug, Trace, Finalize, PartialEq)] pub struct DeclarationPatternObject { @@ -303,6 +350,18 @@ pub struct DeclarationPatternObject { init: Option, } +impl fmt::Display for DeclarationPatternObject { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for binding in &self.bindings { + write!(f, " {},", binding)?; + } + if let Some(ref init) = self.init { + write!(f, " = {}", init)?; + } + Ok(()) + } +} + impl DeclarationPatternObject { pub(in crate::syntax) fn new( bindings: Vec, @@ -476,6 +535,18 @@ pub struct DeclarationPatternArray { init: Option, } +impl fmt::Display for DeclarationPatternArray { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for binding in &self.bindings { + write!(f, " {},", binding)?; + } + if let Some(ref init) = self.init { + write!(f, " = {}", init)?; + } + Ok(()) + } +} + impl DeclarationPatternArray { pub(in crate::syntax) fn new( bindings: Vec, @@ -708,40 +779,6 @@ impl DeclarationPatternArray { } } -#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] -#[derive(Clone, Debug, Trace, Finalize, PartialEq)] -pub enum DeclarationPattern { - Object(DeclarationPatternObject), - Array(DeclarationPatternArray), -} - -impl DeclarationPattern { - pub(in crate::syntax) fn run( - &self, - init: Option, - context: &mut Context, - ) -> Result, Value)>> { - match &self { - DeclarationPattern::Object(pattern) => pattern.run(init, context), - DeclarationPattern::Array(pattern) => pattern.run(init, context), - } - } - - pub fn idents(&self) -> Vec<&str> { - match &self { - DeclarationPattern::Object(pattern) => pattern.idents(), - DeclarationPattern::Array(pattern) => pattern.idents(), - } - } - - pub fn init(&self) -> Option<&Node> { - match &self { - DeclarationPattern::Object(pattern) => pattern.init(), - DeclarationPattern::Array(pattern) => pattern.init(), - } - } -} - #[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] #[derive(Clone, Debug, Trace, Finalize, PartialEq)] pub enum BindingPatternTypeObject { @@ -762,6 +799,45 @@ pub enum BindingPatternTypeObject { }, } +impl fmt::Display for BindingPatternTypeObject { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match &self { + BindingPatternTypeObject::Empty => {} + BindingPatternTypeObject::SingleName { + ident, + property_name, + default_init, + } => { + if ident == property_name { + fmt::Display::fmt(ident, f)?; + } else { + write!(f, "{} : {}", property_name, ident)?; + } + if let Some(ref init) = default_init { + write!(f, " = {}", init)?; + } + } + BindingPatternTypeObject::RestProperty { + property_name, + excluded_keys: _, + } => { + write!(f, "... {}", property_name)?; + } + BindingPatternTypeObject::BindingPattern { + property_name, + pattern, + default_init, + } => { + write!(f, "{} : {}", property_name, pattern)?; + if let Some(ref init) = default_init { + write!(f, " = {}", init)?; + } + } + } + Ok(()) + } +} + #[cfg_attr(feature = "deser", derive(Serialize, Deserialize))] #[derive(Clone, Debug, Trace, Finalize, PartialEq)] pub enum BindingPatternTypeArray { @@ -781,3 +857,33 @@ pub enum BindingPatternTypeArray { pattern: DeclarationPattern, }, } + +impl fmt::Display for BindingPatternTypeArray { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match &self { + BindingPatternTypeArray::Empty => {} + BindingPatternTypeArray::Elision => { + fmt::Display::fmt(",", f)?; + } + BindingPatternTypeArray::SingleName { + ident, + default_init, + } => { + fmt::Display::fmt(ident, f)?; + if let Some(ref init) = default_init { + write!(f, " = {}", init)?; + } + } + BindingPatternTypeArray::BindingPattern { pattern } => { + fmt::Display::fmt(pattern, f)?; + } + BindingPatternTypeArray::SingleNameRest { ident } => { + fmt::Display::fmt(ident, f)?; + } + BindingPatternTypeArray::BindingPatternRest { pattern } => { + write!(f, "... {}", pattern)?; + } + } + Ok(()) + } +}