Skip to content

Commit 4855c4a

Browse files
fix(html): fix comment detection in Astro frontmatter (#7989)
Co-authored-by: Emanuele Stoppa <[email protected]>
1 parent e8f930b commit 4855c4a

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed a regression in Astro frontmatter parsing where comments inside quoted strings were incorrectly detected as actual comments. This caused the parser to prematurely terminate frontmatter parsing when encountering strings like `const test = "//";`.
6+
For example, the following Astro frontmatter now parses correctly:
7+
8+
```astro
9+
---
10+
const test = "// not a real comment";
11+
---
12+
```

crates/biome_html_parser/src/lexer/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,13 @@ impl QuotesSeen {
10561056
return; // Don't track quotes inside comments
10571057
}
10581058

1059-
// Check for comment entry
1060-
if self.prev_byte == Some(b'/') && (byte == b'/' || byte == b'*') {
1059+
// Check for comment entry - but only if we're not inside quotes
1060+
if self.prev_byte == Some(b'/')
1061+
&& (byte == b'/' || byte == b'*')
1062+
&& self.single == 0
1063+
&& self.double == 0
1064+
&& self.template == 0
1065+
{
10611066
self.inside_comment = true;
10621067
self.prev_byte = Some(byte);
10631068
return;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
const test = "//";
3+
---
4+
5+
<div>{test}</div>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
source: crates/biome_html_parser/tests/spec_test.rs
3+
expression: snapshot
4+
---
5+
## Input
6+
7+
```astro
8+
---
9+
const test = "//";
10+
---
11+
12+
<div>{test}</div>
13+
14+
```
15+
16+
17+
## AST
18+
19+
```
20+
HtmlRoot {
21+
bom_token: missing (optional),
22+
frontmatter: AstroFrontmatterElement {
23+
l_fence_token: FENCE@0..3 "---" [] [],
24+
content: AstroEmbeddedContent {
25+
content_token: HTML_LITERAL@3..23 "const test = \"//\";\n" [Newline("\n")] [],
26+
},
27+
r_fence_token: FENCE@23..26 "---" [] [],
28+
},
29+
directive: missing (optional),
30+
html: HtmlElementList [
31+
HtmlElement {
32+
opening_element: HtmlOpeningElement {
33+
l_angle_token: L_ANGLE@26..29 "<" [Newline("\n"), Newline("\n")] [],
34+
name: HtmlTagName {
35+
value_token: HTML_LITERAL@29..32 "div" [] [],
36+
},
37+
attributes: HtmlAttributeList [],
38+
r_angle_token: R_ANGLE@32..33 ">" [] [],
39+
},
40+
children: HtmlElementList [
41+
HtmlSingleTextExpression {
42+
l_curly_token: L_CURLY@33..34 "{" [] [],
43+
expression: HtmlTextExpression {
44+
html_literal_token: HTML_LITERAL@34..38 "test" [] [],
45+
},
46+
r_curly_token: R_CURLY@38..39 "}" [] [],
47+
},
48+
],
49+
closing_element: HtmlClosingElement {
50+
l_angle_token: L_ANGLE@39..40 "<" [] [],
51+
slash_token: SLASH@40..41 "/" [] [],
52+
name: HtmlTagName {
53+
value_token: HTML_LITERAL@41..44 "div" [] [],
54+
},
55+
r_angle_token: R_ANGLE@44..45 ">" [] [],
56+
},
57+
},
58+
],
59+
eof_token: EOF@45..46 "" [Newline("\n")] [],
60+
}
61+
```
62+
63+
## CST
64+
65+
```
66+
67+
0: (empty)
68+
69+
0: [email protected] "---" [] []
70+
71+
0: [email protected] "const test = \"//\";\n" [Newline("\n")] []
72+
2: [email protected] "---" [] []
73+
2: (empty)
74+
75+
76+
77+
0: [email protected] "<" [Newline("\n"), Newline("\n")] []
78+
79+
0: [email protected] "div" [] []
80+
81+
3: [email protected] ">" [] []
82+
83+
84+
0: [email protected] "{" [] []
85+
1: HTML_TEXT_EXPRESSION@34..38
86+
0: HTML_LITERAL@34..38 "test" [] []
87+
2: R_CURLY@38..39 "}" [] []
88+
2: HTML_CLOSING_ELEMENT@39..45
89+
0: L_ANGLE@39..40 "<" [] []
90+
1: SLASH@40..41 "/" [] []
91+
2: HTML_TAG_NAME@41..44
92+
0: HTML_LITERAL@41..44 "div" [] []
93+
3: R_ANGLE@44..45 ">" [] []
94+
4: EOF@45..46 "" [Newline("\n")] []
95+
96+
```

0 commit comments

Comments
 (0)