Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix display for nodes #1312

Merged
merged 46 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
25562ec
Fixed top level indents
macmv Jun 8, 2021
cfda168
Fixed function definitions
macmv Jun 8, 2021
8cc481e
For loops display correctly now
macmv Jun 9, 2021
b09891b
Await expressions display correctly
macmv Jun 9, 2021
054c9c0
Await calls no longer have a custom display() function
macmv Jun 9, 2021
a542c8e
Fixed async function declaration
macmv Jun 9, 2021
fd4804e
Added spacing to do-while loop
macmv Jun 9, 2021
9477001
Fixed for of loop
macmv Jun 9, 2021
ddfcfec
Fixed object declaration formatter
macmv Jun 9, 2021
f05a7b4
Fixed switch statements formatting
macmv Jun 9, 2021
c460f29
Added operator fmt test
macmv Jun 9, 2021
bad3574
Added array display test
macmv Jun 9, 2021
f13f1da
Added tests for await expressions. Unclear if let a = await blah() ha…
macmv Jun 9, 2021
1cc35e5
Added block display test
macmv Jun 9, 2021
b27052f
Added break formatting test
macmv Jun 9, 2021
53e1927
Added a potential test for when block labels are added
macmv Jun 9, 2021
b6ffd89
Added call formatting tests
macmv Jun 9, 2021
047442b
Added a testing utility function for formatting tests
macmv Jun 9, 2021
e677dd0
Using custom testing function instead of a bunch of asserts in format…
macmv Jun 9, 2021
feaf768
Improved formatting of failed parsing formatting tests
macmv Jun 9, 2021
325b262
Added conditional formatting tests
macmv Jun 9, 2021
f288f63
Wrote function tests, and found out that functions are still horribly…
macmv Jun 9, 2021
f56dee8
Fixed arrow function declaration
macmv Jun 9, 2021
c7d66f4
Fixed the formatting for the rest of the functions. Async function ex…
macmv Jun 9, 2021
847eac2
Re-ordered functions to match the output of StatementList
macmv Jun 9, 2021
8ae1744
Fixed async function expressions
macmv Jun 9, 2021
1173d8d
Added field formatting tests
macmv Jun 11, 2021
c568bf6
Added formatting test for 'for' loops
macmv Jun 11, 2021
6c96c08
For in loops now display their label if they have one
macmv Jun 11, 2021
1b9d690
Added test for the rest of the loops
macmv Jun 11, 2021
a941baf
Added fmt for labels for all the types of loops
macmv Jun 11, 2021
0430c39
Added test for new keyword formatting
macmv Jun 11, 2021
b8d870b
Added object formatting
macmv Jun 11, 2021
93fd514
Partially fixed object display function
macmv Jun 11, 2021
8a8a1d0
Split Node::display into two functions, allowing objects to be displa…
macmv Jun 14, 2021
bf81ee4
Added a first implementation of a MethodDefinition formatter
macmv Jun 14, 2021
cd1adab
Added tests for the rest of object function fields (every branch of M…
macmv Jun 14, 2021
923875e
Operator test uses propper formatting test
macmv Jun 14, 2021
f024c68
Added return statment formatter test
macmv Jun 14, 2021
94008d8
Added spread argument formatting test
macmv Jun 14, 2021
f6882c8
Added switch statement formatting test
macmv Jun 14, 2021
094c5b9
Added a formatting test for templates
macmv Jun 14, 2021
1cd9d9a
Added a throw statement formatter test
macmv Jun 14, 2021
5d9ec89
Added try catch test
macmv Jun 14, 2021
32c140f
Removed unused import
macmv Jun 18, 2021
d56b22c
Formatting test now uses eprintln! instead of println!
macmv Jun 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions boa/src/syntax/ast/node/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::fmt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

/// An array is an ordered collection of data (either primitive or object depending upon the
/// language).
///
Expand Down
9 changes: 9 additions & 0 deletions boa/src/syntax/ast/node/array/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn fmt() {
super::super::test_formatting(
r#"
let a = [1, 2, 3, "words", "more words"];
let b = [];
"#,
);
}
14 changes: 5 additions & 9 deletions boa/src/syntax/ast/node/await_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::fmt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

/// An await expression is used within an async function to pause execution and wait for a
/// promise to resolve.
///
Expand All @@ -31,14 +34,6 @@ impl Executable for AwaitExpr {
}
}

