Skip to content

Commit cc0055c

Browse files
committed
update parser for v2
1 parent eebd47d commit cc0055c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1373
-759
lines changed

src/parse/read/expression.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const literals = new Map([['true', true], ['false', false], ['null', null]]);
66
export default function readExpression(parser: Parser) {
77
const start = parser.index;
88

9-
const name = parser.readUntil(/\s*}}/);
9+
const name = parser.readUntil(parser.v2 ? /\s*}/ : /\s*}}/);
1010
if (name && /^[a-z]+$/.test(name)) {
1111
const end = start + name.length;
1212

src/parse/state/mustache.ts

+39-21
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default function mustache(parser: Parser) {
6161

6262
parser.eat(expected, true);
6363
parser.allowWhitespace();
64-
parser.eat('}}', true);
64+
parser.eat(parser.v2 ? '}' : '}}', true);
6565

6666
while (block.elseif) {
6767
block.end = parser.index;
@@ -83,7 +83,7 @@ export default function mustache(parser: Parser) {
8383

8484
block.end = parser.index;
8585
parser.stack.pop();
86-
} else if (parser.eat('elseif')) {
86+
} else if (parser.eat(parser.v2 ? ':elseif' : 'elseif')) {
8787
const block = parser.current();
8888
if (block.type !== 'IfBlock')
8989
parser.error(
@@ -95,7 +95,7 @@ export default function mustache(parser: Parser) {
9595
const expression = readExpression(parser);
9696

9797
parser.allowWhitespace();
98-
parser.eat('}}', true);
98+
parser.eat(parser.v2 ? '}' : '}}', true);
9999

100100
block.else = {
101101
start: parser.index,
@@ -114,7 +114,7 @@ export default function mustache(parser: Parser) {
114114
};
115115

116116
parser.stack.push(block.else.children[0]);
117-
} else if (parser.eat('else')) {
117+
} else if (parser.eat(parser.v2 ? ':else' : 'else')) {
118118
const block = parser.current();
119119
if (block.type !== 'IfBlock' && block.type !== 'EachBlock') {
120120
parser.error(
@@ -123,7 +123,7 @@ export default function mustache(parser: Parser) {
123123
}
124124

125125
parser.allowWhitespace();
126-
parser.eat('}}', true);
126+
parser.eat(parser.v2 ? '}' : '}}', true);
127127

128128
block.else = {
129129
start: parser.index,
@@ -133,7 +133,7 @@ export default function mustache(parser: Parser) {
133133
};
134134

135135
parser.stack.push(block.else);
136-
} else if (parser.eat('then')) {
136+
} else if (parser.eat(parser.v2 ? ':then' : 'then')) {
137137
// TODO DRY out this and the next section
138138
const pendingBlock = parser.current();
139139
if (pendingBlock.type === 'PendingBlock') {
@@ -145,7 +145,7 @@ export default function mustache(parser: Parser) {
145145
awaitBlock.value = parser.readIdentifier();
146146

147147
parser.allowWhitespace();
148-
parser.eat('}}', true);
148+
parser.eat(parser.v2 ? '}' : '}}', true);
149149

150150
const thenBlock: Node = {
151151
start,
@@ -157,7 +157,7 @@ export default function mustache(parser: Parser) {
157157
awaitBlock.then = thenBlock;
158158
parser.stack.push(thenBlock);
159159
}
160-
} else if (parser.eat('catch')) {
160+
} else if (parser.eat(parser.v2 ? ':catch' : 'catch')) {
161161
const thenBlock = parser.current();
162162
if (thenBlock.type === 'ThenBlock') {
163163
thenBlock.end = start;
@@ -168,7 +168,7 @@ export default function mustache(parser: Parser) {
168168
awaitBlock.error = parser.readIdentifier();
169169

170170
parser.allowWhitespace();
171-
parser.eat('}}', true);
171+
parser.eat(parser.v2 ? '}' : '}}', true);
172172

173173
const catchBlock: Node = {
174174
start,
@@ -288,7 +288,7 @@ export default function mustache(parser: Parser) {
288288
parser.allowWhitespace();
289289
}
290290

291-
parser.eat('}}', true);
291+
parser.eat(parser.v2 ? '}' : '}}', true);
292292

293293
parser.current().children.push(block);
294294
parser.stack.push(block);
@@ -302,22 +302,40 @@ export default function mustache(parser: Parser) {
302302
// {{yield}}
303303
// TODO deprecate
304304
parser.allowWhitespace();
305-
parser.eat('}}', true);
306305

307-
parser.current().children.push({
308-
start,
309-
end: parser.index,
310-
type: 'Element',
311-
name: 'slot',
312-
attributes: [],
313-
children: []
314-
});
315-
} else if (parser.eat('{')) {
306+
if (parser.v2) {
307+
const expressionEnd = parser.index;
308+
309+
parser.eat('}', true);
310+
parser.current().children.push({
311+
start,
312+
end: parser.index,
313+
type: 'MustacheTag',
314+
expression: {
315+
start: expressionEnd - 5,
316+
end: expressionEnd,
317+
type: 'Identifier',
318+
name: 'yield'
319+
}
320+
});
321+
} else {
322+
parser.eat('}}', true);
323+
324+
parser.current().children.push({
325+
start,
326+
end: parser.index,
327+
type: 'Element',
328+
name: 'slot',
329+
attributes: [],
330+
children: []
331+
});
332+
}
333+
} else if (parser.eat(parser.v2 ? '@html' : '{')) {
316334
// {{{raw}}} mustache
317335
const expression = readExpression(parser);
318336

319337
parser.allowWhitespace();
320-
parser.eat('}}}', true);
338+
parser.eat(parser.v2 ? '}' : '}}}', true);
321339

322340
parser.current().children.push({
323341
start,

src/parse/state/tag.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export default function tag(parser: Parser) {
170170
}
171171
}
172172

173-
if (name === COMPONENT) {
173+
if (name === (parser.v2 ? 'svelte:component' : ':Component')) {
174174
parser.eat('{', true);
175175
element.expression = readExpression(parser);
176176
parser.allowWhitespace();
@@ -249,6 +249,10 @@ export default function tag(parser: Parser) {
249249
function readTagName(parser: Parser) {
250250
const start = parser.index;
251251

252+
// TODO hoist these back to the top, post-v2
253+
const SELF = parser.v2 ? 'svelte:self' : ':Self';
254+
const COMPONENT = parser.v2 ? 'svelte:component' : ':Component';
255+
252256
if (parser.eat(SELF)) {
253257
// check we're inside a block, otherwise this
254258
// will cause infinite recursion
@@ -289,14 +293,14 @@ function readTagName(parser: Parser) {
289293
function readAttribute(parser: Parser, uniqueNames: Set<string>) {
290294
const start = parser.index;
291295

292-
if (parser.eat('{{')) {
296+
if (parser.eat(parser.v2 ? '{' : '{{')) {
293297
parser.allowWhitespace();
294298
parser.eat('...', true, 'Expected spread operator (...)');
295299

296300
const expression = readExpression(parser);
297301

298302
parser.allowWhitespace();
299-
parser.eat('}}', true);
303+
parser.eat(parser.v2 ? '}' : '}}', true);
300304

301305
return {
302306
start,
@@ -369,17 +373,15 @@ function readSequence(parser: Parser, done: () => boolean) {
369373
});
370374

371375
return chunks;
372-
} else if (parser.eat('{{')) {
376+
} else if (parser.eat(parser.v2 ? '{' : '{{')) {
373377
if (currentChunk.data) {
374378
currentChunk.end = index;
375379
chunks.push(currentChunk);
376380
}
377381

378382
const expression = readExpression(parser);
379383
parser.allowWhitespace();
380-
if (!parser.eat('}}')) {
381-
parser.error(`Expected }}`);
382-
}
384+
parser.eat(parser.v2 ? '}' : '}}', true);
383385

384386
chunks.push({
385387
start: index,

test/parser/samples/attribute-dynamic-boolean/_actual-v2.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020
{
2121
"start": 19,
2222
"end": 29,
23-
"type": "Text",
24-
"data": "{readonly}"
23+
"type": "MustacheTag",
24+
"expression": {
25+
"type": "Identifier",
26+
"start": 20,
27+
"end": 28,
28+
"name": "readonly"
29+
}
2530
}
2631
]
2732
}

test/parser/samples/attribute-dynamic-boolean/output-v2.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
2-
"hash": 3179574701,
2+
"hash": "7xolfv",
33
"html": {
44
"start": 0,
5-
"end": 45,
5+
"end": 41,
66
"type": "Fragment",
77
"children": [
88
{
99
"start": 0,
10-
"end": 45,
10+
"end": 41,
1111
"type": "Element",
1212
"name": "textarea",
1313
"attributes": [
1414
{
1515
"start": 10,
16-
"end": 33,
16+
"end": 29,
1717
"type": "Attribute",
1818
"name": "readonly",
1919
"value": [
2020
{
21-
"start": 20,
22-
"end": 32,
21+
"start": 19,
22+
"end": 29,
2323
"type": "MustacheTag",
2424
"expression": {
2525
"type": "Identifier",
26-
"start": 22,
27-
"end": 30,
26+
"start": 20,
27+
"end": 28,
2828
"name": "readonly"
2929
}
3030
}

test/parser/samples/attribute-dynamic-reserved/_actual-v2.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020
{
2121
"start": 11,
2222
"end": 18,
23-
"type": "Text",
24-
"data": "{class}"
23+
"type": "MustacheTag",
24+
"expression": {
25+
"type": "Identifier",
26+
"start": 12,
27+
"end": 17,
28+
"name": "class"
29+
}
2530
}
2631
]
2732
}

test/parser/samples/attribute-dynamic-reserved/output-v2.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
2-
"hash": 2788845841,
2+
"hash": "l0cddf",
33
"html": {
44
"start": 0,
5-
"end": 29,
5+
"end": 25,
66
"type": "Fragment",
77
"children": [
88
{
99
"start": 0,
10-
"end": 29,
10+
"end": 25,
1111
"type": "Element",
1212
"name": "div",
1313
"attributes": [
1414
{
1515
"start": 5,
16-
"end": 22,
16+
"end": 18,
1717
"type": "Attribute",
1818
"name": "class",
1919
"value": [
2020
{
21-
"start": 12,
22-
"end": 21,
21+
"start": 11,
22+
"end": 18,
2323
"type": "MustacheTag",
2424
"expression": {
2525
"type": "Identifier",
26-
"start": 14,
27-
"end": 19,
26+
"start": 12,
27+
"end": 17,
2828
"name": "class"
2929
}
3030
}

test/parser/samples/attribute-dynamic/_actual-v2.json

+25-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,26 @@
1919
"value": [
2020
{
2121
"start": 12,
22+
"end": 19,
23+
"type": "Text",
24+
"data": "color: "
25+
},
26+
{
27+
"start": 19,
28+
"end": 26,
29+
"type": "MustacheTag",
30+
"expression": {
31+
"type": "Identifier",
32+
"start": 20,
33+
"end": 25,
34+
"name": "color"
35+
}
36+
},
37+
{
38+
"start": 26,
2239
"end": 27,
2340
"type": "Text",
24-
"data": "color: {color};"
41+
"data": ";"
2542
}
2643
]
2744
}
@@ -30,8 +47,13 @@
3047
{
3148
"start": 29,
3249
"end": 36,
33-
"type": "Text",
34-
"data": "{color}"
50+
"type": "MustacheTag",
51+
"expression": {
52+
"type": "Identifier",
53+
"start": 30,
54+
"end": 35,
55+
"name": "color"
56+
}
3557
}
3658
]
3759
}

0 commit comments

Comments
 (0)