Skip to content

Commit 609c81f

Browse files
committed
Workaround for escape bugs
1 parent 78e4ca2 commit 609c81f

File tree

6 files changed

+8565
-6
lines changed

6 files changed

+8565
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
2121
"preversion": "npm run lint && npm test",
2222
"update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts",
23+
"debug-parser": "ts-node --transpile-only ./tools/parser-test.ts",
2324
"eslint-playground": "eslint tests/fixtures --ext .astro --config .eslintrc-for-playground.js --format codeframe",
2425
"benchmark": "ts-node --transpile-only benchmark/index.ts"
2526
},

src/parser/astro-parser/parse.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,39 @@ function fixLocations(node: ParentNode, ctx: Context): void {
177177
start,
178178
)
179179
} else {
180-
start = node.position!.start.offset = tokenIndex(
181-
ctx,
182-
node.value,
183-
start,
184-
)
185-
start += node.value.length
180+
const index = tokenIndexSafe(ctx.code, node.value, start)
181+
if (index != null) {
182+
start = node.position!.start.offset = index
183+
start += node.value.length
184+
} else {
185+
// Workaround for escape bugs
186+
node.position!.start.offset = start
187+
for (const token of node.value.split(/\s+/u)) {
188+
const index = tokenIndexSafe(ctx.code, token, start)
189+
if (index != null) {
190+
start = index + token.length
191+
continue
192+
}
193+
start = skipSpaces(ctx.code, start)
194+
let t = token
195+
if (ctx.code.startsWith("\\", start)) {
196+
const char = JSON.parse(
197+
`"\\${ctx.code[start + 1]}"`,
198+
)
199+
if (char.trim()) {
200+
t = t.slice(1)
201+
}
202+
start += 2
203+
}
204+
start = tokenIndex(ctx, t, start) + t.length
205+
}
206+
start = skipSpaces(ctx.code, start)
207+
208+
node.value = ctx.code.slice(
209+
node.position!.start.offset,
210+
start,
211+
)
212+
}
186213
}
187214
} else if (node.type === "expression") {
188215
start = node.position!.start.offset = tokenIndex(
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{String("\n")}
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "AstroRootFragment",
6+
"children": [
7+
{
8+
"type": "JSXExpressionContainer",
9+
"expression": {
10+
"type": "CallExpression",
11+
"arguments": [
12+
{
13+
"type": "Literal",
14+
"raw": "\"\\n\"",
15+
"value": "\n",
16+
"range": [
17+
8,
18+
12
19+
],
20+
"loc": {
21+
"start": {
22+
"line": 1,
23+
"column": 8
24+
},
25+
"end": {
26+
"line": 1,
27+
"column": 12
28+
}
29+
}
30+
}
31+
],
32+
"callee": {
33+
"type": "Identifier",
34+
"name": "String",
35+
"range": [
36+
1,
37+
7
38+
],
39+
"loc": {
40+
"start": {
41+
"line": 1,
42+
"column": 1
43+
},
44+
"end": {
45+
"line": 1,
46+
"column": 7
47+
}
48+
}
49+
},
50+
"optional": false,
51+
"range": [
52+
1,
53+
13
54+
],
55+
"loc": {
56+
"start": {
57+
"line": 1,
58+
"column": 1
59+
},
60+
"end": {
61+
"line": 1,
62+
"column": 13
63+
}
64+
}
65+
},
66+
"range": [
67+
0,
68+
14
69+
],
70+
"loc": {
71+
"start": {
72+
"line": 1,
73+
"column": 0
74+
},
75+
"end": {
76+
"line": 1,
77+
"column": 14
78+
}
79+
}
80+
},
81+
{
82+
"type": "JSXText",
83+
"raw": "\n",
84+
"value": "\n",
85+
"range": [
86+
14,
87+
15
88+
],
89+
"loc": {
90+
"start": {
91+
"line": 1,
92+
"column": 14
93+
},
94+
"end": {
95+
"line": 2,
96+
"column": 0
97+
}
98+
}
99+
}
100+
],
101+
"range": [
102+
0,
103+
15
104+
],
105+
"loc": {
106+
"start": {
107+
"line": 1,
108+
"column": 0
109+
},
110+
"end": {
111+
"line": 2,
112+
"column": 0
113+
}
114+
}
115+
}
116+
],
117+
"sourceType": "module",
118+
"comments": [],
119+
"tokens": [
120+
{
121+
"type": "Punctuator",
122+
"value": "{",
123+
"range": [
124+
0,
125+
1
126+
],
127+
"loc": {
128+
"start": {
129+
"line": 1,
130+
"column": 0
131+
},
132+
"end": {
133+
"line": 1,
134+
"column": 1
135+
}
136+
}
137+
},
138+
{
139+
"type": "Identifier",
140+
"value": "String",
141+
"range": [
142+
1,
143+
7
144+
],
145+
"loc": {
146+
"start": {
147+
"line": 1,
148+
"column": 1
149+
},
150+
"end": {
151+
"line": 1,
152+
"column": 7
153+
}
154+
}
155+
},
156+
{
157+
"type": "Punctuator",
158+
"value": "(",
159+
"range": [
160+
7,
161+
8
162+
],
163+
"loc": {
164+
"start": {
165+
"line": 1,
166+
"column": 7
167+
},
168+
"end": {
169+
"line": 1,
170+
"column": 8
171+
}
172+
}
173+
},
174+
{
175+
"type": "String",
176+
"value": "\"\\n\"",
177+
"range": [
178+
8,
179+
12
180+
],
181+
"loc": {
182+
"start": {
183+
"line": 1,
184+
"column": 8
185+
},
186+
"end": {
187+
"line": 1,
188+
"column": 12
189+
}
190+
}
191+
},
192+
{
193+
"type": "Punctuator",
194+
"value": ")",
195+
"range": [
196+
12,
197+
13
198+
],
199+
"loc": {
200+
"start": {
201+
"line": 1,
202+
"column": 12
203+
},
204+
"end": {
205+
"line": 1,
206+
"column": 13
207+
}
208+
}
209+
},
210+
{
211+
"type": "Punctuator",
212+
"value": "}",
213+
"range": [
214+
13,
215+
14
216+
],
217+
"loc": {
218+
"start": {
219+
"line": 1,
220+
"column": 13
221+
},
222+
"end": {
223+
"line": 1,
224+
"column": 14
225+
}
226+
}
227+
},
228+
{
229+
"type": "JSXText",
230+
"value": "\n",
231+
"range": [
232+
14,
233+
15
234+
],
235+
"loc": {
236+
"start": {
237+
"line": 1,
238+
"column": 14
239+
},
240+
"end": {
241+
"line": 2,
242+
"column": 0
243+
}
244+
}
245+
}
246+
],
247+
"range": [
248+
0,
249+
15
250+
],
251+
"loc": {
252+
"start": {
253+
"line": 1,
254+
"column": 0
255+
},
256+
"end": {
257+
"line": 2,
258+
"column": 0
259+
}
260+
}
261+
}

0 commit comments

Comments
 (0)