Skip to content

Commit abc7be8

Browse files
authored
Change to include HTMLComment in AST. (#18)
1 parent c3c9148 commit abc7be8

File tree

35 files changed

+1855
-932
lines changed

35 files changed

+1855
-932
lines changed

src/ast.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface Token extends BaseNode {
3131
| "HTMLText"
3232
| "HTMLIdentifier"
3333
| "MustacheKeyword"
34+
| "HTMLComment"
3435
value: string
3536
}
3637

@@ -66,6 +67,7 @@ export type SvelteNode =
6667
| SvelteAttribute
6768
| SvelteSpreadAttribute
6869
| SvelteDirective
70+
| SvelteHTMLComment
6971
| SvelteReactiveStatement
7072

7173
export interface SvelteProgram extends BaseNode {
@@ -93,7 +95,7 @@ export interface SvelteStyleElement extends BaseSvelteElement {
9395
children: [SvelteText]
9496
parent: SvelteProgram
9597
}
96-
export interface SvelteHtmlElement extends BaseSvelteElement {
98+
export interface SvelteHTMLElement extends BaseSvelteElement {
9799
type: "SvelteElement"
98100
kind: "html"
99101
name: SvelteName
@@ -150,7 +152,7 @@ export interface SvelteSpecialElement extends BaseSvelteElement {
150152
| SvelteKeyBlock
151153
}
152154
export type SvelteElement =
153-
| SvelteHtmlElement
155+
| SvelteHTMLElement
154156
| SvelteComponentElement
155157
| SvelteSpecialElement
156158

@@ -169,6 +171,7 @@ type Child =
169171
| SvelteEachBlock
170172
| SvelteAwaitBlock
171173
| SvelteKeyBlock
174+
| SvelteHTMLComment
172175

173176
export interface SvelteText extends BaseNode {
174177
type: "SvelteText"
@@ -298,6 +301,20 @@ export interface SvelteKeyBlock extends BaseNode {
298301
| SvelteAwaitCatchBlock
299302
| SvelteKeyBlock
300303
}
304+
export interface SvelteHTMLComment extends BaseNode {
305+
type: "SvelteHTMLComment"
306+
value: string
307+
parent:
308+
| SvelteProgram
309+
| SvelteElement
310+
| SvelteIfBlock
311+
| SvelteElseBlock
312+
| SvelteEachBlock
313+
| SvelteAwaitPendingBlock
314+
| SvelteAwaitThenBlock
315+
| SvelteAwaitCatchBlock
316+
| SvelteKeyBlock
317+
}
301318

302319
export interface SvelteAttributeNonShorthand extends BaseNode {
303320
type: "SvelteAttribute"

src/parser/converts/element.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type {
2-
Comment,
32
SvelteAwaitBlock,
43
SvelteAwaitCatchBlock,
54
SvelteAwaitPendingBlock,
@@ -9,7 +8,8 @@ import type {
98
SvelteEachBlock,
109
SvelteElement,
1110
SvelteElseBlock,
12-
SvelteHtmlElement,
11+
SvelteHTMLComment,
12+
SvelteHTMLElement,
1313
SvelteIfBlock,
1414
SvelteKeyBlock,
1515
SvelteMustacheTag,
@@ -66,24 +66,19 @@ export function* convertChildren(
6666
| SvelteEachBlock
6767
| SvelteAwaitBlock
6868
| SvelteKeyBlock
69+
| SvelteHTMLComment
6970
> {
7071
for (const child of fragment.children) {
7172
if (child.type === "Comment") {
72-
const comment: Comment = {
73-
type: "Block",
74-
value: child.data,
75-
...ctx.getConvertLocation(child),
76-
}
77-
;(comment as any).html = true
78-
ctx.addComment(comment)
73+
yield convertComment(child, parent, ctx)
7974
continue
8075
}
8176
if (child.type === "Text") {
8277
yield convertText(child, parent, ctx)
8378
continue
8479
}
8580
if (child.type === "Element") {
86-
yield convertHtmlElement(child, parent, ctx)
81+
yield convertHTMLElement(child, parent, ctx)
8782
continue
8883
}
8984
if (child.type === "InlineComponent") {
@@ -155,13 +150,31 @@ export function* convertChildren(
155150
}
156151
}
157152

158-
/** Convert for HtmlElement */
159-
function convertHtmlElement(
153+
/** Convert for HTML Comment */
154+
function convertComment(
155+
node: SvAST.Comment,
156+
parent: SvelteHTMLComment["parent"],
157+
ctx: Context,
158+
): SvelteHTMLComment {
159+
const comment: SvelteHTMLComment = {
160+
type: "SvelteHTMLComment",
161+
value: node.data,
162+
parent,
163+
...ctx.getConvertLocation(node),
164+
}
165+
166+
ctx.addToken("HTMLComment", node)
167+
168+
return comment
169+
}
170+
171+
/** Convert for HTMLElement */
172+
function convertHTMLElement(
160173
node: SvAST.Element | SvAST.Slot,
161-
parent: SvelteHtmlElement["parent"],
174+
parent: SvelteHTMLElement["parent"],
162175
ctx: Context,
163-
): SvelteHtmlElement {
164-
const element: SvelteHtmlElement = {
176+
): SvelteHTMLElement {
177+
const element: SvelteHTMLElement = {
165178
type: "SvelteElement",
166179
kind: "html",
167180
name: null as any,
@@ -300,11 +313,11 @@ function convertComponentElement(
300313
/** Convert for Slot */
301314
function convertSlotElement(
302315
node: SvAST.Slot,
303-
parent: SvelteHtmlElement["parent"],
316+
parent: SvelteHTMLElement["parent"],
304317
ctx: Context,
305-
): SvelteHtmlElement {
306-
// Slot translates to SvelteHtmlElement.
307-
return convertHtmlElement(node, parent, ctx)
318+
): SvelteHTMLElement {
319+
// Slot translates to SvelteHTMLElement.
320+
return convertHTMLElement(node, parent, ctx)
308321
}
309322

310323
/** Convert for window element. e.g. <svelte:window> */

src/visitor-keys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const svelteKeys: SvelteKeysType = {
3737
SvelteDirective: ["expression"],
3838
SvelteReactiveStatement: ["label", "body"],
3939
SvelteText: [],
40+
SvelteHTMLComment: [],
4041
}
4142

4243
export const KEYS: SourceCode.VisitorKeys = unionWith(

tests/fixtures/parser/ast/docs/component-format/01-script/01-export-creates-a-component-prop/01-output.json

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@
110110
"column": 0
111111
}
112112
}
113+
},
114+
{
115+
"type": "SvelteHTMLComment",
116+
"value": " markup (zero or more items) goes here ",
117+
"range": [
118+
80,
119+
126
120+
],
121+
"loc": {
122+
"start": {
123+
"line": 9,
124+
"column": 0
125+
},
126+
"end": {
127+
"line": 9,
128+
"column": 46
129+
}
130+
}
113131
}
114132
],
115133
"comments": [
@@ -132,25 +150,6 @@
132150
"column": 19
133151
}
134152
}
135-
},
136-
{
137-
"type": "Block",
138-
"value": " markup (zero or more items) goes here ",
139-
"range": [
140-
80,
141-
126
142-
],
143-
"loc": {
144-
"start": {
145-
"line": 9,
146-
"column": 0
147-
},
148-
"end": {
149-
"line": 9,
150-
"column": 46
151-
}
152-
},
153-
"html": true
154153
}
155154
],
156155
"sourceType": "module",
@@ -460,6 +459,24 @@
460459
"column": 0
461460
}
462461
}
462+
},
463+
{
464+
"type": "HTMLComment",
465+
"value": "<!-- markup (zero or more items) goes here -->",
466+
"range": [
467+
80,
468+
126
469+
],
470+
"loc": {
471+
"start": {
472+
"line": 9,
473+
"column": 0
474+
},
475+
"end": {
476+
"line": 9,
477+
"column": 46
478+
}
479+
}
463480
}
464481
],
465482
"range": [

tests/fixtures/parser/ast/docs/template-syntax/02-attributes-and-props/07-output.json

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
{
22
"type": "Program",
33
"body": [
4+
{
5+
"type": "SvelteHTMLComment",
6+
"value": " These are equivalent ",
7+
"range": [
8+
0,
9+
29
10+
],
11+
"loc": {
12+
"start": {
13+
"line": 1,
14+
"column": 0
15+
},
16+
"end": {
17+
"line": 1,
18+
"column": 29
19+
}
20+
}
21+
},
422
{
523
"type": "SvelteText",
624
"value": "\n",
@@ -288,10 +306,12 @@
288306
}
289307
}
290308
],
291-
"comments": [
309+
"comments": [],
310+
"sourceType": "module",
311+
"tokens": [
292312
{
293-
"type": "Block",
294-
"value": " These are equivalent ",
313+
"type": "HTMLComment",
314+
"value": "<!-- These are equivalent -->",
295315
"range": [
296316
0,
297317
29
@@ -305,12 +325,8 @@
305325
"line": 1,
306326
"column": 29
307327
}
308-
},
309-
"html": true
310-
}
311-
],
312-
"sourceType": "module",
313-
"tokens": [
328+
}
329+
},
314330
{
315331
"type": "HTMLText",
316332
"value": "\n",

tests/fixtures/parser/ast/docs/template-syntax/04-comments/01-output.json

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
{
22
"type": "Program",
33
"body": [
4+
{
5+
"type": "SvelteHTMLComment",
6+
"value": " this is a comment! ",
7+
"range": [
8+
0,
9+
27
10+
],
11+
"loc": {
12+
"start": {
13+
"line": 1,
14+
"column": 0
15+
},
16+
"end": {
17+
"line": 1,
18+
"column": 27
19+
}
20+
}
21+
},
422
{
523
"type": "SvelteText",
624
"value": "\n",
@@ -77,10 +95,12 @@
7795
}
7896
}
7997
],
80-
"comments": [
98+
"comments": [],
99+
"sourceType": "module",
100+
"tokens": [
81101
{
82-
"type": "Block",
83-
"value": " this is a comment! ",
102+
"type": "HTMLComment",
103+
"value": "<!-- this is a comment! -->",
84104
"range": [
85105
0,
86106
27
@@ -94,12 +114,8 @@
94114
"line": 1,
95115
"column": 27
96116
}
97-
},
98-
"html": true
99-
}
100-
],
101-
"sourceType": "module",
102-
"tokens": [
117+
}
118+
},
103119
{
104120
"type": "HTMLText",
105121
"value": "\n",

0 commit comments

Comments
 (0)