Skip to content

Commit

Permalink
Fix a Parser Idempotency Issue (#3172)
Browse files Browse the repository at this point in the history
* Fix a Parser Idempotency Issue

This PR fixes #3133 by correctly implementing `ToIndentedString`
for `with` statement.

It also replaces the existing `ToInternedString` implementation
because it is implemented for all types implementing
`ToIndentedString` in `boa_interner/src/lib.rs`.

* Add test for with statement formatting

---------

Co-authored-by: raskad <[email protected]>
  • Loading branch information
veera-sivarajan and raskad authored Dec 4, 2023
1 parent e51e628 commit 1fe164b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 6 additions & 6 deletions boa_ast/src/statement/with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
try_break,
visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, ToInternedString};
use boa_interner::{Interner, ToIndentedString, ToInternedString};
use core::ops::ControlFlow;

/// The `with` statement extends the scope chain for a statement.
Expand Down Expand Up @@ -52,12 +52,12 @@ impl From<With> for Statement {
}
}

impl ToInternedString for With {
fn to_interned_string(&self, interner: &Interner) -> String {
impl ToIndentedString for With {
fn to_indented_string(&self, interner: &Interner, indentation: usize) -> String {
format!(
"with ({}) {{{}}}",
self.expression.to_interned_string(interner),
self.statement.to_interned_string(interner)
"with ({}) {}",
self.expression().to_interned_string(interner),
self.statement().to_indented_string(interner, indentation)
)
}
}
Expand Down
12 changes: 12 additions & 0 deletions boa_parser/src/parser/tests/format/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,15 @@ fn switch() {
"#,
);
}

#[test]
fn with() {
test_formatting(
r#"
with (this) {
{
}
}
"#,
);
}

0 comments on commit 1fe164b

Please sign in to comment.