@markuplint/astro-parser 3.4.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
- import type { Attribute, Style, TemplateNode } from '@astrojs/parser';
2
- import { CompileError as AstroCompileError } from '@astrojs/parser';
3
- export { CompileError as AstroCompileError } from '@astrojs/parser';
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 {
3
+ RootNode,
4
+ ElementNode,
5
+ CustomElementNode,
6
+ ComponentNode,
7
+ FragmentNode,
8
+ AttributeNode,
9
+ Node,
10
+ } from '@astrojs/compiler/shared/ast';
11
+ export declare function astroParse(code: string): RootNode;
@@ -1,22 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.astroParse = exports.AstroCompileError = void 0;
4
- const parser_1 = require("@astrojs/parser");
5
- var parser_2 = require("@astrojs/parser");
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
- try {
9
- const ast = (0, parser_1.parse)(code, {});
10
- return {
11
- html: ast.html,
12
- style: ast.css,
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;
@@ -1,3 +1,9 @@
1
- import type { ASTAttribute } from './astro-parser';
1
+ import type { AttributeNode } from './astro-parser';
2
2
  import type { MLASTHTMLAttr } from '@markuplint/ml-ast';
3
- export declare function attrTokenizer(attr: ASTAttribute, rawHtml: string, codeOffset: number): MLASTHTMLAttr;
3
+ export declare function attrTokenizer(
4
+ attr: AttributeNode,
5
+ nextAttr: AttributeNode | null,
6
+ rawHtml: string,
7
+ startTag: string,
8
+ startTagEndOffset: number,
9
+ ): MLASTHTMLAttr;
@@ -2,85 +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
- function attrTokenizer(attr, rawHtml, codeOffset) {
6
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
7
- const { raw, startOffset } = (0, parser_utils_1.sliceFragment)(rawHtml, attr.start + codeOffset, attr.end + codeOffset);
8
- const isDynamicValue = ((_b = (_a = attr.value) === null || _a === void 0 ? void 0 : _a.some) === null || _b === void 0 ? void 0 : _b.call(_a, v => {
9
- // @ts-ignore
10
- return v.type === 'MustacheTag';
11
- })) || undefined;
12
- const attrToken = (0, parser_utils_1.createTokenFromRawCode)(raw, startOffset, rawHtml);
13
- const beforeCode = rawHtml.slice(0, attrToken.startOffset);
14
- const beforeAttrMatched = beforeCode.match(/\s+$/m);
15
- const beforeAttrChar = (beforeAttrMatched === null || beforeAttrMatched === void 0 ? void 0 : beforeAttrMatched[0]) || '';
16
- const spacesBeforeName = (0, parser_utils_1.createTokenFromRawCode)(beforeAttrChar, attrToken.startOffset - beforeAttrChar.length, rawHtml);
17
- const name = (0, parser_utils_1.createTokenFromRawCode)(attr.name, spacesBeforeName.endOffset, rawHtml);
18
- let rawValue;
19
- let rawValueStart;
20
- if (
21
- //
22
- !attr.value ||
23
- // @ts-ignore
24
- attr.value === true ||
25
- //
26
- attr.value.length === 0) {
27
- rawValue = '';
28
- rawValueStart = name.endOffset;
5
+ const mustacheTag = {
6
+ start: '{',
7
+ end: '}',
8
+ };
9
+ function attrTokenizer(
10
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
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");
29
17
  }
30
- else if (isDynamicValue) {
31
- rawValue = (_d = (_c = attr.value[0]) === null || _c === void 0 ? void 0 : _c.expression.codeChunks.join('')) !== null && _d !== void 0 ? _d : '';
32
- rawValueStart = (_f = (_e = attr.value[0]) === null || _e === void 0 ? void 0 : _e.expression.start) !== null && _f !== void 0 ? _f : name.endOffset;
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;
33
30
  }
34
31
  else {
35
- rawValue = (_h = (_g = attr.value[0]) === null || _g === void 0 ? void 0 : _g.raw) !== null && _h !== void 0 ? _h : '';
36
- rawValueStart = (_k = (_j = attr.value[0]) === null || _j === void 0 ? void 0 : _j.start) !== null && _k !== void 0 ? _k : name.endOffset;
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;
37
41
  }
38
- const value = (0, parser_utils_1.createTokenFromRawCode)(rawValue, rawValueStart + codeOffset, rawHtml);
39
- const eq = (0, parser_utils_1.sliceFragment)(rawHtml, name.endOffset, value.startOffset);
40
- const eqRegexp = /^(?<bs>\s*)(?<eq>=)(?<as>\s*)(?<squot>"|'|{)?$/g;
41
- const exp = eqRegexp.exec(eq.raw);
42
- const bsChar = ((_l = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _l === void 0 ? void 0 : _l.bs) || '';
43
- const eqChar = ((_m = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _m === void 0 ? void 0 : _m.eq) || '';
44
- const asChar = ((_o = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _o === void 0 ? void 0 : _o.as) || '';
45
- const squotChar = ((_p = exp === null || exp === void 0 ? void 0 : exp.groups) === null || _p === void 0 ? void 0 : _p.squot) || '';
46
- const spacesBeforeEqual = (0, parser_utils_1.createTokenFromRawCode)(bsChar, name.endOffset, rawHtml);
47
- const equal = (0, parser_utils_1.createTokenFromRawCode)(eqChar, spacesBeforeEqual.endOffset, rawHtml);
48
- const spacesAfterEqual = (0, parser_utils_1.createTokenFromRawCode)(asChar, equal.endOffset, rawHtml);
49
- const startQuote = (0, parser_utils_1.createTokenFromRawCode)(squotChar, spacesAfterEqual.endOffset, rawHtml);
50
- const endQuote = (0, parser_utils_1.createTokenFromRawCode)(squotChar === '{' ? '}' : squotChar, value.endOffset, rawHtml);
51
- const result = {
52
- type: 'html-attr',
53
- uuid: (0, parser_utils_1.uuid)(),
54
- raw: attrToken.raw,
55
- startOffset: attrToken.startOffset,
56
- endOffset: attrToken.endOffset,
57
- startLine: attrToken.startLine,
58
- endLine: attrToken.endLine,
59
- startCol: attrToken.startCol,
60
- endCol: attrToken.endCol,
61
- spacesBeforeName,
62
- name,
63
- spacesBeforeEqual,
64
- equal,
65
- spacesAfterEqual,
66
- startQuote,
67
- value,
68
- endQuote,
69
- isDuplicatable: false,
70
- isDynamicValue,
71
- nodeName: name.raw,
72
- parentNode: null,
73
- nextNode: null,
74
- prevNode: null,
75
- isFragment: false,
76
- isGhost: false,
77
- };
78
42
  /**
79
43
  * Detects Template Directive
80
44
  *
81
45
  * @see https://docs.astro.build/en/reference/directives-reference/
82
46
  */
83
- const [, directive] = name.raw.match(/^([^:]+):([^:]+)$/) || [];
47
+ const [, directive] = (_c = result.name.raw.match(/^([^:]+):([^:]+)$/)) !== null && _c !== void 0 ? _c : [];
84
48
  if (directive) {
85
49
  const lowerCaseDirectiveName = directive.toLowerCase();
86
50
  switch (lowerCaseDirectiveName) {
package/lib/parse.js CHANGED
@@ -5,52 +5,42 @@ 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
- if (ast instanceof astro_parser_1.AstroCompileError) {
11
- return {
12
- nodeList: [],
13
- isFragment: true,
14
- parseError: ast.stack || ast.message,
15
- };
16
- }
17
- if (!ast.html) {
18
- return {
19
- nodeList: [],
20
- isFragment: false,
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);
27
- }
28
- const nodeList = (0, html_parser_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);
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);
39
23
  }
24
+ const nodeList = (0, parser_utils_1.flattenNodes)(htmlRawNodeList, rawCode);
40
25
  return {
41
26
  nodeList,
42
27
  isFragment: true,
43
28
  };
44
29
  };
45
30
  exports.parse = parse;
46
- function traverse(rootNode, parentNode = null, scopeNS, rawHtml, offset, options) {
31
+ function traverse(
32
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
33
+ rootNode, parentNode = null, scopeNS, rawHtml, offset, options, inExpression) {
47
34
  const nodeList = [];
48
- if (!rootNode.children) {
35
+ if (!('children' in rootNode)) {
36
+ return nodeList;
37
+ }
38
+ if (rootNode.children.length === 0) {
49
39
  return nodeList;
50
40
  }
51
41
  let prevNode = null;
52
42
  for (const astNode of rootNode.children) {
53
- const node = nodeize(astNode, prevNode, parentNode, scopeNS, rawHtml, offset, options);
43
+ const node = nodeize(astNode, prevNode, parentNode, scopeNS, rawHtml, offset, options, inExpression);
54
44
  if (!node) {
55
45
  continue;
56
46
  }
@@ -68,22 +58,34 @@ function traverse(rootNode, parentNode = null, scopeNS, rawHtml, offset, options
68
58
  }
69
59
  return nodeList;
70
60
  }
71
- function nodeize(originNode, prevNode, parentNode, scopeNS, rawHtml, offset = 0, options) {
72
- var _a, _b;
61
+ function nodeize(
62
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
63
+ originNode,
64
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
65
+ prevNode,
66
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
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
+ }
73
72
  const nextNode = null;
74
- const startOffset = originNode.start + offset;
75
- 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;
76
75
  const { startLine, endLine, startCol, endCol, raw } = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, endOffset);
77
76
  if (scopeNS === 'http://www.w3.org/1999/xhtml' &&
78
- originNode.type === 'Element' &&
79
- ((_a = originNode.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'svg') {
77
+ originNode.type === 'element' &&
78
+ ((_c = originNode.name) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === 'svg') {
80
79
  scopeNS = 'http://www.w3.org/2000/svg';
81
80
  }
82
81
  else if (scopeNS === 'http://www.w3.org/2000/svg' && parentNode && parentNode.nodeName === 'foreignObject') {
83
82
  scopeNS = 'http://www.w3.org/1999/xhtml';
84
83
  }
85
84
  switch (originNode.type) {
86
- case 'Text': {
85
+ case 'text': {
86
+ if (inExpression) {
87
+ return null;
88
+ }
87
89
  const node = {
88
90
  uuid: (0, parser_utils_1.uuid)(),
89
91
  raw,
@@ -103,18 +105,36 @@ function nodeize(originNode, prevNode, parentNode, scopeNS, rawHtml, offset = 0,
103
105
  };
104
106
  return node;
105
107
  }
106
- case 'MustacheTag': {
107
- if (!originNode.expression.children || originNode.expression.children.length === 0) {
108
- return {
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 = {
109
129
  uuid: (0, parser_utils_1.uuid)(),
110
- raw,
111
- startOffset,
112
- endOffset,
113
- startLine,
114
- endLine,
115
- startCol,
116
- endCol,
117
- nodeName: originNode.type,
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',
118
138
  type: 'psblock',
119
139
  parentNode,
120
140
  prevNode,
@@ -123,53 +143,32 @@ function nodeize(originNode, prevNode, parentNode, scopeNS, rawHtml, offset = 0,
123
143
  isGhost: false,
124
144
  };
125
145
  }
126
- // console.log(originNode, originNode.expression);
127
- let stub = raw;
128
- const blocks = [];
129
- const [, codeStart, , codeEnd] = raw.match(/(^{\s*)((?:.|\s)*)(}$)/m) || [];
130
- const chunks = originNode.expression.codeChunks.map((chunk, i) => {
131
- if (i === 0) {
132
- return `${codeStart}${chunk}`;
133
- }
134
- else if (i === originNode.expression.codeChunks.length - 1) {
135
- return `${chunk}${codeEnd}`;
136
- }
137
- return chunk;
138
- });
139
- for (const chunk of chunks) {
140
- const i = stub.indexOf(chunk);
141
- if (i === -1) {
142
- throw new Error(`Invalid chunk: "${chunk}" from ${raw}`);
143
- }
144
- const prevBlock = blocks[blocks.length - 1];
145
- const prevBlockEndOffset = prevBlock ? prevBlock.endOffset : originNode.start;
146
- const loc = (0, parser_utils_1.sliceFragment)(rawHtml, prevBlockEndOffset + i, prevBlockEndOffset + i + chunk.length);
147
- blocks.push({
148
- uuid: (0, parser_utils_1.uuid)(),
149
- raw: chunk,
150
- startOffset: loc.startOffset,
151
- endOffset: loc.endOffset,
152
- startLine: loc.startLine,
153
- endLine: loc.endLine,
154
- startCol: loc.startCol,
155
- endCol: loc.endCol,
156
- nodeName: originNode.type,
157
- type: 'psblock',
158
- parentNode,
159
- prevNode,
160
- nextNode,
161
- isFragment: false,
162
- isGhost: false,
163
- });
164
- 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);
165
165
  }
166
- if (blocks[0]) {
167
- const childNodes = traverse(originNode.expression, blocks[0], scopeNS, rawHtml, blocks[0].endOffset);
168
- blocks[0].childNodes = childNodes;
166
+ if (closeExpression) {
167
+ return [node, closeExpression];
169
168
  }
170
- return blocks;
169
+ return node;
171
170
  }
172
- case 'Comment': {
171
+ case 'comment': {
173
172
  return {
174
173
  uuid: (0, parser_utils_1.uuid)(),
175
174
  raw,
@@ -188,9 +187,11 @@ function nodeize(originNode, prevNode, parentNode, scopeNS, rawHtml, offset = 0,
188
187
  isGhost: false,
189
188
  };
190
189
  }
191
- case 'InlineComponent':
192
- case 'Element': {
193
- if (((_b = originNode.name) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === '!doctype') {
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') {
194
195
  return {
195
196
  uuid: (0, parser_utils_1.uuid)(),
196
197
  raw,
@@ -212,47 +213,60 @@ function nodeize(originNode, prevNode, parentNode, scopeNS, rawHtml, offset = 0,
212
213
  isGhost: false,
213
214
  };
214
215
  }
215
- return parseElement(originNode.name, originNode, scopeNS, rawHtml, startLine, startCol, startOffset, parentNode, prevNode, nextNode, offset, options);
216
- }
217
- case 'Fragment': {
218
- return originNode.children ? traverse(originNode, parentNode, scopeNS, rawHtml, offset) : null;
216
+ return parseElement(originNode, scopeNS, rawHtml, startLine, startCol, startOffset, parentNode, prevNode, nextNode, offset, options);
219
217
  }
220
218
  default: {
221
219
  return null;
222
220
  }
223
221
  }
224
222
  }
225
- function parseElement(nodeName, originNode, scopeNS, rawHtml, startLine, startCol, startOffset, parentNode, prevNode, nextNode, offset, options) {
226
- var _a, _b;
227
- const { raw } = (0, parser_utils_1.sliceFragment)(rawHtml, originNode.start + offset, originNode.end + offset);
223
+ function parseElement(
224
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
225
+ originNode, scopeNS, rawHtml, startLine, startCol, startOffset,
226
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
227
+ parentNode,
228
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
229
+ prevNode,
230
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
231
+ nextNode, offset, options) {
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
+ }
228
236
  let childrenStart;
229
237
  let childrenEnd;
230
- if (originNode.children && originNode.children[0]) {
231
- childrenStart = originNode.children[0].start + offset;
232
- childrenEnd = ((_b = (_a = originNode.children[originNode.children.length - 1]) === null || _a === void 0 ? void 0 : _a.end) !== null && _b !== void 0 ? _b : 0) + offset;
233
- }
234
- else if (originNode.content) {
235
- childrenStart = originNode.content.start + offset;
236
- childrenEnd = originNode.content.end + offset;
237
- }
238
- else if (/\/>$/.test(raw)) {
239
- childrenStart = originNode.end + offset;
240
- 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;
241
241
  }
242
242
  else {
243
- const expectedEndTag = `</${nodeName}>`;
244
- const endTagStart = originNode.end + offset - expectedEndTag.length;
245
- childrenStart = endTagStart;
246
- childrenEnd = endTagStart;
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
+ // });
247
261
  }
248
- const startTagStartOffset = originNode.start + offset;
262
+ const startTagStartOffset = originNode.position.start.offset + offset;
249
263
  const startTagEndOffset = childrenStart;
250
264
  const startTagRaw = rawHtml.slice(startTagStartOffset, startTagEndOffset);
251
265
  const tagTokens = (0, html_parser_1.parseRawTag)(startTagRaw, startLine, startCol, startOffset);
252
266
  const tagName = tagTokens.tagName;
253
267
  let endTag = null;
254
- if (childrenEnd < originNode.end + offset) {
255
- 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);
256
270
  const endTagTokens = (0, html_parser_1.parseRawTag)(endTagLoc.raw, endTagLoc.startLine, endTagLoc.startCol, endTagLoc.startOffset);
257
271
  const endTagName = endTagTokens.tagName;
258
272
  endTag = {
@@ -291,7 +305,12 @@ function parseElement(nodeName, originNode, scopeNS, rawHtml, startLine, startCo
291
305
  type: 'starttag',
292
306
  namespace: scopeNS,
293
307
  elementType: (0, parser_utils_1.detectElementType)(tagName, options === null || options === void 0 ? void 0 : options.authoredElementName, /^[A-Z]|\./),
294
- attributes: originNode.attributes.map((attr) => (0, attr_tokenizer_1.attrTokenizer)(attr, rawHtml, offset)),
308
+ attributes: originNode.attributes.map((
309
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
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
+ }),
295
314
  hasSpreadAttr: false,
296
315
  parentNode,
297
316
  prevNode,
@@ -307,15 +326,8 @@ function parseElement(nodeName, originNode, scopeNS, rawHtml, startLine, startCo
307
326
  if (endTag) {
308
327
  endTag.pearNode = startTag;
309
328
  }
310
- startTag.childNodes = traverse(originNode, startTag, scopeNS, rawHtml, offset);
329
+ startTag.childNodes = ['style', 'script'].includes(tagName)
330
+ ? undefined
331
+ : traverse(originNode, startTag, scopeNS, rawHtml, offset, options);
311
332
  return startTag;
312
333
  }
313
- function parseStyle(nodes, scopeNS, rawHtml, offset, options) {
314
- const result = [];
315
- for (const node of nodes) {
316
- const { startLine, startCol, startOffset } = (0, parser_utils_1.sliceFragment)(rawHtml, node.start, node.end);
317
- const styleEl = parseElement('style', node, scopeNS, rawHtml, startLine, startCol, startOffset, null, null, null, offset, options);
318
- result.push(styleEl);
319
- }
320
- return result;
321
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/astro-parser",
3
- "version": "3.4.0",
3
+ "version": "3.6.0",
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.4.0",
21
- "@markuplint/ml-ast": "3.0.0",
22
- "@markuplint/parser-utils": "3.4.0",
23
+ "@markuplint/html-parser": "3.6.0",
24
+ "@markuplint/ml-ast": "3.1.0",
25
+ "@markuplint/parser-utils": "3.6.0",
26
+ "astro-eslint-parser": "^0.13.2",
23
27
  "tslib": "^2.4.1"
24
28
  },
25
- "gitHead": "a83e0f5f214a9bbcc0286b9e269074ddca6189e7"
29
+ "gitHead": "715dd53d3b1064a9bcf616c1533921cad9e3b187"
26
30
  }