@markuplint/astro-parser 3.5.0 → 3.6.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/astro-parser.d.ts +3 -11
- package/lib/astro-parser.js +11 -16
- package/lib/attr-tokenizer.d.ts +2 -2
- package/lib/attr-tokenizer.js +32 -70
- package/lib/index.d.ts +1 -1
- package/lib/parse.js +127 -135
- package/package.json +8 -4
package/lib/astro-parser.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export type ASTNode = TemplateNode;
|
|
5
|
-
export type ASTStyleNode = Style;
|
|
6
|
-
export type ASTAttribute = Attribute;
|
|
7
|
-
export type AstroAST = {
|
|
8
|
-
html?: ASTNode;
|
|
9
|
-
style?: ASTStyleNode[];
|
|
10
|
-
};
|
|
11
|
-
export declare function astroParse(code: string): AstroAST | AstroCompileError;
|
|
1
|
+
import type { RootNode } from '@astrojs/compiler/shared/ast';
|
|
2
|
+
export type { RootNode, ElementNode, CustomElementNode, ComponentNode, FragmentNode, AttributeNode, Node, } from '@astrojs/compiler/shared/ast';
|
|
3
|
+
export declare function astroParse(code: string): RootNode;
|
package/lib/astro-parser.js
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.astroParse =
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
Object.defineProperty(exports, "AstroCompileError", { enumerable: true, get: function () { return parser_2.CompileError; } });
|
|
3
|
+
exports.astroParse = void 0;
|
|
4
|
+
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
5
|
+
const astro_eslint_parser_1 = require("astro-eslint-parser");
|
|
7
6
|
function astroParse(code) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
catch (e) {
|
|
16
|
-
if (e instanceof parser_1.CompileError) {
|
|
17
|
-
return e;
|
|
18
|
-
}
|
|
19
|
-
throw e;
|
|
7
|
+
const { result } = (0, astro_eslint_parser_1.parseTemplate)(code);
|
|
8
|
+
if (result.diagnostics[0]) {
|
|
9
|
+
const error = result.diagnostics[0];
|
|
10
|
+
throw new parser_utils_1.ParserError(error.text, {
|
|
11
|
+
line: error.location.line,
|
|
12
|
+
col: error.location.column,
|
|
13
|
+
});
|
|
20
14
|
}
|
|
15
|
+
return result.ast;
|
|
21
16
|
}
|
|
22
17
|
exports.astroParse = astroParse;
|
package/lib/attr-tokenizer.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AttributeNode } from './astro-parser';
|
|
2
2
|
import type { MLASTHTMLAttr } from '@markuplint/ml-ast';
|
|
3
|
-
export declare function attrTokenizer(attr:
|
|
3
|
+
export declare function attrTokenizer(attr: AttributeNode, nextAttr: AttributeNode | null, rawHtml: string, startTag: string, startTagEndOffset: number): MLASTHTMLAttr;
|
package/lib/attr-tokenizer.js
CHANGED
|
@@ -2,87 +2,49 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.attrTokenizer = void 0;
|
|
4
4
|
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
5
|
+
const mustacheTag = {
|
|
6
|
+
start: '{',
|
|
7
|
+
end: '}',
|
|
8
|
+
};
|
|
5
9
|
function attrTokenizer(
|
|
6
10
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
7
|
-
attr,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
})) || undefined;
|
|
14
|
-
const attrToken = (0, parser_utils_1.createTokenFromRawCode)(raw, startOffset, rawHtml);
|
|
15
|
-
const beforeCode = rawHtml.slice(0, attrToken.startOffset);
|
|
16
|
-
const beforeAttrMatched = beforeCode.match(/\s+$/m);
|
|
17
|
-
const beforeAttrChar = (beforeAttrMatched === null || beforeAttrMatched === void 0 ? void 0 : beforeAttrMatched[0]) || '';
|
|
18
|
-
const spacesBeforeName = (0, parser_utils_1.createTokenFromRawCode)(beforeAttrChar, attrToken.startOffset - beforeAttrChar.length, rawHtml);
|
|
19
|
-
const name = (0, parser_utils_1.createTokenFromRawCode)(attr.name, spacesBeforeName.endOffset, rawHtml);
|
|
20
|
-
let rawValue;
|
|
21
|
-
let rawValueStart;
|
|
22
|
-
if (
|
|
23
|
-
//
|
|
24
|
-
!attr.value ||
|
|
25
|
-
// @ts-ignore
|
|
26
|
-
attr.value === true ||
|
|
27
|
-
//
|
|
28
|
-
attr.value.length === 0) {
|
|
29
|
-
rawValue = '';
|
|
30
|
-
rawValueStart = name.endOffset;
|
|
11
|
+
attr,
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
13
|
+
nextAttr, rawHtml, startTag, startTagEndOffset) {
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
if (!attr.position) {
|
|
16
|
+
throw new TypeError("Attr doesn't have position");
|
|
31
17
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
18
|
+
if (attr.position.end) {
|
|
19
|
+
throw new TypeError('Node may is an attribute because it has end position');
|
|
20
|
+
}
|
|
21
|
+
let nextAttrBeforeSpaceOffset;
|
|
22
|
+
if (nextAttr) {
|
|
23
|
+
if (!nextAttr.position) {
|
|
24
|
+
throw new TypeError("NextAttr doesn't have position");
|
|
25
|
+
}
|
|
26
|
+
if (nextAttr.position.end) {
|
|
27
|
+
throw new TypeError('NextAttr Node may is an attribute because it has end position');
|
|
28
|
+
}
|
|
29
|
+
nextAttrBeforeSpaceOffset = (0, parser_utils_1.getSpaceBefore)(nextAttr.position.start.offset, rawHtml).startOffset;
|
|
35
30
|
}
|
|
36
31
|
else {
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
const close = (_b = (_a = /(\/?>)$/.exec(startTag)) === null || _a === void 0 ? void 0 : _a[1]) !== null && _b !== void 0 ? _b : '';
|
|
33
|
+
nextAttrBeforeSpaceOffset = startTagEndOffset - close.length;
|
|
34
|
+
}
|
|
35
|
+
const { raw, startOffset } = (0, parser_utils_1.sliceFragment)(rawHtml, attr.position.start.offset, nextAttrBeforeSpaceOffset);
|
|
36
|
+
const result = (0, parser_utils_1.parseAttr)(raw, startOffset, rawHtml, {
|
|
37
|
+
valueDelimiters: [...parser_utils_1.defaultValueDelimiters, mustacheTag],
|
|
38
|
+
});
|
|
39
|
+
if (result.startQuote.raw === mustacheTag.start) {
|
|
40
|
+
result.isDynamicValue = true;
|
|
39
41
|
}
|
|
40
|
-
const value = (0, parser_utils_1.createTokenFromRawCode)(rawValue, rawValueStart + codeOffset, rawHtml);
|
|
41
|
-
const eq = (0, parser_utils_1.sliceFragment)(rawHtml, name.endOffset, value.startOffset);
|
|
42
|
-
const eqRegexp = /^(?<bs>\s*)(?<eq>=)(?<as>\s*)(?<squot>"|'|{)?$/g;
|
|
43
|
-
const exp = eqRegexp.exec(eq.raw);
|
|
44
|
-
const bsChar = ((_l = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _l === void 0 ? void 0 : _l.bs) || '';
|
|
45
|
-
const eqChar = ((_m = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _m === void 0 ? void 0 : _m.eq) || '';
|
|
46
|
-
const asChar = ((_o = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _o === void 0 ? void 0 : _o.as) || '';
|
|
47
|
-
const squotChar = ((_p = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _p === void 0 ? void 0 : _p.squot) || '';
|
|
48
|
-
const spacesBeforeEqual = (0, parser_utils_1.createTokenFromRawCode)(bsChar, name.endOffset, rawHtml);
|
|
49
|
-
const equal = (0, parser_utils_1.createTokenFromRawCode)(eqChar, spacesBeforeEqual.endOffset, rawHtml);
|
|
50
|
-
const spacesAfterEqual = (0, parser_utils_1.createTokenFromRawCode)(asChar, equal.endOffset, rawHtml);
|
|
51
|
-
const startQuote = (0, parser_utils_1.createTokenFromRawCode)(squotChar, spacesAfterEqual.endOffset, rawHtml);
|
|
52
|
-
const endQuote = (0, parser_utils_1.createTokenFromRawCode)(squotChar === '{' ? '}' : squotChar, value.endOffset, rawHtml);
|
|
53
|
-
const result = {
|
|
54
|
-
type: 'html-attr',
|
|
55
|
-
uuid: (0, parser_utils_1.uuid)(),
|
|
56
|
-
raw: attrToken.raw,
|
|
57
|
-
startOffset: attrToken.startOffset,
|
|
58
|
-
endOffset: attrToken.endOffset,
|
|
59
|
-
startLine: attrToken.startLine,
|
|
60
|
-
endLine: attrToken.endLine,
|
|
61
|
-
startCol: attrToken.startCol,
|
|
62
|
-
endCol: attrToken.endCol,
|
|
63
|
-
spacesBeforeName,
|
|
64
|
-
name,
|
|
65
|
-
spacesBeforeEqual,
|
|
66
|
-
equal,
|
|
67
|
-
spacesAfterEqual,
|
|
68
|
-
startQuote,
|
|
69
|
-
value,
|
|
70
|
-
endQuote,
|
|
71
|
-
isDuplicatable: false,
|
|
72
|
-
isDynamicValue,
|
|
73
|
-
nodeName: name.raw,
|
|
74
|
-
parentNode: null,
|
|
75
|
-
nextNode: null,
|
|
76
|
-
prevNode: null,
|
|
77
|
-
isFragment: false,
|
|
78
|
-
isGhost: false,
|
|
79
|
-
};
|
|
80
42
|
/**
|
|
81
43
|
* Detects Template Directive
|
|
82
44
|
*
|
|
83
45
|
* @see https://docs.astro.build/en/reference/directives-reference/
|
|
84
46
|
*/
|
|
85
|
-
const [, directive] = name.raw.match(/^([^:]+):([^:]+)$/)
|
|
47
|
+
const [, directive] = (_c = result.name.raw.match(/^([^:]+):([^:]+)$/)) !== null && _c !== void 0 ? _c : [];
|
|
86
48
|
if (directive) {
|
|
87
49
|
const lowerCaseDirectiveName = directive.toLowerCase();
|
|
88
50
|
switch (lowerCaseDirectiveName) {
|
package/lib/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { parse } from './parse';
|
|
2
|
-
export declare const endTag =
|
|
2
|
+
export declare const endTag = 'xml';
|
package/lib/parse.js
CHANGED
|
@@ -5,38 +5,23 @@ const html_parser_1 = require("@markuplint/html-parser");
|
|
|
5
5
|
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
6
6
|
const astro_parser_1 = require("./astro-parser");
|
|
7
7
|
const attr_tokenizer_1 = require("./attr-tokenizer");
|
|
8
|
-
const parse = (rawCode, options) => {
|
|
8
|
+
const parse = (rawCode, options = {}) => {
|
|
9
|
+
var _a, _b;
|
|
9
10
|
const ast = (0, astro_parser_1.astroParse)(rawCode);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
const htmlRawNodeList = traverse(ast.html, null, 'http://www.w3.org/1999/xhtml', rawCode);
|
|
24
|
-
if (ast.style) {
|
|
25
|
-
const styleNodes = parseStyle(ast.style, 'http://www.w3.org/1999/xhtml', rawCode, 0, options);
|
|
26
|
-
htmlRawNodeList.push(...styleNodes);
|
|
11
|
+
const htmlRawNodeList = traverse(ast, null, 'http://www.w3.org/1999/xhtml', rawCode, 0, options);
|
|
12
|
+
const firstOffset = (_b = (_a = htmlRawNodeList[0]) === null || _a === void 0 ? void 0 : _a.startOffset) !== null && _b !== void 0 ? _b : 0;
|
|
13
|
+
if (firstOffset > 0) {
|
|
14
|
+
const head = rawCode.slice(0, firstOffset);
|
|
15
|
+
const ast = (0, html_parser_1.parse)(head, {
|
|
16
|
+
...options,
|
|
17
|
+
ignoreFrontMatter: true,
|
|
18
|
+
});
|
|
19
|
+
const headNodes = ast.nodeList.filter(node => {
|
|
20
|
+
return !node.isGhost;
|
|
21
|
+
});
|
|
22
|
+
htmlRawNodeList.unshift(...headNodes);
|
|
27
23
|
}
|
|
28
24
|
const nodeList = (0, parser_utils_1.flattenNodes)(htmlRawNodeList, rawCode);
|
|
29
|
-
// Remove `</template>`
|
|
30
|
-
const templateEndTagIndex = nodeList.findIndex(node => /\s*<\/\s*template\s*>\s*/i.test(node.raw));
|
|
31
|
-
if (templateEndTagIndex !== -1) {
|
|
32
|
-
const templateEndTag = nodeList[templateEndTagIndex];
|
|
33
|
-
for (const node of nodeList) {
|
|
34
|
-
if (node.nextNode && node.nextNode.uuid === (templateEndTag === null || templateEndTag === void 0 ? void 0 : templateEndTag.uuid)) {
|
|
35
|
-
node.nextNode = null;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
nodeList.splice(templateEndTagIndex, 1);
|
|
39
|
-
}
|
|
40
25
|
return {
|
|
41
26
|
nodeList,
|
|
42
27
|
isFragment: true,
|
|
@@ -45,14 +30,17 @@ const parse = (rawCode, options) => {
|
|
|
45
30
|
exports.parse = parse;
|
|
46
31
|
function traverse(
|
|
47
32
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
48
|
-
rootNode, parentNode = null, scopeNS, rawHtml, offset, options) {
|
|
33
|
+
rootNode, parentNode = null, scopeNS, rawHtml, offset, options, inExpression) {
|
|
49
34
|
const nodeList = [];
|
|
50
|
-
if (!rootNode
|
|
35
|
+
if (!('children' in rootNode)) {
|
|
36
|
+
return nodeList;
|
|
37
|
+
}
|
|
38
|
+
if (rootNode.children.length === 0) {
|
|
51
39
|
return nodeList;
|
|
52
40
|
}
|
|
53
41
|
let prevNode = null;
|
|
54
42
|
for (const astNode of rootNode.children) {
|
|
55
|
-
const node = nodeize(astNode, prevNode, parentNode, scopeNS, rawHtml, offset, options);
|
|
43
|
+
const node = nodeize(astNode, prevNode, parentNode, scopeNS, rawHtml, offset, options, inExpression);
|
|
56
44
|
if (!node) {
|
|
57
45
|
continue;
|
|
58
46
|
}
|
|
@@ -76,22 +64,28 @@ originNode,
|
|
|
76
64
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
77
65
|
prevNode,
|
|
78
66
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
79
|
-
parentNode, scopeNS, rawHtml, offset = 0, options) {
|
|
80
|
-
var _a, _b;
|
|
67
|
+
parentNode, scopeNS, rawHtml, offset = 0, options, inExpression) {
|
|
68
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
69
|
+
if (!originNode.position) {
|
|
70
|
+
throw new TypeError("Node doesn't have position");
|
|
71
|
+
}
|
|
81
72
|
const nextNode = null;
|
|
82
|
-
const startOffset = originNode.start + offset;
|
|
83
|
-
const endOffset = originNode.end + offset;
|
|
73
|
+
const startOffset = originNode.position.start.offset + offset;
|
|
74
|
+
const endOffset = ((_b = (_a = originNode.position.end) === null || _a === void 0 ? void 0 : _a.offset) !== null && _b !== void 0 ? _b : originNode.position.start.offset) + offset;
|
|
84
75
|
const { startLine, endLine, startCol, endCol, raw } = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, endOffset);
|
|
85
76
|
if (scopeNS === 'http://www.w3.org/1999/xhtml' &&
|
|
86
|
-
originNode.type === '
|
|
87
|
-
((
|
|
77
|
+
originNode.type === 'element' &&
|
|
78
|
+
((_c = originNode.name) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === 'svg') {
|
|
88
79
|
scopeNS = 'http://www.w3.org/2000/svg';
|
|
89
80
|
}
|
|
90
81
|
else if (scopeNS === 'http://www.w3.org/2000/svg' && parentNode && parentNode.nodeName === 'foreignObject') {
|
|
91
82
|
scopeNS = 'http://www.w3.org/1999/xhtml';
|
|
92
83
|
}
|
|
93
84
|
switch (originNode.type) {
|
|
94
|
-
case '
|
|
85
|
+
case 'text': {
|
|
86
|
+
if (inExpression) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
95
89
|
const node = {
|
|
96
90
|
uuid: (0, parser_utils_1.uuid)(),
|
|
97
91
|
raw,
|
|
@@ -111,18 +105,36 @@ parentNode, scopeNS, rawHtml, offset = 0, options) {
|
|
|
111
105
|
};
|
|
112
106
|
return node;
|
|
113
107
|
}
|
|
114
|
-
case '
|
|
115
|
-
|
|
116
|
-
|
|
108
|
+
case 'expression': {
|
|
109
|
+
let _endOffset = endOffset;
|
|
110
|
+
let _startLine = startLine;
|
|
111
|
+
let _endLine = endLine;
|
|
112
|
+
let _startCol = startCol;
|
|
113
|
+
let _endCol = endCol;
|
|
114
|
+
let _raw = raw;
|
|
115
|
+
let closeExpression = null;
|
|
116
|
+
const firstChild = originNode.children[0];
|
|
117
|
+
const lastChild = originNode.children[originNode.children.length - 1];
|
|
118
|
+
if (firstChild && lastChild && firstChild !== lastChild) {
|
|
119
|
+
_endOffset = (_f = (_e = (_d = firstChild.position) === null || _d === void 0 ? void 0 : _d.end) === null || _e === void 0 ? void 0 : _e.offset) !== null && _f !== void 0 ? _f : endOffset;
|
|
120
|
+
const startLoc = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, _endOffset);
|
|
121
|
+
_startLine = startLoc.startLine;
|
|
122
|
+
_endLine = startLoc.endLine;
|
|
123
|
+
_startCol = startLoc.startCol;
|
|
124
|
+
_endCol = startLoc.endCol;
|
|
125
|
+
_raw = startLoc.raw;
|
|
126
|
+
const closeStartOffset = (_h = (_g = lastChild.position) === null || _g === void 0 ? void 0 : _g.start.offset) !== null && _h !== void 0 ? _h : startOffset;
|
|
127
|
+
const closeLoc = (0, parser_utils_1.sliceFragment)(rawHtml, closeStartOffset, endOffset);
|
|
128
|
+
closeExpression = {
|
|
117
129
|
uuid: (0, parser_utils_1.uuid)(),
|
|
118
|
-
raw,
|
|
119
|
-
startOffset,
|
|
120
|
-
endOffset,
|
|
121
|
-
startLine,
|
|
122
|
-
endLine,
|
|
123
|
-
startCol,
|
|
124
|
-
endCol,
|
|
125
|
-
nodeName:
|
|
130
|
+
raw: closeLoc.raw,
|
|
131
|
+
startOffset: closeStartOffset,
|
|
132
|
+
endOffset: closeLoc.endOffset,
|
|
133
|
+
startLine: closeLoc.startLine,
|
|
134
|
+
endLine: closeLoc.endLine,
|
|
135
|
+
startCol: closeLoc.startCol,
|
|
136
|
+
endCol: closeLoc.endCol,
|
|
137
|
+
nodeName: 'MustacheTag',
|
|
126
138
|
type: 'psblock',
|
|
127
139
|
parentNode,
|
|
128
140
|
prevNode,
|
|
@@ -131,53 +143,32 @@ parentNode, scopeNS, rawHtml, offset = 0, options) {
|
|
|
131
143
|
isGhost: false,
|
|
132
144
|
};
|
|
133
145
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const prevBlockEndOffset = prevBlock ? prevBlock.endOffset : originNode.start;
|
|
154
|
-
const loc = (0, parser_utils_1.sliceFragment)(rawHtml, prevBlockEndOffset + i, prevBlockEndOffset + i + chunk.length);
|
|
155
|
-
blocks.push({
|
|
156
|
-
uuid: (0, parser_utils_1.uuid)(),
|
|
157
|
-
raw: chunk,
|
|
158
|
-
startOffset: loc.startOffset,
|
|
159
|
-
endOffset: loc.endOffset,
|
|
160
|
-
startLine: loc.startLine,
|
|
161
|
-
endLine: loc.endLine,
|
|
162
|
-
startCol: loc.startCol,
|
|
163
|
-
endCol: loc.endCol,
|
|
164
|
-
nodeName: originNode.type,
|
|
165
|
-
type: 'psblock',
|
|
166
|
-
parentNode,
|
|
167
|
-
prevNode,
|
|
168
|
-
nextNode,
|
|
169
|
-
isFragment: false,
|
|
170
|
-
isGhost: false,
|
|
171
|
-
});
|
|
172
|
-
stub = stub.slice(i + chunk.length);
|
|
146
|
+
const node = {
|
|
147
|
+
uuid: (0, parser_utils_1.uuid)(),
|
|
148
|
+
raw: _raw,
|
|
149
|
+
startOffset,
|
|
150
|
+
endOffset: _endOffset,
|
|
151
|
+
startLine: _startLine,
|
|
152
|
+
endLine: _endLine,
|
|
153
|
+
startCol: _startCol,
|
|
154
|
+
endCol: _endCol,
|
|
155
|
+
nodeName: 'MustacheTag',
|
|
156
|
+
type: 'psblock',
|
|
157
|
+
parentNode,
|
|
158
|
+
prevNode,
|
|
159
|
+
nextNode,
|
|
160
|
+
isFragment: false,
|
|
161
|
+
isGhost: false,
|
|
162
|
+
};
|
|
163
|
+
if (originNode.children.length > 0) {
|
|
164
|
+
node.childNodes = traverse(originNode, parentNode, scopeNS, rawHtml, offset, options, true);
|
|
173
165
|
}
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
blocks[0].childNodes = childNodes;
|
|
166
|
+
if (closeExpression) {
|
|
167
|
+
return [node, closeExpression];
|
|
177
168
|
}
|
|
178
|
-
return
|
|
169
|
+
return node;
|
|
179
170
|
}
|
|
180
|
-
case '
|
|
171
|
+
case 'comment': {
|
|
181
172
|
return {
|
|
182
173
|
uuid: (0, parser_utils_1.uuid)(),
|
|
183
174
|
raw,
|
|
@@ -196,9 +187,11 @@ parentNode, scopeNS, rawHtml, offset = 0, options) {
|
|
|
196
187
|
isGhost: false,
|
|
197
188
|
};
|
|
198
189
|
}
|
|
199
|
-
case '
|
|
200
|
-
case '
|
|
201
|
-
|
|
190
|
+
case 'component':
|
|
191
|
+
case 'custom-element':
|
|
192
|
+
case 'fragment':
|
|
193
|
+
case 'element': {
|
|
194
|
+
if (((_j = originNode.name) === null || _j === void 0 ? void 0 : _j.toLowerCase()) === '!doctype') {
|
|
202
195
|
return {
|
|
203
196
|
uuid: (0, parser_utils_1.uuid)(),
|
|
204
197
|
raw,
|
|
@@ -220,17 +213,14 @@ parentNode, scopeNS, rawHtml, offset = 0, options) {
|
|
|
220
213
|
isGhost: false,
|
|
221
214
|
};
|
|
222
215
|
}
|
|
223
|
-
return parseElement(originNode
|
|
224
|
-
}
|
|
225
|
-
case 'Fragment': {
|
|
226
|
-
return originNode.children ? traverse(originNode, parentNode, scopeNS, rawHtml, offset) : null;
|
|
216
|
+
return parseElement(originNode, scopeNS, rawHtml, startLine, startCol, startOffset, parentNode, prevNode, nextNode, offset, options);
|
|
227
217
|
}
|
|
228
218
|
default: {
|
|
229
219
|
return null;
|
|
230
220
|
}
|
|
231
221
|
}
|
|
232
222
|
}
|
|
233
|
-
function parseElement(
|
|
223
|
+
function parseElement(
|
|
234
224
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
235
225
|
originNode, scopeNS, rawHtml, startLine, startCol, startOffset,
|
|
236
226
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
@@ -239,36 +229,44 @@ parentNode,
|
|
|
239
229
|
prevNode,
|
|
240
230
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
241
231
|
nextNode, offset, options) {
|
|
242
|
-
var _a, _b;
|
|
243
|
-
|
|
232
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
|
|
233
|
+
if (!originNode.position) {
|
|
234
|
+
throw new TypeError("Node doesn't have position");
|
|
235
|
+
}
|
|
244
236
|
let childrenStart;
|
|
245
237
|
let childrenEnd;
|
|
246
|
-
if (originNode.children
|
|
247
|
-
childrenStart = originNode.children[0].start + offset;
|
|
248
|
-
childrenEnd = ((
|
|
249
|
-
}
|
|
250
|
-
else if (originNode.content) {
|
|
251
|
-
childrenStart = originNode.content.start + offset;
|
|
252
|
-
childrenEnd = originNode.content.end + offset;
|
|
253
|
-
}
|
|
254
|
-
else if (/\/>$/.test(raw)) {
|
|
255
|
-
childrenStart = originNode.end + offset;
|
|
256
|
-
childrenEnd = originNode.end + offset;
|
|
238
|
+
if (originNode.children[0]) {
|
|
239
|
+
childrenStart = ((_c = (_b = (_a = originNode.children[0].position) === null || _a === void 0 ? void 0 : _a.start) === null || _b === void 0 ? void 0 : _b.offset) !== null && _c !== void 0 ? _c : 0) + offset;
|
|
240
|
+
childrenEnd = ((_g = (_f = (_e = (_d = originNode.children[originNode.children.length - 1]) === null || _d === void 0 ? void 0 : _d.position) === null || _e === void 0 ? void 0 : _e.end) === null || _f === void 0 ? void 0 : _f.offset) !== null && _g !== void 0 ? _g : 0) + offset;
|
|
257
241
|
}
|
|
258
242
|
else {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
243
|
+
childrenStart = offset + ((_k = (_j = (_h = originNode.position.end) === null || _h === void 0 ? void 0 : _h.offset) !== null && _j !== void 0 ? _j : nextNode === null || nextNode === void 0 ? void 0 : nextNode.endOffset) !== null && _k !== void 0 ? _k : rawHtml.length - offset);
|
|
244
|
+
childrenEnd = childrenStart;
|
|
245
|
+
const startTagStartOffset = originNode.position.start.offset + offset;
|
|
246
|
+
const startTagEndOffset = childrenStart;
|
|
247
|
+
const tag = rawHtml.slice(startTagStartOffset, startTagEndOffset);
|
|
248
|
+
const expectedCloseTag = `</${originNode.name}>`;
|
|
249
|
+
if (tag.includes(expectedCloseTag)) {
|
|
250
|
+
childrenStart -= expectedCloseTag.length;
|
|
251
|
+
childrenEnd = childrenStart;
|
|
252
|
+
}
|
|
253
|
+
// console.log({
|
|
254
|
+
// tag,
|
|
255
|
+
// startTagStartOffset,
|
|
256
|
+
// startTagEndOffset,
|
|
257
|
+
// expectedCloseTag,
|
|
258
|
+
// childrenStart,
|
|
259
|
+
// childrenEnd,
|
|
260
|
+
// });
|
|
263
261
|
}
|
|
264
|
-
const startTagStartOffset = originNode.start + offset;
|
|
262
|
+
const startTagStartOffset = originNode.position.start.offset + offset;
|
|
265
263
|
const startTagEndOffset = childrenStart;
|
|
266
264
|
const startTagRaw = rawHtml.slice(startTagStartOffset, startTagEndOffset);
|
|
267
265
|
const tagTokens = (0, html_parser_1.parseRawTag)(startTagRaw, startLine, startCol, startOffset);
|
|
268
266
|
const tagName = tagTokens.tagName;
|
|
269
267
|
let endTag = null;
|
|
270
|
-
if (childrenEnd < originNode.end + offset) {
|
|
271
|
-
const endTagLoc = (0, parser_utils_1.sliceFragment)(rawHtml, childrenEnd, originNode.end + offset);
|
|
268
|
+
if (childrenEnd < ((_m = (_l = originNode.position.end) === null || _l === void 0 ? void 0 : _l.offset) !== null && _m !== void 0 ? _m : 0) + offset) {
|
|
269
|
+
const endTagLoc = (0, parser_utils_1.sliceFragment)(rawHtml, childrenEnd, ((_p = (_o = originNode.position.end) === null || _o === void 0 ? void 0 : _o.offset) !== null && _p !== void 0 ? _p : 0) + offset);
|
|
272
270
|
const endTagTokens = (0, html_parser_1.parseRawTag)(endTagLoc.raw, endTagLoc.startLine, endTagLoc.startCol, endTagLoc.startOffset);
|
|
273
271
|
const endTagName = endTagTokens.tagName;
|
|
274
272
|
endTag = {
|
|
@@ -309,7 +307,10 @@ nextNode, offset, options) {
|
|
|
309
307
|
elementType: (0, parser_utils_1.detectElementType)(tagName, options === null || options === void 0 ? void 0 : options.authoredElementName, /^[A-Z]|\./),
|
|
310
308
|
attributes: originNode.attributes.map((
|
|
311
309
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
312
|
-
attr
|
|
310
|
+
attr, i) => {
|
|
311
|
+
var _a;
|
|
312
|
+
return (0, attr_tokenizer_1.attrTokenizer)(attr, (_a = originNode.attributes[i + 1]) !== null && _a !== void 0 ? _a : null, rawHtml, startTagRaw, startOffset + startTagRaw.length);
|
|
313
|
+
}),
|
|
313
314
|
hasSpreadAttr: false,
|
|
314
315
|
parentNode,
|
|
315
316
|
prevNode,
|
|
@@ -325,17 +326,8 @@ nextNode, offset, options) {
|
|
|
325
326
|
if (endTag) {
|
|
326
327
|
endTag.pearNode = startTag;
|
|
327
328
|
}
|
|
328
|
-
startTag.childNodes =
|
|
329
|
+
startTag.childNodes = ['style', 'script'].includes(tagName)
|
|
330
|
+
? undefined
|
|
331
|
+
: traverse(originNode, startTag, scopeNS, rawHtml, offset, options);
|
|
329
332
|
return startTag;
|
|
330
333
|
}
|
|
331
|
-
function parseStyle(
|
|
332
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
333
|
-
nodes, scopeNS, rawHtml, offset, options) {
|
|
334
|
-
const result = [];
|
|
335
|
-
for (const node of nodes) {
|
|
336
|
-
const { startLine, startCol, startOffset } = (0, parser_utils_1.sliceFragment)(rawHtml, node.start, node.end);
|
|
337
|
-
const styleEl = parseElement('style', node, scopeNS, rawHtml, startLine, startCol, startOffset, null, null, null, offset, options);
|
|
338
|
-
result.push(styleEl);
|
|
339
|
-
}
|
|
340
|
-
return result;
|
|
341
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/astro-parser",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.1",
|
|
4
4
|
"description": "astro parser for markuplint",
|
|
5
5
|
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
6
|
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
@@ -15,12 +15,16 @@
|
|
|
15
15
|
"build": "tsc",
|
|
16
16
|
"clean": "tsc --build --clean"
|
|
17
17
|
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@astrojs/parser": "0.22.2"
|
|
20
|
+
},
|
|
18
21
|
"dependencies": {
|
|
19
22
|
"@astrojs/parser": "0.20",
|
|
20
|
-
"@markuplint/html-parser": "3.
|
|
23
|
+
"@markuplint/html-parser": "3.6.1",
|
|
21
24
|
"@markuplint/ml-ast": "3.1.0",
|
|
22
|
-
"@markuplint/parser-utils": "3.
|
|
25
|
+
"@markuplint/parser-utils": "3.6.1",
|
|
26
|
+
"astro-eslint-parser": "^0.13.2",
|
|
23
27
|
"tslib": "^2.4.1"
|
|
24
28
|
},
|
|
25
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "3cdf5a088b2da03773d5d4461d0e65ec32290a00"
|
|
26
30
|
}
|