Skip to content

Commit d56223b

Browse files
authored
fix: improve script lang attribute detection (#10046)
closes #10038
1 parent 346041f commit d56223b

File tree

4 files changed

+150
-2
lines changed

4 files changed

+150
-2
lines changed

.changeset/hip-balloons-begin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: improve script `lang` attribute detection

packages/svelte/src/compiler/phases/1-parse/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import read_options from './read/options.js';
1111
const regex_position_indicator = / \(\d+:\d+\)$/;
1212

1313
const regex_lang_attribute =
14-
/<!--[^]*?-->|<script\s+(?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=(["'])?([^"' >]+)\1[^>]*>/;
14+
/<!--[^]*?-->|<script\s+(?:[^>]*|(?:[^=>'"/]+=(?:"[^"]*"|'[^']*'|[^>\s]+)\s+)*)lang=(["'])?([^"' >]+)\1[^>]*>/g;
1515

1616
export class Parser {
1717
/**
@@ -49,7 +49,14 @@ export class Parser {
4949

5050
this.template = template.trimRight();
5151

52-
this.ts = regex_lang_attribute.exec(template)?.[2] === 'ts';
52+
let match_lang;
53+
54+
do match_lang = regex_lang_attribute.exec(template);
55+
while (match_lang && match_lang[0][1] !== 's'); // ensure it starts with '<s' to match script tags
56+
57+
regex_lang_attribute.lastIndex = 0; // reset matched index to pass tests - otherwise declare the regex inside the constructor
58+
59+
this.ts = match_lang?.[2] === 'ts';
5360

5461
this.root = {
5562
css: null,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!--should not error out-->
2+
<script lang="ts">
3+
let count: number;
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"css": null,
3+
"js": [],
4+
"start": 0,
5+
"end": 27,
6+
"type": "Root",
7+
"fragment": {
8+
"type": "Fragment",
9+
"nodes": [
10+
{
11+
"type": "Comment",
12+
"start": 0,
13+
"end": 27,
14+
"data": "should not error out",
15+
"ignores": []
16+
},
17+
{
18+
"type": "Text",
19+
"start": 27,
20+
"end": 28,
21+
"raw": "\n",
22+
"data": "\n"
23+
}
24+
],
25+
"transparent": false
26+
},
27+
"options": null,
28+
"instance": {
29+
"type": "Script",
30+
"start": 28,
31+
"end": 76,
32+
"context": "default",
33+
"content": {
34+
"type": "Program",
35+
"start": 46,
36+
"end": 67,
37+
"loc": {
38+
"start": {
39+
"line": 1,
40+
"column": 0
41+
},
42+
"end": {
43+
"line": 4,
44+
"column": 0
45+
}
46+
},
47+
"body": [
48+
{
49+
"type": "VariableDeclaration",
50+
"start": 48,
51+
"end": 66,
52+
"loc": {
53+
"start": {
54+
"line": 3,
55+
"column": 1
56+
},
57+
"end": {
58+
"line": 3,
59+
"column": 19
60+
}
61+
},
62+
"declarations": [
63+
{
64+
"type": "VariableDeclarator",
65+
"start": 52,
66+
"end": 65,
67+
"loc": {
68+
"start": {
69+
"line": 3,
70+
"column": 5
71+
},
72+
"end": {
73+
"line": 3,
74+
"column": 18
75+
}
76+
},
77+
"id": {
78+
"type": "Identifier",
79+
"start": 52,
80+
"end": 18,
81+
"loc": {
82+
"start": {
83+
"line": 3,
84+
"column": 5
85+
},
86+
"end": {
87+
"line": 3,
88+
"column": 18
89+
}
90+
},
91+
"name": "count",
92+
"typeAnnotation": {
93+
"type": "TSTypeAnnotation",
94+
"start": 57,
95+
"end": 65,
96+
"loc": {
97+
"start": {
98+
"line": 3,
99+
"column": 10
100+
},
101+
"end": {
102+
"line": 3,
103+
"column": 18
104+
}
105+
},
106+
"typeAnnotation": {
107+
"type": "TSNumberKeyword",
108+
"start": 59,
109+
"end": 65,
110+
"loc": {
111+
"start": {
112+
"line": 3,
113+
"column": 12
114+
},
115+
"end": {
116+
"line": 3,
117+
"column": 18
118+
}
119+
}
120+
}
121+
}
122+
},
123+
"init": null
124+
}
125+
],
126+
"kind": "let"
127+
}
128+
],
129+
"sourceType": "module"
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)