@markuplint/svelte-parser 4.7.12 → 4.18.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/index.d.ts +7 -0
- package/lib/index.js +7 -0
- package/lib/parse-block.d.ts +11 -0
- package/lib/parse-block.js +11 -0
- package/lib/parser.d.ts +52 -0
- package/lib/parser.js +61 -8
- package/lib/svelte-parser/index.d.ts +13 -0
- package/lib/svelte-parser/index.js +7 -0
- package/lib/sveltekit-parser.d.ts +6 -0
- package/lib/sveltekit-parser.js +6 -0
- package/package.json +6 -6
package/lib/index.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* Svelte component parser for markuplint. Provides a parser that transforms Svelte
|
|
4
|
+
* template syntax into markuplint's AST, supporting Svelte-specific constructs such as
|
|
5
|
+
* `{#if}`, `{#each}`, `{#await}`, `{#key}`, `{#snippet}`, expression tags,
|
|
6
|
+
* and bind/class/event directives.
|
|
7
|
+
*/
|
|
1
8
|
export { parser } from './parser.js';
|
package/lib/index.js
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module
|
|
3
|
+
* Svelte component parser for markuplint. Provides a parser that transforms Svelte
|
|
4
|
+
* template syntax into markuplint's AST, supporting Svelte-specific constructs such as
|
|
5
|
+
* `{#if}`, `{#each}`, `{#await}`, `{#key}`, `{#snippet}`, expression tags,
|
|
6
|
+
* and bind/class/event directives.
|
|
7
|
+
*/
|
|
1
8
|
export { parser } from './parser.js';
|
package/lib/parse-block.d.ts
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import type { SvelteParser } from './parser.js';
|
|
2
2
|
import type { ChildToken, Token } from '@markuplint/parser-utils';
|
|
3
3
|
import type { SvelteBlock } from './svelte-parser/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Extracts the open and close tag tokens from a Svelte block construct
|
|
6
|
+
* (e.g., `{#each}...{/each}`, `{#key}...{/key}`).
|
|
7
|
+
* Locates the closing `{/xxx}` tag via regex and computes the opening token
|
|
8
|
+
* based on the block's child fragment boundaries.
|
|
9
|
+
*
|
|
10
|
+
* @param parser - The SvelteParser instance used to slice source fragments
|
|
11
|
+
* @param token - The child token representing the entire block range
|
|
12
|
+
* @param originBlockNode - The Svelte AST block node being parsed
|
|
13
|
+
* @returns An object containing the `openToken` and `closeToken` for the block
|
|
14
|
+
*/
|
|
4
15
|
export declare function parseBlock(parser: SvelteParser, token: ChildToken, originBlockNode: SvelteBlock): {
|
|
5
16
|
openToken: Token;
|
|
6
17
|
closeToken: Token;
|
package/lib/parse-block.js
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the open and close tag tokens from a Svelte block construct
|
|
3
|
+
* (e.g., `{#each}...{/each}`, `{#key}...{/key}`).
|
|
4
|
+
* Locates the closing `{/xxx}` tag via regex and computes the opening token
|
|
5
|
+
* based on the block's child fragment boundaries.
|
|
6
|
+
*
|
|
7
|
+
* @param parser - The SvelteParser instance used to slice source fragments
|
|
8
|
+
* @param token - The child token representing the entire block range
|
|
9
|
+
* @param originBlockNode - The Svelte AST block node being parsed
|
|
10
|
+
* @returns An object containing the `openToken` and `closeToken` for the block
|
|
11
|
+
*/
|
|
1
12
|
export function parseBlock(
|
|
2
13
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
3
14
|
parser, token,
|
package/lib/parser.d.ts
CHANGED
|
@@ -2,6 +2,13 @@ import type { SvelteNode } from './svelte-parser/index.js';
|
|
|
2
2
|
import type { MLASTNodeTreeItem, MLASTParentNode, MLASTPreprocessorSpecificBlock, MLASTPreprocessorSpecificBlockConditionalType } from '@markuplint/ml-ast';
|
|
3
3
|
import type { ChildToken, ParseOptions, Token } from '@markuplint/parser-utils';
|
|
4
4
|
import { ParserError, Parser } from '@markuplint/parser-utils';
|
|
5
|
+
/**
|
|
6
|
+
* Parser implementation for Svelte component templates.
|
|
7
|
+
* Extends the base Parser to handle Svelte elements, text, comments,
|
|
8
|
+
* expression tags, control flow blocks (`{#if}`, `{#each}`, `{#await}`,
|
|
9
|
+
* `{#key}`, `{#snippet}`), directives (`bind:`, `class:`, `on:`),
|
|
10
|
+
* and shorthand attribute syntax.
|
|
11
|
+
*/
|
|
5
12
|
export declare class SvelteParser extends Parser<SvelteNode> {
|
|
6
13
|
#private;
|
|
7
14
|
readonly specificBindDirective: ReadonlySet<string>;
|
|
@@ -12,13 +19,58 @@ export declare class SvelteParser extends Parser<SvelteNode> {
|
|
|
12
19
|
};
|
|
13
20
|
parse(raw: string, options?: ParseOptions): import("@markuplint/ml-ast").MLASTDocument;
|
|
14
21
|
parseError(error: any): ParserError;
|
|
22
|
+
/**
|
|
23
|
+
* Converts a Svelte AST node into markuplint node tree items.
|
|
24
|
+
* Dispatches on the node type to handle Text, Comment, ExpressionTag,
|
|
25
|
+
* elements (Component, RegularElement), and control flow blocks
|
|
26
|
+
* (IfBlock, EachBlock, AwaitBlock, KeyBlock, SnippetBlock).
|
|
27
|
+
*
|
|
28
|
+
* @param originNode - The Svelte AST node to convert
|
|
29
|
+
* @param parentNode - The parent node in the markuplint tree, or null for root nodes
|
|
30
|
+
* @param depth - The nesting depth of the node
|
|
31
|
+
* @returns An array of markuplint node tree items
|
|
32
|
+
*/
|
|
15
33
|
nodeize(originNode: SvelteNode, parentNode: MLASTParentNode | null, depth: number): readonly MLASTNodeTreeItem[];
|
|
34
|
+
/**
|
|
35
|
+
* Visits a text token, converting `<script>` tags embedded in Svelte template
|
|
36
|
+
* text into preprocessor-specific blocks rather than treating them as raw text.
|
|
37
|
+
*
|
|
38
|
+
* @param token - The child token representing the text content
|
|
39
|
+
* @returns An array of markuplint node tree items
|
|
40
|
+
*/
|
|
16
41
|
visitText(token: ChildToken): readonly MLASTNodeTreeItem[];
|
|
42
|
+
/**
|
|
43
|
+
* Visits a preprocessor-specific block token and enforces that exactly one
|
|
44
|
+
* block node is produced. Throws a ParserError if the result is empty
|
|
45
|
+
* or contains multiple nodes.
|
|
46
|
+
*
|
|
47
|
+
* @param token - The child token with node name and fragment flag
|
|
48
|
+
* @param childNodes - The child Svelte AST nodes within the block
|
|
49
|
+
* @param conditionalType - The conditional block type identifier, or null
|
|
50
|
+
* @returns A single-element tuple containing the preprocessor-specific block
|
|
51
|
+
*/
|
|
17
52
|
visitPsBlock(token: ChildToken & {
|
|
18
53
|
readonly nodeName: string;
|
|
19
54
|
readonly isFragment: boolean;
|
|
20
55
|
}, childNodes?: readonly SvelteNode[], conditionalType?: MLASTPreprocessorSpecificBlockConditionalType): readonly [MLASTPreprocessorSpecificBlock];
|
|
56
|
+
/**
|
|
57
|
+
* Visits child nodes and verifies that no sibling nodes with differing
|
|
58
|
+
* hierarchy levels are produced. Throws a ParserError if unexpected
|
|
59
|
+
* sibling nodes are discovered.
|
|
60
|
+
*
|
|
61
|
+
* @param children - The child Svelte AST nodes to visit
|
|
62
|
+
* @param parentNode - The parent node in the markuplint tree
|
|
63
|
+
* @returns An empty array (all children are attached via the visitor)
|
|
64
|
+
*/
|
|
21
65
|
visitChildren(children: readonly SvelteNode[], parentNode: MLASTParentNode | null): never[];
|
|
66
|
+
/**
|
|
67
|
+
* Visits an attribute token, handling Svelte-specific syntax including
|
|
68
|
+
* curly-brace expression values, shorthand attributes (`{name}`),
|
|
69
|
+
* `bind:` / `class:` directives, and duplicatable class attributes.
|
|
70
|
+
*
|
|
71
|
+
* @param token - The token representing the attribute
|
|
72
|
+
* @returns The parsed attribute node with Svelte-specific metadata
|
|
73
|
+
*/
|
|
22
74
|
visitAttr(token: Token): (import("@markuplint/ml-ast").MLASTSpreadAttr & {
|
|
23
75
|
__rightText?: string;
|
|
24
76
|
}) | {
|
package/lib/parser.js
CHANGED
|
@@ -8,6 +8,13 @@ import { getNamespace } from '@markuplint/html-parser';
|
|
|
8
8
|
import { ParserError, Parser, AttrState } from '@markuplint/parser-utils';
|
|
9
9
|
import { parseBlock } from './parse-block.js';
|
|
10
10
|
import { svelteParse } from './svelte-parser/index.js';
|
|
11
|
+
/**
|
|
12
|
+
* Parser implementation for Svelte component templates.
|
|
13
|
+
* Extends the base Parser to handle Svelte elements, text, comments,
|
|
14
|
+
* expression tags, control flow blocks (`{#if}`, `{#each}`, `{#await}`,
|
|
15
|
+
* `{#key}`, `{#snippet}`), directives (`bind:`, `class:`, `on:`),
|
|
16
|
+
* and shorthand attribute syntax.
|
|
17
|
+
*/
|
|
11
18
|
export class SvelteParser extends Parser {
|
|
12
19
|
constructor() {
|
|
13
20
|
super({
|
|
@@ -49,6 +56,17 @@ export class SvelteParser extends Parser {
|
|
|
49
56
|
}
|
|
50
57
|
return super.parseError(error);
|
|
51
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts a Svelte AST node into markuplint node tree items.
|
|
61
|
+
* Dispatches on the node type to handle Text, Comment, ExpressionTag,
|
|
62
|
+
* elements (Component, RegularElement), and control flow blocks
|
|
63
|
+
* (IfBlock, EachBlock, AwaitBlock, KeyBlock, SnippetBlock).
|
|
64
|
+
*
|
|
65
|
+
* @param originNode - The Svelte AST node to convert
|
|
66
|
+
* @param parentNode - The parent node in the markuplint tree, or null for root nodes
|
|
67
|
+
* @param depth - The nesting depth of the node
|
|
68
|
+
* @returns An array of markuplint node tree items
|
|
69
|
+
*/
|
|
52
70
|
nodeize(
|
|
53
71
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
54
72
|
originNode, parentNode, depth) {
|
|
@@ -205,6 +223,13 @@ export class SvelteParser extends Parser {
|
|
|
205
223
|
}
|
|
206
224
|
}
|
|
207
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Visits a text token, converting `<script>` tags embedded in Svelte template
|
|
228
|
+
* text into preprocessor-specific blocks rather than treating them as raw text.
|
|
229
|
+
*
|
|
230
|
+
* @param token - The child token representing the text content
|
|
231
|
+
* @returns An array of markuplint node tree items
|
|
232
|
+
*/
|
|
208
233
|
visitText(token) {
|
|
209
234
|
const nodes = super.visitText(token, {
|
|
210
235
|
researchTags: false,
|
|
@@ -221,6 +246,16 @@ export class SvelteParser extends Parser {
|
|
|
221
246
|
return node;
|
|
222
247
|
});
|
|
223
248
|
}
|
|
249
|
+
/**
|
|
250
|
+
* Visits a preprocessor-specific block token and enforces that exactly one
|
|
251
|
+
* block node is produced. Throws a ParserError if the result is empty
|
|
252
|
+
* or contains multiple nodes.
|
|
253
|
+
*
|
|
254
|
+
* @param token - The child token with node name and fragment flag
|
|
255
|
+
* @param childNodes - The child Svelte AST nodes within the block
|
|
256
|
+
* @param conditionalType - The conditional block type identifier, or null
|
|
257
|
+
* @returns A single-element tuple containing the preprocessor-specific block
|
|
258
|
+
*/
|
|
224
259
|
visitPsBlock(token, childNodes = [], conditionalType = null) {
|
|
225
260
|
const nodes = super.visitPsBlock(token, childNodes, conditionalType);
|
|
226
261
|
const block = nodes.at(0);
|
|
@@ -232,6 +267,15 @@ export class SvelteParser extends Parser {
|
|
|
232
267
|
}
|
|
233
268
|
return [block];
|
|
234
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Visits child nodes and verifies that no sibling nodes with differing
|
|
272
|
+
* hierarchy levels are produced. Throws a ParserError if unexpected
|
|
273
|
+
* sibling nodes are discovered.
|
|
274
|
+
*
|
|
275
|
+
* @param children - The child Svelte AST nodes to visit
|
|
276
|
+
* @param parentNode - The parent node in the markuplint tree
|
|
277
|
+
* @returns An empty array (all children are attached via the visitor)
|
|
278
|
+
*/
|
|
235
279
|
visitChildren(
|
|
236
280
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
237
281
|
children, parentNode) {
|
|
@@ -241,6 +285,14 @@ export class SvelteParser extends Parser {
|
|
|
241
285
|
}
|
|
242
286
|
return [];
|
|
243
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Visits an attribute token, handling Svelte-specific syntax including
|
|
290
|
+
* curly-brace expression values, shorthand attributes (`{name}`),
|
|
291
|
+
* `bind:` / `class:` directives, and duplicatable class attributes.
|
|
292
|
+
*
|
|
293
|
+
* @param token - The token representing the attribute
|
|
294
|
+
* @returns The parsed attribute node with Svelte-specific metadata
|
|
295
|
+
*/
|
|
244
296
|
visitAttr(token) {
|
|
245
297
|
const attr = super.visitAttr(token, {
|
|
246
298
|
quoteSet: [
|
|
@@ -388,14 +440,15 @@ originBlockNode) {
|
|
|
388
440
|
}
|
|
389
441
|
catchToken = this.sliceFragment(token.startOffset + catchExpStart, catchExpEndCharOffset);
|
|
390
442
|
}
|
|
391
|
-
const expressions = [
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
443
|
+
const expressions = [
|
|
444
|
+
this.visitPsBlock({
|
|
445
|
+
...awaitExpToken,
|
|
446
|
+
depth: token.depth,
|
|
447
|
+
parentNode: token.parentNode,
|
|
448
|
+
nodeName: 'await',
|
|
449
|
+
isFragment: false,
|
|
450
|
+
}, originBlockNode.pending?.nodes, 'await')[0],
|
|
451
|
+
];
|
|
399
452
|
if (thenToken) {
|
|
400
453
|
expressions.push(this.visitPsBlock({
|
|
401
454
|
...thenToken,
|
|
@@ -1,10 +1,23 @@
|
|
|
1
1
|
import type { AST } from 'svelte/compiler';
|
|
2
|
+
/** Union of Svelte AST node types that can appear as children in a Svelte template fragment. */
|
|
2
3
|
export type SvelteNode = AST.Text | AST.Comment | AST.Tag | AST.ElementLike | AST.Block;
|
|
4
|
+
/** Represents a Svelte `{#if}` block with consequent, alternate, and elseif branches. */
|
|
3
5
|
export type SvelteIfBlock = AST.IfBlock;
|
|
6
|
+
/** Represents a Svelte `{#each}` block with iteration body and optional fallback. */
|
|
4
7
|
export type SvelteEachBlock = AST.EachBlock;
|
|
8
|
+
/** Represents a Svelte `{#await}` block with pending, then, and catch branches. */
|
|
5
9
|
export type SvelteAwaitBlock = AST.AwaitBlock;
|
|
10
|
+
/**
|
|
11
|
+
* Parses a Svelte template string into an array of top-level AST nodes
|
|
12
|
+
* using the Svelte compiler's modern parser mode.
|
|
13
|
+
*
|
|
14
|
+
* @param template - The raw Svelte template source code
|
|
15
|
+
* @returns An array of top-level Svelte AST nodes from the template fragment
|
|
16
|
+
*/
|
|
6
17
|
export declare function svelteParse(template: string): SvelteNode[];
|
|
18
|
+
/** Union of all Svelte directive and attribute types that can appear on elements. */
|
|
7
19
|
export type SvelteDirective = Directive | AST.Attribute | AST.SpreadAttribute;
|
|
20
|
+
/** Union of all Svelte block types that have opening/closing tag syntax. */
|
|
8
21
|
export type SvelteBlock = AST.EachBlock | AST.IfBlock | AST.AwaitBlock | AST.KeyBlock | AST.SnippetBlock | AST.SvelteBoundary;
|
|
9
22
|
type Directive = AST.AnimateDirective | AST.BindDirective | AST.ClassDirective | AST.LetDirective | AST.OnDirective | AST.StyleDirective | AST.TransitionDirective | AST.UseDirective;
|
|
10
23
|
export {};
|
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import { parse } from 'svelte/compiler';
|
|
2
|
+
/**
|
|
3
|
+
* Parses a Svelte template string into an array of top-level AST nodes
|
|
4
|
+
* using the Svelte compiler's modern parser mode.
|
|
5
|
+
*
|
|
6
|
+
* @param template - The raw Svelte template source code
|
|
7
|
+
* @returns An array of top-level Svelte AST nodes from the template fragment
|
|
8
|
+
*/
|
|
2
9
|
export function svelteParse(template) {
|
|
3
10
|
const ast = parse(template, { modern: true });
|
|
4
11
|
return ast.fragment.nodes ?? [];
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { HtmlParser } from '@markuplint/html-parser';
|
|
2
|
+
/**
|
|
3
|
+
* Parser for SvelteKit app template files (e.g., `app.html`).
|
|
4
|
+
* Extends the standard HTML parser to handle SvelteKit placeholder tags
|
|
5
|
+
* such as `%sveltekit.head%` and `%sveltekit.body%`, which are treated
|
|
6
|
+
* as opaque preprocessor-specific blocks.
|
|
7
|
+
*/
|
|
2
8
|
declare class SvelteKitTemplateParser extends HtmlParser {
|
|
3
9
|
constructor();
|
|
4
10
|
}
|
package/lib/sveltekit-parser.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { HtmlParser } from '@markuplint/html-parser';
|
|
2
|
+
/**
|
|
3
|
+
* Parser for SvelteKit app template files (e.g., `app.html`).
|
|
4
|
+
* Extends the standard HTML parser to handle SvelteKit placeholder tags
|
|
5
|
+
* such as `%sveltekit.head%` and `%sveltekit.body%`, which are treated
|
|
6
|
+
* as opaque preprocessor-specific blocks.
|
|
7
|
+
*/
|
|
2
8
|
class SvelteKitTemplateParser extends HtmlParser {
|
|
3
9
|
constructor() {
|
|
4
10
|
super({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/svelte-parser",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.18.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>",
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"clean": "tsc --build --clean tsconfig.build.json"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@markuplint/html-parser": "4.
|
|
33
|
-
"@markuplint/ml-ast": "4.
|
|
34
|
-
"@markuplint/parser-utils": "4.
|
|
35
|
-
"svelte": "5.
|
|
32
|
+
"@markuplint/html-parser": "4.18.0",
|
|
33
|
+
"@markuplint/ml-ast": "4.18.0",
|
|
34
|
+
"@markuplint/parser-utils": "4.18.0",
|
|
35
|
+
"svelte": "5.55.4"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "1885af6349def3f19df975b9e9c399dd47361de1"
|
|
38
38
|
}
|