Skip to content

Commit 4d561fb

Browse files
committed
fix: do not throw error if <script> exists inside {@html}
1 parent 73e8892 commit 4d561fb

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed

.changeset/sharp-guests-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
fix: prevent errors when `<script>` tags are used inside `{@html}`

src/context/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ type SelfClosingBlock = {
383383
startTagRange: [number, number];
384384
};
385385

386+
function isValidStartTagOpenIndex(
387+
code: string,
388+
startTagOpenIndex: number,
389+
): boolean {
390+
const prev = code.slice(0, startTagOpenIndex);
391+
return />\s*$|^\s*$/m.test(prev);
392+
}
393+
386394
/** Extract <script> blocks */
387395
function* extractBlocks(code: string): IterableIterator<Block> {
388396
const startTagOpenRe = /<!--[\s\S]*?-->|<(script|style|template)([\s>])/giu;
@@ -396,6 +404,10 @@ function* extractBlocks(code: string): IterableIterator<Block> {
396404
continue;
397405
}
398406
const startTagStart = startTagOpenMatch.index;
407+
if (!isValidStartTagOpenIndex(code, startTagStart)) {
408+
continue;
409+
}
410+
399411
let startTagEnd = startTagOpenRe.lastIndex;
400412

401413
const lowerTag = tag.toLowerCase() as "script" | "style" | "template";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{@html `<script>var x = ${50}</script>`}
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "SvelteMustacheTag",
6+
"kind": "raw",
7+
"expression": {
8+
"type": "TemplateLiteral",
9+
"expressions": [
10+
{
11+
"type": "Literal",
12+
"raw": "50",
13+
"value": 50,
14+
"range": [
15+
26,
16+
28
17+
],
18+
"loc": {
19+
"start": {
20+
"line": 1,
21+
"column": 26
22+
},
23+
"end": {
24+
"line": 1,
25+
"column": 28
26+
}
27+
}
28+
}
29+
],
30+
"quasis": [
31+
{
32+
"type": "TemplateElement",
33+
"tail": false,
34+
"value": {
35+
"cooked": "<script>var x = ",
36+
"raw": "<script>var x = "
37+
},
38+
"range": [
39+
7,
40+
26
41+
],
42+
"loc": {
43+
"start": {
44+
"line": 1,
45+
"column": 7
46+
},
47+
"end": {
48+
"line": 1,
49+
"column": 26
50+
}
51+
}
52+
},
53+
{
54+
"type": "TemplateElement",
55+
"tail": true,
56+
"value": {
57+
"cooked": "</script>",
58+
"raw": "</script>"
59+
},
60+
"range": [
61+
28,
62+
39
63+
],
64+
"loc": {
65+
"start": {
66+
"line": 1,
67+
"column": 28
68+
},
69+
"end": {
70+
"line": 1,
71+
"column": 39
72+
}
73+
}
74+
}
75+
],
76+
"range": [
77+
7,
78+
39
79+
],
80+
"loc": {
81+
"start": {
82+
"line": 1,
83+
"column": 7
84+
},
85+
"end": {
86+
"line": 1,
87+
"column": 39
88+
}
89+
}
90+
},
91+
"range": [
92+
0,
93+
40
94+
],
95+
"loc": {
96+
"start": {
97+
"line": 1,
98+
"column": 0
99+
},
100+
"end": {
101+
"line": 1,
102+
"column": 40
103+
}
104+
}
105+
}
106+
],
107+
"sourceType": "module",
108+
"comments": [],
109+
"tokens": [
110+
{
111+
"type": "Punctuator",
112+
"value": "{",
113+
"range": [
114+
0,
115+
1
116+
],
117+
"loc": {
118+
"start": {
119+
"line": 1,
120+
"column": 0
121+
},
122+
"end": {
123+
"line": 1,
124+
"column": 1
125+
}
126+
}
127+
},
128+
{
129+
"type": "MustacheKeyword",
130+
"value": "@html",
131+
"range": [
132+
1,
133+
6
134+
],
135+
"loc": {
136+
"start": {
137+
"line": 1,
138+
"column": 1
139+
},
140+
"end": {
141+
"line": 1,
142+
"column": 6
143+
}
144+
}
145+
},
146+
{
147+
"type": "Template",
148+
"value": "`<script>var x = ${",
149+
"range": [
150+
7,
151+
26
152+
],
153+
"loc": {
154+
"start": {
155+
"line": 1,
156+
"column": 7
157+
},
158+
"end": {
159+
"line": 1,
160+
"column": 26
161+
}
162+
}
163+
},
164+
{
165+
"type": "Numeric",
166+
"value": "50",
167+
"range": [
168+
26,
169+
28
170+
],
171+
"loc": {
172+
"start": {
173+
"line": 1,
174+
"column": 26
175+
},
176+
"end": {
177+
"line": 1,
178+
"column": 28
179+
}
180+
}
181+
},
182+
{
183+
"type": "Template",
184+
"value": "}</script>`",
185+
"range": [
186+
28,
187+
39
188+
],
189+
"loc": {
190+
"start": {
191+
"line": 1,
192+
"column": 28
193+
},
194+
"end": {
195+
"line": 1,
196+
"column": 39
197+
}
198+
}
199+
},
200+
{
201+
"type": "Punctuator",
202+
"value": "}",
203+
"range": [
204+
39,
205+
40
206+
],
207+
"loc": {
208+
"start": {
209+
"line": 1,
210+
"column": 39
211+
},
212+
"end": {
213+
"line": 1,
214+
"column": 40
215+
}
216+
}
217+
}
218+
],
219+
"range": [
220+
0,
221+
41
222+
],
223+
"loc": {
224+
"start": {
225+
"line": 1,
226+
"column": 0
227+
},
228+
"end": {
229+
"line": 2,
230+
"column": 0
231+
}
232+
}
233+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"type": "global",
3+
"variables": [
4+
{
5+
"name": "$$slots",
6+
"identifiers": [],
7+
"defs": [],
8+
"references": []
9+
},
10+
{
11+
"name": "$$props",
12+
"identifiers": [],
13+
"defs": [],
14+
"references": []
15+
},
16+
{
17+
"name": "$$restProps",
18+
"identifiers": [],
19+
"defs": [],
20+
"references": []
21+
}
22+
],
23+
"references": [],
24+
"childScopes": [
25+
{
26+
"type": "module",
27+
"variables": [],
28+
"references": [],
29+
"childScopes": [],
30+
"through": []
31+
}
32+
],
33+
"through": []
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy