@markuplint/svelte-parser 3.6.1 → 3.7.0

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.
package/lib/attr.d.ts CHANGED
@@ -1,10 +1,5 @@
1
1
  import type { SvelteDirective } from './svelte-parser';
2
2
  import type { MLASTAttr } from '@markuplint/ml-ast';
3
- export declare function attr(
4
- attr: SvelteDirective,
5
- rawHTML: string,
6
- ):
7
- | MLASTAttr
8
- | {
9
- __spreadAttr: true;
10
- };
3
+ export declare function attr(attr: SvelteDirective, rawHTML: string): MLASTAttr | {
4
+ __spreadAttr: true;
5
+ };
@@ -1,8 +1,2 @@
1
1
  import type { MLASTHTMLAttr } from '@markuplint/ml-ast';
2
- export default function directiveTokenizer(
3
- raw: string,
4
- rawValue: string,
5
- line: number,
6
- col: number,
7
- startOffset: number,
8
- ): MLASTHTMLAttr;
2
+ export default function directiveTokenizer(raw: string, rawValue: string, line: number, col: number, startOffset: number): MLASTHTMLAttr;
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { parse } from './parse';
2
- export declare const endTag = 'xml';
2
+ export declare const endTag = "xml";
package/lib/nodeize.d.ts CHANGED
@@ -1,9 +1,3 @@
1
1
  import type { SvelteNode } from './svelte-parser';
2
2
  import type { MLASTNode, MLASTParentNode, ParserOptions } from '@markuplint/ml-ast';
3
- export declare function nodeize(
4
- originNode: SvelteNode,
5
- prevNode: MLASTNode | null,
6
- parentNode: MLASTParentNode | null,
7
- rawHtml: string,
8
- options?: ParserOptions,
9
- ): MLASTNode | MLASTNode[] | null;
3
+ export declare function nodeize(originNode: SvelteNode, prevNode: MLASTNode | null, parentNode: MLASTParentNode | null, rawHtml: string, options?: ParserOptions): MLASTNode | MLASTNode[] | null;
@@ -1,13 +1,3 @@
1
1
  import type { SvelteNode } from './svelte-parser';
2
2
  import type { MLASTNode, MLASTParentNode, MLASTPreprocessorSpecificBlock, ParserOptions } from '@markuplint/ml-ast';
3
- export declare function parseCtrlBlock(
4
- ctrlName: 'if' | 'each',
5
- originNode: SvelteNode,
6
- raw: string,
7
- rawHtml: string,
8
- startOffset: number,
9
- parentNode: MLASTParentNode | null,
10
- prevNode: MLASTNode | null,
11
- nextNode: MLASTNode | null,
12
- options?: ParserOptions,
13
- ): MLASTPreprocessorSpecificBlock[];
3
+ export declare function parseCtrlBlock(ctrlName: 'if' | 'each', originNode: SvelteNode, raw: string, rawHtml: string, startOffset: number, parentNode: MLASTParentNode | null, prevNode: MLASTNode | null, nextNode: MLASTNode | null, options?: ParserOptions): MLASTPreprocessorSpecificBlock[];
package/lib/traverse.d.ts CHANGED
@@ -1,8 +1,3 @@
1
1
  import type { SvelteNode } from './svelte-parser';
2
2
  import type { MLASTNode, MLASTParentNode, ParserOptions } from '@markuplint/ml-ast';
3
- export declare function traverse(
4
- astNodes: readonly SvelteNode[],
5
- parentNode: MLASTParentNode | null | undefined,
6
- rawHtml: string,
7
- options?: ParserOptions,
8
- ): MLASTNode[];
3
+ export declare function traverse(astNodes: readonly SvelteNode[], parentNode: MLASTParentNode | null | undefined, rawHtml: string, options?: ParserOptions): MLASTNode[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/svelte-parser",
3
- "version": "3.6.1",
3
+ "version": "3.7.0",
4
4
  "description": "Svelte parser for markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -16,11 +16,11 @@
16
16
  "clean": "tsc --build --clean"
17
17
  },
18
18
  "dependencies": {
19
- "@markuplint/html-parser": "3.6.1",
19
+ "@markuplint/html-parser": "3.8.0",
20
20
  "@markuplint/ml-ast": "3.1.0",
21
- "@markuplint/parser-utils": "3.6.1",
22
- "svelte": "^3.57.0",
23
- "tslib": "^2.4.1"
21
+ "@markuplint/parser-utils": "3.8.0",
22
+ "svelte": "^3.58.0",
23
+ "tslib": "^2.5.0"
24
24
  },
25
- "gitHead": "3cdf5a088b2da03773d5d4461d0e65ec32290a00"
25
+ "gitHead": "af370797bfc887e5a5a2ff57fbaa8392ac98ead2"
26
26
  }
