Skip to content

Commit

Permalink
feat: detect comments in Astro expr smartly (close #51)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Sep 19, 2024
1 parent e943217 commit 0848421
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
4 changes: 1 addition & 3 deletions dprint_plugin/tests/integration/biome/basic.astro.snap
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,4 @@ document.getElementById("button").addEventListener("click", handleClick);
/* <svg><rect x="182.56" y="230.58" width="100.48" height="104.25" rx="50.24" ry="50.24"></rect></svg> */
}

{
/* https://example.com */
}
{/* https://example.com */}
4 changes: 1 addition & 3 deletions dprint_plugin/tests/integration/dprint_ts/basic.astro.snap
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,4 @@ document.getElementById("button").addEventListener("click", handleClick);
/* <svg><rect x="182.56" y="230.58" width="100.48" height="104.25" rx="50.24" ry="50.24"></rect></svg> */
}

{
/* https://example.com */
}
{/* https://example.com */}
1 change: 1 addition & 0 deletions markup_fmt/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct AstroAttribute<'s> {

pub struct AstroExpr<'s> {
pub children: Vec<AstroExprChild<'s>>,
pub has_line_comment: bool,
pub start: usize,
}

Expand Down
15 changes: 13 additions & 2 deletions markup_fmt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ impl<'s> Parser<'s> {
};

let mut children = Vec::with_capacity(1);
let mut has_line_comment = false;
let mut pair_stack = vec![];
let mut pos = self
.chars
Expand Down Expand Up @@ -632,8 +633,17 @@ impl<'s> Parser<'s> {
}
'/' if !matches!(pair_stack.last(), Some('\'' | '"' | '`' | '/' | '*')) => {
self.chars.next();
if let Some((_, c)) = self.chars.next_if(|(_, c)| *c == '/' || *c == '*') {
pair_stack.push(c);
match self.chars.peek() {
Some((_, '/')) => {
pair_stack.push('/');
has_line_comment = true;
self.chars.next();
}
Some((_, '*')) => {
pair_stack.push('*');
self.chars.next();
}
_ => {}
}
}
'\n' => {
Expand Down Expand Up @@ -663,6 +673,7 @@ impl<'s> Parser<'s> {

Ok(AstroExpr {
children,
has_line_comment,
start: start + 1,
})
}
Expand Down
2 changes: 1 addition & 1 deletion markup_fmt/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl<'s> DocGen<'s> for AstroExpr<'s> {
.nest_with_ctx(ctx)
.append(Doc::line_or_nil())
.append(Doc::text("}"));
if script.contains("//") {
if self.has_line_comment {
doc
} else {
doc.group()
Expand Down

0 comments on commit 0848421

Please sign in to comment.