@markuplint/svelte-parser 4.0.0-alpha.3 → 4.0.0-alpha.5
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/LICENSE +1 -1
- package/lib/attr.js +3 -3
- package/lib/directive-tokenizer.d.ts +1 -1
- package/lib/directive-tokenizer.js +15 -100
- package/lib/nodeize.js +9 -8
- package/lib/parse-ctrl-block.js +4 -3
- package/lib/parse.js +12 -12
- package/lib/svelte-parser/index.d.ts +1 -1
- package/lib/svelte-parser/index.js +1 -1
- package/lib/traverse.js +1 -1
- package/package.json +6 -7
package/LICENSE
CHANGED
package/lib/attr.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { defaultValueDelimiters, parseAttr, sliceFragment } from '@markuplint/parser-utils';
|
|
2
|
-
import directiveTokenizer from './directive-tokenizer.js';
|
|
2
|
+
import { directiveTokenizer } from './directive-tokenizer.js';
|
|
3
3
|
const mustacheTag = {
|
|
4
4
|
start: '{',
|
|
5
5
|
end: '}',
|
|
6
6
|
};
|
|
7
|
-
const specificBindDirective = ['bind:group', 'bind:this'];
|
|
7
|
+
const specificBindDirective = new Set(['bind:group', 'bind:this']);
|
|
8
8
|
export function attr(
|
|
9
9
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
10
10
|
attr, rawHTML) {
|
|
@@ -33,7 +33,7 @@ attr, rawHTML) {
|
|
|
33
33
|
: '';
|
|
34
34
|
token = directiveTokenizer(raw, valueToken, startLine, startCol, startOffset);
|
|
35
35
|
}
|
|
36
|
-
if (!specificBindDirective.
|
|
36
|
+
if (!specificBindDirective.has(token.name.raw) && /^bind:/i.test(token.name.raw)) {
|
|
37
37
|
// Remove "bind:"
|
|
38
38
|
token.potentialName = token.name.raw.slice(5);
|
|
39
39
|
token.isDirective = undefined;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { MLASTHTMLAttr } from '@markuplint/ml-ast';
|
|
2
|
-
export
|
|
2
|
+
export declare function directiveTokenizer(raw: string, rawValue: string, line: number, col: number, startOffset: number): MLASTHTMLAttr;
|
|
@@ -1,101 +1,16 @@
|
|
|
1
|
-
import { attrTokenizer } from '@markuplint/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const [before, after] = raw.split(rawValue);
|
|
17
|
-
const beforeMatchedMap = before?.match(reBeforeStructure);
|
|
18
|
-
const beforeWithoutNameMatchedMap = before?.match(reBeforeStructureWithoutName);
|
|
19
|
-
const afterMatchedMap = after?.match(reAfterStructure);
|
|
20
|
-
if (beforeMatchedMap && afterMatchedMap) {
|
|
21
|
-
spacesBeforeAttrString = beforeMatchedMap[1] ?? '';
|
|
22
|
-
nameChars = beforeMatchedMap[2] ?? '';
|
|
23
|
-
spacesBeforeEqualChars = beforeMatchedMap[3] ?? '';
|
|
24
|
-
equalChars = beforeMatchedMap[4] ?? null;
|
|
25
|
-
spacesAfterEqualChars = beforeMatchedMap[5] ?? '';
|
|
26
|
-
valueChars = (beforeMatchedMap[6] ?? '') + rawValue + (afterMatchedMap[1] ?? '');
|
|
27
|
-
}
|
|
28
|
-
else if (beforeWithoutNameMatchedMap && afterMatchedMap) {
|
|
29
|
-
valueChars = (beforeWithoutNameMatchedMap[1] ?? '') + rawValue + (afterMatchedMap[1] ?? '');
|
|
30
|
-
}
|
|
31
|
-
else if (reNameOnly.test(raw)) {
|
|
32
|
-
const token = attrTokenizer(raw, line, col, startOffset);
|
|
33
|
-
token.isDirective = true;
|
|
34
|
-
return token;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
throw new SyntaxError('Illegal attribute token');
|
|
38
|
-
}
|
|
39
|
-
let offset = startOffset;
|
|
40
|
-
const attrToken = tokenizer(raw, line, col, offset);
|
|
41
|
-
const spacesBeforeName = tokenizer(spacesBeforeAttrString, line, col, offset);
|
|
42
|
-
line = spacesBeforeName.endLine;
|
|
43
|
-
col = spacesBeforeName.endCol;
|
|
44
|
-
offset = spacesBeforeName.endOffset;
|
|
45
|
-
const name = tokenizer(nameChars, line, col, offset);
|
|
46
|
-
line = name.endLine;
|
|
47
|
-
col = name.endCol;
|
|
48
|
-
offset = name.endOffset;
|
|
49
|
-
const spacesBeforeEqual = tokenizer(spacesBeforeEqualChars, line, col, offset);
|
|
50
|
-
line = spacesBeforeEqual.endLine;
|
|
51
|
-
col = spacesBeforeEqual.endCol;
|
|
52
|
-
offset = spacesBeforeEqual.endOffset;
|
|
53
|
-
const equal = tokenizer(equalChars, line, col, offset);
|
|
54
|
-
line = equal.endLine;
|
|
55
|
-
col = equal.endCol;
|
|
56
|
-
offset = equal.endOffset;
|
|
57
|
-
const spacesAfterEqual = tokenizer(spacesAfterEqualChars, line, col, offset);
|
|
58
|
-
line = spacesAfterEqual.endLine;
|
|
59
|
-
col = spacesAfterEqual.endCol;
|
|
60
|
-
offset = spacesAfterEqual.endOffset;
|
|
61
|
-
const startQuote = tokenizer('{', line, col, offset);
|
|
62
|
-
line = startQuote.endLine;
|
|
63
|
-
col = startQuote.endCol;
|
|
64
|
-
offset = startQuote.endOffset;
|
|
65
|
-
const value = tokenizer(valueChars, line, col, offset);
|
|
66
|
-
line = value.endLine;
|
|
67
|
-
col = value.endCol;
|
|
68
|
-
offset = value.endOffset;
|
|
69
|
-
const endQuote = tokenizer('}', line, col, offset);
|
|
70
|
-
line = endQuote.endLine;
|
|
71
|
-
col = endQuote.endCol;
|
|
72
|
-
offset = endQuote.endOffset;
|
|
73
|
-
const result = {
|
|
74
|
-
type: 'html-attr',
|
|
75
|
-
uuid: uuid(),
|
|
76
|
-
raw: attrToken.raw,
|
|
77
|
-
startOffset: attrToken.startOffset,
|
|
78
|
-
endOffset: attrToken.endOffset,
|
|
79
|
-
startLine: attrToken.startLine,
|
|
80
|
-
endLine: attrToken.endLine,
|
|
81
|
-
startCol: attrToken.startCol,
|
|
82
|
-
endCol: attrToken.endCol,
|
|
83
|
-
spacesBeforeName,
|
|
84
|
-
name,
|
|
85
|
-
spacesBeforeEqual,
|
|
86
|
-
equal,
|
|
87
|
-
spacesAfterEqual,
|
|
88
|
-
startQuote,
|
|
89
|
-
value,
|
|
90
|
-
endQuote,
|
|
91
|
-
isDirective: true,
|
|
92
|
-
isDuplicatable: false,
|
|
93
|
-
nodeName: name.raw,
|
|
94
|
-
parentNode: null,
|
|
95
|
-
nextNode: null,
|
|
96
|
-
prevNode: null,
|
|
97
|
-
isFragment: false,
|
|
98
|
-
isGhost: false,
|
|
99
|
-
};
|
|
100
|
-
return result;
|
|
1
|
+
import { AttrState, attrTokenizer } from '@markuplint/parser-utils';
|
|
2
|
+
export function directiveTokenizer(raw, rawValue, line, col, startOffset) {
|
|
3
|
+
const nameOnly = raw.trim().startsWith('{') && raw.trim().endsWith('}');
|
|
4
|
+
const directiveToken = attrTokenizer(raw, line, col, startOffset, [
|
|
5
|
+
{ start: '"', end: '"' },
|
|
6
|
+
{ start: "'", end: "'" },
|
|
7
|
+
{ start: '{', end: '}' },
|
|
8
|
+
], nameOnly ? AttrState.BeforeValue : AttrState.BeforeName, [
|
|
9
|
+
{ start: '"', end: '"' },
|
|
10
|
+
{ start: "'", end: "'" },
|
|
11
|
+
{ start: '`', end: '`' },
|
|
12
|
+
{ start: '${', end: '}' },
|
|
13
|
+
]);
|
|
14
|
+
directiveToken.isDirective = true;
|
|
15
|
+
return directiveToken;
|
|
101
16
|
}
|
package/lib/nodeize.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getNamespace
|
|
2
|
-
import { detectElementType, sliceFragment, uuid } from '@markuplint/parser-utils';
|
|
1
|
+
import { getNamespace } from '@markuplint/html-parser';
|
|
2
|
+
import { detectElementType, sliceFragment, uuid, tagParser } from '@markuplint/parser-utils';
|
|
3
3
|
import { attr } from './attr.js';
|
|
4
4
|
import { parseCtrlBlock } from './parse-ctrl-block.js';
|
|
5
5
|
import { traverse } from './traverse.js';
|
|
@@ -96,7 +96,7 @@ parentNode, rawHtml, options) {
|
|
|
96
96
|
const directives = originNode.attributes.map(a => attr(a, rawHtml)) ?? [];
|
|
97
97
|
const attributes = directives.filter((d) => !('__spreadAttr' in d));
|
|
98
98
|
const hasSpreadAttr = directives.some(d => '__spreadAttr' in d);
|
|
99
|
-
const tagTokens =
|
|
99
|
+
const tagTokens = tagParser(startTagLocation.raw, startTagLocation.startLine, startTagLocation.startCol, startTagLocation.startOffset);
|
|
100
100
|
const namespace = getNamespace(originNode.name, parentNamespace);
|
|
101
101
|
const startTag = {
|
|
102
102
|
uuid: uuid(),
|
|
@@ -104,7 +104,7 @@ parentNode, rawHtml, options) {
|
|
|
104
104
|
nodeName: originNode.name,
|
|
105
105
|
type: 'starttag',
|
|
106
106
|
namespace,
|
|
107
|
-
elementType: detectElementType(originNode.name, options?.authoredElementName, /[A-Z]
|
|
107
|
+
elementType: detectElementType(originNode.name, options?.authoredElementName, /[.A-Z]/),
|
|
108
108
|
attributes,
|
|
109
109
|
hasSpreadAttr,
|
|
110
110
|
parentNode,
|
|
@@ -112,7 +112,7 @@ parentNode, rawHtml, options) {
|
|
|
112
112
|
nextNode,
|
|
113
113
|
pearNode: endTag,
|
|
114
114
|
selfClosingSolidus: tagTokens.selfClosingSolidus,
|
|
115
|
-
endSpace: tagTokens.
|
|
115
|
+
endSpace: tagTokens.afterAttrSpaces,
|
|
116
116
|
isFragment: false,
|
|
117
117
|
isGhost: false,
|
|
118
118
|
tagOpenChar: '<',
|
|
@@ -189,7 +189,8 @@ parentNode, rawHtml, options) {
|
|
|
189
189
|
awaitCatch.childNodes = traverse(awaitCatchNode.children, awaitCatch, rawHtml, options);
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
|
-
|
|
192
|
+
// eslint-disable-next-line regexp/strict
|
|
193
|
+
const reEndTag = /{\/await}$/i;
|
|
193
194
|
let endTag = null;
|
|
194
195
|
if (reEndTag.test(raw)) {
|
|
195
196
|
const endTagRawMatched = raw.match(reEndTag);
|
|
@@ -258,7 +259,7 @@ parentNode, rawHtml, options) {
|
|
|
258
259
|
startTag.endCol = firstChild.startCol;
|
|
259
260
|
startTag.raw = rawHtml.slice(startTag.startOffset, startTag.endOffset);
|
|
260
261
|
}
|
|
261
|
-
const lastChild = startTag.childNodes
|
|
262
|
+
const lastChild = startTag.childNodes.at(-1);
|
|
262
263
|
if (lastChild && lastChild.endOffset > startTag.endOffset) {
|
|
263
264
|
const startOffset = lastChild.endOffset;
|
|
264
265
|
const startLine = lastChild.endLine;
|
|
@@ -283,7 +284,7 @@ parentNode, rawHtml, options) {
|
|
|
283
284
|
};
|
|
284
285
|
}
|
|
285
286
|
}
|
|
286
|
-
return endTag
|
|
287
|
+
return endTag == null ? startTag : [startTag, endTag];
|
|
287
288
|
}
|
|
288
289
|
}
|
|
289
290
|
}
|
package/lib/parse-ctrl-block.js
CHANGED
|
@@ -13,7 +13,8 @@ nextNode, options) {
|
|
|
13
13
|
return parseIfBlock(originNode, raw, rawHtml, startOffset, originNode.start, parentNode, prevNode, nextNode, options);
|
|
14
14
|
}
|
|
15
15
|
const children = originNode.children ?? [];
|
|
16
|
-
|
|
16
|
+
// eslint-disable-next-line regexp/strict
|
|
17
|
+
const reEndTag = /{\/each}$/i;
|
|
17
18
|
const startTagEndOffset = children.length > 0 ? children[0]?.start ?? 0 : raw.replace(reEndTag, '').length + startOffset;
|
|
18
19
|
const startTagLocation = sliceFragment(rawHtml, startOffset, startTagEndOffset);
|
|
19
20
|
const tag = {
|
|
@@ -33,7 +34,7 @@ nextNode, options) {
|
|
|
33
34
|
let elseTag = null;
|
|
34
35
|
if (originNode.else) {
|
|
35
36
|
const elseNode = originNode.else;
|
|
36
|
-
const elseTagStartOffset = children.length > 0 ? children
|
|
37
|
+
const elseTagStartOffset = children.length > 0 ? children.at(-1)?.end ?? 0 : startTagLocation.endOffset;
|
|
37
38
|
const elseTagLocation = sliceFragment(rawHtml, elseTagStartOffset, elseNode.start);
|
|
38
39
|
elseTag = {
|
|
39
40
|
uuid: uuid(),
|
|
@@ -113,7 +114,7 @@ nextNode, options) {
|
|
|
113
114
|
const elseOrElseIfTags = [];
|
|
114
115
|
if (originNode.else) {
|
|
115
116
|
const elseNode = originNode.else;
|
|
116
|
-
const elseTagStartOffset = children.length > 0 ? children
|
|
117
|
+
const elseTagStartOffset = children.length > 0 ? children.at(-1)?.end ?? 0 : startTagLocation.endOffset;
|
|
117
118
|
const elseTagLocation = sliceFragment(rawHtml, elseTagStartOffset, elseNode.start);
|
|
118
119
|
if (elseNode.children) {
|
|
119
120
|
if (elseNode.children.length === 1 && elseNode.children[0]?.type === 'IfBlock') {
|
package/lib/parse.js
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
import { flattenNodes, ParserError, ignoreBlock, restoreNode } from '@markuplint/parser-utils';
|
|
2
|
-
import svelteParse from './svelte-parser/index.js';
|
|
2
|
+
import { svelteParse } from './svelte-parser/index.js';
|
|
3
3
|
import { traverse } from './traverse.js';
|
|
4
4
|
export const parse = (rawCode, options) => {
|
|
5
5
|
const blocks = ignoreBlock(rawCode, [
|
|
6
6
|
{
|
|
7
7
|
type: 'Script',
|
|
8
|
-
start:
|
|
9
|
-
end:
|
|
8
|
+
start: '<script',
|
|
9
|
+
end: '</script>',
|
|
10
10
|
},
|
|
11
11
|
{
|
|
12
12
|
type: 'Style',
|
|
13
|
-
start:
|
|
14
|
-
end:
|
|
13
|
+
start: '<style',
|
|
14
|
+
end: '</style>',
|
|
15
15
|
},
|
|
16
16
|
], '-');
|
|
17
17
|
let ast;
|
|
18
18
|
try {
|
|
19
19
|
ast = svelteParse(blocks.replaced);
|
|
20
20
|
}
|
|
21
|
-
catch (
|
|
22
|
-
if (
|
|
21
|
+
catch (error) {
|
|
22
|
+
if (error instanceof Error && 'start' in error && 'end' in error && 'frame' in error) {
|
|
23
23
|
// @ts-ignore
|
|
24
|
-
const raw = rawCode.slice(
|
|
24
|
+
const raw = rawCode.slice(error.start.character, error.end.character);
|
|
25
25
|
throw new ParserError(
|
|
26
26
|
// @ts-ignore
|
|
27
|
-
|
|
27
|
+
error.message + '\n' + error.frame, {
|
|
28
28
|
// @ts-ignore
|
|
29
|
-
line:
|
|
29
|
+
line: error.start.line,
|
|
30
30
|
// @ts-ignore
|
|
31
|
-
col:
|
|
31
|
+
col: error.start.column,
|
|
32
32
|
raw,
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
return {
|
|
36
36
|
nodeList: [],
|
|
37
37
|
isFragment: true,
|
|
38
|
-
parseError:
|
|
38
|
+
parseError: error instanceof Error ? error.message : new Error(`${error}`).message,
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
const nodes = traverse(ast, null, blocks.replaced, options);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Directive, TemplateNode, Attribute, SpreadAttribute } from 'svelte/types/compiler/interfaces';
|
|
2
2
|
export type SvelteNode = TemplateNode;
|
|
3
|
-
export
|
|
3
|
+
export declare function svelteParse(template: string): SvelteNode[];
|
|
4
4
|
export type SvelteDirective = Directive | Attribute | SpreadAttribute;
|
package/lib/traverse.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/svelte-parser",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.5",
|
|
4
4
|
"description": "Svelte parser for markuplint",
|
|
5
5
|
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
6
|
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
@@ -21,11 +21,10 @@
|
|
|
21
21
|
"clean": "tsc --build --clean"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@markuplint/html-parser": "4.0.0-alpha.
|
|
25
|
-
"@markuplint/ml-ast": "4.0.0-alpha.
|
|
26
|
-
"@markuplint/parser-utils": "4.0.0-alpha.
|
|
27
|
-
"svelte": "^4.2.
|
|
28
|
-
"tslib": "^2.6.2"
|
|
24
|
+
"@markuplint/html-parser": "4.0.0-alpha.5",
|
|
25
|
+
"@markuplint/ml-ast": "4.0.0-alpha.5",
|
|
26
|
+
"@markuplint/parser-utils": "4.0.0-alpha.5",
|
|
27
|
+
"svelte": "^4.2.2"
|
|
29
28
|
},
|
|
30
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "0c3e4690662edf1765bcc4b6411ec5507c1e2ea3"
|
|
31
30
|
}
|