Skip to content

Commit

Permalink
fix: fix comments parsed as tags in Astro
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane committed Sep 18, 2024
1 parent 014e861 commit 09dfed7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
8 changes: 8 additions & 0 deletions dprint_plugin/tests/integration/basic.astro
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ item).join()}></div>
let title =
'My Site';
---

{[].map((value) => {
console.log(value);

return <div></div><h1></h1>
})}

{/* <svg><rect x="182.56" y="230.58" width="100.48" height="104.25" rx="50.24" ry="50.24"></rect></svg> */}
12 changes: 12 additions & 0 deletions dprint_plugin/tests/integration/biome/basic.astro.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,15 @@ document.getElementById("button").addEventListener("click", handleClick);

<!-- Below should be treated as text node. -->
--- let title = 'My Site'; ---

{
[].map((value) => {
console.log(value);

return <div></div><h1></h1>;
})
}

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

<!-- Below should be treated as text node. -->
--- let title = 'My Site'; ---

{
[].map((value) => {
console.log(value);

return <div></div><h1></h1>;
})
}

{
/* <svg><rect x="182.56" y="230.58" width="100.48" height="104.25" rx="50.24" ry="50.24"></rect></svg> */
}
50 changes: 45 additions & 5 deletions markup_fmt/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl<'s> Parser<'s> {
};

let mut children = Vec::with_capacity(1);
let mut braces_stack = 0u8;
let mut pair_stack = vec![];
let mut pos = self
.chars
.peek()
Expand All @@ -552,13 +552,13 @@ impl<'s> Parser<'s> {
while let Some((i, c)) = self.chars.peek() {
match c {
'{' => {
braces_stack += 1;
pair_stack.push('{');
self.chars.next();
}
'}' => {
let i = *i;
self.chars.next();
if braces_stack == 0 {
if pair_stack.is_empty() {
debug_assert!(matches!(
children.last(),
Some(AstroExprChild::Template(..)) | None
Expand All @@ -568,9 +568,9 @@ impl<'s> Parser<'s> {
}));
break;
}
braces_stack -= 1;
pair_stack.pop();
}
'<' => {
'<' if !matches!(pair_stack.last(), Some('/' | '*' | '\'' | '"' | '`')) => {
let i = *i;
let mut chars = self.chars.clone();
chars.next();
Expand Down Expand Up @@ -615,6 +615,46 @@ impl<'s> Parser<'s> {
self.chars.next();
}
}
'\'' | '"' | '`' => {
let last = pair_stack.last();
if last.is_some_and(|last| last == c) {
pair_stack.pop();
} else if matches!(last, Some('$' | '{') | None) {
pair_stack.push(*c);
}
self.chars.next();
}
'$' if matches!(pair_stack.last(), Some('`')) => {
self.chars.next();
if self.chars.next_if(|(_, c)| *c == '{').is_some() {
pair_stack.push('$');
}
}
'/' if !matches!(pair_stack.last(), Some('\'' | '"' | '`')) => {
self.chars.next();
if let Some((_, c)) = self.chars.next_if(|(_, c)| *c == '/' || *c == '*') {
pair_stack.push(c);
}
}
'\n' => {
self.chars.next();
if let Some('/') = pair_stack.last() {
pair_stack.pop();
}
}
'*' => {
self.chars.next();
if self
.chars
.next_if(|(_, c)| *c == '/' && matches!(pair_stack.last(), Some('*')))
.is_some()
{
pair_stack.pop();
}
}
'\\' if matches!(pair_stack.last(), Some('\'' | '"' | '`')) => {
self.chars.next();
}
_ => {
self.chars.next();
}
Expand Down

0 comments on commit 09dfed7

Please sign in to comment.