mirror of https://github.com/boa-dev/boa.git
Browse Source
Currently we have no explicit representation for parenthesized expressions which makes some behaviours impossible to detect. A bonus is that we can now turn AST that contains parenthesized expressions back to code. This Pull Request changes the following: - Add an AST node for parenthesized expressions. - Adjust some conversions and checks to "ignore"/"expand" parenthesized expressions. - Fix some tests that had parenthesized expressions.pull/2769/head
raskad
2 years ago
14 changed files with 221 additions and 96 deletions
@ -0,0 +1,66 @@
|
||||
use super::Expression; |
||||
use crate::visitor::{VisitWith, Visitor, VisitorMut}; |
||||
use boa_interner::{Interner, ToInternedString}; |
||||
use core::ops::ControlFlow; |
||||
|
||||
/// A parenthesized expression.
|
||||
///
|
||||
/// More information:
|
||||
/// - [ECMAScript reference][spec]
|
||||
/// - [MDN documentation][mdn]
|
||||
///
|
||||
/// [spec]: https://tc39.es/ecma262/#sec-grouping-operator
|
||||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Grouping
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
||||
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] |
||||
#[derive(Clone, Debug, PartialEq)] |
||||
pub struct Parenthesized { |
||||
expression: Box<Expression>, |
||||
} |
||||
|
||||
impl Parenthesized { |
||||
/// Creates a parenthesized expression.
|
||||
#[inline] |
||||
#[must_use] |
||||
pub fn new(expression: Expression) -> Self { |
||||
Self { |
||||
expression: Box::new(expression), |
||||
} |
||||
} |
||||
|
||||
/// Gets the expression of this parenthesized expression.
|
||||
#[inline] |
||||
#[must_use] |
||||
pub const fn expression(&self) -> &Expression { |
||||
&self.expression |
||||
} |
||||
} |
||||
|
||||
impl From<Parenthesized> for Expression { |
||||
fn from(p: Parenthesized) -> Self { |
||||
Self::Parenthesized(p) |
||||
} |
||||
} |
||||
|
||||
impl ToInternedString for Parenthesized { |
||||
#[inline] |
||||
fn to_interned_string(&self, interner: &Interner) -> String { |
||||
format!("({})", self.expression.to_interned_string(interner)) |
||||
} |
||||
} |
||||
|
||||
impl VisitWith for Parenthesized { |
||||
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy> |
||||
where |
||||
V: Visitor<'a>, |
||||
{ |
||||
visitor.visit_expression(&self.expression) |
||||
} |
||||
|
||||
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy> |
||||
where |
||||
V: VisitorMut<'a>, |
||||
{ |
||||
visitor.visit_expression_mut(&mut self.expression) |
||||
} |
||||
} |
Loading…
Reference in new issue