Skip to content

Commit 531e75d

Browse files
committed
parse attachments
1 parent 1d773ef commit 531e75d

File tree

4 files changed

+168
-2
lines changed

4 files changed

+168
-2
lines changed

packages/svelte/src/compiler/phases/1-parse/state/element.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,14 +480,32 @@ function read_static_attribute(parser) {
480480

481481
/**
482482
* @param {Parser} parser
483-
* @returns {AST.Attribute | AST.SpreadAttribute | AST.Directive | null}
483+
* @returns {AST.Attribute | AST.SpreadAttribute | AST.Directive | AST.UseTag | null}
484484
*/
485485
function read_attribute(parser) {
486486
const start = parser.index;
487487

488488
if (parser.eat('{')) {
489489
parser.allow_whitespace();
490490

491+
if (parser.eat('@use')) {
492+
parser.require_whitespace();
493+
494+
const expression = read_expression(parser);
495+
parser.allow_whitespace();
496+
parser.eat('}', true);
497+
498+
/** @type {AST.UseTag} */
499+
const use = {
500+
type: 'UseTag',
501+
start,
502+
end: parser.index,
503+
expression
504+
};
505+
506+
return use;
507+
}
508+
491509
if (parser.eat('...')) {
492510
const expression = read_expression(parser);
493511

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ export namespace AST {
174174
};
175175
}
176176

177+
/** A `{@use foo(...)} tag */
178+
export interface UseTag extends BaseNode {
179+
type: 'UseTag';
180+
expression: Expression;
181+
}
182+
177183
/** An `animate:` directive */
178184
export interface AnimateDirective extends BaseNode {
179185
type: 'AnimateDirective';
@@ -273,7 +279,7 @@ export namespace AST {
273279

274280
interface BaseElement extends BaseNode {
275281
name: string;
276-
attributes: Array<Attribute | SpreadAttribute | Directive>;
282+
attributes: Array<Attribute | SpreadAttribute | Directive | UseTag>;
277283
fragment: Fragment;
278284
}
279285

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div {@use (node) => {}} {@use (node) => {}}></div>
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"css": null,
3+
"js": [],
4+
"start": 0,
5+
"end": 51,
6+
"type": "Root",
7+
"fragment": {
8+
"type": "Fragment",
9+
"nodes": [
10+
{
11+
"type": "RegularElement",
12+
"start": 0,
13+
"end": 51,
14+
"name": "div",
15+
"attributes": [
16+
{
17+
"type": "UseTag",
18+
"start": 5,
19+
"end": 24,
20+
"expression": {
21+
"type": "ArrowFunctionExpression",
22+
"start": 11,
23+
"end": 23,
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 11
28+
},
29+
"end": {
30+
"line": 1,
31+
"column": 23
32+
}
33+
},
34+
"id": null,
35+
"expression": false,
36+
"generator": false,
37+
"async": false,
38+
"params": [
39+
{
40+
"type": "Identifier",
41+
"start": 12,
42+
"end": 16,
43+
"loc": {
44+
"start": {
45+
"line": 1,
46+
"column": 12
47+
},
48+
"end": {
49+
"line": 1,
50+
"column": 16
51+
}
52+
},
53+
"name": "node"
54+
}
55+
],
56+
"body": {
57+
"type": "BlockStatement",
58+
"start": 21,
59+
"end": 23,
60+
"loc": {
61+
"start": {
62+
"line": 1,
63+
"column": 21
64+
},
65+
"end": {
66+
"line": 1,
67+
"column": 23
68+
}
69+
},
70+
"body": []
71+
}
72+
}
73+
},
74+
{
75+
"type": "UseTag",
76+
"start": 25,
77+
"end": 44,
78+
"expression": {
79+
"type": "ArrowFunctionExpression",
80+
"start": 31,
81+
"end": 43,
82+
"loc": {
83+
"start": {
84+
"line": 1,
85+
"column": 31
86+
},
87+
"end": {
88+
"line": 1,
89+
"column": 43
90+
}
91+
},
92+
"id": null,
93+
"expression": false,
94+
"generator": false,
95+
"async": false,
96+
"params": [
97+
{
98+
"type": "Identifier",
99+
"start": 32,
100+
"end": 36,
101+
"loc": {
102+
"start": {
103+
"line": 1,
104+
"column": 32
105+
},
106+
"end": {
107+
"line": 1,
108+
"column": 36
109+
}
110+
},
111+
"name": "node"
112+
}
113+
],
114+
"body": {
115+
"type": "BlockStatement",
116+
"start": 41,
117+
"end": 43,
118+
"loc": {
119+
"start": {
120+
"line": 1,
121+
"column": 41
122+
},
123+
"end": {
124+
"line": 1,
125+
"column": 43
126+
}
127+
},
128+
"body": []
129+
}
130+
}
131+
}
132+
],
133+
"fragment": {
134+
"type": "Fragment",
135+
"nodes": []
136+
}
137+
}
138+
]
139+
},
140+
"options": null
141+
}

0 commit comments

Comments
 (0)