@markuplint/svelte-parser 4.6.2 → 4.7.0-alpha.1

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,8 @@
1
+ /// <reference types="svelte" />
2
+ import type { SvelteParser } from './parser.js';
3
+ import type { ChildToken, Token } from '@markuplint/parser-utils';
4
+ import type { Block } from 'svelte/compiler';
5
+ export declare function parseBlock(parser: SvelteParser, token: ChildToken, originBlockNode: Block): {
6
+ openToken: Token;
7
+ closeToken: Token;
8
+ };
@@ -0,0 +1,47 @@
1
+ export function parseBlock(
2
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
3
+ parser, token,
4
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
5
+ originBlockNode) {
6
+ const range = token.raw;
7
+ /**
8
+ * `{#xxx}...{:xxx}...{/xxx}`
9
+ * find___^
10
+ */
11
+ // eslint-disable-next-line regexp/strict
12
+ const eachCloseStartIndex = range.match(/{\s*\/[a-z]+\s*}$/)?.index;
13
+ if (eachCloseStartIndex == null) {
14
+ throw new SyntaxError('Block close tag not found');
15
+ }
16
+ /**
17
+ * `{/xxx}`
18
+ */
19
+ const closeToken = parser.sliceFragment(token.startOffset + eachCloseStartIndex, originBlockNode.end);
20
+ const fragment = originBlockNode.type === 'IfBlock'
21
+ ? originBlockNode.consequent.nodes
22
+ : originBlockNode.type === 'AwaitBlock'
23
+ ? originBlockNode.pending?.nodes
24
+ : originBlockNode.type === 'KeyBlock'
25
+ ? originBlockNode.fragment.nodes
26
+ : originBlockNode.body.nodes;
27
+ const fragStart = fragment?.at(0)?.start;
28
+ const fragEnd = fragment?.at(-1)?.end;
29
+ /**
30
+ * This token does not guarantee an open tag.
31
+ * For example, it might include `:then` or `:else`.
32
+ * Therefore, this variable is not used in
33
+ * `EachBlock` or `AwaitBlock`.
34
+ * It is only used in `KeyBlock` and `SnippetBlock`.
35
+ */
36
+ let openToken;
37
+ if (fragStart != null && fragEnd != null) {
38
+ openToken = parser.sliceFragment(token.startOffset, fragStart);
39
+ }
40
+ else {
41
+ openToken = parser.sliceFragment(token.startOffset, eachCloseStartIndex);
42
+ }
43
+ return {
44
+ openToken,
45
+ closeToken,
46
+ };
47
+ }
@@ -0,0 +1,8 @@
1
+ /// <reference types="svelte" />
2
+ import type { SvelteParser } from './parser.js';
3
+ import type { ChildToken, Token } from '@markuplint/parser-utils';
4
+ import type { Block } from 'svelte/compiler';
5
+ export declare function parseBlock(parser: SvelteParser, token: ChildToken, originBlockNode: Block): {
6
+ openToken: Token;
7
+ closeToken: Token;
8
+ };
@@ -0,0 +1,47 @@
1
+ export function parseBlock(
2
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
3
+ parser, token,
4
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
5
+ originBlockNode) {
6
+ const range = token.raw;
7
+ /**
8
+ * `{#xxx}...{:xxx}...{/xxx}`
9
+ * find___^
10
+ */
11
+ // eslint-disable-next-line regexp/strict
12
+ const eachCloseStartIndex = range.match(/{\s*\/[a-z]+\s*}$/)?.index;
13
+ if (eachCloseStartIndex == null) {
14
+ throw new SyntaxError('Block close tag not found');
15
+ }
16
+ /**
17
+ * `{/xxx}`
18
+ */
19
+ const closeToken = parser.sliceFragment(token.startOffset + eachCloseStartIndex, originBlockNode.end);
20
+ const fragment = originBlockNode.type === 'IfBlock'
21
+ ? originBlockNode.consequent.nodes
22
+ : originBlockNode.type === 'AwaitBlock'
23
+ ? originBlockNode.pending?.nodes
24
+ : originBlockNode.type === 'KeyBlock'
25
+ ? originBlockNode.fragment.nodes
26
+ : originBlockNode.body.nodes;
27
+ const fragStart = fragment?.at(0)?.start;
28
+ const fragEnd = fragment?.at(-1)?.end;
29
+ /**
30
+ * This token does not guarantee an open tag.
31
+ * For example, it might include `:then` or `:else`.
32
+ * Therefore, this variable is not used in
33
+ * `EachBlock` or `AwaitBlock`.
34
+ * It is only used in `KeyBlock` and `SnippetBlock`.
35
+ */
36
+ let openToken;
37
+ if (fragStart != null && fragEnd != null) {
38
+ openToken = parser.sliceFragment(token.startOffset, fragStart);
39
+ }
40
+ else {
41
+ openToken = parser.sliceFragment(token.startOffset, eachCloseStartIndex);
42
+ }
43
+ return {
44
+ openToken,
45
+ closeToken,
46
+ };
47
+ }
package/lib/parser.d.ts CHANGED
@@ -1,14 +1,13 @@
1
- /// <reference types="svelte" />
2
1
  import type { SvelteNode } from './svelte-parser/index.js';
