Skip to content

Commit 60cef2e

Browse files
committed
Workaround for escape bugs
1 parent 0de5e89 commit 60cef2e

File tree

5 files changed

+9126
-7
lines changed

5 files changed

+9126
-7
lines changed

src/parser/astro-parser/parse.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ function fixLocations(node: ParentNode, ctx: Context): void {
126126
walk(
127127
node,
128128
ctx.code,
129-
(node) => {
129+
// eslint-disable-next-line complexity -- X(
130+
(node, parent) => {
130131
if (node.type === "frontmatter") {
131132
start = node.position!.start.offset = tokenIndex(
132133
ctx,
@@ -161,12 +162,28 @@ function fixLocations(node: ParentNode, ctx: Context): void {
161162
node.position!.start.offset = tokenIndex(ctx, "<!--", start)
162163
start = getCommentEndOffset(node, ctx)
163164
} else if (node.type === "text") {
164-
start = node.position!.start.offset = tokenIndex(
165-
ctx,
166-
node.value,
167-
start,
168-
)
169-
start += node.value.length
165+
if (
166+
parent.type === "element" &&
167+
(parent.name === "script" || parent.name === "style")
168+
) {
169+
node.position!.start.offset = start
170+
start = ctx.code.indexOf(`</${parent.name}`, start)
171+
if (start < 0) {
172+
start = ctx.code.length
173+
}
174+
// Workaround for escape bugs
175+
node.value = ctx.code.slice(
176+
node.position!.start.offset,
177+
start,
178+
)
179+
} else {
180+
start = node.position!.start.offset = tokenIndex(
181+
ctx,
182+
node.value,
183+
start,
184+
)
185+
start += node.value.length
186+
}
170187
} else if (node.type === "expression") {
171188
start = node.position!.start.offset = tokenIndex(
172189
ctx,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
const lf = '\n'
3+
---
4+
{lf}
5+
<script>
6+
const lf2 = '\n'
7+
</script>

0 commit comments

Comments
 (0)