Skip to content

Commit 80ce4ad

Browse files
committed
Workaround for text bugs
1 parent a43aa4f commit 80ce4ad

File tree

4 files changed

+9237
-11
lines changed

4 files changed

+9237
-11
lines changed

src/parser/astro-parser/parse.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function parseByService(code: string): ParseResult {
4343
const ast = JSON.parse(jsonAst)
4444
return { ast }
4545
} catch {
46+
// FIXME: Workaround for escape bugs
4647
// Adjust because may get the wrong escape as JSON.
4748
const ast = JSON.parse(
4849
jsonAst.replace(/\\./gu, (m) => {
@@ -171,7 +172,7 @@ function fixLocations(node: ParentNode, ctx: Context): void {
171172
if (start < 0) {
172173
start = ctx.code.length
173174
}
174-
// Workaround for escape bugs
175+
// FIXME: Workaround for escape bugs
175176
node.value = ctx.code.slice(
176177
node.position!.start.offset,
177178
start,
@@ -182,26 +183,31 @@ function fixLocations(node: ParentNode, ctx: Context): void {
182183
start = node.position!.start.offset = index
183184
start += node.value.length
184185
} else {
185-
// Workaround for escape bugs
186+
// FIXME: Workaround for escape bugs
186187
node.position!.start.offset = start
187-
for (const token of node.value.split(/\s+/u)) {
188-
const index = tokenIndexSafe(ctx.code, token, start)
188+
const value = node.value.replace(/\s+/gu, "")
189+
for (
190+
let charIndex = 0;
191+
charIndex < value.length;
192+
charIndex++
193+
) {
194+
const char = value[charIndex]
195+
const index = tokenIndexSafe(ctx.code, char, start)
189196
if (index != null) {
190-
start = index + token.length
197+
start = index + 1
191198
continue
192199
}
193200
start = skipSpaces(ctx.code, start)
194-
let t = token
195201
if (ctx.code.startsWith("\\", start)) {
196-
const char = JSON.parse(
202+
const codeChar = JSON.parse(
197203
`"\\${ctx.code[start + 1]}"`,
198204
)
199-
if (char.trim()) {
200-
t = t.slice(1)
201-
}
202205
start += 2
206+
if (codeChar === char) {
207+
continue
208+
}
203209
}
204-
start = tokenIndex(ctx, t, start) + t.length
210+
start = tokenIndex(ctx, char, start) + 1
205211
}
206212
start = skipSpaces(ctx.code, start)
207213

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
const foo = true
3+
---
4+
{(foo ? { text: 'foo' } : {}).foo}

0 commit comments

Comments
 (0)