@markuplint/svelte-parser 3.0.0-alpha.6 → 3.0.0-dev.176
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/README.md +4 -6
- package/lib/attr.js +4 -2
- package/lib/directive-tokenizer.js +11 -10
- package/lib/nodeize.js +17 -84
- package/lib/parse-ctrl-block.d.ts +13 -0
- package/lib/parse-ctrl-block.js +175 -0
- package/lib/parse.js +2 -2
- package/lib/svelte-parser/index.js +2 -1
- package/lib/traverse.d.ts +1 -1
- package/lib/traverse.js +8 -2
- package/package.json +7 -7
- package/test/index.spec.js +508 -0
- package/test/parse-ctrl-block.spec.js +267 -0
- package/test/svelte-parser/index.spec.js +109 -0
- package/tsconfig.test.json +0 -3
- package/tsconfig.tsbuildinfo +0 -1
package/README.md
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
# @markuplint/svelte-parser
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@markuplint/svelte-parser)
|
|
4
|
-
[](https://travis-ci.org/markuplint/markuplint)
|
|
5
|
-
[](https://coveralls.io/github/markuplint/markuplint?branch=main)
|
|
6
4
|
|
|
7
|
-
|
|
5
|
+
Use **markuplint** with [**Svelte**](https://svelte.dev/).
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
## Install
|
|
10
8
|
|
|
11
|
-
```
|
|
9
|
+
```shell
|
|
12
10
|
$ npm install @markuplint/svelte-parser
|
|
13
11
|
|
|
14
12
|
$ yarn add @markuplint/svelte-parser
|
|
@@ -16,7 +14,7 @@ $ yarn add @markuplint/svelte-parser
|
|
|
16
14
|
|
|
17
15
|
## Usage
|
|
18
16
|
|
|
19
|
-
Add `parser` option
|
|
17
|
+
Add `parser` option to your [configuration](https://markuplint.dev/configuration/#properties/parser).
|
|
20
18
|
|
|
21
19
|
```json
|
|
22
20
|
{
|
package/lib/attr.js
CHANGED
|
@@ -6,7 +6,9 @@ const html_parser_1 = require("@markuplint/html-parser");
|
|
|
6
6
|
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
7
7
|
const directive_tokenizer_1 = tslib_1.__importDefault(require("./directive-tokenizer"));
|
|
8
8
|
const specificBindDirective = ['bind:group', 'bind:this'];
|
|
9
|
-
function attr(
|
|
9
|
+
function attr(
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
11
|
+
attr, rawHTML) {
|
|
10
12
|
const isShorthand = attr.value && Array.isArray(attr.value)
|
|
11
13
|
? attr.value.some((val) => val.type === 'AttributeShorthand')
|
|
12
14
|
: false;
|
|
@@ -46,7 +48,7 @@ function attr(attr, rawHTML) {
|
|
|
46
48
|
token.isDynamicValue = true;
|
|
47
49
|
}
|
|
48
50
|
const [baseName, subName] = token.name.raw.split(':');
|
|
49
|
-
if (baseName.toLowerCase() === 'class') {
|
|
51
|
+
if ((baseName === null || baseName === void 0 ? void 0 : baseName.toLowerCase()) === 'class') {
|
|
50
52
|
token.isDuplicatable = true;
|
|
51
53
|
if (subName) {
|
|
52
54
|
token.potentialName = 'class';
|
|
@@ -9,6 +9,7 @@ const reBeforeStructure = /^(\s*)([^\x00-\x1f\x7f-\x9f {>/=]+)(\s*)(=)(\s*){(\s*
|
|
|
9
9
|
const reBeforeStructureWithoutName = /^{(\s*)$/;
|
|
10
10
|
const reAfterStructure = /(\s*)}/;
|
|
11
11
|
function directiveTokenizer(raw, rawValue, line, col, startOffset) {
|
|
12
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
12
13
|
let spacesBeforeAttrString = '';
|
|
13
14
|
let nameChars = '';
|
|
14
15
|
let spacesBeforeEqualChars = '';
|
|
@@ -16,19 +17,19 @@ function directiveTokenizer(raw, rawValue, line, col, startOffset) {
|
|
|
16
17
|
let spacesAfterEqualChars = '';
|
|
17
18
|
let valueChars = '';
|
|
18
19
|
const [before, after] = raw.split(rawValue);
|
|
19
|
-
const beforeMatchedMap = before.match(reBeforeStructure);
|
|
20
|
-
const beforeWithoutNameMatchedMap = before.match(reBeforeStructureWithoutName);
|
|
21
|
-
const afterMatchedMap = after.match(reAfterStructure);
|
|
20
|
+
const beforeMatchedMap = before === null || before === void 0 ? void 0 : before.match(reBeforeStructure);
|
|
21
|
+
const beforeWithoutNameMatchedMap = before === null || before === void 0 ? void 0 : before.match(reBeforeStructureWithoutName);
|
|
22
|
+
const afterMatchedMap = after === null || after === void 0 ? void 0 : after.match(reAfterStructure);
|
|
22
23
|
if (beforeMatchedMap && afterMatchedMap) {
|
|
23
|
-
spacesBeforeAttrString = beforeMatchedMap[1];
|
|
24
|
-
nameChars = beforeMatchedMap[2];
|
|
25
|
-
spacesBeforeEqualChars = beforeMatchedMap[3]
|
|
26
|
-
equalChars = beforeMatchedMap[4]
|
|
27
|
-
spacesAfterEqualChars = beforeMatchedMap[5]
|
|
28
|
-
valueChars = (beforeMatchedMap[6]
|
|
24
|
+
spacesBeforeAttrString = (_a = beforeMatchedMap[1]) !== null && _a !== void 0 ? _a : '';
|
|
25
|
+
nameChars = (_b = beforeMatchedMap[2]) !== null && _b !== void 0 ? _b : '';
|
|
26
|
+
spacesBeforeEqualChars = (_c = beforeMatchedMap[3]) !== null && _c !== void 0 ? _c : '';
|
|
27
|
+
equalChars = (_d = beforeMatchedMap[4]) !== null && _d !== void 0 ? _d : null;
|
|
28
|
+
spacesAfterEqualChars = (_e = beforeMatchedMap[5]) !== null && _e !== void 0 ? _e : '';
|
|
29
|
+
valueChars = ((_f = beforeMatchedMap[6]) !== null && _f !== void 0 ? _f : '') + rawValue + ((_g = afterMatchedMap[1]) !== null && _g !== void 0 ? _g : '');
|
|
29
30
|
}
|
|
30
31
|
else if (beforeWithoutNameMatchedMap && afterMatchedMap) {
|
|
31
|
-
valueChars = (beforeWithoutNameMatchedMap[1]
|
|
32
|
+
valueChars = ((_h = beforeWithoutNameMatchedMap[1]) !== null && _h !== void 0 ? _h : '') + rawValue + ((_j = afterMatchedMap[1]) !== null && _j !== void 0 ? _j : '');
|
|
32
33
|
}
|
|
33
34
|
else if (reNameOnly.test(raw)) {
|
|
34
35
|
const token = (0, html_parser_1.attrTokenizer)(raw, line, col, startOffset);
|
package/lib/nodeize.js
CHANGED
|
@@ -4,9 +4,16 @@ exports.nodeize = void 0;
|
|
|
4
4
|
const html_parser_1 = require("@markuplint/html-parser");
|
|
5
5
|
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
6
6
|
const attr_1 = require("./attr");
|
|
7
|
+
const parse_ctrl_block_1 = require("./parse-ctrl-block");
|
|
7
8
|
const traverse_1 = require("./traverse");
|
|
8
|
-
function nodeize(
|
|
9
|
-
|
|
9
|
+
function nodeize(
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
11
|
+
originNode,
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
13
|
+
prevNode,
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
15
|
+
parentNode, rawHtml, options) {
|
|
16
|
+
var _a, _b, _c, _d, _e, _f;
|
|
10
17
|
const nextNode = null;
|
|
11
18
|
const { startOffset, endOffset, startLine, endLine, startCol, endCol, raw } = (0, parser_utils_1.sliceFragment)(rawHtml, originNode.start, originNode.end);
|
|
12
19
|
const parentNamespace = parentNode && 'namespace' in parentNode ? parentNode.namespace : 'http://www.w3.org/1999/xhtml';
|
|
@@ -51,11 +58,9 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
|
|
|
51
58
|
};
|
|
52
59
|
}
|
|
53
60
|
case 'Element': {
|
|
54
|
-
const children = originNode.children
|
|
61
|
+
const children = (_a = originNode.children) !== null && _a !== void 0 ? _a : [];
|
|
55
62
|
const reEndTag = new RegExp(`</${originNode.name}\\s*>$`, 'i');
|
|
56
|
-
const startTagEndOffset = children.length
|
|
57
|
-
? children[0].start
|
|
58
|
-
: raw.replace(reEndTag, '').length + startOffset;
|
|
63
|
+
const startTagEndOffset = children.length > 0 ? (_c = (_b = children[0]) === null || _b === void 0 ? void 0 : _b.start) !== null && _c !== void 0 ? _c : 0 : raw.replace(reEndTag, '').length + startOffset;
|
|
59
64
|
const startTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, startTagEndOffset);
|
|
60
65
|
let endTag = null;
|
|
61
66
|
if (reEndTag.test(raw)) {
|
|
@@ -91,7 +96,7 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
|
|
|
91
96
|
tagCloseChar: '>',
|
|
92
97
|
};
|
|
93
98
|
}
|
|
94
|
-
const directives = originNode.attributes.map(a => (0, attr_1.attr)(a, rawHtml))
|
|
99
|
+
const directives = (_d = originNode.attributes.map(a => (0, attr_1.attr)(a, rawHtml))) !== null && _d !== void 0 ? _d : [];
|
|
95
100
|
const attributes = directives.filter((d) => !('__spreadAttr' in d));
|
|
96
101
|
const hasSpreadAttr = directives.some(d => '__spreadAttr' in d);
|
|
97
102
|
const tagTokens = (0, html_parser_1.parseRawTag)(startTagLocation.raw, startTagLocation.startLine, startTagLocation.startCol, startTagLocation.startOffset);
|
|
@@ -125,10 +130,11 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
|
|
|
125
130
|
return startTag;
|
|
126
131
|
}
|
|
127
132
|
case 'IfBlock': {
|
|
128
|
-
|
|
133
|
+
const ifBlocks = (0, parse_ctrl_block_1.parseCtrlBlock)('if', originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options);
|
|
134
|
+
return ifBlocks;
|
|
129
135
|
}
|
|
130
136
|
case 'EachBlock': {
|
|
131
|
-
return
|
|
137
|
+
return (0, parse_ctrl_block_1.parseCtrlBlock)('each', originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options);
|
|
132
138
|
}
|
|
133
139
|
case 'AwaitBlock': {
|
|
134
140
|
const pendingNode = originNode.pending;
|
|
@@ -150,7 +156,7 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
|
|
|
150
156
|
let then = null;
|
|
151
157
|
if (originNode.then) {
|
|
152
158
|
const thenNode = originNode.then;
|
|
153
|
-
const thenTag = (0, parser_utils_1.sliceFragment)(rawHtml, thenNode.start, (
|
|
159
|
+
const thenTag = (0, parser_utils_1.sliceFragment)(rawHtml, thenNode.start, (_e = (thenNode.children && thenNode.children[0] && thenNode.children[0].start)) !== null && _e !== void 0 ? _e : thenNode.end);
|
|
154
160
|
then = {
|
|
155
161
|
uuid: (0, parser_utils_1.uuid)(),
|
|
156
162
|
...thenTag,
|
|
@@ -169,7 +175,7 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
|
|
|
169
175
|
let awaitCatch = null;
|
|
170
176
|
if (originNode.catch) {
|
|
171
177
|
const awaitCatchNode = originNode.catch;
|
|
172
|
-
const awaitCatchTag = (0, parser_utils_1.sliceFragment)(rawHtml, awaitCatchNode.start, (
|
|
178
|
+
const awaitCatchTag = (0, parser_utils_1.sliceFragment)(rawHtml, awaitCatchNode.start, (_f = (awaitCatchNode.children && awaitCatchNode.children[0] && awaitCatchNode.children[0].start)) !== null && _f !== void 0 ? _f : awaitCatchNode.end);
|
|
173
179
|
awaitCatch = {
|
|
174
180
|
uuid: (0, parser_utils_1.uuid)(),
|
|
175
181
|
...awaitCatchTag,
|
|
@@ -248,76 +254,3 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
|
|
|
248
254
|
}
|
|
249
255
|
}
|
|
250
256
|
exports.nodeize = nodeize;
|
|
251
|
-
function solveCtrlBlock(ctrlName, originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options) {
|
|
252
|
-
const children = originNode.children || [];
|
|
253
|
-
const reEndTag = new RegExp(`{/${ctrlName}}$`, 'i');
|
|
254
|
-
const startTagEndOffset = children.length ? children[0].start : raw.replace(reEndTag, '').length + startOffset;
|
|
255
|
-
const startTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, startTagEndOffset);
|
|
256
|
-
let endTag = null;
|
|
257
|
-
const endTagRawMatched = raw.match(reEndTag);
|
|
258
|
-
if (endTagRawMatched) {
|
|
259
|
-
const endTagRaw = endTagRawMatched[0];
|
|
260
|
-
const endTagStartOffset = originNode.end - endTagRaw.length;
|
|
261
|
-
const endTagEndOffset = originNode.end;
|
|
262
|
-
const endTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, endTagStartOffset, endTagEndOffset);
|
|
263
|
-
endTag = {
|
|
264
|
-
uuid: (0, parser_utils_1.uuid)(),
|
|
265
|
-
raw: endTagRaw,
|
|
266
|
-
startOffset: endTagStartOffset,
|
|
267
|
-
endOffset: endTagEndOffset,
|
|
268
|
-
startLine: endTagLocation.startLine,
|
|
269
|
-
endLine: endTagLocation.endLine,
|
|
270
|
-
startCol: endTagLocation.startCol,
|
|
271
|
-
endCol: endTagLocation.endCol,
|
|
272
|
-
nodeName: originNode.type,
|
|
273
|
-
type: 'psblock',
|
|
274
|
-
parentNode,
|
|
275
|
-
prevNode,
|
|
276
|
-
nextNode,
|
|
277
|
-
isFragment: false,
|
|
278
|
-
isGhost: false,
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
let elseTag = null;
|
|
282
|
-
if (originNode.else) {
|
|
283
|
-
const elseNode = originNode.else;
|
|
284
|
-
const elseTagStartOffset = children.length ? children[children.length - 1].end : startTagLocation.endOffset;
|
|
285
|
-
const elseTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, elseTagStartOffset, elseNode.start);
|
|
286
|
-
elseTag = {
|
|
287
|
-
uuid: (0, parser_utils_1.uuid)(),
|
|
288
|
-
...elseTagLocation,
|
|
289
|
-
nodeName: elseNode.type,
|
|
290
|
-
type: 'psblock',
|
|
291
|
-
parentNode,
|
|
292
|
-
prevNode,
|
|
293
|
-
nextNode,
|
|
294
|
-
isFragment: false,
|
|
295
|
-
isGhost: false,
|
|
296
|
-
};
|
|
297
|
-
if (elseNode.children) {
|
|
298
|
-
elseTag.childNodes = (0, traverse_1.traverse)(elseNode.children, elseTag, rawHtml, options);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
const tag = {
|
|
302
|
-
uuid: (0, parser_utils_1.uuid)(),
|
|
303
|
-
...startTagLocation,
|
|
304
|
-
nodeName: originNode.type,
|
|
305
|
-
type: 'psblock',
|
|
306
|
-
parentNode,
|
|
307
|
-
prevNode,
|
|
308
|
-
nextNode,
|
|
309
|
-
isFragment: false,
|
|
310
|
-
isGhost: false,
|
|
311
|
-
};
|
|
312
|
-
if (originNode.children) {
|
|
313
|
-
tag.childNodes = (0, traverse_1.traverse)(originNode.children, tag, rawHtml, options);
|
|
314
|
-
}
|
|
315
|
-
const tags = [tag];
|
|
316
|
-
if (elseTag) {
|
|
317
|
-
tags.push(elseTag);
|
|
318
|
-
}
|
|
319
|
-
if (endTag) {
|
|
320
|
-
tags.push(endTag);
|
|
321
|
-
}
|
|
322
|
-
return tags;
|
|
323
|
-
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SvelteNode } from './svelte-parser';
|
|
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[];
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseCtrlBlock = void 0;
|
|
4
|
+
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
5
|
+
const traverse_1 = require("./traverse");
|
|
6
|
+
function parseCtrlBlock(ctrlName,
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
8
|
+
originNode, raw, rawHtml, startOffset,
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
10
|
+
parentNode,
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
12
|
+
prevNode,
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
14
|
+
nextNode, options) {
|
|
15
|
+
var _a, _b, _c, _d, _e;
|
|
16
|
+
if (ctrlName === 'if') {
|
|
17
|
+
return parseIfBlock(originNode, raw, rawHtml, startOffset, originNode.start, parentNode, prevNode, nextNode, options);
|
|
18
|
+
}
|
|
19
|
+
const children = (_a = originNode.children) !== null && _a !== void 0 ? _a : [];
|
|
20
|
+
const reEndTag = new RegExp('{/each}$', 'i');
|
|
21
|
+
const startTagEndOffset = children.length > 0 ? (_c = (_b = children[0]) === null || _b === void 0 ? void 0 : _b.start) !== null && _c !== void 0 ? _c : 0 : raw.replace(reEndTag, '').length + startOffset;
|
|
22
|
+
const startTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, startTagEndOffset);
|
|
23
|
+
const tag = {
|
|
24
|
+
uuid: (0, parser_utils_1.uuid)(),
|
|
25
|
+
...startTagLocation,
|
|
26
|
+
nodeName: originNode.type,
|
|
27
|
+
type: 'psblock',
|
|
28
|
+
parentNode,
|
|
29
|
+
prevNode,
|
|
30
|
+
nextNode,
|
|
31
|
+
isFragment: false,
|
|
32
|
+
isGhost: false,
|
|
33
|
+
};
|
|
34
|
+
if (originNode.children) {
|
|
35
|
+
tag.childNodes = (0, traverse_1.traverse)(originNode.children, tag, rawHtml, options);
|
|
36
|
+
}
|
|
37
|
+
let elseTag = null;
|
|
38
|
+
if (originNode.else) {
|
|
39
|
+
const elseNode = originNode.else;
|
|
40
|
+
const elseTagStartOffset = children.length > 0 ? (_e = (_d = children[children.length - 1]) === null || _d === void 0 ? void 0 : _d.end) !== null && _e !== void 0 ? _e : 0 : startTagLocation.endOffset;
|
|
41
|
+
const elseTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, elseTagStartOffset, elseNode.start);
|
|
42
|
+
elseTag = {
|
|
43
|
+
uuid: (0, parser_utils_1.uuid)(),
|
|
44
|
+
...elseTagLocation,
|
|
45
|
+
nodeName: elseNode.type,
|
|
46
|
+
type: 'psblock',
|
|
47
|
+
parentNode,
|
|
48
|
+
prevNode,
|
|
49
|
+
nextNode,
|
|
50
|
+
isFragment: false,
|
|
51
|
+
isGhost: false,
|
|
52
|
+
};
|
|
53
|
+
if (elseNode.children) {
|
|
54
|
+
elseTag.childNodes = (0, traverse_1.traverse)(elseNode.children, elseTag, rawHtml, options);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
let endTag = null;
|
|
58
|
+
const endTagRawMatched = raw.match(reEndTag);
|
|
59
|
+
if (endTagRawMatched) {
|
|
60
|
+
const endTagRaw = endTagRawMatched[0];
|
|
61
|
+
const endTagStartOffset = originNode.end - endTagRaw.length;
|
|
62
|
+
const endTagEndOffset = originNode.end;
|
|
63
|
+
const endTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, endTagStartOffset, endTagEndOffset);
|
|
64
|
+
endTag = {
|
|
65
|
+
uuid: (0, parser_utils_1.uuid)(),
|
|
66
|
+
raw: endTagRaw,
|
|
67
|
+
startOffset: endTagStartOffset,
|
|
68
|
+
endOffset: endTagEndOffset,
|
|
69
|
+
startLine: endTagLocation.startLine,
|
|
70
|
+
endLine: endTagLocation.endLine,
|
|
71
|
+
startCol: endTagLocation.startCol,
|
|
72
|
+
endCol: endTagLocation.endCol,
|
|
73
|
+
nodeName: originNode.type,
|
|
74
|
+
type: 'psblock',
|
|
75
|
+
parentNode,
|
|
76
|
+
prevNode,
|
|
77
|
+
nextNode,
|
|
78
|
+
isFragment: false,
|
|
79
|
+
isGhost: false,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const tags = [tag];
|
|
83
|
+
if (elseTag) {
|
|
84
|
+
tags.push(elseTag);
|
|
85
|
+
}
|
|
86
|
+
if (endTag) {
|
|
87
|
+
tags.push(endTag);
|
|
88
|
+
}
|
|
89
|
+
return tags;
|
|
90
|
+
}
|
|
91
|
+
exports.parseCtrlBlock = parseCtrlBlock;
|
|
92
|
+
function parseIfBlock(
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
94
|
+
originNode, raw, rawHtml, statementStartOffset, tokenStartOffset,
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
96
|
+
parentNode,
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
98
|
+
prevNode,
|
|
99
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
100
|
+
nextNode, options) {
|
|
101
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
102
|
+
const children = (_a = originNode.children) !== null && _a !== void 0 ? _a : [];
|
|
103
|
+
const startTagEndOffset = (_c = (_b = children[0]) === null || _b === void 0 ? void 0 : _b.start) !== null && _c !== void 0 ? _c : tokenStartOffset;
|
|
104
|
+
const startTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, tokenStartOffset, startTagEndOffset);
|
|
105
|
+
const tag = {
|
|
106
|
+
uuid: (0, parser_utils_1.uuid)(),
|
|
107
|
+
...startTagLocation,
|
|
108
|
+
nodeName: originNode.elseif ? 'ElseIfBlock' : originNode.type,
|
|
109
|
+
type: 'psblock',
|
|
110
|
+
parentNode,
|
|
111
|
+
prevNode,
|
|
112
|
+
nextNode,
|
|
113
|
+
isFragment: false,
|
|
114
|
+
isGhost: false,
|
|
115
|
+
};
|
|
116
|
+
if (originNode.children) {
|
|
117
|
+
tag.childNodes = (0, traverse_1.traverse)(originNode.children, tag, rawHtml, options);
|
|
118
|
+
}
|
|
119
|
+
const elseOrElseIfTags = [];
|
|
120
|
+
if (originNode.else) {
|
|
121
|
+
const elseNode = originNode.else;
|
|
122
|
+
const elseTagStartOffset = children.length > 0 ? (_e = (_d = children[children.length - 1]) === null || _d === void 0 ? void 0 : _d.end) !== null && _e !== void 0 ? _e : 0 : startTagLocation.endOffset;
|
|
123
|
+
const elseTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, elseTagStartOffset, elseNode.start);
|
|
124
|
+
if (elseNode.children) {
|
|
125
|
+
if (elseNode.children.length === 1 && ((_f = elseNode.children[0]) === null || _f === void 0 ? void 0 : _f.type) === 'IfBlock') {
|
|
126
|
+
const elseIfTags = parseIfBlock(elseNode.children[0], elseTagLocation.raw, rawHtml, statementStartOffset, (_j = (_h = (_g = originNode.children) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.end) !== null && _j !== void 0 ? _j : startTagLocation.endOffset, parentNode, null, null, options);
|
|
127
|
+
elseOrElseIfTags.push(...elseIfTags);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const elseTag = {
|
|
131
|
+
uuid: (0, parser_utils_1.uuid)(),
|
|
132
|
+
...elseTagLocation,
|
|
133
|
+
nodeName: elseNode.type,
|
|
134
|
+
type: 'psblock',
|
|
135
|
+
parentNode,
|
|
136
|
+
prevNode,
|
|
137
|
+
nextNode,
|
|
138
|
+
isFragment: false,
|
|
139
|
+
isGhost: false,
|
|
140
|
+
};
|
|
141
|
+
elseTag.childNodes = (0, traverse_1.traverse)(elseNode.children, elseTag, rawHtml, options);
|
|
142
|
+
elseOrElseIfTags.push(elseTag);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (originNode.elseif) {
|
|
147
|
+
return [tag, ...elseOrElseIfTags];
|
|
148
|
+
}
|
|
149
|
+
const endTagRawMatched = raw.match('{/if}');
|
|
150
|
+
if (!endTagRawMatched) {
|
|
151
|
+
throw new Error('Missing the end token `{/if}`');
|
|
152
|
+
}
|
|
153
|
+
const endTagRaw = endTagRawMatched[0];
|
|
154
|
+
const endTagStartOffset = originNode.end - endTagRaw.length;
|
|
155
|
+
const endTagEndOffset = originNode.end;
|
|
156
|
+
const endTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, endTagStartOffset, endTagEndOffset);
|
|
157
|
+
const endTag = {
|
|
158
|
+
uuid: (0, parser_utils_1.uuid)(),
|
|
159
|
+
raw: endTagRaw,
|
|
160
|
+
startOffset: endTagStartOffset,
|
|
161
|
+
endOffset: endTagEndOffset,
|
|
162
|
+
startLine: endTagLocation.startLine,
|
|
163
|
+
endLine: endTagLocation.endLine,
|
|
164
|
+
startCol: endTagLocation.startCol,
|
|
165
|
+
endCol: endTagLocation.endCol,
|
|
166
|
+
nodeName: originNode.type,
|
|
167
|
+
type: 'psblock',
|
|
168
|
+
parentNode,
|
|
169
|
+
prevNode,
|
|
170
|
+
nextNode,
|
|
171
|
+
isFragment: false,
|
|
172
|
+
isGhost: false,
|
|
173
|
+
};
|
|
174
|
+
return [tag, ...elseOrElseIfTags, endTag];
|
|
175
|
+
}
|
package/lib/parse.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parse = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const html_parser_1 = require("@markuplint/html-parser");
|
|
6
5
|
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
7
6
|
const svelte_parser_1 = tslib_1.__importDefault(require("./svelte-parser"));
|
|
8
7
|
const traverse_1 = require("./traverse");
|
|
@@ -43,7 +42,8 @@ const parse = (rawCode, options) => {
|
|
|
43
42
|
parseError: err instanceof Error ? err.message : new Error(`${err}`).message,
|
|
44
43
|
};
|
|
45
44
|
}
|
|
46
|
-
const
|
|
45
|
+
const nodes = (0, traverse_1.traverse)(ast, null, blocks.replaced, options);
|
|
46
|
+
const nodeList = (0, parser_utils_1.restoreNode)((0, parser_utils_1.flattenNodes)(nodes, blocks.replaced), blocks);
|
|
47
47
|
return {
|
|
48
48
|
nodeList,
|
|
49
49
|
isFragment: true,
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const compiler_1 = require("svelte/compiler");
|
|
4
4
|
function svelteParse(template) {
|
|
5
|
+
var _a;
|
|
5
6
|
const ast = (0, compiler_1.parse)(template, { customElement: true });
|
|
6
7
|
const start = ast.html.start;
|
|
7
|
-
const children = ast.html.children
|
|
8
|
+
const children = (_a = ast.html.children) !== null && _a !== void 0 ? _a : [];
|
|
8
9
|
if (children[0] && children[0].end === start) {
|
|
9
10
|
children.shift();
|
|
10
11
|
}
|
package/lib/traverse.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SvelteNode } from './svelte-parser';
|
|
2
2
|
import type { MLASTNode, MLASTParentNode, ParserOptions } from '@markuplint/ml-ast';
|
|
3
3
|
export declare function traverse(
|
|
4
|
-
astNodes: SvelteNode[],
|
|
4
|
+
astNodes: readonly SvelteNode[],
|
|
5
5
|
parentNode: MLASTParentNode | null | undefined,
|
|
6
6
|
rawHtml: string,
|
|
7
7
|
options?: ParserOptions,
|
package/lib/traverse.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.traverse = void 0;
|
|
4
4
|
const nodeize_1 = require("./nodeize");
|
|
5
|
-
function traverse(
|
|
5
|
+
function traverse(
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
7
|
+
astNodes, parentNode = null, rawHtml, options) {
|
|
6
8
|
const nodeList = [];
|
|
7
9
|
let prevNode = null;
|
|
8
10
|
for (const astNode of astNodes) {
|
|
@@ -12,7 +14,11 @@ function traverse(astNodes, parentNode = null, rawHtml, options) {
|
|
|
12
14
|
}
|
|
13
15
|
let node;
|
|
14
16
|
if (Array.isArray(nodes)) {
|
|
15
|
-
|
|
17
|
+
const lastNode = nodes[nodes.length - 1];
|
|
18
|
+
if (!lastNode) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
node = lastNode;
|
|
16
22
|
}
|
|
17
23
|
else {
|
|
18
24
|
node = nodes;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/svelte-parser",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-dev.176+f6ad62e9",
|
|
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.0.0-
|
|
20
|
-
"@markuplint/ml-ast": "3.0.0-
|
|
21
|
-
"@markuplint/parser-utils": "3.0.0-
|
|
22
|
-
"svelte": "^3.
|
|
23
|
-
"tslib": "^2.4.
|
|
19
|
+
"@markuplint/html-parser": "3.0.0-dev.176+f6ad62e9",
|
|
20
|
+
"@markuplint/ml-ast": "3.0.0-dev.176+f6ad62e9",
|
|
21
|
+
"@markuplint/parser-utils": "3.0.0-dev.176+f6ad62e9",
|
|
22
|
+
"svelte": "^3.58.0",
|
|
23
|
+
"tslib": "^2.4.1"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "f6ad62e992e1569be4067f1e90d2d6017a658f57"
|
|
26
26
|
}
|