impl AwaitExpr {
/// Implements the display formatting with indentation.
pub(super) fn display(&self, f: &mut fmt::Formatter<'_>, indentation: usize) -> fmt::Result {
writeln!(f, "await ")?;
self.expr.display(f, indentation)
}
}

impl<T> From<T> for AwaitExpr
where
T: Into<Box<Node>>,
Expand All @@ -50,7 +45,8 @@ where

impl fmt::Display for AwaitExpr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.display(f, 0)
write!(f, "await ")?;
self.expr.display(f, 0)
}
}

Expand Down
9 changes: 9 additions & 0 deletions boa/src/syntax/ast/node/await_expr/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn fmt() {
// TODO: `let a = await fn()` is invalid syntax as of writing. It should be tested here once implemented.
super::super::test_formatting(
r#"
await function_call();
"#,
);
}
3 changes: 3 additions & 0 deletions boa/src/syntax/ast/node/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use std::fmt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

/// A `block` statement (or compound statement in other languages) is used to group zero or
/// more statements.
///
Expand Down
22 changes: 22 additions & 0 deletions boa/src/syntax/ast/node/block/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[test]
fn fmt() {
super::super::test_formatting(
r#"
{
let a = function_call();
console.log("hello");
}
another_statement();
"#,
);
// TODO: Once block labels are implemtned, this should be tested:
// super::super::test_formatting(
// r#"
// block_name: {
// let a = function_call();
// console.log("hello");
// }
// another_statement();
// "#,
// );
}
31 changes: 31 additions & 0 deletions boa/src/syntax/ast/node/break_node/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,34 @@ fn check_post_state() {
&InterpreterState::Break(Some("label".into()))
);
}

#[test]
fn fmt() {
// Blocks do not store their label, so we cannot test with
// the outer block having a label.
//
// TODO: Once block labels are implemented, this test should
// include them:
//
// ```
// outer: {
// while (true) {
// break outer;
// }
// skipped_call();
// }
// ```
super::super::test_formatting(
r#"
{
while (true) {
break outer;
}
skipped_call();
}
while (true) {
break;
}
"#,
);
}
3 changes: 3 additions & 0 deletions boa/src/syntax/ast/node/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use std::fmt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

/// Calling the function actually performs the specified actions with the indicated parameters.
///
/// Defining a function does not execute it. Defining it simply names the function and
Expand Down
10 changes: 10 additions & 0 deletions boa/src/syntax/ast/node/call/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[test]
fn fmt() {
super::super::test_formatting(
r#"
call_1(1, 2, 3);
call_2("argument here");
call_3();
"#,
);
}
3 changes: 3 additions & 0 deletions boa/src/syntax/ast/node/conditional/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ pub mod conditional_op;
pub mod if_node;

pub use self::{conditional_op::ConditionalOp, if_node::If};

#[cfg(test)]
mod tests;
13 changes: 13 additions & 0 deletions boa/src/syntax/ast/node/conditional/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[test]
fn fmt() {
super::super::test_formatting(
r#"
let a = true ? 5 : 6;
if (false) {
a = 10;
} else {
a = 20;
}
"#,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ impl ArrowFunctionDecl {
) -> fmt::Result {
write!(f, "(")?;
join_nodes(f, &self.params)?;
f.write_str(") => ")?;
self.body.display(f, indentation)
if self.body().is_empty() {
f.write_str(") => {}")
} else {
f.write_str(") => {\n")?;
self.body.display(f, indentation + 1)?;
write!(f, "{}}}", " ".repeat(indentation))
}
}
}

