@markuplint/svelte-parser 4.5.0 → 4.6.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/parser.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="svelte" />
2
2
  import type { SvelteNode } from './svelte-parser/index.js';
3
- import type { MLASTParentNode, MLASTPreprocessorSpecificBlock } from '@markuplint/ml-ast';
3
+ import type { MLASTParentNode, MLASTPreprocessorSpecificBlock, MLASTPreprocessorSpecificBlockConditionalType } from '@markuplint/ml-ast';
4
4
  import type { ChildToken, ParseOptions, Token } from '@markuplint/parser-utils';
5
5
  import { ParserError, Parser } from '@markuplint/parser-utils';
6
6
  declare class SvelteParser extends Parser<SvelteNode> {
@@ -16,7 +16,7 @@ declare class SvelteParser extends Parser<SvelteNode> {
16
16
  nodeize(originNode: SvelteNode, parentNode: MLASTParentNode | null, depth: number): readonly import("@markuplint/ml-ast").MLASTNodeTreeItem[];
17
17
  visitPsBlock(token: ChildToken & {
18
18
  readonly nodeName: string;
19
- }, childNodes?: readonly SvelteNode[]): readonly [MLASTPreprocessorSpecificBlock];
19
+ }, childNodes?: readonly SvelteNode[], conditionalType?: MLASTPreprocessorSpecificBlockConditionalType): readonly [MLASTPreprocessorSpecificBlock];
20
20
  visitChildren(children: readonly SvelteNode[], parentNode: MLASTParentNode | null): never[];
21
21
  visitAttr(token: Token): (import("@markuplint/ml-ast").MLASTSpreadAttr & {
22
22
  __rightText?: string | undefined;
package/lib/parser.js CHANGED
@@ -121,8 +121,8 @@ class SvelteParser extends Parser {
121
121
  }
122
122
  }
123
123
  }
124
- visitPsBlock(token, childNodes = []) {
125
- const nodes = super.visitPsBlock(token, childNodes);
124
+ visitPsBlock(token, childNodes = [], conditionalType = null) {
125
+ const nodes = super.visitPsBlock(token, childNodes, conditionalType);
126
126
  const block = nodes.at(0);
127
127
  if (!block || block.type !== 'psblock') {
128
128
  throw new ParserError('Parse error', token);
@@ -213,7 +213,12 @@ class SvelteParser extends Parser {
213
213
  depth: token.depth,
214
214
  parentNode: token.parentNode,
215
215
  nodeName: ifElseBlock.nodeName,
216
- }, ifElseBlock.children)[0];
216
+ }, ifElseBlock.children, {
217
+ if: 'if',
218
+ elseif: 'if:elseif',
219
+ else: 'if:else',
220
+ '/if': 'end',
221
+ }[ifElseBlock.nodeName])[0];
217
222
  expressions.push(expression);
218
223
  }
219
224
  return expressions;
@@ -231,16 +236,21 @@ class SvelteParser extends Parser {
231
236
  if (!['IfBlock', 'ElseBlock'].includes(node.type)) {
232
237
  break;
233
238
  }
234
- const type = node.elseif ? 'elseif' : 'else';
239
+ const type = node.elseif ? 'if:elseif' : 'if:else';
235
240
  nodeList.set(node, type);
236
241
  node = node.else ?? node.children?.[0] ?? null;
237
242
  }
238
243
  continue;
239
244
  }
240
- let type = prop || blockType;
241
- if (prop === 'pending') {
242
- type = 'await';
243
- }
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;
244
254
  nodeList.set(node, type);
245
255
  }
246
256
  let lastChild = null;
@@ -252,15 +262,17 @@ class SvelteParser extends Parser {
252
262
  start = originBlockNode.start;
253
263
  }
254
264
  end = node.children?.[0]?.start ?? end;
255
- if (type === 'else' && originBlockNode.type === 'EachBlock') {
265
+ if (type === 'if:else' && originBlockNode.type === 'EachBlock') {
266
+ type = 'each:empty';
256
267
  start = lastChild?.end ?? start;
257
268
  }
258
- if (['else', 'elseif'].includes(type) && originBlockNode.type === 'IfBlock') {
269
+ if (['if:else', 'if:elseif'].includes(type) &&
270
+ originBlockNode.type === 'IfBlock') {
259
271
  start = lastChild?.end ?? start;
260
272
  }
261
273
  const tag = this.sliceFragment(start, end);
262
- if (type === 'else' && node.type === 'ElseBlock' && node.children?.[0]?.type === 'IfBlock') {
263
- type = 'elseif';
274
+ if (type === 'if:else' && node.type === 'ElseBlock' && node.children?.[0]?.type === 'IfBlock') {
275
+ type = 'if:elseif';
264
276
  }
265
277
  if (node.children && Array.isArray(node.children)) {
266
278
  lastChild = node.children.at(-1) ?? null;
@@ -272,8 +284,8 @@ class SvelteParser extends Parser {
272
284
  ...tag,
273
285
  depth: token.depth,
274
286
  parentNode: token.parentNode,
275
- nodeName: type,
276
- }, node.children)[0];
287
+ nodeName: type ?? blockType,
288
+ }, node.children, type)[0];
277
289
  expressions.push(expression);
278
290
  }
279
291
  const lastText = this.sliceFragment(lastChild?.end ?? originBlockNode.end, originBlockNode.end);
@@ -287,7 +299,7 @@ class SvelteParser extends Parser {
287
299
  depth: token.depth,
288
300
  parentNode: token.parentNode,
289
301
  nodeName: '/' + blockType,
290
- })[0];
302
+ }, [], 'end')[0];
291
303
  expressions.push(expression);
292
304
  }
293
305
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/svelte-parser",
3
- "version": "4.5.0",
3
+ "version": "4.6.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>",
@@ -24,10 +24,10 @@
24
24
  "clean": "tsc --build --clean"
25
25
  },
26
26
  "dependencies": {
27
- "@markuplint/html-parser": "4.5.0",
28
- "@markuplint/ml-ast": "4.2.0",
29
- "@markuplint/parser-utils": "4.5.0",
30
- "svelte": "4.2.14"
27
+ "@markuplint/html-parser": "4.6.0",
28
+ "@markuplint/ml-ast": "4.3.0",
29
+ "@markuplint/parser-utils": "4.6.0",
30
+ "svelte": "4.2.15"
31
31
  },
32
- "gitHead": "d5c8786b0dbbd82cdd89018dd57941d62bbe8d06"
32
+ "gitHead": "b8d7bae9bdcdad63ff79abe21b88be12abde3633"
33
33
  }