Skip to content

Commit 304df3d

Browse files
authored
Breaking: update AST (#51)
* Breaking: update AST - Add `SvelteDirectiveKey` node that hold key information of `SvelteDirective`. - Add `SvelteSpecialDirectiveKey` node that hold key information of `SvelteSpecialDirective`. * fix
1 parent b024ec6 commit 304df3d

File tree

165 files changed

+8689
-2296
lines changed

Some content is hidden

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

165 files changed

+8689
-2296
lines changed

src/ast.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export type SvelteNode =
7272
| SvelteSpreadAttribute
7373
| SvelteDirective
7474
| SvelteSpecialDirective
75+
| SvelteDirectiveKey
76+
| SvelteSpecialDirectiveKey
7577
| SvelteHTMLComment
7678
| SvelteReactiveStatement
7779

@@ -409,10 +411,16 @@ export type SvelteDirective =
409411
| SvelteLetDirective
410412
| SvelteRefDirective
411413
| SvelteTransitionDirective
412-
interface BaseSvelteDirective extends BaseNode {
413-
type: "SvelteDirective"
414+
export interface SvelteDirectiveKey extends BaseNode {
415+
type: "SvelteDirectiveKey"
414416
name: ESTree.Identifier
415417
modifiers: string[]
418+
parent: SvelteDirective
419+
}
420+
421+
interface BaseSvelteDirective extends BaseNode {
422+
type: "SvelteDirective"
423+
key: SvelteDirectiveKey
416424
parent: SvelteStartTag
417425
}
418426

@@ -450,9 +458,14 @@ export interface SvelteTransitionDirective extends BaseSvelteDirective {
450458
outro: boolean
451459
expression: null | ESTree.Expression
452460
}
461+
export interface SvelteSpecialDirectiveKey extends BaseNode {
462+
type: "SvelteSpecialDirectiveKey"
463+
parent: SvelteSpecialDirective
464+
}
453465
export interface SvelteSpecialDirective extends BaseNode {
454466
type: "SvelteSpecialDirective"
455467
kind: "this"
468+
key: SvelteSpecialDirectiveKey
456469
expression: ESTree.Expression
457470
parent: SvelteStartTag /* & { parent: SvelteSpecialElement } */
458471
}

src/parser/converts/attr.ts

+33-24
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ function convertBindingDirective(
193193
const directive: SvelteBindingDirective = {
194194
type: "SvelteDirective",
195195
kind: "Binding",
196-
name: null as any,
197-
modifiers: node.modifiers,
196+
key: null as any,
198197
expression: null,
199198
parent,
200199
...ctx.getConvertLocation(node),
@@ -233,8 +232,7 @@ function convertEventHandlerDirective(
233232
const directive: SvelteEventHandlerDirective = {
234233
type: "SvelteDirective",
235234
kind: "EventHandler",
236-
name: null as any,
237-
modifiers: node.modifiers,
235+
key: null as any,
238236
expression: null,
239237
parent,
240238
...ctx.getConvertLocation(node),
@@ -266,8 +264,7 @@ function convertClassDirective(
266264
const directive: SvelteClassDirective = {
267265
type: "SvelteDirective",
268266
kind: "Class",
269-
name: null as any,
270-
modifiers: node.modifiers,
267+
key: null as any,
271268
expression: null,
272269
parent,
273270
...ctx.getConvertLocation(node),
@@ -292,8 +289,7 @@ function convertTransitionDirective(
292289
kind: "Transition",
293290
intro: node.intro,
294291
outro: node.outro,
295-
name: null as any,
296-
modifiers: node.modifiers,
292+
key: null as any,
297293
expression: null,
298294
parent,
299295
...ctx.getConvertLocation(node),
@@ -303,7 +299,7 @@ function convertTransitionDirective(
303299
directive,
304300
ctx,
305301
buildProcessExpressionForExpression(directive, ctx, null),
306-
(name) => ctx.scriptLet.addExpression(name, directive),
302+
(name) => ctx.scriptLet.addExpression(name, directive.key),
307303
)
308304
return directive
309305
}
@@ -317,8 +313,7 @@ function convertAnimationDirective(
317313
const directive: SvelteAnimationDirective = {
318314
type: "SvelteDirective",
319315
kind: "Animation",
320-
name: null as any,
321-
modifiers: node.modifiers,
316+
key: null as any,
322317
expression: null,
323318
parent,
324319
...ctx.getConvertLocation(node),
@@ -328,7 +323,7 @@ function convertAnimationDirective(
328323
directive,
329324
ctx,
330325
buildProcessExpressionForExpression(directive, ctx, null),
331-
(name) => ctx.scriptLet.addExpression(name, directive),
326+
(name) => ctx.scriptLet.addExpression(name, directive.key),
332327
)
333328
return directive
334329
}
@@ -342,8 +337,7 @@ function convertActionDirective(
342337
const directive: SvelteActionDirective = {
343338
type: "SvelteDirective",
344339
kind: "Action",
345-
name: null as any,
346-
modifiers: node.modifiers,
340+
key: null as any,
347341
expression: null,
348342
parent,
349343
...ctx.getConvertLocation(node),
@@ -353,7 +347,7 @@ function convertActionDirective(
353347
directive,
354348
ctx,
355349
buildProcessExpressionForExpression(directive, ctx, null),
356-
(name) => ctx.scriptLet.addExpression(name, directive),
350+
(name) => ctx.scriptLet.addExpression(name, directive.key),
357351
)
358352
return directive
359353
}
@@ -367,8 +361,7 @@ function convertLetDirective(
367361
const directive: SvelteLetDirective = {
368362
type: "SvelteDirective",
369363
kind: "Let",
370-
name: null as any,
371-
modifiers: node.modifiers,
364+
key: null as any,
372365
expression: null,
373366
parent,
374367
...ctx.getConvertLocation(node),
@@ -422,24 +415,33 @@ function processDirective<
422415
end: nameIndex + node.name.length,
423416
}
424417

418+
let keyEndIndex = nameRange.end
419+
425420
// modifiers
426421
if (ctx.code[nameRange.end] === "|") {
427422
let nextStart = nameRange.end + 1
428423
let nextEnd = indexOf(
429424
ctx.code,
430-
(c) => c === "=" || c === ">" || c === "|" || !c.trim(),
425+
(c) =>
426+
c === "=" || c === ">" || c === "/" || c === "|" || !c.trim(),
431427
nextStart,
432428
)
433429
ctx.addToken("HTMLIdentifier", { start: nextStart, end: nextEnd })
434430
while (ctx.code[nextEnd] === "|") {
435431
nextStart = nextEnd + 1
436432
nextEnd = indexOf(
437433
ctx.code,
438-
(c) => c === "=" || c === ">" || c === "|" || !c.trim(),
434+
(c) =>
435+
c === "=" ||
436+
c === ">" ||
437+
c === "/" ||
438+
c === "|" ||
439+
!c.trim(),
439440
nextStart,
440441
)
441442
ctx.addToken("HTMLIdentifier", { start: nextStart, end: nextEnd })
442443
}
444+
keyEndIndex = nextEnd
443445
}
444446

445447
let isShorthand = false
@@ -455,18 +457,25 @@ function processDirective<
455457
})
456458
}
457459

460+
const key = (directive.key = {
461+
type: "SvelteDirectiveKey",
462+
name: null as any,
463+
modifiers: node.modifiers,
464+
parent: directive,
465+
...ctx.getConvertLocation({ start: node.start, end: keyEndIndex }),
466+
})
467+
458468
// put name
459-
directive.name = {
469+
key.name = {
460470
type: "Identifier",
461471
name: node.name,
462-
// @ts-expect-error -- ignore
463-
parent: directive,
472+
parent: key,
464473
...ctx.getConvertLocation(nameRange),
465474
}
466475
if (!isShorthand) {
467476
if (processName) {
468-
processName(directive.name).push((es) => {
469-
directive.name = es
477+
processName(key.name).push((es) => {
478+
key.name = es
470479
})
471480
} else {
472481
ctx.addToken("HTMLIdentifier", nameRange)

src/parser/converts/element.ts

+6
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,16 @@ function convertSpecialElement(
318318
const thisAttr: SvelteSpecialDirective = {
319319
type: "SvelteSpecialDirective",
320320
kind: "this",
321+
key: null as any,
321322
expression: null as any,
322323
parent: element.startTag,
323324
...ctx.getConvertLocation({ start: startIndex, end: endIndex }),
324325
}
326+
thisAttr.key = {
327+
type: "SvelteSpecialDirectiveKey",
328+
parent: thisAttr,
329+
...ctx.getConvertLocation({ start: startIndex, end: eqIndex }),
330+
}
325331
ctx.addToken("HTMLIdentifier", {
326332
start: startIndex,
327333
end: eqIndex,

src/visitor-keys.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ const svelteKeys: SvelteKeysType = {
3838
SvelteAttribute: ["key", "value"],
3939
SvelteShorthandAttribute: ["key", "value"],
4040
SvelteSpreadAttribute: ["argument"],
41-
SvelteDirective: ["expression"],
42-
SvelteSpecialDirective: ["expression"],
41+
SvelteDirective: ["key", "expression"],
42+
SvelteSpecialDirective: ["key", "expression"],
43+
SvelteDirectiveKey: ["name"],
44+
SvelteSpecialDirectiveKey: [],
4345
SvelteText: [],
4446
SvelteHTMLComment: [],
4547
SvelteReactiveStatement: ["label", "body"],

tests/fixtures/parser/ast/blog/write-less-code01-output.json

+46-12
Original file line numberDiff line numberDiff line change
@@ -323,25 +323,42 @@
323323
{
324324
"type": "SvelteDirective",
325325
"kind": "Binding",
326-
"name": {
327-
"type": "Identifier",
328-
"name": "value",
326+
"key": {
327+
"type": "SvelteDirectiveKey",
328+
"name": {
329+
"type": "Identifier",
330+
"name": "value",
331+
"range": [
332+
70,
333+
75
334+
],
335+
"loc": {
336+
"start": {
337+
"line": 6,
338+
"column": 26
339+
},
340+
"end": {
341+
"line": 6,
342+
"column": 31
343+
}
344+
}
345+
},
346+
"modifiers": [],
329347
"range": [
330-
70,
348+
65,
331349
75
332350
],
333351
"loc": {
334352
"start": {
335353
"line": 6,
336-
"column": 26
354+
"column": 21
337355
},
338356
"end": {
339357
"line": 6,
340358
"column": 31
341359
}
342360
}
343361
},
344-
"modifiers": [],
345362
"expression": {
346363
"type": "Identifier",
347364
"name": "a",
@@ -510,25 +527,42 @@
510527
{
511528
"type": "SvelteDirective",
512529
"kind": "Binding",
513-
"name": {
514-
"type": "Identifier",
515-
"name": "value",
530+
"key": {
531+
"type": "SvelteDirectiveKey",
532+
"name": {
533+
"type": "Identifier",
534+
"name": "value",
535+
"range": [
536+
107,
537+
112
538+
],
539+
"loc": {
540+
"start": {
541+
"line": 7,
542+
"column": 26
543+
},
544+
"end": {
545+
"line": 7,
546+
"column": 31
547+
}
548+
}
549+
},
550+
"modifiers": [],
516551
"range": [
517-
107,
552+
102,
518553
112
519554
],
520555
"loc": {
521556
"start": {
522557
"line": 7,
523-
"column": 26
558+
"column": 21
524559
},
525560
"end": {
526561
"line": 7,
527562
"column": 31
528563
}
529564
}
530565
},
531-
"modifiers": [],
532566
"expression": {
533567
"type": "Identifier",
534568
"name": "b",

0 commit comments

Comments
 (0)