Skip to content

Commit

Permalink
Removed some duplicate code, added ToIndentedString
Browse files Browse the repository at this point in the history
  • Loading branch information
Razican committed Oct 22, 2022
1 parent 80017fd commit f66ade8
Show file tree
Hide file tree
Showing 24 changed files with 393 additions and 370 deletions.
29 changes: 12 additions & 17 deletions boa_engine/src/syntax/ast/declaration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use boa_interner::{Interner, ToInternedString};
use tap::Tap;

use super::{
expression::Identifier,
function::{AsyncFunction, AsyncGenerator, Class, Function, Generator},
ContainsSymbol,
};
use boa_interner::{Interner, ToIndentedString, ToInternedString};
use tap::Tap;

mod variable;

Expand Down Expand Up @@ -34,17 +33,6 @@ pub enum Declaration {
}

impl Declaration {
pub(super) fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
match self {
Declaration::Function(f) => f.to_indented_string(interner, indentation),
Declaration::Generator(g) => g.to_indented_string(interner, indentation),
Declaration::AsyncFunction(af) => af.to_indented_string(interner, indentation),
Declaration::AsyncGenerator(ag) => ag.to_indented_string(interner, indentation),
Declaration::Class(c) => c.to_indented_string(interner, indentation),
Declaration::Lexical(l) => l.to_interned_string(interner).tap_mut(|s| s.push(';')),
}
}

/// Return the lexically declared names of a `Declaration`.
///
/// The returned list may contain duplicates.
Expand Down Expand Up @@ -146,8 +134,15 @@ impl Declaration {
}
}