@@ -0,0 +1,508 @@
1
+ const { attributesToDebugMaps, nodeListToDebugMaps } = require('@markuplint/parser-utils');
2
+
3
+ const { parse } = require('../lib/');
4
+
5
+ describe('parser', () => {
6
+ test('syntax error', () => {
7
+ expect(() => {
8
+ parse('<div></div\nattr>');
9
+ }).toThrow('Expected >\n1: <div></div\n2: attr>\n ^');
10
+ });
11
+
12
+ test('standard', () => {
13
+ const r = parse('<div>text</div>');
14
+ const map = nodeListToDebugMaps(r.nodeList);
15
+ expect(map).toStrictEqual([
16
+ '[1:1]>[1:6](0,5)div: <div>',
17
+ '[1:6]>[1:10](5,9)#text: text',
18
+ '[1:10]>[1:16](9,15)div: </div>',
19
+ ]);
20
+ });
21
+
22
+ test('with script', () => {
23
+ const r = parse(`<script>let i = 1;</script>
24
+
25
+ <div data-attr={i}>{i}</div>
26
+ <span></span>`);
27
+ const map = nodeListToDebugMaps(r.nodeList);
28
+ expect(map).toStrictEqual([
29
+ '[1:1]>[1:28](0,27)#ps:Script: <script>let␣i␣=␣1;</script>',
30
+ '[1:28]>[3:1](27,29)#text: ⏎⏎',
31
+ '[3:1]>[3:20](29,48)div: <div␣data-attr={i}>',
32
+ '[3:20]>[3:23](48,51)#ps:MustacheTag: {i}',
33
+ '[3:23]>[3:29](51,57)div: </div>',
34
+ '[3:29]>[4:2](57,59)#text: ⏎→',
35
+ '[4:2]>[4:8](59,65)span: <span>',
36
+ '[4:8]>[4:15](65,72)span: </span>',
37
+ ]);
38
+ });
39
+
40
+ test('with script (complex order)', () => {
41
+ const r = parse(`
42
+ <div>1</div>
43
+ <script>let i = 1;</script>
44
+ <div>2</div>
45
+ <style>div { display: none; }</style>
46
+ <div>3</div>
47
+ `);
48
+ const map = nodeListToDebugMaps(r.nodeList);
49
+ expect(map).toStrictEqual([
50
+ '[1:1]>[2:1](0,1)#text: ⏎',
51
+ '[2:1]>[2:6](1,6)div: <div>',
52
+ '[2:6]>[2:7](6,7)#text: 1',
53
+ '[2:7]>[2:13](7,13)div: </div>',
54
+ '[2:13]>[3:1](13,14)#text: ⏎',
55
+ '[3:1]>[3:28](14,41)#ps:Script: <script>let␣i␣=␣1;</script>',
56
+ '[3:28]>[4:1](41,42)#text: ⏎',
57
+ '[4:1]>[4:6](42,47)div: <div>',
58
+ '[4:6]>[4:7](47,48)#text: 2',
59
+ '[4:7]>[4:13](48,54)div: </div>',
60
+ '[4:13]>[5:1](54,55)#text: ⏎',
61
+ '[5:1]>[5:38](55,92)#ps:Style: <style>div␣{␣display:␣none;␣}</style>',
62
+ '[5:38]>[6:1](92,93)#text: ⏎',
63
+ '[6:1]>[6:6](93,98)div: <div>',
64
+ '[6:6]>[6:7](98,99)#text: 3',
65
+ '[6:7]>[6:13](99,105)div: </div>',
66
+ '[6:13]>[7:1](105,106)#text: ⏎',
67
+ ]);
68
+ });
69
+
70
+ test('variable', () => {
71
+ const r = parse('<div>{variable}</div>');
72
+ const map = nodeListToDebugMaps(r.nodeList);
73
+ expect(map).toStrictEqual([
74
+ '[1:1]>[1:6](0,5)div: <div>',
75
+ '[1:6]>[1:16](5,15)#ps:MustacheTag: {variable}',
76
+ '[1:16]>[1:22](15,21)div: </div>',
77
+ ]);
78
+ });
79
+
80
+ test('if statement', () => {
81
+ const r = parse('<div>{#if bool}true{:else}false{/if}</div>');
82
+ const map = nodeListToDebugMaps(r.nodeList);
83
+ expect(map).toStrictEqual([
84
+ '[1:1]>[1:6](0,5)div: <div>',
85
+ '[1:6]>[1:16](5,15)IfBlock: {#if␣bool}',
86
+ '[1:16]>[1:20](15,19)#text: true',
87
+ '[1:20]>[1:27](19,26)ElseBlock: {:else}',
88
+ '[1:27]>[1:32](26,31)#text: false',
89
+ '[1:32]>[1:37](31,36)IfBlock: {/if}',
90
+ '[1:37]>[1:43](36,42)div: </div>',
91
+ ]);
92
+ });
93
+
94
+ test('else if statement', () => {
95
+ const r = parse(`<div>
96
+ {#if porridge.temperature > 100}
97
+ <p>too hot!</p>
98
+ {:else if 80 > porridge.temperature}
99
+ <p>too cold!</p>
100
+ {:else}
101
+ <p>just right!</p>
102
+ {/if}
103
+ </div>`);
104
+ const map = nodeListToDebugMaps(r.nodeList);
105
+ expect(map).toStrictEqual([
106
+ '[1:1]>[1:6](0,5)div: <div>',
107
+ '[1:6]>[2:2](5,7)#text: ⏎→',
108
+ '[2:2]>[3:3](7,42)IfBlock: {#if␣porridge.temperature␣>␣100}⏎→→',
109
+ '[3:3]>[3:6](42,45)p: <p>',
110
+ '[3:6]>[3:14](45,53)#text: too␣hot!',
111
+ '[3:14]>[3:18](53,57)p: </p>',
112
+ '[3:18]>[5:3](57,98)ElseIfBlock: ⏎→{:else␣if␣80␣>␣porridge.temperature}⏎→→',
113
+ '[5:3]>[5:6](98,101)p: <p>',
114
+ '[5:6]>[5:15](101,110)#text: too␣cold!',
115
+ '[5:15]>[5:19](110,114)p: </p>',
116
+ '[5:19]>[6:9](114,123)ElseBlock: ⏎→{:else}',
117
+ '[6:9]>[7:3](123,126)#text: ⏎→→',
118
+ '[7:3]>[7:6](126,129)p: <p>',
119
+ '[7:6]>[7:17](129,140)#text: just␣right!',
120
+ '[7:17]>[7:21](140,144)p: </p>',
121
+ '[7:21]>[8:2](144,146)#text: ⏎→',
122
+ '[8:2]>[8:7](146,151)IfBlock: {/if}',
123
+ '[8:7]>[9:1](151,152)#text: ⏎',
124
+ '[9:1]>[9:7](152,158)div: </div>',
125
+ ]);
126
+ });
127
+
128
+ test('each statement', () => {
129
+ const r = parse('{#each expression as name}...{/each}');
130
+ const map = nodeListToDebugMaps(r.nodeList);
131
+ expect(map).toStrictEqual([
132
+ '[1:1]>[1:27](0,26)EachBlock: {#each␣expression␣as␣name}',
133
+ '[1:27]>[1:30](26,29)#text: ...',
134
+ '[1:30]>[1:37](29,36)EachBlock: {/each}',
135
+ ]);
136
+ });
137
+
138
+ test('each else statement', () => {
139
+ const r = parse('{#each expression as name}...{:else}...{/each}');
140
+ const map = nodeListToDebugMaps(r.nodeList);
141
+ expect(map).toStrictEqual([
142
+ '[1:1]>[1:27](0,26)EachBlock: {#each␣expression␣as␣name}',
143
+ '[1:27]>[1:30](26,29)#text: ...',
144
+ '[1:30]>[1:37](29,36)ElseBlock: {:else}',
145
+ '[1:37]>[1:40](36,39)#text: ...',
146
+ '[1:40]>[1:47](39,46)EachBlock: {/each}',
147
+ ]);
148
+ });
149
+
150
+ test('deep each statement', () => {
151
+ const r = parse('<ul>{#each a as b}<li>{#each c as d}{/each}</li>{/each}</ul>');
152
+ const map = nodeListToDebugMaps(r.nodeList);
153
+ expect(map).toStrictEqual([
154
+ '[1:1]>[1:5](0,4)ul: <ul>',
155
+ '[1:5]>[1:19](4,18)EachBlock: {#each␣a␣as␣b}',
156
+ '[1:19]>[1:23](18,22)li: <li>',
157
+ '[1:23]>[1:37](22,36)EachBlock: {#each␣c␣as␣d}',
158
+ '[1:37]>[1:44](36,43)EachBlock: {/each}',
159
+ '[1:44]>[1:49](43,48)li: </li>',
160
+ '[1:49]>[1:56](48,55)EachBlock: {/each}',
161
+ '[1:56]>[1:61](55,60)ul: </ul>',
162
+ ]);
163
+ });
164
+
165
+ test('await then catch statement', () => {
166
+ const r = parse('{#await expression}...{:then name}...{:catch name}...{/await}');
167
+ const map = nodeListToDebugMaps(r.nodeList);
168
+ expect(map).toStrictEqual([
169
+ '[1:1]>[1:20](0,19)PendingBlock: {#await␣expression}',
170
+ '[1:20]>[1:23](19,22)#text: ...',
171
+ '[1:23]>[1:35](22,34)ThenBlock: {:then␣name}',
172
+ '[1:35]>[1:38](34,37)#text: ...',
173
+ '[1:38]>[1:51](37,50)CatchBlock: {:catch␣name}',
174
+ '[1:51]>[1:54](50,53)#text: ...',
175
+ '[1:54]>[1:62](53,61)AwaitBlock: {/await}',
176
+ ]);
177
+ });
178
+
179
+ test('attribute', () => {
180
+ const r = parse('<el attr-name="value" />');
181
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
182
+ expect(attr).toStrictEqual([
183
+ [
184
+ '[1:5]>[1:22](4,21)attr-name: attr-name="value"',
185
+ ' [1:5]>[1:5](4,4)bN: ',
186
+ ' [1:5]>[1:14](4,13)name: attr-name',
187
+ ' [1:14]>[1:14](13,13)bE: ',
188
+ ' [1:14]>[1:15](13,14)equal: =',
189
+ ' [1:15]>[1:15](14,14)aE: ',
190
+ ' [1:15]>[1:16](14,15)sQ: "',
191
+ ' [1:16]>[1:21](15,20)value: value',
192
+ ' [1:21]>[1:22](20,21)eQ: "',
193
+ ' isDirective: false',
194
+ ' isDynamicValue: false',
195
+ ],
196
+ ]);
197
+ });
198
+
199
+ test('event directive', () => {
200
+ const r = parse('<el on:eventname={ `abc${def}ghi` } />');
201
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
202
+ expect(attr).toStrictEqual([
203
+ [
204
+ '[1:5]>[1:36](4,35)on:eventname: on:eventname={␣`abc${def}ghi`␣}',
205
+ ' [1:5]>[1:5](4,4)bN: ',
206
+ ' [1:5]>[1:17](4,16)name: on:eventname',
207
+ ' [1:17]>[1:17](16,16)bE: ',
208
+ ' [1:17]>[1:18](16,17)equal: =',
209
+ ' [1:18]>[1:18](17,17)aE: ',
210
+ ' [1:18]>[1:19](17,18)sQ: {',
211
+ ' [1:19]>[1:35](18,34)value: ␣`abc${def}ghi`␣',
212
+ ' [1:35]>[1:36](34,35)eQ: }',
213
+ ' isDirective: true',
214
+ ' isDynamicValue: false',
215
+ ],
216
+ ]);
217
+ });
218
+
219
+ test('event directive 2', () => {
220
+ const r = parse('<el on:eventname|modifiers = {handler} />');
221
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
222
+ expect(attr).toStrictEqual([
223
+ [
224
+ '[1:5]>[1:39](4,38)on:eventname|modifiers: on:eventname|modifiers␣=␣{handler}',
225
+ ' [1:5]>[1:5](4,4)bN: ',
226
+ ' [1:5]>[1:27](4,26)name: on:eventname|modifiers',
227
+ ' [1:27]>[1:28](26,27)bE: ␣',
228
+ ' [1:28]>[1:29](27,28)equal: =',
229
+ ' [1:29]>[1:30](28,29)aE: ␣',
230
+ ' [1:30]>[1:31](29,30)sQ: {',
231
+ ' [1:31]>[1:38](30,37)value: handler',
232
+ ' [1:38]>[1:39](37,38)eQ: }',
233
+ ' isDirective: true',
234
+ ' isDynamicValue: false',
235
+ ],
236
+ ]);
237
+ });
238
+
239
+ test('event directive 3', () => {
240
+ const r = parse('<el on:eventname />');
241
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
242
+ expect(attr).toStrictEqual([
243
+ [
244
+ '[1:5]>[1:17](4,16)on:eventname: on:eventname',
245
+ ' [1:5]>[1:5](4,4)bN: ',
246
+ ' [1:5]>[1:17](4,16)name: on:eventname',
247
+ ' [1:17]>[1:17](16,16)bE: ',
248
+ ' [1:17]>[1:17](16,16)equal: ',
249
+ ' [1:17]>[1:17](16,16)aE: ',
250
+ ' [1:17]>[1:17](16,16)sQ: ',
251
+ ' [1:17]>[1:17](16,16)value: ',
252
+ ' [1:17]>[1:17](16,16)eQ: ',
253
+ ' isDirective: true',
254
+ ' isDynamicValue: false',
255
+ ],
256
+ ]);
257
+ });
258
+
259
+ test('bind directive', () => {
260
+ const r = parse('<el bind:property={variable} />');
261
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
262
+ expect(attr).toStrictEqual([
263
+ [
264
+ '[1:5]>[1:29](4,28)property: bind:property={variable}',
265
+ ' [1:5]>[1:5](4,4)bN: ',
266
+ ' [1:5]>[1:18](4,17)name: bind:property',
267
+ ' [1:18]>[1:18](17,17)bE: ',
268
+ ' [1:18]>[1:19](17,18)equal: =',
269
+ ' [1:19]>[1:19](18,18)aE: ',
270
+ ' [1:19]>[1:20](18,19)sQ: {',
271
+ ' [1:20]>[1:28](19,27)value: variable',
272
+ ' [1:28]>[1:29](27,28)eQ: }',
273
+ ' isDirective: false',
274
+ ' isDynamicValue: true',
275
+ ' potentialName: property',
276
+ ],
277
+ ]);
278
+ });
279
+
280
+ test('bind directive 2', () => {
281
+ const r = parse('<el bind:property />');
282
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
283
+ expect(attr).toStrictEqual([
284
+ [
285
+ '[1:5]>[1:18](4,17)property: bind:property',
286
+ ' [1:5]>[1:5](4,4)bN: ',
287
+ ' [1:5]>[1:18](4,17)name: bind:property',
288
+ ' [1:18]>[1:18](17,17)bE: ',
289
+ ' [1:18]>[1:18](17,17)equal: ',
290
+ ' [1:18]>[1:18](17,17)aE: ',
291
+ ' [1:18]>[1:18](17,17)sQ: ',
292
+ ' [1:18]>[1:18](17,17)value: ',
293
+ ' [1:18]>[1:18](17,17)eQ: ',
294
+ ' isDirective: false',
295
+ ' isDynamicValue: true',
296
+ ' potentialName: property',
297
+ ],
298
+ ]);
299
+ });
300
+
301
+ test('other directives', () => {
302
+ const r = parse(`<el
303
+ class:name={value}
304
+ use:action={parameters}
305
+ transition:fn
306
+ transition:fn2={params}
307
+ transition:fn3|local
308
+ transition:fn4|local={params}
309
+ transition:fade="{{ duration: 2000 }}"
310
+ in:whoosh
311
+ in:fn
312
+ in:fn2={params}
313
+ in:fn3|local
314
+ in:fn4|local={params}
315
+ out:fn
316
+ out:fn2={params}
317
+ out:fn3|local
318
+ out:fn4|local={params}
319
+ animate:name
320
+ animate:name2={params}
321
+ />`);
322
+ const attrs = r.nodeList[0].attributes;
323
+ expect(attrs.every(attr => (attr.type === 'html-attr' ? attr.isDirective : false))).toBe(true);
324
+ });
325
+
326
+ test('multiple class directives', () => {
327
+ const r = parse('<el class:selected="{isSelected}" class:focused="{isFocused}" />');
328
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
329
+ expect(attr).toStrictEqual([
330
+ [
331
+ '[1:5]>[1:34](4,33)class: class:selected="{isSelected}"',
332
+ ' [1:5]>[1:5](4,4)bN: ',
333
+ ' [1:5]>[1:19](4,18)name: class:selected',
334
+ ' [1:19]>[1:19](18,18)bE: ',
335
+ ' [1:19]>[1:20](18,19)equal: =',
336
+ ' [1:20]>[1:20](19,19)aE: ',
337
+ ' [1:20]>[1:21](19,20)sQ: "',
338
+ ' [1:21]>[1:33](20,32)value: {isSelected}',
339
+ ' [1:33]>[1:34](32,33)eQ: "',
340
+ ' isDirective: true',
341
+ ' isDynamicValue: true',
342
+ ' potentialName: class',
343
+ ],
344
+ [
345
+ '[1:35]>[1:62](34,61)class: class:focused="{isFocused}"',
346
+ ' [1:35]>[1:35](34,34)bN: ',
347
+ ' [1:35]>[1:48](34,47)name: class:focused',
348
+ ' [1:48]>[1:48](47,47)bE: ',
349
+ ' [1:48]>[1:49](47,48)equal: =',
350
+ ' [1:49]>[1:49](48,48)aE: ',
351
+ ' [1:49]>[1:50](48,49)sQ: "',
352
+ ' [1:50]>[1:61](49,60)value: {isFocused}',
353
+ ' [1:61]>[1:62](60,61)eQ: "',
354
+ ' isDirective: true',
355
+ ' isDynamicValue: true',
356
+ ' potentialName: class',
357
+ ],
358
+ ]);
359
+ });
360
+
361
+ test('shorthand', () => {
362
+ const r = parse('<el {items} />');
363
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
364
+ expect(attr).toStrictEqual([
365
+ [
366
+ '[1:5]>[1:12](4,11)items: {items}',
367
+ ' [1:5]>[1:5](4,4)bN: ',
368
+ ' [1:5]>[1:5](4,4)name: ',
369
+ ' [1:5]>[1:5](4,4)bE: ',
370
+ ' [1:5]>[1:5](4,4)equal: ',
371
+ ' [1:5]>[1:5](4,4)aE: ',
372
+ ' [1:5]>[1:6](4,5)sQ: {',
373
+ ' [1:6]>[1:11](5,10)value: items',
374
+ ' [1:11]>[1:12](10,11)eQ: }',
375
+ ' isDirective: false',
376
+ ' isDynamicValue: true',
377
+ ' potentialName: items',
378
+ ],
379
+ ]);
380
+ });
381
+
382
+ test('spread attributes', () => {
383
+ const r = parse('<el { ... attrs} />');
384
+ const attr = attributesToDebugMaps(r.nodeList[0].attributes);
385
+ expect(attr).toStrictEqual([]);
386
+ // @ts-ignore
387
+ expect(r.nodeList[0].hasSpreadAttr).toBeTruthy();
388
+ });
389
+
390
+ test('namespace', () => {
391
+ const doc = parse('<div><svg><text /></svg></div>');
392
+ expect(doc.nodeList[0].nodeName).toBe('div');
393
+ expect(doc.nodeList[0].namespace).toBe('http://www.w3.org/1999/xhtml');
394
+ expect(doc.nodeList[1].nodeName).toBe('svg');
395
+ expect(doc.nodeList[1].namespace).toBe('http://www.w3.org/2000/svg');
396
+ expect(doc.nodeList[2].nodeName).toBe('text');
397
+ expect(doc.nodeList[2].namespace).toBe('http://www.w3.org/2000/svg');
398
+ });
399
+ });
400
+
401
+ describe('Issues', () => {
402
+ test('#444', () => {
403
+ parse(`<script lang="ts">
404
+ let count= 0
405
+ const increment = () => {
406
+ count += 1
407
+ }
408
+ </script>
409
+
410
+ <button on:click={increment}>
411
+ Clicks: {count}
412
+ </button>`);
413
+ // No Error is success
414
+ });
415
+
416
+ test('#503', () => {
417
+ const r = parse(`<nav>
418
+ <ul>
419
+ <li>
420
+ <p>test</p>
421
+ <ul>
422
+ <li>
423
+ <p>test</p>
424
+ <ul>
425
+ <li>test</li>
426
+ </ul>
427
+ </li>
428
+ <li>test</li>
429
+ </ul>
430
+ </li>
431
+ </ul>
432
+ </nav>`);
433
+ const map = nodeListToDebugMaps(r.nodeList);
434
+ expect(map).toStrictEqual([
435
+ '[1:1]>[1:6](0,5)nav: <nav>',
436
+ '[1:6]>[2:2](5,7)#text: ⏎→',
437
+ '[2:2]>[2:6](7,11)ul: <ul>',
438
+ '[2:6]>[3:3](11,14)#text: ⏎→→',
439
+ '[3:3]>[3:7](14,18)li: <li>',
440
+ '[3:7]>[4:4](18,22)#text: ⏎→→→',
441
+ '[4:4]>[4:7](22,25)p: <p>',
442
+ '[4:7]>[4:11](25,29)#text: test',
443
+ '[4:11]>[4:15](29,33)p: </p>',
444
+ '[4:15]>[5:4](33,37)#text: ⏎→→→',
445
+ '[5:4]>[5:8](37,41)ul: <ul>',
446
+ '[5:8]>[6:5](41,46)#text: ⏎→→→→',
447
+ '[6:5]>[6:9](46,50)li: <li>',
448
+ '[6:9]>[7:6](50,56)#text: ⏎→→→→→',
449
+ '[7:6]>[7:9](56,59)p: <p>',
450
+ '[7:9]>[7:13](59,63)#text: test',
451
+ '[7:13]>[7:17](63,67)p: </p>',
452
+ '[7:17]>[8:6](67,73)#text: ⏎→→→→→',
453
+ '[8:6]>[8:10](73,77)ul: <ul>',
454
+ '[8:10]>[9:7](77,84)#text: ⏎→→→→→→',
455
+ '[9:7]>[9:11](84,88)li: <li>',
456
+ '[9:11]>[9:15](88,92)#text: test',
457
+ '[9:15]>[9:20](92,97)li: </li>',
458
+ '[9:20]>[10:6](97,103)#text: ⏎→→→→→',
459
+ '[10:6]>[10:11](103,108)ul: </ul>',
460
+ '[10:11]>[11:5](108,113)#text: ⏎→→→→',
461
+ '[11:5]>[11:10](113,118)li: </li>',
462
+ '[11:10]>[12:5](118,123)#text: ⏎→→→→',
463
+ '[12:5]>[12:9](123,127)li: <li>',
464
+ '[12:9]>[12:13](127,131)#text: test',
465
+ '[12:13]>[12:18](131,136)li: </li>',
466
+ '[12:18]>[13:4](136,140)#text: ⏎→→→',
467
+ '[13:4]>[13:9](140,145)ul: </ul>',
468
+ '[13:9]>[14:3](145,148)#text: ⏎→→',
469
+ '[14:3]>[14:8](148,153)li: </li>',
470
+ '[14:8]>[15:2](153,155)#text: ⏎→',
471
+ '[15:2]>[15:7](155,160)ul: </ul>',
472
+ '[15:7]>[16:1](160,161)#text: ⏎',
473
+ '[16:1]>[16:7](161,167)nav: </nav>',
474
+ ]);
475
+ });
476
+
477
+ test('#698', () => {
478
+ const r = parse(`<ul>
479
+ {#if cond === valueA}
480
+ <li>A</li>
481
+ {:else if cond === valueB}
482
+ <li>B</li>
483
+ {/if}
484
+ </ul>`);
485
+ const map = nodeListToDebugMaps(r.nodeList);
486
+ expect(map).toStrictEqual([
487
+ '[1:1]>[1:5](0,4)ul: <ul>',
488
+ '[1:5]>[2:2](4,6)#text: ⏎→',
489
+ '[2:2]>[3:3](6,30)IfBlock: {#if␣cond␣===␣valueA}⏎→→',
490
+ '[3:3]>[3:7](30,34)li: <li>',
491
+ '[3:7]>[3:8](34,35)#text: A',
492
+ '[3:8]>[3:13](35,40)li: </li>',
493
+ '[3:13]>[5:3](40,71)ElseIfBlock: ⏎→{:else␣if␣cond␣===␣valueB}⏎→→',
494
+ '[5:3]>[5:7](71,75)li: <li>',
495
+ '[5:7]>[5:8](75,76)#text: B',
496
+ '[5:8]>[5:13](76,81)li: </li>',
497
+ '[5:13]>[6:2](81,83)#text: ⏎→',
498
+ '[6:2]>[6:7](83,88)IfBlock: {/if}',
499
+ '[6:7]>[7:1](88,89)#text: ⏎',
500
+ '[7:1]>[7:6](89,94)ul: </ul>',
501
+ ]);
502
+
503
+ expect(r.nodeList[0].childNodes[4].raw).toBe('{/if}');
504
+ expect(r.nodeList[11].raw).toBe('{/if}');
505
+ expect(r.nodeList[0].childNodes[4].raw).toBe(r.nodeList[11].raw);
506
+ expect(r.nodeList[0].childNodes[4].uuid).toBe(r.nodeList[11].uuid);
507
+ });
508
+ });
@@ -0,0 +1,267 @@
1
+ const { parseCtrlBlock } = require('../lib/parse-ctrl-block');
2
+ const svelteParse = require('../lib/svelte-parser').default;
3
+
4
+ describe('parser', () => {
5
+ test('if', () => {
6
+ const raw = '{#if cond}text{/if}';
7
+ const ast = svelteParse(raw);
8
+ expect(parseCtrlBlock('if', ast[0], raw, raw, 0, null, null, null)).toEqual([
9
+ expect.objectContaining({
10
+ type: 'psblock',
11
+ startOffset: 0,
12
+ endOffset: 10,
13
+ startLine: 1,
14
+ endLine: 1,
15
+ startCol: 1,
16
+ endCol: 11,
17
+ raw: '{#if cond}',
18
+ nodeName: 'IfBlock',
19
+ parentNode: null,
20
+ prevNode: null,
21
+ nextNode: null,
22
+ childNodes: [
23
+ expect.objectContaining({
24
+ type: 'text',
25
+ startOffset: 10,
26
+ endOffset: 14,
27
+ startLine: 1,
28
+ endLine: 1,
29
+ startCol: 11,
30
+ endCol: 15,
31
+ raw: 'text',
32
+ nodeName: '#text',
33
+ // parentNode: [Circular],
34
+ prevNode: null,
35
+ nextNode: null,
36
+ isFragment: false,
37
+ isGhost: false,
38
+ }),
39
+ ],
40
+ isFragment: false,
41
+ isGhost: false,
42
+ }),
43
+ expect.objectContaining({
44
+ type: 'psblock',
45
+ startOffset: 14,
46
+ endOffset: 19,
47
+ startLine: 1,
48
+ endLine: 1,
49
+ startCol: 15,
50
+ endCol: 20,
51
+ raw: '{/if}',
52
+ nodeName: 'IfBlock',
53
+ parentNode: null,
54
+ prevNode: null,
55
+ nextNode: null,
56
+ isFragment: false,
57
+ isGhost: false,
58
+ }),
59
+ ]);
60
+ });
61
+
62
+ test('if else', () => {
63
+ const raw = '{#if cond}text1{:else}text2{/if}';
64
+ const ast = svelteParse(raw);
65
+ expect(parseCtrlBlock('if', ast[0], raw, raw, 0, null, null, null)).toEqual([
66
+ expect.objectContaining({
67
+ type: 'psblock',
68
+ startOffset: 0,
69
+ endOffset: 10,
70
+ startLine: 1,
71
+ endLine: 1,
72
+ startCol: 1,
73
+ endCol: 11,
74
+ raw: '{#if cond}',
75
+ nodeName: 'IfBlock',
76
+ parentNode: null,
77
+ prevNode: null,
78
+ nextNode: null,
79
+ childNodes: [
80
+ expect.objectContaining({
81
+ type: 'text',
82
+ startOffset: 10,
83
+ endOffset: 15,
84
+ startLine: 1,
85
+ endLine: 1,
86
+ startCol: 11,
87
+ endCol: 16,
88
+ raw: 'text1',
89
+ nodeName: '#text',
90
+ // parentNode: [Circular],
91
+ prevNode: null,
92
+ nextNode: null,
93
+ isFragment: false,
94
+ isGhost: false,
95
+ }),
96
+ ],
97
+ isFragment: false,
98
+ isGhost: false,
99
+ }),
100
+ expect.objectContaining({
101
+ type: 'psblock',
102
+ startOffset: 15,
103
+ endOffset: 22,
104
+ startLine: 1,
105
+ endLine: 1,
106
+ startCol: 16,
107
+ endCol: 23,
108
+ raw: '{:else}',
109
+ nodeName: 'ElseBlock',
110
+ parentNode: null,
111
+ prevNode: null,
112
+ nextNode: null,
113
+ childNodes: [
114
+ expect.objectContaining({
115
+ type: 'text',
116
+ startOffset: 22,
117
+ endOffset: 27,
118
+ startLine: 1,
119
+ endLine: 1,
120
+ startCol: 23,
121
+ endCol: 28,
122
+ raw: 'text2',
123
+ nodeName: '#text',
124
+ // parentNode: [Circular],
125
+ prevNode: null,
126
+ nextNode: null,
127
+ isFragment: false,
128
+ isGhost: false,
129
+ }),
130
+ ],
131
+ isFragment: false,
132
+ isGhost: false,
133
+ }),
134
+ expect.objectContaining({
135
+ type: 'psblock',
136
+ startOffset: 27,
137
+ endOffset: 32,
138
+ startLine: 1,
139
+ endLine: 1,
140
+ startCol: 28,
141
+ endCol: 33,
142
+ raw: '{/if}',
143
+ nodeName: 'IfBlock',
144
+ parentNode: null,
145
+ prevNode: null,
146
+ nextNode: null,
147
+ isFragment: false,
148
+ isGhost: false,
149
+ }),
150
+ ]);
151
+ });
152
+
153
+ test('if else if', () => {
154
+ const raw = '{#if cond}text1{:else if cond}text2{/if}';
155
+ const ast = svelteParse(raw);
156
+ const ast2 = parseCtrlBlock('if', ast[0], raw, raw, 0, null, null, null);
157
+ expect(ast2).toEqual([
158
+ expect.objectContaining({
159
+ type: 'psblock',
160
+ startOffset: 0,
161
+ endOffset: 10,
162
+ startLine: 1,
163
+ endLine: 1,
164
+ startCol: 1,
165
+ endCol: 11,
166
+ raw: '{#if cond}',
167
+ nodeName: 'IfBlock',
168
+ parentNode: null,
169
+ prevNode: null,
170
+ nextNode: null,
171
+ childNodes: [
172
+ expect.objectContaining({
173
+ type: 'text',
174
+ startOffset: 10,
175
+ endOffset: 15,
176
+ startLine: 1,
177
+ endLine: 1,
178
+ startCol: 11,
179
+ endCol: 16,
180
+ raw: 'text1',
181
+ nodeName: '#text',
182
+ // parentNode: [Circular],
183
+ prevNode: null,
184
+ nextNode: null,
185
+ isFragment: false,
186
+ isGhost: false,
187
+ }),
188
+ ],
189
+ isFragment: false,
190
+ isGhost: false,
191
+ }),
192
+ expect.objectContaining({
193
+ type: 'psblock',
194
+ startOffset: 15,
195
+ endOffset: 30,
196
+ startLine: 1,
197
+ endLine: 1,
198
+ startCol: 16,
199
+ endCol: 31,
200
+ raw: '{:else if cond}',
201
+ nodeName: 'ElseIfBlock',
202
+ parentNode: null,
203
+ prevNode: null,
204
+ nextNode: null,
205
+ childNodes: [
206
+ expect.objectContaining({
207
+ type: 'text',
208
+ startOffset: 30,
209
+ endOffset: 35,
210
+ startLine: 1,
211
+ endLine: 1,
212
+ startCol: 31,
213
+ endCol: 36,
214
+ raw: 'text2',
215
+ nodeName: '#text',
216
+ // parentNode: [Circular],
217
+ prevNode: null,
218
+ nextNode: null,
219
+ isFragment: false,
220
+ isGhost: false,
221
+ }),
222
+ ],
223
+ isFragment: false,
224
+ isGhost: false,
225
+ }),
226
+ expect.objectContaining({
227
+ type: 'psblock',
228
+ startOffset: 35,
229
+ endOffset: 40,
230
+ startLine: 1,
231
+ endLine: 1,
232
+ startCol: 36,
233
+ endCol: 41,
234
+ raw: '{/if}',
235
+ nodeName: 'IfBlock',
236
+ parentNode: null,
237
+ prevNode: null,
238
+ nextNode: null,
239
+ isFragment: false,
240
+ isGhost: false,
241
+ }),
242
+ ]);
243
+ });
244
+
245
+ test('if else if else if else if...', () => {
246
+ const raw =
247
+ '{#if cond}text1{:else if cond}text2{:else if cond}text3{:else if cond}text4{:else if cond}text5{/if}';
248
+ const ast = svelteParse(raw);
249
+ const ast2 = parseCtrlBlock('if', ast[0], raw, raw, 0, null, null, null);
250
+ expect(ast2.length).toBe(6);
251
+ });
252
+
253
+ test('white spaces and line breaks', () => {
254
+ const raw = ` {#if cond}
255
+ text1
256
+ {:else if cond}
257
+ text2
258
+ {:else if cond}
259
+ text3
260
+ {/if}
261
+
262
+ `;
263
+ const ast = svelteParse(raw);
264
+ const ast2 = parseCtrlBlock('if', ast[0], raw, raw, 0, null, null, null);
265
+ expect(ast2.length).toBe(4);
266
+ });
267
+ });
@@ -0,0 +1,109 @@
1
+ const svelteParse = require('../../lib/svelte-parser').default;
2
+
3
+ describe('parser', () => {
4
+ test('if', () => {
5
+ expect(svelteParse('{#if cond}text{/if}')).toEqual([
6
+ {
7
+ type: 'IfBlock',
8
+ start: 0,
9
+ end: 19,
10
+ expression: {
11
+ end: 9,
12
+ loc: { end: { column: 9, line: 1 }, start: { column: 5, line: 1 } },
13
+ name: 'cond',
14
+ start: 5,
15
+ type: 'Identifier',
16
+ },
17
+ children: [{ data: 'text', end: 14, raw: 'text', start: 10, type: 'Text' }],
18
+ },
19
+ ]);
20
+ });
21
+
22
+ test('if else', () => {
23
+ expect(svelteParse('{#if cond}text1{:else}text2{/if}')).toEqual([
24
+ {
25
+ type: 'IfBlock',
26
+ start: 0,
27
+ end: 32,
28
+ expression: {
29
+ end: 9,
30
+ loc: { end: { column: 9, line: 1 }, start: { column: 5, line: 1 } },
31
+ name: 'cond',
32
+ start: 5,
33
+ type: 'Identifier',
34
+ },
35
+ children: [{ data: 'text1', end: 15, raw: 'text1', start: 10, type: 'Text' }],
36
+ else: {
37
+ children: [{ data: 'text2', end: 27, raw: 'text2', start: 22, type: 'Text' }],
38
+ end: 27,
39
+ start: 22,
40
+ type: 'ElseBlock',
41
+ },
42
+ },
43
+ ]);
44
+ });
45
+
46
+ test('if else if', () => {
47
+ expect(svelteParse('{#if cond}text1{:else if cond}text2{/if}')).toEqual([
48
+ {
49
+ type: 'IfBlock',
50
+ start: 0,
51
+ end: 40,
52
+ expression: {
53
+ end: 9,
54
+ loc: { end: { column: 9, line: 1 }, start: { column: 5, line: 1 } },
55
+ name: 'cond',
56
+ start: 5,
57
+ type: 'Identifier',
58
+ },
59
+ children: [{ data: 'text1', end: 15, raw: 'text1', start: 10, type: 'Text' }],
60
+ else: {
61
+ type: 'ElseBlock',
62
+ start: 30,
63
+ end: 35,
64
+ children: [
65
+ {
66
+ type: 'IfBlock',
67
+ start: 30,
68
+ end: 40,
69
+ elseif: true,
70
+ children: [{ data: 'text2', end: 35, raw: 'text2', start: 30, type: 'Text' }],
71
+ expression: {
72
+ end: 29,
73
+ loc: { end: { column: 29, line: 1 }, start: { column: 25, line: 1 } },
74
+ name: 'cond',
75
+ start: 25,
76
+ type: 'Identifier',
77
+ },
78
+ },
79
+ ],
80
+ },
81
+ },
82
+ ]);
83
+ });
84
+
85
+ test('white spaces with if', () => {
86
+ expect(svelteParse('text1 {#if cond} text2 {/if} ')).toEqual([
87
+ {
88
+ data: 'text1 ',
89
+ end: 7,
90
+ raw: 'text1 ',
91
+ start: 0,
92
+ type: 'Text',
93
+ },
94
+ {
95
+ type: 'IfBlock',
96
+ start: 7,
97
+ end: 31,
98
+ expression: {
99
+ end: 16,
100
+ loc: { end: { column: 16, line: 1 }, start: { column: 12, line: 1 } },
101
+ name: 'cond',
102
+ start: 12,
103
+ type: 'Identifier',
104
+ },
105
+ children: [{ data: 'text2', end: 26, raw: ' text2 ', start: 17, type: 'Text' }],
106
+ },
107
+ ]);
108
+ });
109
+ });