3
2
  import type { MLASTParentNode, MLASTPreprocessorSpecificBlock, MLASTPreprocessorSpecificBlockConditionalType } from '@markuplint/ml-ast';
4
3
  import type { ChildToken, ParseOptions, Token } from '@markuplint/parser-utils';
5
4
  import { ParserError, Parser } from '@markuplint/parser-utils';
6
- declare class SvelteParser extends Parser<SvelteNode> {
5
+ export declare class SvelteParser extends Parser<SvelteNode> {
7
6
  #private;
8
7
  readonly specificBindDirective: ReadonlySet<string>;
9
8
  constructor();
10
9
  tokenize(): {
11
- ast: import("svelte/types/compiler/interfaces").TemplateNode[];
10
+ ast: SvelteNode[];
12
11
  isFragment: boolean;
13
12
  };
14
13
  parse(raw: string, options?: ParseOptions): import("@markuplint/ml-ast").MLASTDocument;
@@ -57,7 +56,5 @@ declare class SvelteParser extends Parser<SvelteNode> {
57
56
  * @returns
58
57
  */
59
58
  detectElementType(nodeName: string): import("@markuplint/ml-ast").ElementType;
60
- visitExpression(token: ChildToken, originBlockNode: SvelteNode): MLASTPreprocessorSpecificBlock[];
61
59
  }
62
60
  export declare const parser: SvelteParser;
63
- export {};
package/lib/parser.js CHANGED
@@ -3,11 +3,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
3
3
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
5
  };
6
- var _SvelteParser_instances, _SvelteParser_traverseIfBlock;
6
+ var _SvelteParser_instances, _SvelteParser_parseAwaitBlock, _SvelteParser_parseEachBlock, _SvelteParser_traverseIfBlock;
7
7
  import { getNamespace } from '@markuplint/html-parser';
8
8
  import { ParserError, Parser, AttrState } from '@markuplint/parser-utils';
9
- import { svelteParse } from './svelte-parser/index.js';
10
- class SvelteParser extends Parser {
9
+ import { parseBlock } from './parse-block.js';
10
+ import { blockOrTags, svelteParse } from './svelte-parser/index.js';
11
+ export class SvelteParser extends Parser {
11
12
  constructor() {
12
13
  super({
13
14
  endTagType: 'xml',
@@ -52,8 +53,21 @@ class SvelteParser extends Parser {
52
53
  nodeize(
53
54
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
54
55
  originNode, parentNode, depth) {
55
- const token = this.sliceFragment(originNode.start, originNode.end);
56
+ let token = this.sliceFragment(originNode.start, originNode.end);
56
57
  const parentNamespace = parentNode && 'namespace' in parentNode ? parentNode.namespace : 'http://www.w3.org/1999/xhtml';
58
+ /**
59
+ * Temporarily correct location shift issue with the new parser.
60
+ */
61
+ if (blockOrTags.includes(originNode.type) && !token.raw.startsWith('{')) {
62
+ let start = token.startOffset;
63
+ while (this.rawCode[start] !== '{') {
64
+ start--;
65
+ }
66
+ token = {
67
+ ...token,
68
+ ...this.sliceFragment(start, originNode.end),
69
+ };
70
+ }
57
71
  switch (originNode.type) {
58
72
  case 'Text': {
59
73
  return this.visitText({
@@ -69,17 +83,17 @@ class SvelteParser extends Parser {
69
83
  parentNode,
70
84
  });
71
85
  }
72
- case 'MustacheTag': {
86
+ case 'ExpressionTag': {
73
87
  return this.visitPsBlock({
74
88
  ...token,
75
89
  depth,
76
90
  parentNode,
77
- nodeName: 'MustacheTag',
91
+ nodeName: 'ExpressionTag',
78
92
  });
79
93
  }
80
- case 'InlineComponent':
81
- case 'Element': {
82
- const children = originNode.children ?? [];
94
+ case 'Component':
95
+ case 'RegularElement': {
96
+ const children = originNode.fragment.nodes ?? [];
83
97
  const reEndTag = new RegExp(`</${originNode.name}\\s*>$`, 'i');
84
98
  const startTagEndOffset = children.length > 0
85
99
  ? children[0]?.start ?? 0
@@ -91,7 +105,7 @@ class SvelteParser extends Parser {
91
105
  parentNode,
92
106
  nodeName: originNode.name,
93
107
  namespace: getNamespace(originNode.name, parentNamespace),
94
- }, originNode.children, {
108
+ }, originNode.fragment.nodes, {
95
109
  createEndTagToken: () => {
96
110
  if (!reEndTag.test(token.raw)) {
97
111
  return null;
@@ -112,12 +126,89 @@ class SvelteParser extends Parser {
112
126
  },
113
127
  });
114
128
  }
115
- default: {
116
- return this.visitExpression({
129
+ case 'IfBlock': {
130
+ const expressions = [];
131
+ const ifElseBlocks = __classPrivateFieldGet(this, _SvelteParser_instances, "m", _SvelteParser_traverseIfBlock).call(this, originNode, token.startOffset);
132
+ for (const ifElseBlock of ifElseBlocks) {
133
+ const expression = this.visitPsBlock({
134
+ ...ifElseBlock,
135
+ depth,
136
+ parentNode,
137
+ nodeName: ifElseBlock.type,
138
+ }, ifElseBlock.children, {
139
+ if: 'if',
140
+ elseif: 'if:elseif',
141
+ else: 'if:else',
142
+ '/if': 'end',
143
+ }[ifElseBlock.type])[0];
144
+ expressions.push(expression);
145
+ }
146
+ return expressions;
147
+ }
148
+ case 'EachBlock': {
149
+ return __classPrivateFieldGet(this, _SvelteParser_instances, "m", _SvelteParser_parseEachBlock).call(this, {
150
+ ...token,
151
+ depth,
152
+ parentNode,
153
+ }, originNode);
154
+ }
155
+ case 'AwaitBlock': {
156
+ return __classPrivateFieldGet(this, _SvelteParser_instances, "m", _SvelteParser_parseAwaitBlock).call(this, {
157
+ ...token,
158
+ depth,
159
+ parentNode,
160
+ }, originNode);
161
+ }
162
+ case 'KeyBlock': {
163
+ const { openToken, closeToken } = parseBlock(this, {
164
+ ...token,
165
+ depth,
166
+ parentNode,
167
+ }, originNode);
168
+ return [
169
+ this.visitPsBlock({
170
+ ...openToken,
171
+ depth,
172
+ parentNode,
173
+ nodeName: 'key',
174
+ }, originNode.fragment.nodes)[0],
175
+ this.visitPsBlock({
176
+ ...closeToken,
177
+ depth,
178
+ parentNode,
179
+ nodeName: '/key',
180
+ })[0],
181
+ ];
182
+ }
183
+ case 'SnippetBlock': {
184
+ const { openToken, closeToken } = parseBlock(this, {
117
185
  ...token,
118
186
  depth,
119
187
  parentNode,
120
188
  }, originNode);
189
+ return [
190
+ this.visitPsBlock({
191
+ ...openToken,
192
+ depth,
193
+ parentNode,
194
+ nodeName: 'snippet',
195
+ }, originNode.body.nodes)[0],
196
+ this.visitPsBlock({
197
+ ...closeToken,
198
+ depth,
199
+ parentNode,
200
+ nodeName: '/snippet',
201
+ })[0],
202
+ ];
203
+ }
204
+ default: {
205
+ const childNodes = 'fragment' in originNode ? originNode.fragment.nodes : [];
206
+ return this.visitPsBlock({
207
+ ...token,
208
+ depth,
209
+ parentNode,
210
+ nodeName: originNode.type,
211
+ }, childNodes);
121
212
  }
122
213
  }
123
214
  }
@@ -200,134 +291,199 @@ class SvelteParser extends Parser {
200
291
  detectElementType(nodeName) {
201
292
  return super.detectElementType(nodeName, /^[A-Z]|\./);
202
293
  }
203
- visitExpression(token,
204
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
205
- originBlockNode) {
206
- const props = ['', 'else', 'pending', 'then', 'catch'];
207
- const expressions = [];
208
- if (originBlockNode.type === 'IfBlock') {
209
- const ifElseBlocks = __classPrivateFieldGet(this, _SvelteParser_instances, "m", _SvelteParser_traverseIfBlock).call(this, originBlockNode);
210
- for (const ifElseBlock of ifElseBlocks) {
211
- const expression = this.visitPsBlock({
212
- ...ifElseBlock,
213
- depth: token.depth,
214
- parentNode: token.parentNode,
215
- nodeName: ifElseBlock.nodeName,
216
- }, ifElseBlock.children, {
217
- if: 'if',
218
- elseif: 'if:elseif',
219
- else: 'if:else',
220
- '/if': 'end',
221
- }[ifElseBlock.nodeName])[0];
222
- expressions.push(expression);
223
- }
224
- return expressions;
294
+ }
295
+ _SvelteParser_instances = new WeakSet(), _SvelteParser_parseAwaitBlock = function _SvelteParser_parseAwaitBlock(token,
296
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
297
+ originBlockNode) {
298
+ const { closeToken } = parseBlock(this, token, originBlockNode);
299
+ const pendingNodes = originBlockNode.pending?.nodes ?? [];
300
+ const thenNodes = originBlockNode.then?.nodes ?? [];
301
+ const pendingEnd = pendingNodes.at(-1)?.end;
302
+ const thenEnd = thenNodes.at(-1)?.end;
303
+ // @ts-ignore - new Svelte Compiler Type doesn't support `start` and `end` yet.
304
+ const awaitConditionEnd = originBlockNode.expression.end;
305
+ /**
306
+ * `{#await expression}...{:then name}...{:catch name}...{/await}`
307
+ * find___^ and cut
308
+ *
309
+ * `}...{:then name}...{:catch name}...{/await}`
310
+ */
311
+ const rawAwaitConditionBelow = this.rawCode.slice(awaitConditionEnd, originBlockNode.end);
312
+ /**
313
+ * `}...{:then name}...{:catch name}...{/await}`
314
+ * ^___find
315
+ */
316
+ const awaitExpEnd = awaitConditionEnd + rawAwaitConditionBelow.indexOf('}') + 1;
317
+ /**
318
+ * `{#await expression}`
319
+ */
320
+ const awaitExpToken = this.sliceFragment(token.startOffset, awaitExpEnd);
321
+ let thenToken = null;
322
+ /**
323
+ * `{#await expression}...{:then name}...{:catch name}...{/await}`
324
+ * find___^
325
+ */
326
+ const thenExpStart = pendingEnd ?? awaitExpEnd;
327
+ /**
328
+ * `{:then name}...{:catch name}...{/await}`
329
+ */
330
+ const rawPendingNodesBelow = this.rawCode.slice(thenExpStart, originBlockNode.end);
331
+ if (
332
+ // eslint-disable-next-line regexp/strict
333
+ /^{\s*:then[\s|}]/.test(rawPendingNodesBelow)) {
334
+ let thenExpEndCharOffset;
335
+ if (originBlockNode.value) {
336
+ const thenIdentifierEnd =
337
+ // @ts-ignore - new Svelte Compiler Type doesn't support `start` and `end` yet.
338
+ originBlockNode.value.end;
339
+ const rawThenExpCloseCharAndBelow = this.rawCode.slice(thenIdentifierEnd, originBlockNode.end);
340
+ const thenExpEndCharIndex = rawThenExpCloseCharAndBelow.indexOf('}') + 1;
341
+ thenExpEndCharOffset = thenIdentifierEnd + thenExpEndCharIndex;
225
342
  }
226
- const blockType = originBlockNode.type.toLowerCase().replace('block', '');
227
- const nodeList = new Map();
228
- for (const prop of props) {
229
- let node = (originBlockNode[prop] ?? originBlockNode);
230
- if (nodeList.has(node)) {
231
- continue;
232
- }
233
- if (node.type === 'ElseBlock' && node.children?.[0]?.elseif) {
234
- node = node.children[0];
235
- while (node != null) {
236
- if (!['IfBlock', 'ElseBlock'].includes(node.type)) {
237
- break;
238
- }
239
- const type = node.elseif ? 'if:elseif' : 'if:else';
240
- nodeList.set(node, type);
241
- node = node.else ?? node.children?.[0] ?? null;
242
- }
243
- continue;
244
- }
245
- const typeMap = {
246
- if: 'if',
247
- else: 'if:else',
248
- each: 'each',
249
- pending: 'await',
250
- then: 'await:then', // eslint-disable-line unicorn/no-thenable
251
- catch: 'await:catch',
252
- };
253
- const type = typeMap[prop || blockType] ?? null;
254
- nodeList.set(node, type);
343
+ else {
344
+ thenExpEndCharOffset = thenExpStart + rawPendingNodesBelow.indexOf('}') + 1;
255
345
  }
256
- let lastChild = null;
257
- for (const [node, _type] of nodeList.entries()) {
258
- let type = _type;
259
- let start = node.start;
260
- let end = node.end;
261
- if (type === 'await') {
262
- start = originBlockNode.start;
263
- }
264
- end = node.children?.[0]?.start ?? end;
265
- if (type === 'if:else' && originBlockNode.type === 'EachBlock') {
266
- type = 'each:empty';
267
- start = lastChild?.end ?? start;
268
- }
269
- if (['if:else', 'if:elseif'].includes(type) &&
270
- originBlockNode.type === 'IfBlock') {
271
- start = lastChild?.end ?? start;
272
- }
273
- const tag = this.sliceFragment(start, end);
274
- if (type === 'if:else' && node.type === 'ElseBlock' && node.children?.[0]?.type === 'IfBlock') {
275
- type = 'if:elseif';
276
- }
277
- if (node.children && Array.isArray(node.children)) {
278
- lastChild = node.children.at(-1) ?? null;
279
- }
280
- if (tag.raw === '') {
281
- continue;
282
- }
283
- const expression = this.visitPsBlock({
284
- ...tag,
285
- depth: token.depth,
286
- parentNode: token.parentNode,
287
- nodeName: type ?? blockType,
288
- }, node.children, type)[0];
289
- expressions.push(expression);
346
+ thenToken = this.sliceFragment(token.startOffset + thenExpStart, thenExpEndCharOffset);
347
+ }
348
+ let catchToken = null;
349
+ /**
350
+ * `{#await expression}...{:then name}...{:catch name}...{/await}`
351
+ * find___^
352
+ *
353
+ * If `then` block is not found:
354
+ *
355
+ * `{#await expression}...{:catch name}...{/await}`
356
+ * find___^
357
+ */
358
+ const catchExpStart = thenToken
359
+ ? thenEnd ?? thenToken.startOffset + thenToken.raw.length
360
+ : pendingEnd ?? awaitExpEnd;
361
+ /**
362
+ * `{:catch name}...{/await}`
363
+ */
364
+ const rawThenNodesBelow = this.rawCode.slice(catchExpStart, originBlockNode.end);
365
+ if (
366
+ // eslint-disable-next-line regexp/strict
367
+ /^{\s*:catch[\s|}]/.test(rawThenNodesBelow)) {
368
+ let catchExpEndCharOffset;
369
+ if (originBlockNode.error) {
370
+ const catchIdentifierEnd =
371
+ // @ts-ignore - new Svelte Compiler Type doesn't support `start` and `end` yet.
372
+ originBlockNode.error.end;
373
+ const rawCatchExpCloseCharAndBelow = this.rawCode.slice(catchIdentifierEnd, originBlockNode.end);
374
+ const catchExpEndCharIndex = rawCatchExpCloseCharAndBelow.indexOf('}') + 1;
375
+ catchExpEndCharOffset = catchIdentifierEnd + catchExpEndCharIndex;
290
376
  }
291
- const lastText = this.sliceFragment(lastChild?.end ?? originBlockNode.end, originBlockNode.end);
292
- if (lastText.raw) {
293
- // Cut before whitespace
294
- const index = lastText.raw.search(/\S/);
295
- const lastToken = this.sliceFragment(lastText.startOffset + index, originBlockNode.end);
296
- if (lastToken.raw) {
297
- const expression = this.visitPsBlock({
298
- ...lastToken,
299
- depth: token.depth,
300
- parentNode: token.parentNode,
301
- nodeName: '/' + blockType,
302
- }, [], 'end')[0];
303
- expressions.push(expression);
304
- }
377
+ else {
378
+ catchExpEndCharOffset = catchExpStart + rawThenNodesBelow.indexOf('}') + 1;
305
379
  }
306
- return expressions;
380
+ catchToken = this.sliceFragment(token.startOffset + catchExpStart, catchExpEndCharOffset);
307
381
  }
308
- }
309
- _SvelteParser_instances = new WeakSet(), _SvelteParser_traverseIfBlock = function _SvelteParser_traverseIfBlock(
382
+ const expressions = [];
383
+ expressions.push(this.visitPsBlock({
384
+ ...awaitExpToken,
385
+ depth: token.depth,
386
+ parentNode: token.parentNode,
387
+ nodeName: 'await',
388
+ }, originBlockNode.pending?.nodes, 'await')[0]);
389
+ if (thenToken) {
390
+ expressions.push(this.visitPsBlock({
391
+ ...thenToken,
392
+ depth: token.depth,
393
+ parentNode: token.parentNode,
394
+ nodeName: 'await:then',
395
+ }, originBlockNode.then?.nodes, 'await:then')[0]);
396
+ }
397
+ if (catchToken) {
398
+ expressions.push(this.visitPsBlock({
399
+ ...catchToken,
400
+ depth: token.depth,
401
+ parentNode: token.parentNode,
402
+ nodeName: 'await:catch',
403
+ }, originBlockNode.catch?.nodes, 'await:catch')[0]);
404
+ }
405
+ expressions.push(this.visitPsBlock({
406
+ ...closeToken,
407
+ depth: token.depth,
408
+ parentNode: token.parentNode,
409
+ nodeName: '/await',
410
+ }, undefined, 'end')[0]);
411
+ return expressions;
412
+ }, _SvelteParser_parseEachBlock = function _SvelteParser_parseEachBlock(token,
413
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
414
+ originBlockNode) {
415
+ const expressions = [];
416
+ /**
417
+ * `{/each}`
418
+ */
419
+ const { closeToken } = parseBlock(this, token, originBlockNode);
420
+ /**
421
+ * `{#each expression as name}...{:else}...{/each}`
422
+ * find___^
423
+ */
424
+ const bodyStart = originBlockNode.body.nodes.at(0)?.start ?? closeToken.startOffset;
425
+ /**
426
+ * `{#each expression as name}...{:else}...{/each}`
427
+ * find___^
428
+ */
429
+ const fallbackScopeStart = originBlockNode.fallback?.nodes.at(0)?.start ?? closeToken.startOffset;
430
+ /**
431
+ * `{#each expression as name}...{:else}`
432
+ */
433
+ const rawUntilFallbackScope = this.rawCode.slice(token.startOffset, fallbackScopeStart);
434
+ let elseToken = null;
435
+ /**
436
+ * `{#each expression as name}...{:else}`
437
+ * find___^
438
+ */
439
+ // eslint-disable-next-line regexp/strict
440
+ const elseTokenStart = rawUntilFallbackScope.match(/{\s*:else\s*}$/)?.index;
441
+ if (elseTokenStart != null) {
442
+ elseToken = this.sliceFragment(token.startOffset + elseTokenStart, fallbackScopeStart);
443
+ }
444
+ const eachToken = this.sliceFragment(token.startOffset, bodyStart);
445
+ expressions.push(this.visitPsBlock({
446
+ ...eachToken,
447
+ depth: token.depth,
448
+ parentNode: token.parentNode,
449
+ nodeName: 'each',
450
+ }, originBlockNode.body.nodes, 'each')[0]);
451
+ if (elseToken) {
452
+ expressions.push(this.visitPsBlock({
453
+ ...elseToken,
454
+ depth: token.depth,
455
+ parentNode: token.parentNode,
456
+ nodeName: 'each:empty',
457
+ }, originBlockNode.fallback?.nodes, 'each:empty')[0]);
458
+ }
459
+ expressions.push(this.visitPsBlock({
460
+ ...closeToken,
461
+ depth: token.depth,
462
+ parentNode: token.parentNode,
463
+ nodeName: '/each',
464
+ }, undefined, 'end')[0]);
465
+ return expressions;
466
+ }, _SvelteParser_traverseIfBlock = function _SvelteParser_traverseIfBlock(
310
467
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
311
- originBlockNode, start, nodeName = 'if') {
468
+ originBlockNode, start, type = 'if') {
312
469
  const result = [];
313
- start = start ?? originBlockNode.start;
314
- const end = originBlockNode.children?.[0]?.start ?? originBlockNode.end;
470
+ const end = originBlockNode.consequent.nodes?.[0]?.start ?? originBlockNode.end;
315
471
  const tag = this.sliceFragment(start, end);
316
- const children = originBlockNode.children ?? [];
317
- result.push({ ...tag, children, nodeName });
318
- if (originBlockNode.else) {
319
- if (originBlockNode.else.children[0].type === 'IfBlock') {
320
- const elseif = __classPrivateFieldGet(this, _SvelteParser_instances, "m", _SvelteParser_traverseIfBlock).call(this, originBlockNode.else.children[0], children.at(-1)?.end, 'elseif');
472
+ const children = originBlockNode.consequent.nodes;
473
+ result.push({ ...tag, children, type });
474
+ if (originBlockNode.alternate) {
475
+ if (originBlockNode.alternate.nodes?.[0]?.type === 'IfBlock') {
476
+ const elseif = __classPrivateFieldGet(this, _SvelteParser_instances, "m", _SvelteParser_traverseIfBlock).call(this, originBlockNode.alternate.nodes[0], children.at(-1)?.end ?? start, 'elseif');
321
477
  result.push(...elseif);
322
478
  }
323
479
  else {
324
480
  const start = children.at(-1)?.end ?? originBlockNode.end;
325
- const end = originBlockNode.else.children[0].start;
481
+ const end = originBlockNode.alternate.nodes?.[0]?.start;
326
482
  const tag = this.sliceFragment(start, end);
327
483
  result.push({
328
484
  ...tag,
329
- children: originBlockNode.else.children,
330
- nodeName: 'else',
485
+ children: originBlockNode.alternate.nodes,
486
+ type: 'else',
331
487
  });
332
488
  }
333
489
  }
@@ -336,7 +492,7 @@ originBlockNode, start, nodeName = 'if') {
336
492
  const end = originBlockNode.end;
337
493
  const tag = this.sliceFragment(start, end);
338
494
  if (tag.raw) {
339
- result.push({ ...tag, children: [], nodeName: '/if' });
495
+ result.push({ ...tag, children: [], type: '/if' });
340
496
  }
341
497
  }
342
498
  return result;
@@ -1,5 +1,9 @@
1
1
  /// <reference types="svelte" />
2
- import type { Directive, TemplateNode, Attribute, SpreadAttribute } from 'svelte/types/compiler/interfaces';
3
- export type SvelteNode = TemplateNode;
2
+ import type { Attribute, AwaitBlock, Block, Comment, Directive, EachBlock, ElementLike, IfBlock, SpreadAttribute, Tag, Text } from 'svelte/compiler';
3
+ export type SvelteNode = Text | Tag | ElementLike | Comment | Block;
4
+ export type SvelteIfBlock = IfBlock;
5
+ export type SvelteEachBlock = EachBlock;
6
+ export type SvelteAwaitBlock = AwaitBlock;
4
7
  export declare function svelteParse(template: string): SvelteNode[];
5
8
  export type SvelteDirective = Directive | Attribute | SpreadAttribute;
9
+ export declare const blockOrTags: string[];
@@ -1,10 +1,16 @@
1
1
  import { parse } from 'svelte/compiler';
2
2
  export function svelteParse(template) {
3
- const ast = parse(template, { customElement: true });
4
- const start = ast.html.start;
5
- const children = ast.html.children ?? [];
6
- if (children[0] && children[0].end === start) {
7
- children.shift();
8
- }
9
- return children;
3
+ const ast = parse(template, { modern: true });
4
+ return ast.fragment.nodes ?? [];
10
5
  }
6
+ export const blockOrTags = [
7
+ 'IfBlock',
8
+ 'EachBlock',
9
+ 'AwaitBlock',
10
+ 'KeyBlock',
11
+ 'SnippetBlock',
12
+ 'HtmlTag',
13
+ 'DebugTag',
14
+ 'ConstTag',
15
+ 'RenderTag',
16
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/svelte-parser",
3
- "version": "4.6.2",
3
+ "version": "4.7.0-alpha.1",
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,6 +16,9 @@
16
16
  }
17
17
  },
18
18
  "types": "lib/index.d.ts",
19
+ "files": [
20
+ "lib"
21
+ ],
19
22
  "publishConfig": {
20
23
  "access": "public"
21
24
  },
@@ -27,7 +30,6 @@
27
30
  "@markuplint/html-parser": "4.6.2",
28
31
  "@markuplint/ml-ast": "4.3.1",
29
32
  "@markuplint/parser-utils": "4.6.2",
30
- "svelte": "4.2.16"
31
- },
32
- "gitHead": "ca7dc6bf40eac4f2813e492878f889eb77751a70"
33
+ "svelte": "5.0.0-next.141"
34
+ }
33
35
  }
package/CHANGELOG.md DELETED
@@ -1,16 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
- See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
-
6
- ## [4.6.2](https://github.com/markuplint/markuplint/compare/@markuplint/svelte-parser@4.6.1...@markuplint/svelte-parser@4.6.2) (2024-05-12)
7
-
8
- **Note:** Version bump only for package @markuplint/svelte-parser
9
-
10
- ## [4.6.1](https://github.com/markuplint/markuplint/compare/@markuplint/svelte-parser@4.6.1-alpha.0...@markuplint/svelte-parser@4.6.1) (2024-05-04)
11
-
12
- **Note:** Version bump only for package @markuplint/svelte-parser
13
-
14
- ## [4.6.1-alpha.0](https://github.com/markuplint/markuplint/compare/@markuplint/svelte-parser@4.6.0...@markuplint/svelte-parser@4.6.1-alpha.0) (2024-05-04)
15
-
16
- **Note:** Version bump only for package @markuplint/svelte-parser
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2017-2024 Yusuke Hirao
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.