Skip to content

Commit b91a67b

Browse files
authored
feat: include script and svelte:options attributes in ast (#11241)
* include attributes of script tags in ast * include attributes of svelte:options tag in ast * add changeset * improve types * improve test
1 parent 7785d58 commit b91a67b

File tree

11 files changed

+288
-5
lines changed

11 files changed

+288
-5
lines changed

.changeset/serious-crabs-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
feat: include `script` and `svelte:options` attributes in ast

packages/svelte/src/compiler/legacy.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,15 @@ export function convert(source, ast) {
8686
if (instance) {
8787
// @ts-ignore
8888
delete instance.parent;
89+
// @ts-ignore
90+
delete instance.attributes;
8991
}
9092

9193
if (module) {
9294
// @ts-ignore
9395
delete module.parent;
96+
// @ts-ignore
97+
delete module.attributes;
9498
}
9599

96100
return {

packages/svelte/src/compiler/phases/1-parse/read/options.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export default function read_options(node) {
1111
/** @type {import('#compiler').SvelteOptions} */
1212
const component_options = {
1313
start: node.start,
14-
end: node.end
14+
end: node.end,
15+
// @ts-ignore
16+
attributes: node.attributes
1517
};
1618

1719
if (!node) {

packages/svelte/src/compiler/phases/1-parse/read/script.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export function read_script(parser, start, attributes) {
6363
end: parser.index,
6464
context: get_context(attributes),
6565
content: ast,
66-
parent: null
66+
parent: null,
67+
// @ts-ignore
68+
attributes: attributes
6769
};
6870
}

packages/svelte/src/compiler/types/template.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export interface SvelteOptions {
9393
*/
9494
extend?: ArrowFunctionExpression | Identifier;
9595
};
96+
attributes: Attribute[];
9697
}
9798

9899
/** Static text */
@@ -467,6 +468,7 @@ export interface Script extends BaseNode {
467468
type: 'Script';
468469
context: string;
469470
content: Program;
471+
attributes: Attribute[];
470472
}
471473

472474
declare module 'estree' {

packages/svelte/tests/parser-modern/samples/comment-before-script/output.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@
133133
"value": "should not error out"
134134
}
135135
]
136-
}
136+
},
137+
"attributes": [
138+
{
139+
"type": "Attribute",
140+
"start": 36,
141+
"end": 45,
142+
"name": "lang",
143+
"value": [
144+
{
145+
"start": 42,
146+
"end": 44,
147+
"type": "Text",
148+
"raw": "ts",
149+
"data": "ts"
150+
}
151+
]
152+
}
153+
]
137154
}
138155
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<svelte:options customElement="my-custom-element" runes={true} />
2+
3+
<script context="module" lang="ts">
4+
</script>
5+
6+
<script lang="ts" generics="T extends { foo: number }">
7+
</script>
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
{
2+
"css": null,
3+
"js": [],
4+
"start": 0,
5+
"end": 112,
6+
"type": "Root",
7+
"fragment": {
8+
"type": "Fragment",
9+
"nodes": [
10+
{
11+
"type": "Text",
12+
"start": 65,
13+
"end": 67,
14+
"raw": "\n\n",
15+
"data": "\n\n"
16+
},
17+
{
18+
"type": "Text",
19+
"start": 112,
20+
"end": 114,
21+
"raw": "\n\n",
22+
"data": "\n\n"
23+
}
24+
],
25+
"transparent": false
26+
},
27+
"options": {
28+
"start": 0,
29+
"end": 65,
30+
"attributes": [
31+
{
32+
"type": "Attribute",
33+
"start": 16,
34+
"end": 49,
35+
"name": "customElement",
36+
"value": [
37+
{
38+
"start": 31,
39+
"end": 48,
40+
"type": "Text",
41+
"raw": "my-custom-element",
42+
"data": "my-custom-element",
43+
"parent": null
44+
}
45+
],
46+
"parent": null,
47+
"metadata": {
48+
"dynamic": false,
49+
"delegated": null
50+
}
51+
},
52+
{
53+
"type": "Attribute",
54+
"start": 50,
55+
"end": 62,
56+
"name": "runes",
57+
"value": [
58+
{
59+
"type": "ExpressionTag",
60+
"start": 56,
61+
"end": 62,
62+
"expression": {
63+
"type": "Literal",
64+
"start": 57,
65+
"end": 61,
66+
"loc": {
67+
"start": {
68+
"line": 1,
69+
"column": 57
70+
},
71+
"end": {
72+
"line": 1,
73+
"column": 61
74+
}
75+
},
76+
"value": true,
77+
"raw": "true"
78+
},
79+
"parent": null,
80+
"metadata": {
81+
"contains_call_expression": false,
82+
"dynamic": false
83+
}
84+
}
85+
],
86+
"parent": null,
87+
"metadata": {
88+
"dynamic": false,
89+
"delegated": null
90+
}
91+
}
92+
],
93+
"customElement": {
94+
"tag": "my-custom-element"
95+
},
96+
"runes": true
97+
},
98+
"module": {
99+
"type": "Script",
100+
"start": 67,
101+
"end": 112,
102+
"context": "module",
103+
"content": {
104+
"type": "Program",
105+
"start": 102,
106+
"end": 103,
107+
"loc": {
108+
"start": {
109+
"line": 1,
110+
"column": 0
111+
},
112+
"end": {
113+
"line": 4,
114+
"column": 0
115+
}
116+
},
117+
"body": [],
118+
"sourceType": "module"
119+
},
120+
"attributes": [
121+
{
122+
"type": "Attribute",
123+
"start": 75,
124+
"end": 91,
125+
"name": "context",
126+
"value": [
127+
{
128+
"start": 84,
129+
"end": 90,
130+
"type": "Text",
131+
"raw": "module",
132+
"data": "module"
133+
}
134+
]
135+
},
136+
{
137+
"type": "Attribute",
138+
"start": 92,
139+
"end": 101,
140+
"name": "lang",
141+
"value": [
142+
{
143+
"start": 98,
144+
"end": 100,
145+
"type": "Text",
146+
"raw": "ts",
147+
"data": "ts"
148+
}
149+
]
150+
}
151+
]
152+
},
153+
"instance": {
154+
"type": "Script",
155+
"start": 114,
156+
"end": 179,
157+
"context": "default",
158+
"content": {
159+
"type": "Program",
160+
"start": 169,
161+
"end": 170,
162+
"loc": {
163+
"start": {
164+
"line": 1,
165+
"column": 0
166+
},
167+
"end": {
168+
"line": 7,
169+
"column": 0
170+
}
171+
},
172+
"body": [],
173+
"sourceType": "module"
174+
},
175+
"attributes": [
176+
{
177+
"type": "Attribute",
178+
"start": 122,
179+
"end": 131,
180+
"name": "lang",
181+
"value": [
182+
{
183+
"start": 128,
184+
"end": 130,
185+
"type": "Text",
186+
"raw": "ts",
187+
"data": "ts"
188+
}
189+
]
190+
},
191+
{
192+
"type": "Attribute",
193+
"start": 132,
194+
"end": 168,
195+
"name": "generics",
196+
"value": [
197+
{
198+
"start": 142,
199+
"end": 167,
200+
"type": "Text",
201+
"raw": "T extends { foo: number }",
202+
"data": "T extends { foo: number }"
203+
}
204+
]
205+
}
206+
]
207+
}
208+
}

