@markuplint/svelte-parser 4.7.0-alpha.0 → 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.
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/parse-block.d.ts +8 -0
- package/lib/parse-block.js +47 -0
- package/lib/parse-blokc.d.ts +8 -0
- package/lib/parse-blokc.js +47 -0
- package/lib/parser.d.ts +60 -0
- package/lib/parser.js +500 -0
- package/lib/svelte-parser/index.d.ts +9 -0
- package/lib/svelte-parser/index.js +16 -0
- package/lib/sveltekit-parser.d.ts +6 -0
- package/lib/sveltekit-parser.js +15 -0
- package/package.json +5 -2
- package/CHANGELOG.md +0 -16
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { parser } from './parser.js';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { parser } from './parser.js';
|
|
@@ -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
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { SvelteNode } from './svelte-parser/index.js';
|
|
2
|
+
import type { MLASTParentNode, MLASTPreprocessorSpecificBlock, MLASTPreprocessorSpecificBlockConditionalType } from '@markuplint/ml-ast';
|
|
3
|
+
import type { ChildToken, ParseOptions, Token } from '@markuplint/parser-utils';
|
|
4
|
+
import { ParserError, Parser } from '@markuplint/parser-utils';
|
|
5
|
+
export declare class SvelteParser extends Parser<SvelteNode> {
|
|
6
|
+
#private;
|
|
7
|
+
readonly specificBindDirective: ReadonlySet<string>;
|
|
8
|
+
constructor();
|
|
9
|
+
tokenize(): {
|
|
10
|
+
ast: SvelteNode[];
|
|
11
|
+
isFragment: boolean;
|
|
12
|
+
};
|
|
13
|
+
parse(raw: string, options?: ParseOptions): import("@markuplint/ml-ast").MLASTDocument;
|
|
14
|
+
parseError(error: any): ParserError;
|
|
15
|
+
nodeize(originNode: SvelteNode, parentNode: MLASTParentNode | null, depth: number): readonly import("@markuplint/ml-ast").MLASTNodeTreeItem[];
|
|
16
|
+
visitPsBlock(token: ChildToken & {
|
|
17
|
+
readonly nodeName: string;
|
|
18
|
+
}, childNodes?: readonly SvelteNode[], conditionalType?: MLASTPreprocessorSpecificBlockConditionalType): readonly [MLASTPreprocessorSpecificBlock];
|
|
19
|
+
visitChildren(children: readonly SvelteNode[], parentNode: MLASTParentNode | null): never[];
|
|
20
|
+
visitAttr(token: Token): (import("@markuplint/ml-ast").MLASTSpreadAttr & {
|
|
21
|
+
__rightText?: string | undefined;
|
|
22
|
+
}) | {
|
|
23
|
+
isDynamicValue: true | undefined;
|
|
24
|
+
isDirective: true | undefined;
|
|
25
|
+
isDuplicatable: boolean;
|
|
26
|
+
potentialName: string | undefined;
|
|
27
|
+
type: "attr";
|
|
28
|
+
nodeName: string;
|
|
29
|
+
spacesBeforeName: import("@markuplint/ml-ast").MLASTToken;
|
|
30
|
+
name: import("@markuplint/ml-ast").MLASTToken;
|
|
31
|
+
spacesBeforeEqual: import("@markuplint/ml-ast").MLASTToken;
|
|
32
|
+
equal: import("@markuplint/ml-ast").MLASTToken;
|
|
33
|
+
spacesAfterEqual: import("@markuplint/ml-ast").MLASTToken;
|
|
34
|
+
startQuote: import("@markuplint/ml-ast").MLASTToken;
|
|
35
|
+
value: import("@markuplint/ml-ast").MLASTToken;
|
|
36
|
+
endQuote: import("@markuplint/ml-ast").MLASTToken;
|
|
37
|
+
potentialValue?: string | undefined;
|
|
38
|
+
valueType?: "string" | "number" | "boolean" | "code" | undefined;
|
|
39
|
+
candidate?: string | undefined;
|
|
40
|
+
uuid: string;
|
|
41
|
+
raw: string;
|
|
42
|
+
startOffset: number;
|
|
43
|
+
endOffset: number;
|
|
44
|
+
startLine: number;
|
|
45
|
+
endLine: number;
|
|
46
|
+
startCol: number;
|
|
47
|
+
endCol: number;
|
|
48
|
+
__rightText?: string | undefined;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* > A lowercase tag, like `<div>`, denotes a regular HTML element.
|
|
52
|
+
* A capitalised tag, such as `<Widget>` or `<Namespace.Widget>`, indicates a component.
|
|
53
|
+
*
|
|
54
|
+
* @see https://svelte.io/docs/basic-markup#tags
|
|
55
|
+
* @param nodeName
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
detectElementType(nodeName: string): import("@markuplint/ml-ast").ElementType;
|
|
59
|
+
}
|
|
60
|
+
export declare const parser: SvelteParser;
|
package/lib/parser.js
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
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
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _SvelteParser_instances, _SvelteParser_parseAwaitBlock, _SvelteParser_parseEachBlock, _SvelteParser_traverseIfBlock;
|
|
7
|
+
import { getNamespace } from '@markuplint/html-parser';
|
|
8
|
+
import { ParserError, Parser, AttrState } from '@markuplint/parser-utils';
|
|
9
|
+
import { parseBlock } from './parse-block.js';
|
|
10
|
+
import { blockOrTags, svelteParse } from './svelte-parser/index.js';
|
|
11
|
+
export class SvelteParser extends Parser {
|
|
12
|
+
constructor() {
|
|
13
|
+
super({
|
|
14
|
+
endTagType: 'xml',
|
|
15
|
+
tagNameCaseSensitive: true,
|
|
16
|
+
ignoreTags: [
|
|
17
|
+
{
|
|
18
|
+
type: 'Script',
|
|
19
|
+
start: '<script',
|
|
20
|
+
end: '</script>',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
type: 'Style',
|
|
24
|
+
start: '<style',
|
|
25
|
+
end: '</style>',
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
maskChar: '-',
|
|
29
|
+
});
|
|
30
|
+
_SvelteParser_instances.add(this);
|
|
31
|
+
this.specificBindDirective = new Set(['group', 'this']);
|
|
32
|
+
}
|
|
33
|
+
tokenize() {
|
|
34
|
+
return {
|
|
35
|
+
ast: svelteParse(this.rawCode),
|
|
36
|
+
isFragment: true,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
parse(raw, options) {
|
|
40
|
+
return super.parse(raw, {
|
|
41
|
+
...options,
|
|
42
|
+
ignoreFrontMatter: false,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
parseError(error) {
|
|
46
|
+
if (error instanceof Error && 'start' in error && 'end' in error && 'frame' in error) {
|
|
47
|
+
// @ts-ignore
|
|
48
|
+
const token = this.sliceFragment(error.start.character, error.end.character);
|
|
49
|
+
throw new ParserError(error.message + '\n' + error.frame, token);
|
|
50
|
+
}
|
|
51
|
+
return super.parseError(error);
|
|
52
|
+
}
|
|
53
|
+
nodeize(
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
55
|
+
originNode, parentNode, depth) {
|
|
56
|
+
let token = this.sliceFragment(originNode.start, originNode.end);
|
|
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
|
+
}
|
|
71
|
+
switch (originNode.type) {
|
|
72
|
+
case 'Text': {
|
|
73
|
+
return this.visitText({
|
|
74
|
+
...token,
|
|
75
|
+
depth,
|
|
76
|
+
parentNode,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
case 'Comment': {
|
|
80
|
+
return this.visitComment({
|
|
81
|
+
...token,
|
|
82
|
+
depth,
|
|
83
|
+
parentNode,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
case 'ExpressionTag': {
|
|
87
|
+
return this.visitPsBlock({
|
|
88
|
+
...token,
|
|
89
|
+
depth,
|
|
90
|
+
parentNode,
|
|
91
|
+
nodeName: 'ExpressionTag',
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
case 'Component':
|
|
95
|
+
case 'RegularElement': {
|
|
96
|
+
const children = originNode.fragment.nodes ?? [];
|
|
97
|
+
const reEndTag = new RegExp(`</${originNode.name}\\s*>$`, 'i');
|
|
98
|
+
const startTagEndOffset = children.length > 0
|
|
99
|
+
? children[0]?.start ?? 0
|
|
100
|
+
: token.raw.replace(reEndTag, '').length + token.startOffset;
|
|
101
|
+
const startTagLocation = this.sliceFragment(token.startOffset, startTagEndOffset);
|
|
102
|
+
return this.visitElement({
|
|
103
|
+
...startTagLocation,
|
|
104
|
+
depth,
|
|
105
|
+
parentNode,
|
|
106
|
+
nodeName: originNode.name,
|
|
107
|
+
namespace: getNamespace(originNode.name, parentNamespace),
|
|
108
|
+
}, originNode.fragment.nodes, {
|
|
109
|
+
createEndTagToken: () => {
|
|
110
|
+
if (!reEndTag.test(token.raw)) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
const endTagRawMatched = token.raw.match(reEndTag);
|
|
114
|
+
if (!endTagRawMatched) {
|
|
115
|
+
throw new Error('Parse error');
|
|
116
|
+
}
|
|
117
|
+
const endTagRaw = endTagRawMatched[0];
|
|
118
|
+
const endTagStartOffset = token.startOffset + token.raw.lastIndexOf(endTagRaw);
|
|
119
|
+
const endTagEndOffset = endTagStartOffset + endTagRaw.length;
|
|
120
|
+
const endTagLocation = this.sliceFragment(endTagStartOffset, endTagEndOffset);
|
|
121
|
+
return {
|
|
122
|
+
...endTagLocation,
|
|
123
|
+
depth,
|
|
124
|
+
parentNode,
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
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, {
|
|
185
|
+
...token,
|
|
186
|
+
depth,
|
|
187
|
+
parentNode,
|
|
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);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
visitPsBlock(token, childNodes = [], conditionalType = null) {
|
|
216
|
+
const nodes = super.visitPsBlock(token, childNodes, conditionalType);
|
|
217
|
+
const block = nodes.at(0);
|
|
218
|
+
if (!block || block.type !== 'psblock') {
|
|
219
|
+
throw new ParserError('Parse error', token);
|
|
220
|
+
}
|
|
221
|
+
if (nodes.length > 1) {
|
|
222
|
+
throw new ParserError('Parse error', nodes.at(1));
|
|
223
|
+
}
|
|
224
|
+
return [block];
|
|
225
|
+
}
|
|
226
|
+
visitChildren(
|
|
227
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
228
|
+
children, parentNode) {
|
|
229
|
+
const siblings = super.visitChildren(children, parentNode);
|
|
230
|
+
if (siblings.length > 0) {
|
|
231
|
+
throw new ParserError('Discovered child nodes with differing hierarchy levels', siblings[0]);
|
|
232
|
+
}
|
|
233
|
+
return [];
|
|
234
|
+
}
|
|
235
|
+
visitAttr(token) {
|
|
236
|
+
const attr = super.visitAttr(token, {
|
|
237
|
+
quoteSet: [
|
|
238
|
+
{ start: '"', end: '"', type: 'string' },
|
|
239
|
+
{ start: "'", end: "'", type: 'string' },
|
|
240
|
+
{ start: '{', end: '}', type: 'script' },
|
|
241
|
+
],
|
|
242
|
+
startState:
|
|
243
|
+
// is shorthand attribute
|
|
244
|
+
token.raw.trim().startsWith('{') ? AttrState.BeforeValue : AttrState.BeforeName,
|
|
245
|
+
});
|
|
246
|
+
if (attr.type === 'spread') {
|
|
247
|
+
return attr;
|
|
248
|
+
}
|
|
249
|
+
let isDynamicValue = attr.startQuote.raw === '{' || undefined;
|
|
250
|
+
let potentialName;
|
|
251
|
+
let isDirective;
|
|
252
|
+
let isDuplicatable = false;
|
|
253
|
+
if (isDynamicValue && attr.name.raw === '') {
|
|
254
|
+
potentialName = attr.value.raw;
|
|
255
|
+
}
|
|
256
|
+
const [baseName, subName] = attr.name.raw.split(':');
|
|
257
|
+
if (subName) {
|
|
258
|
+
isDirective = true;
|
|
259
|
+
if (baseName === 'bind' && !this.specificBindDirective.has(subName)) {
|
|
260
|
+
potentialName = subName;
|
|
261
|
+
isDirective = undefined;
|
|
262
|
+
isDynamicValue = true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (baseName?.toLowerCase() === 'class') {
|
|
266
|
+
isDuplicatable = true;
|
|
267
|
+
if (subName) {
|
|
268
|
+
potentialName = 'class';
|
|
269
|
+
isDynamicValue = true;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (attr.startQuote.raw === '{' && attr.endQuote.raw === '}') {
|
|
273
|
+
isDynamicValue = true;
|
|
274
|
+
}
|
|
275
|
+
return {
|
|
276
|
+
...attr,
|
|
277
|
+
isDynamicValue,
|
|
278
|
+
isDirective,
|
|
279
|
+
isDuplicatable,
|
|
280
|
+
potentialName,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* > A lowercase tag, like `<div>`, denotes a regular HTML element.
|
|
285
|
+
* A capitalised tag, such as `<Widget>` or `<Namespace.Widget>`, indicates a component.
|
|
286
|
+
*
|
|
287
|
+
* @see https://svelte.io/docs/basic-markup#tags
|
|
288
|
+
* @param nodeName
|
|
289
|
+
* @returns
|
|
290
|
+
*/
|
|
291
|
+
detectElementType(nodeName) {
|
|
292
|
+
return super.detectElementType(nodeName, /^[A-Z]|\./);
|
|
293
|
+
}
|
|
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;
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
thenExpEndCharOffset = thenExpStart + rawPendingNodesBelow.indexOf('}') + 1;
|
|
345
|
+
}
|
|
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;
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
catchExpEndCharOffset = catchExpStart + rawThenNodesBelow.indexOf('}') + 1;
|
|
379
|
+
}
|
|
380
|
+
catchToken = this.sliceFragment(token.startOffset + catchExpStart, catchExpEndCharOffset);
|
|
381
|
+
}
|
|
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(
|
|
467
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
468
|
+
originBlockNode, start, type = 'if') {
|
|
469
|
+
const result = [];
|
|
470
|
+
const end = originBlockNode.consequent.nodes?.[0]?.start ?? originBlockNode.end;
|
|
471
|
+
const tag = this.sliceFragment(start, end);
|
|
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');
|
|
477
|
+
result.push(...elseif);
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
const start = children.at(-1)?.end ?? originBlockNode.end;
|
|
481
|
+
const end = originBlockNode.alternate.nodes?.[0]?.start;
|
|
482
|
+
const tag = this.sliceFragment(start, end);
|
|
483
|
+
result.push({
|
|
484
|
+
...tag,
|
|
485
|
+
children: originBlockNode.alternate.nodes,
|
|
486
|
+
type: 'else',
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
{
|
|
491
|
+
const start = result.at(-1)?.children.at(-1)?.end ?? originBlockNode.end;
|
|
492
|
+
const end = originBlockNode.end;
|
|
493
|
+
const tag = this.sliceFragment(start, end);
|
|
494
|
+
if (tag.raw) {
|
|
495
|
+
result.push({ ...tag, children: [], type: '/if' });
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return result;
|
|
499
|
+
};
|
|
500
|
+
export const parser = new SvelteParser();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="svelte" />
|
|
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;
|
|
7
|
+
export declare function svelteParse(template: string): SvelteNode[];
|
|
8
|
+
export type SvelteDirective = Directive | Attribute | SpreadAttribute;
|
|
9
|
+
export declare const blockOrTags: string[];
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { parse } from 'svelte/compiler';
|
|
2
|
+
export function svelteParse(template) {
|
|
3
|
+
const ast = parse(template, { modern: true });
|
|
4
|
+
return ast.fragment.nodes ?? [];
|
|
5
|
+
}
|
|
6
|
+
export const blockOrTags = [
|
|
7
|
+
'IfBlock',
|
|
8
|
+
'EachBlock',
|
|
9
|
+
'AwaitBlock',
|
|
10
|
+
'KeyBlock',
|
|
11
|
+
'SnippetBlock',
|
|
12
|
+
'HtmlTag',
|
|
13
|
+
'DebugTag',
|
|
14
|
+
'ConstTag',
|
|
15
|
+
'RenderTag',
|
|
16
|
+
];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { HtmlParser } from '@markuplint/html-parser';
|
|
2
|
+
class SvelteKitTemplateParser extends HtmlParser {
|
|
3
|
+
constructor() {
|
|
4
|
+
super({
|
|
5
|
+
ignoreTags: [
|
|
6
|
+
{
|
|
7
|
+
type: 'sveltekit-placeholder',
|
|
8
|
+
start: '%sveltekit.',
|
|
9
|
+
end: '%',
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export const parser = new SvelteKitTemplateParser();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/svelte-parser",
|
|
3
|
-
"version": "4.7.0-alpha.
|
|
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,6 +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": "next"
|
|
33
|
+
"svelte": "5.0.0-next.141"
|
|
31
34
|
}
|
|
32
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
|