Expand Down
20 changes: 9 additions & 11 deletions boa/src/syntax/ast/node/declaration/async_function_decl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,17 @@ impl AsyncFunctionDecl {
indentation: usize,
) -> fmt::Result {
match &self.name {
Some(name) => {
write!(f, "async function {}(", name)?;
}
None => {
write!(f, "async function (")?;
}
Some(name) => write!(f, "async function {}(", name)?,
None => write!(f, "async function (")?,
}
join_nodes(f, &self.parameters)?;
f.write_str(") {{")?;

self.body.display(f, indentation + 1)?;

writeln!(f, "}}")
if self.body().is_empty() {
f.write_str(") {}")
} else {
f.write_str(") {\n")?;
self.body.display(f, indentation + 1)?;
write!(f, "{}}}", " ".repeat(indentation))
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions boa/src/syntax/ast/node/declaration/async_function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,19 @@ impl AsyncFunctionExpr {
f: &mut fmt::Formatter<'_>,
indentation: usize,
) -> fmt::Result {
f.write_str("function")?;
f.write_str("async function")?;
if let Some(ref name) = self.name {
write!(f, " {}", name)?;
}
f.write_str("(")?;
join_nodes(f, &self.parameters)?;
f.write_str(") {{")?;

self.body.display(f, indentation + 1)?;

writeln!(f, "}}")
if self.body().is_empty() {
f.write_str(") {}")
} else {
f.write_str(") {\n")?;
self.body.display(f, indentation + 1)?;
write!(f, "{}}}", " ".repeat(indentation))
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions boa/src/syntax/ast/node/declaration/function_decl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ impl FunctionDecl {
) -> fmt::Result {
write!(f, "function {}(", self.name)?;
join_nodes(f, &self.parameters)?;
f.write_str(") {{")?;

self.body.display(f, indentation + 1)?;

writeln!(f, "}}")
if self.body().is_empty() {
f.write_str(") {}")
} else {
f.write_str(") {\n")?;
self.body.display(f, indentation + 1)?;
write!(f, "{}}}", " ".repeat(indentation))
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions boa/src/syntax/ast/node/declaration/function_expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ impl FunctionExpr {
}
f.write_str("(")?;
join_nodes(f, &self.parameters)?;
f.write_str(") {{")?;

self.body.display(f, indentation + 1)?;

writeln!(f, "}}")
if self.body().is_empty() {
f.write_str(") {}")
} else {
f.write_str(") {\n")?;
self.body.display(f, indentation + 1)?;
write!(f, "{}}}", " ".repeat(indentation))
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions boa/src/syntax/ast/node/declaration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,32 @@ fn duplicate_function_name() {

assert_eq!(&exec(scenario), "12");
}

#[test]
fn fmt() {
super::super::test_formatting(
r#"
function func(a, b) {
console.log(a);
};
function func_2(a, b) {};
let arrow_func = (a, b) => {
console.log("in multi statement arrow");
console.log(b);
};
async function async_func(a, b) {
console.log(a);
};
pass_async_func(async function(a, b) {
console.log("in async callback", a);
});
pass_func(function(a, b) {
console.log("in callback", a);
});
let arrow_func_2 = (a, b) => {};
async function async_func_2(a, b) {};
pass_async_func(async function(a, b) {});
pass_func(function(a, b) {});
"#,
);
}
3 changes: 3 additions & 0 deletions boa/src/syntax/ast/node/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ pub mod get_const_field;
pub mod get_field;

pub use self::{get_const_field::GetConstField, get_field::GetField};

#[cfg(test)]
mod tests;
10 changes: 10 additions & 0 deletions boa/src/syntax/ast/node/field/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[test]
fn fmt() {
super::super::test_formatting(
r#"
a.field_name;
a[5];
a["other_field_name"];
"#,
);
}
14 changes: 5 additions & 9 deletions boa/src/syntax/ast/node/iteration/continue_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ impl Executable for Continue {

impl fmt::Display for Continue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"continue{}",
if let Some(label) = self.label() {
format!(" {}", label)
} else {
String::new()
}
)
write!(f, "continue")?;
if let Some(label) = self.label() {
write!(f, " {}", label)?;
}
Ok(())
}
}

Expand Down
7 changes: 5 additions & 2 deletions boa/src/syntax/ast/node/iteration/do_while_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@ impl DoWhileLoop {
f: &mut fmt::Formatter<'_>,
indentation: usize,
) -> fmt::Result {
write!(f, "do")?;
if let Some(ref label) = self.label {
write!(f, "{}: ", label)?;
}
write!(f, "do ")?;
self.body().display(f, indentation)?;
write!(f, "while ({})", self.cond())
write!(f, " while ({})", self.cond())
}
}

Expand Down
8 changes: 5 additions & 3 deletions boa/src/syntax/ast/node/iteration/for_in_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ impl ForInLoop {
}

pub fn display(&self, f: &mut fmt::Formatter<'_>, indentation: usize) -> fmt::Result {
write!(f, "for ({} in {}) {{", self.variable, self.expr,)?;
self.body().display(f, indentation + 1)?;
f.write_str("}")
if let Some(ref label) = self.label {
write!(f, "{}: ", label)?;
}
write!(f, "for ({} in {}) ", self.variable, self.expr)?;
self.body().display(f, indentation)
}
}

Expand Down