impl ToInternedString for Declaration {
fn to_interned_string(&self, interner: &Interner) -> String {
self.to_indented_string(interner, 0)
impl ToIndentedString for Declaration {
fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
match self {
Declaration::Function(f) => f.to_indented_string(interner, indentation),
Declaration::Generator(g) => g.to_indented_string(interner, indentation),
Declaration::AsyncFunction(af) => af.to_indented_string(interner, indentation),
Declaration::AsyncGenerator(ag) => ag.to_indented_string(interner, indentation),
Declaration::Class(c) => c.to_indented_string(interner, indentation),
Declaration::Lexical(l) => l.to_interned_string(interner).tap_mut(|s| s.push(';')),
}
}
}
2 changes: 1 addition & 1 deletion boa_engine/src/syntax/ast/expression/await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::syntax::ast::ContainsSymbol;

use super::Expression;
use boa_interner::{Interner, ToInternedString};
use boa_interner::{Interner, ToIndentedString, ToInternedString};

/// An await expression is used within an async function to pause execution and wait for a
/// promise to resolve.
Expand Down
39 changes: 17 additions & 22 deletions boa_engine/src/syntax/ast/expression/literal/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::syntax::ast::{
property::{MethodDefinition, PropertyDefinition},
ContainsSymbol,
};
use boa_interner::{Interner, ToInternedString};
use boa_interner::{Interner, ToIndentedString, ToInternedString};

/// Objects in JavaScript may be defined as an unordered collection of related data, of
/// primitive or reference types, in the form of “key: value” pairs.
Expand Down Expand Up @@ -39,13 +39,27 @@ pub struct ObjectLiteral {
}

impl ObjectLiteral {
/// Gets the object literal properties
#[inline]
pub fn properties(&self) -> &[PropertyDefinition] {
&self.properties
}

/// Implements the display formatting with indentation.
pub(crate) fn to_indented_string(&self, interner: &Interner, indent_n: usize) -> String {
#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.properties
.iter()
.any(PropertyDefinition::contains_arguments)
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.properties.iter().any(|prop| prop.contains(symbol))
}
}

impl ToIndentedString for ObjectLiteral {
fn to_indented_string(&self, interner: &Interner, indent_n: usize) -> String {
let mut buf = "{\n".to_owned();
let indentation = " ".repeat(indent_n + 1);
for property in self.properties().iter() {
Expand Down Expand Up @@ -119,25 +133,6 @@ impl ObjectLiteral {

buf
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.properties
.iter()
.any(PropertyDefinition::contains_arguments)
}

#[inline]
pub(crate) fn contains(&self, symbol: ContainsSymbol) -> bool {
self.properties.iter().any(|prop| prop.contains(symbol))
}
}

impl ToInternedString for ObjectLiteral {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
self.to_indented_string(interner, 0)
}
}

impl<T> From<T> for ObjectLiteral
Expand Down
14 changes: 4 additions & 10 deletions boa_engine/src/syntax/ast/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use boa_interner::{Interner, Sym, ToInternedString};
use boa_interner::{Interner, Sym, ToIndentedString, ToInternedString};

use self::{
access::{PrivatePropertyAccess, PropertyAccess, SuperPropertyAccess},
Expand Down Expand Up @@ -142,12 +142,6 @@ pub enum Expression {
}

impl Expression {
/// Creates a string of the value of the expression with the given indentation.
#[inline]
pub fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
self.to_no_indent_string(interner, indentation)
}

/// Implements the display formatting with indentation.
///
/// This will not prefix the value with any indentation. If you want to prefix this with proper
Expand Down Expand Up @@ -281,9 +275,9 @@ impl From<Expression> for Statement {
}
}

impl ToInternedString for Expression {
impl ToIndentedString for Expression {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
self.to_indented_string(interner, 0)
fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
self.to_no_indent_string(interner, indentation)
}
}
38 changes: 19 additions & 19 deletions boa_engine/src/syntax/ast/function/arrow_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::syntax::ast::{
expression::{Expression, Identifier},
join_nodes, ContainsSymbol, StatementList,
};
use boa_interner::{Interner, ToInternedString};
use boa_interner::{Interner, ToIndentedString};

use super::FormalParameterList;

Expand All @@ -29,6 +29,7 @@ pub struct ArrowFunction {

impl ArrowFunction {
/// Creates a new `ArrowFunctionDecl` AST Expression.
#[inline]
pub(in crate::syntax) fn new(
name: Option<Identifier>,
params: FormalParameterList,
Expand All @@ -42,40 +43,29 @@ impl ArrowFunction {
}

/// Gets the name of the function declaration.
#[inline]
pub fn name(&self) -> Option<Identifier> {
self.name
}

/// Sets the name of the function declaration.
#[inline]
pub fn set_name(&mut self, name: Option<Identifier>) {
self.name = name;
}

/// Gets the list of parameters of the arrow function.
#[inline]
pub(crate) fn parameters(&self) -> &FormalParameterList {
&self.parameters
}

/// Gets the body of the arrow function.
#[inline]
pub(crate) fn body(&self) -> &StatementList {
&self.body
}

/// Implements the display formatting with indentation.
pub(crate) fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
let mut buf = format!("({}", join_nodes(interner, &self.parameters.parameters));
if self.body().statements().is_empty() {
buf.push_str(") => {}");
} else {
buf.push_str(&format!(
") => {{\n{}{}}}",
self.body.to_indented_string(interner, indentation + 1),
" ".repeat(indentation)
));
}
buf
}

#[inline]
pub(crate) fn contains_arguments(&self) -> bool {
self.parameters.contains_arguments() || self.body.contains_arguments()
Expand All @@ -97,9 +87,19 @@ impl ArrowFunction {
}
}

impl ToInternedString for ArrowFunction {
fn to_interned_string(&self, interner: &Interner) -> String {
self.to_indented_string(interner, 0)
impl ToIndentedString for ArrowFunction {
fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
let mut buf = format!("({}", join_nodes(interner, &self.parameters.parameters));
if self.body().statements().is_empty() {
buf.push_str(") => {}");
} else {
buf.push_str(&format!(
") => {{\n{}{}}}",
self.body.to_indented_string(interner, indentation + 1),
" ".repeat(indentation)
));
}
buf
}
}

Expand Down
19 changes: 10 additions & 9 deletions boa_engine/src/syntax/ast/function/async_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::syntax::ast::{
expression::{Expression, Identifier},
join_nodes, Declaration, StatementList,
};
use boa_interner::{Interner, ToInternedString};
use boa_interner::{Interner, ToIndentedString};

use super::FormalParameterList;

Expand All @@ -27,6 +27,7 @@ pub struct AsyncFunction {

impl AsyncFunction {
/// Creates a new function expression
#[inline]
pub(in crate::syntax) fn new(
name: Option<Identifier>,
parameters: FormalParameterList,
Expand All @@ -40,22 +41,26 @@ impl AsyncFunction {
}

/// Gets the name of the function declaration.
#[inline]
pub fn name(&self) -> Option<Identifier> {
self.name
}

/// Gets the list of parameters of the function declaration.
#[inline]
pub fn parameters(&self) -> &FormalParameterList {
&self.parameters
}

/// Gets the body of the function declaration.
#[inline]
pub fn body(&self) -> &StatementList {
&self.body
}
}

/// Implements the display formatting with indentation.
pub(crate) fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
impl ToIndentedString for AsyncFunction {
fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
let mut buf = "async function".to_owned();
if let Some(name) = self.name {
buf.push_str(&format!(" {}", interner.resolve_expect(name.sym())));
Expand All @@ -77,19 +82,15 @@ impl AsyncFunction {
}
}

impl ToInternedString for AsyncFunction {
fn to_interned_string(&self, interner: &Interner) -> String {
self.to_indented_string(interner, 0)
}
}

impl From<AsyncFunction> for Expression {
#[inline]
fn from(expr: AsyncFunction) -> Self {
Self::AsyncFunction(expr)
}
}

impl From<AsyncFunction> for Declaration {
#[inline]
fn from(f: AsyncFunction) -> Self {
Self::AsyncFunction(f)
}
Expand Down
18 changes: 10 additions & 8 deletions boa_engine/src/syntax/ast/function/async_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::syntax::ast::{
expression::{Expression, Identifier},
join_nodes, Declaration, StatementList,
};
use boa_interner::{Interner, ToInternedString};
use boa_interner::{Interner, ToIndentedString};

use super::FormalParameterList;

Expand All @@ -24,6 +24,7 @@ pub struct AsyncGenerator {

impl AsyncGenerator {
/// Creates a new async generator expression
#[inline]
pub(in crate::syntax) fn new(
name: Option<Identifier>,
parameters: FormalParameterList,
Expand All @@ -37,21 +38,26 @@ impl AsyncGenerator {
}

/// Gets the name of the async generator expression
#[inline]
pub fn name(&self) -> Option<Identifier> {
self.name
}

/// Gets the list of parameters of the async generator expression
#[inline]
pub fn parameters(&self) -> &FormalParameterList {
&self.parameters
}

/// Gets the body of the async generator expression
#[inline]
pub fn body(&self) -> &StatementList {
&self.body
}
}

pub(crate) fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
impl ToIndentedString for AsyncGenerator {
fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
let mut buf = "async function*".to_owned();
if let Some(name) = self.name {
buf.push_str(&format!(" {}", interner.resolve_expect(name.sym())));
Expand All @@ -66,19 +72,15 @@ impl AsyncGenerator {
}
}

impl ToInternedString for AsyncGenerator {
fn to_interned_string(&self, interner: &Interner) -> String {
self.to_indented_string(interner, 0)
}
}

impl From<AsyncGenerator> for Expression {
#[inline]
fn from(expr: AsyncGenerator) -> Self {
Self::AsyncGenerator(expr)
}
}

impl From<AsyncGenerator> for Declaration {
#[inline]
fn from(f: AsyncGenerator) -> Self {
Self::AsyncGenerator(f)
}
Expand Down
Loading

0 comments on commit f66ade8

Please sign in to comment.