@markuplint/astro-parser 3.0.0-canary.3 → 3.0.0-dev.176

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,444 @@
1
+ const { astroParse } = require('../lib/astro-parser');
2
+
3
+ it('Basic', () => {
4
+ const ast = astroParse(`---
5
+ const name = "World";
6
+ ---
7
+ <!-- Comment -->
8
+ <style>
9
+ div {
10
+ color: red;
11
+ }
12
+ </style>
13
+ <div data-attr="v">Hello {name}!</div>
14
+ `);
15
+ expect(ast).toStrictEqual(
16
+ expect.objectContaining({
17
+ children: [
18
+ {
19
+ position: {
20
+ end: {
21
+ column: 4,
22
+ line: 3,
23
+ offset: 29,
24
+ },
25
+ start: {
26
+ column: 1,
27
+ line: 1,
28
+ offset: 0,
29
+ },
30
+ },
31
+ type: 'frontmatter',
32
+ value: '\nconst name = "World";\n',
33
+ },
34
+ {
35
+ position: {
36
+ end: {
37
+ column: 17,
38
+ line: 4,
39
+ offset: 46,
40
+ },
41
+ start: {
42
+ column: 5,
43
+ line: 4,
44
+ offset: 30,
45
+ },
46
+ },
47
+ type: 'comment',
48
+ value: ' Comment ',
49
+ },
50
+ {
51
+ attributes: [],
52
+ children: [
53
+ {
54
+ position: {
55
+ end: {
56
+ column: 1,
57
+ line: 9,
58
+ offset: 79,
59
+ },
60
+ start: {
61
+ column: 8,
62
+ line: 5,
63
+ offset: 54,
64
+ },
65
+ },
66
+ type: 'text',
67
+ value: '\ndiv {\n color: red;\n}\n',
68
+ },
69
+ ],
70
+ name: 'style',
71
+ position: {
72
+ end: {
73
+ column: 9,
74
+ line: 9,
75
+ offset: 87,
76
+ },
77
+ start: {
78
+ column: 1,
79
+ line: 5,
80
+ offset: 47,
81
+ },
82
+ },
83
+ type: 'element',
84
+ },
85
+ {
86
+ position: {
87
+ end: {
88
+ column: 1,
89
+ line: 10,
90
+ offset: 88,
91
+ },
92
+ start: {
93
+ column: 9,
94
+ line: 9,
95
+ offset: 87,
96
+ },
97
+ },
98
+ type: 'text',
99
+ value: '\n',
100
+ },
101
+ {
102
+ attributes: [
103
+ {
104
+ kind: 'quoted',
105
+ name: 'data-attr',
106
+ position: {
107
+ start: {
108
+ column: 6,
109
+ line: 10,
110
+ offset: 93,
111
+ },
112
+ },
113
+ type: 'attribute',
114
+ value: 'v',
115
+ },
116
+ ],
117
+ children: [
118
+ {
119
+ position: {
120
+ end: {
121
+ column: 26,
122
+ line: 10,
123
+ offset: 113,
124
+ },
125
+ start: {
126
+ column: 20,
127
+ line: 10,
128
+ offset: 107,
129
+ },
130
+ },
131
+ type: 'text',
132
+ value: 'Hello ',
133
+ },
134
+ {
135
+ children: [
136
+ {
137
+ position: {
138
+ end: {
139
+ column: 31,
140
+ line: 10,
141
+ offset: 118,
142
+ },
143
+ start: {
144
+ column: 27,
145
+ line: 10,
146
+ offset: 114,
147
+ },
148
+ },
149
+ type: 'text',
150
+ value: 'name',
151
+ },
152
+ ],
153
+ position: {
154
+ end: {
155
+ column: 9,
156
+ line: 11,
157
+ offset: 119,
158
+ },
159
+ start: {
160
+ column: 25,
161
+ line: 10,
162
+ offset: 113,
163
+ },
164
+ },
165
+ type: 'expression',
166
+ },
167
+ {
168
+ position: {
169
+ end: {
170
+ column: 33,
171
+ line: 10,
172
+ offset: 120,
173
+ },
174
+ start: {
175
+ column: 32,
176
+ line: 10,
177
+ offset: 119,
178
+ },
179
+ },
180
+ type: 'text',
181
+ value: '!',
182
+ },
183
+ ],
184
+ name: 'div',
185
+ position: {
186
+ end: {
187
+ column: 39,
188
+ line: 10,
189
+ offset: 126,
190
+ },
191
+ start: {
192
+ column: 1,
193
+ line: 10,
194
+ offset: 88,
195
+ },
196
+ },
197
+ type: 'element',
198
+ },
199
+ {
200
+ position: {
201
+ end: {
202
+ column: 1,
203
+ line: 11,
204
+ offset: 127,
205
+ },
206
+ start: {
207
+ column: 39,
208
+ line: 10,
209
+ offset: 126,
210
+ },
211
+ },
212
+ type: 'text',
213
+ value: '\n',
214
+ },
215
+ ],
216
+ type: 'root',
217
+ }),
218
+ );
219
+ });
220
+
221
+ it('Attr and Template Directive', () => {
222
+ const ast = astroParse('<div a b=c d="e" f=`g` x:y prop={ prop }></div>');
223
+ expect(ast.children?.[0].attributes).toStrictEqual(
224
+ expect.objectContaining([
225
+ {
226
+ type: 'attribute',
227
+ kind: 'empty',
228
+ name: 'a',
229
+ value: '',
230
+ position: { start: { column: 6, line: 1, offset: 5 } },
231
+ },
232
+ {
233
+ type: 'attribute',
234
+ kind: 'quoted',
235
+ name: 'b',
236
+ value: 'c',
237
+ position: { start: { column: 8, line: 1, offset: 7 } },
238
+ },
239
+ {
240
+ type: 'attribute',
241
+ kind: 'quoted',
242
+ name: 'd',
243
+ value: 'e',
244
+ position: { start: { column: 12, line: 1, offset: 11 } },
245
+ },
246
+ {
247
+ type: 'attribute',
248
+ kind: 'template-literal',
249
+ name: 'f',
250
+ value: 'g',
251
+ position: { start: { column: 18, line: 1, offset: 17 } },
252
+ },
253
+ {
254
+ type: 'attribute',
255
+ kind: 'empty',
256
+ name: 'x:y',
257
+ value: '',
258
+ position: { start: { column: 24, line: 1, offset: 23 } },
259
+ },
260
+ {
261
+ type: 'attribute',
262
+ kind: 'expression',
263
+ name: 'prop',
264
+ value: ' prop ',
265
+ position: { start: { column: 28, line: 1, offset: 27 } },
266
+ },
267
+ ]),
268
+ );
269
+ });
270
+
271
+ test('Greater-than sign in attribute value', () => {
272
+ const ast = astroParse('<div attr="a>b"></div>');
273
+ expect(ast.children?.[0].attributes).toStrictEqual(
274
+ expect.objectContaining([
275
+ {
276
+ kind: 'quoted',
277
+ name: 'attr',
278
+ position: { start: { column: 6, line: 1, offset: 5 } },
279
+ type: 'attribute',
280
+ value: 'a>b',
281
+ },
282
+ ]),
283
+ );
284
+ });
285
+
286
+ describe('Issues', () => {
287
+ test('#803', () => {
288
+ const code = `<html lang="en">
289
+ <head>
290
+ <meta charset="utf-8" />
291
+ <title>Title</title>
292
+ <meta name="viewport" content="width=device-width" />
293
+ </head>
294
+ </html>
295
+ `;
296
+ const ast = astroParse(code);
297
+ expect(ast).toStrictEqual(
298
+ expect.objectContaining({
299
+ type: 'root',
300
+ children: [
301
+ {
302
+ type: 'element',
303
+ name: 'html',
304
+ position: { start: { line: 1, column: 2, offset: 0 } },
305
+ attributes: [
306
+ {
307
+ type: 'attribute',
308
+ kind: 'quoted',
309
+ name: 'lang',
310
+ value: 'en',
311
+ position: { start: { line: 1, column: 7, offset: 6 } },
312
+ },
313
+ ],
314
+ children: [
315
+ {
316
+ type: 'text',
317
+ value: '\n\t',
318
+ position: {
319
+ start: { line: 1, column: 17, offset: 16 },
320
+ end: { line: 2, column: 2, offset: 18 },
321
+ },
322
+ },
323
+ {
324
+ type: 'element',
325
+ name: 'head',
326
+ attributes: [],
327
+ position: {
328
+ start: { line: 2, column: 2, offset: 18 },
329
+ end: { line: 6, column: 9, offset: 139 },
330
+ },
331
+ children: [
332
+ {
333
+ type: 'text',
334
+ value: '\n\t\t',
335
+ position: {
336
+ start: { line: 2, column: 8, offset: 24 },
337
+ end: { line: 3, column: 3, offset: 27 },
338
+ },
339
+ },
340
+ {
341
+ type: 'element',
342
+ name: 'meta',
343
+ position: { start: { line: 3, column: 4, offset: 27 } },
344
+ attributes: [
345
+ {
346
+ type: 'attribute',
347
+ kind: 'quoted',
348
+ name: 'charset',
349
+ value: 'utf-8',
350
+ position: { start: { line: 3, column: 9, offset: 33 } },
351
+ },
352
+ ],
353
+ children: [],
354
+ },
355
+ {
356
+ type: 'text',
357
+ value: '\n\t\t',
358
+ position: {
359
+ start: { line: 3, column: 27, offset: 51 },
360
+ end: { line: 4, column: 3, offset: 54 },
361
+ },
362
+ },
363
+ {
364
+ type: 'element',
365
+ name: 'title',
366
+ position: {
367
+ start: { line: 4, column: 3, offset: 54 },
368
+ end: { line: 4, column: 23, offset: 74 },
369
+ },
370
+ attributes: [],
371
+ children: [
372
+ {
373
+ type: 'text',
374
+ value: 'Title',
375
+ position: {
376
+ start: { line: 4, column: 10, offset: 61 },
377
+ end: { line: 4, column: 15, offset: 66 },
378
+ },
379
+ },
380
+ ],
381
+ },
382
+ {
383
+ type: 'text',
384
+ value: '\n\t\t',
385
+ position: {
386
+ start: { line: 4, column: 23, offset: 74 },
387
+ end: { line: 5, column: 3, offset: 77 },
388
+ },
389
+ },
390
+ {
391
+ type: 'element',
392
+ name: 'meta',
393
+ attributes: [
394
+ {
395
+ type: 'attribute',
396
+ kind: 'quoted',
397
+ name: 'name',
398
+ value: 'viewport',
399
+ position: { start: { line: 5, column: 9, offset: 83 } },
400
+ },
401
+ {
402
+ type: 'attribute',
403
+ kind: 'quoted',
404
+ name: 'content',
405
+ value: 'width=device-width',
406
+ position: { start: { line: 5, column: 25, offset: 99 } },
407
+ },
408
+ ],
409
+ position: { start: { line: 5, column: 4, offset: 77 } },
410
+ children: [],
411
+ },
412
+ {
413
+ type: 'text',
414
+ value: '\n\t',
415
+ position: {
416
+ start: { line: 5, column: 56, offset: 130 },
417
+ end: { line: 6, column: 2, offset: 132 },
418
+ },
419
+ },
420
+ ],
421
+ },
422
+ {
423
+ type: 'text',
424
+ value: '\n',
425
+ position: {
426
+ start: { line: 6, column: 9, offset: 139 },
427
+ end: { line: 7, column: 1, offset: 140 },
428
+ },
429
+ },
430
+ {
431
+ type: 'text',
432
+ value: '',
433
+ position: {
434
+ start: { line: 7, column: 8, offset: 140 },
435
+ end: { line: 8, column: 1, offset: 140 },
436
+ },
437
+ },
438
+ ],
439
+ },
440
+ ],
441
+ }),
442
+ );
443
+ });
444
+ });