packages/svelte/tests/parser-modern/samples/snippets/output.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@
206206
},
207207
"body": [],
208208
"sourceType": "module"
209-
}
209+
},
210+
"attributes": [
211+
{
212+
"type": "Attribute",
213+
"start": 8,
214+
"end": 17,
215+
"name": "lang",
216+
"value": [
217+
{
218+
"start": 14,
219+
"end": 16,
220+
"type": "Text",
221+
"raw": "ts",
222+
"data": "ts"
223+
}
224+
]
225+
}
226+
]
210227
}
211228
}

packages/svelte/tests/parser-modern/samples/typescript-in-event-handler/output.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,23 @@
477477
}
478478
],
479479
"sourceType": "module"
480-
}
480+
},
481+
"attributes": [
482+
{
483+
"type": "Attribute",
484+
"start": 8,
485+
"end": 17,
486+
"name": "lang",
487+
"value": [
488+
{
489+
"start": 14,
490+
"end": 16,
491+
"type": "Text",
492+
"raw": "ts",
493+
"data": "ts"
494+
}
495+
]
496+
}
497+
]
481498
}
482499
}

packages/svelte/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ declare module 'svelte/compiler' {
13161316
*/
13171317
extend?: ArrowFunctionExpression | Identifier;
13181318
};
1319+
attributes: Attribute[];
13191320
}
13201321

13211322
/** Static text */
@@ -1690,6 +1691,7 @@ declare module 'svelte/compiler' {
16901691
type: 'Script';
16911692
context: string;
16921693
content: Program;
1694+
attributes: Attribute[];
16931695
}
16941696
/**
16951697
* The result of a preprocessor run. If the preprocessor does not return a result, it is assumed that the code is unchanged.

0 commit comments

Comments
 (0)