astro-eslint-parser 0.4.5 → 0.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.
- package/README.md +8 -10
- package/lib/index.d.ts +345 -10
- package/lib/index.js +1744 -47
- package/lib/index.mjs +1719 -0
- package/package.json +22 -17
- package/lib/ast/astro.d.ts +0 -49
- package/lib/ast/astro.js +0 -2
- package/lib/ast/base.d.ts +0 -6
- package/lib/ast/base.js +0 -2
- package/lib/ast/index.d.ts +0 -8
- package/lib/ast/index.js +0 -18
- package/lib/ast/jsx.d.ts +0 -91
- package/lib/ast/jsx.js +0 -2
- package/lib/astro/index.d.ts +0 -56
- package/lib/astro/index.js +0 -357
- package/lib/astro-tools/index.d.ts +0 -21
- package/lib/astro-tools/index.js +0 -26
- package/lib/context/index.d.ts +0 -55
- package/lib/context/index.js +0 -158
- package/lib/context/parser-options.d.ts +0 -8
- package/lib/context/parser-options.js +0 -71
- package/lib/context/resolve-parser/espree.d.ts +0 -6
- package/lib/context/resolve-parser/espree.js +0 -41
- package/lib/context/resolve-parser/index.d.ts +0 -5
- package/lib/context/resolve-parser/index.js +0 -30
- package/lib/context/script.d.ts +0 -34
- package/lib/context/script.js +0 -178
- package/lib/debug.d.ts +0 -2
- package/lib/debug.js +0 -8
- package/lib/errors.d.ts +0 -14
- package/lib/errors.js +0 -20
- package/lib/markdown/frontmatter.d.ts +0 -9
- package/lib/markdown/frontmatter.js +0 -53
- package/lib/markdown/index.d.ts +0 -19
- package/lib/markdown/index.js +0 -60
- package/lib/markdown/mdast-util-from-markdown-service.d.ts +0 -5
- package/lib/markdown/mdast-util-from-markdown-service.js +0 -12
- package/lib/markdown/mdast-util-from-markdown-worker.d.ts +0 -2
- package/lib/markdown/mdast-util-from-markdown-worker.js +0 -8
- package/lib/markdown/process-markdown.d.ts +0 -6
- package/lib/markdown/process-markdown.js +0 -82
- package/lib/markdown/yaml.d.ts +0 -9
- package/lib/markdown/yaml.js +0 -86
- package/lib/parser/astro-parser/astrojs-compiler-service.d.ts +0 -5
- package/lib/parser/astro-parser/astrojs-compiler-service.js +0 -12
- package/lib/parser/astro-parser/astrojs-compiler-worker.d.ts +0 -1
- package/lib/parser/astro-parser/astrojs-compiler-worker.js +0 -11
- package/lib/parser/astro-parser/parse.d.ts +0 -6
- package/lib/parser/astro-parser/parse.js +0 -267
- package/lib/parser/index.d.ts +0 -21
- package/lib/parser/index.js +0 -72
- package/lib/parser/lru-cache.d.ts +0 -7
- package/lib/parser/lru-cache.js +0 -32
- package/lib/parser/process-template.d.ts +0 -7
- package/lib/parser/process-template.js +0 -381
- package/lib/parser/script.d.ts +0 -7
- package/lib/parser/script.js +0 -42
- package/lib/parser/sort.d.ts +0 -6
- package/lib/parser/sort.js +0 -15
- package/lib/parser/template.d.ts +0 -10
- package/lib/parser/template.js +0 -102
- package/lib/parser/ts-patch.d.ts +0 -8
- package/lib/parser/ts-patch.js +0 -65
- package/lib/traverse.d.ts +0 -27
- package/lib/traverse.js +0 -93
- package/lib/types.d.ts +0 -21
- package/lib/types.js +0 -2
- package/lib/visitor-keys.d.ts +0 -2
- package/lib/visitor-keys.js +0 -14
package/lib/astro/index.js
DELETED
|
@@ -1,357 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.skipSpaces = exports.calcCommentEndOffset = exports.getEndTag = exports.getSelfClosingTag = exports.calcContentEndOffset = exports.getEndOffset = exports.calcAttributeValueStartOffset = exports.calcAttributeEndOffset = exports.calcStartTagEndOffset = exports.walk = exports.walkElements = exports.isParent = exports.isTag = void 0;
|
|
4
|
-
const errors_1 = require("../errors");
|
|
5
|
-
/**
|
|
6
|
-
* Checks if the given node is TagLikeNode
|
|
7
|
-
*/
|
|
8
|
-
function isTag(node) {
|
|
9
|
-
return (node.type === "element" ||
|
|
10
|
-
node.type === "custom-element" ||
|
|
11
|
-
node.type === "component" ||
|
|
12
|
-
node.type === "fragment");
|
|
13
|
-
}
|
|
14
|
-
exports.isTag = isTag;
|
|
15
|
-
/**
|
|
16
|
-
* Checks if the given node is ParentNode
|
|
17
|
-
*/
|
|
18
|
-
function isParent(node) {
|
|
19
|
-
return Array.isArray(node.children);
|
|
20
|
-
}
|
|
21
|
-
exports.isParent = isParent;
|
|
22
|
-
/** walk element nodes */
|
|
23
|
-
function walkElements(parent, code, enter, leave, parents = []) {
|
|
24
|
-
const children = getSortedChildren(parent, code);
|
|
25
|
-
const currParents = [parent, ...parents];
|
|
26
|
-
for (const node of children) {
|
|
27
|
-
enter(node, currParents);
|
|
28
|
-
if (isParent(node)) {
|
|
29
|
-
walkElements(node, code, enter, leave, currParents);
|
|
30
|
-
}
|
|
31
|
-
leave(node, currParents);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exports.walkElements = walkElements;
|
|
35
|
-
/** walk nodes */
|
|
36
|
-
function walk(parent, code, enter, leave) {
|
|
37
|
-
walkElements(parent, code, (node, parents) => {
|
|
38
|
-
enter(node, parents);
|
|
39
|
-
if (isTag(node)) {
|
|
40
|
-
const attrParents = [node, ...parents];
|
|
41
|
-
for (const attr of node.attributes) {
|
|
42
|
-
enter(attr, attrParents);
|
|
43
|
-
leave(attr, attrParents);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}, leave);
|
|
47
|
-
}
|
|
48
|
-
exports.walk = walk;
|
|
49
|
-
/**
|
|
50
|
-
* Get end offset of start tag
|
|
51
|
-
*/
|
|
52
|
-
function calcStartTagEndOffset(node, ctx) {
|
|
53
|
-
const lastAttr = node.attributes[node.attributes.length - 1];
|
|
54
|
-
let beforeCloseIndex;
|
|
55
|
-
if (lastAttr) {
|
|
56
|
-
beforeCloseIndex = calcAttributeEndOffset(lastAttr, ctx);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
const info = getTokenInfo(ctx, [`<${node.name}`], node.position.start.offset);
|
|
60
|
-
beforeCloseIndex = info.index + info.match.length;
|
|
61
|
-
}
|
|
62
|
-
const info = getTokenInfo(ctx, [[">", "/>"]], beforeCloseIndex);
|
|
63
|
-
return info.index + info.match.length;
|
|
64
|
-
}
|
|
65
|
-
exports.calcStartTagEndOffset = calcStartTagEndOffset;
|
|
66
|
-
/**
|
|
67
|
-
* Get end offset of attribute
|
|
68
|
-
*/
|
|
69
|
-
function calcAttributeEndOffset(node, ctx) {
|
|
70
|
-
let info;
|
|
71
|
-
if (node.kind === "empty") {
|
|
72
|
-
info = getTokenInfo(ctx, [node.name], node.position.start.offset);
|
|
73
|
-
}
|
|
74
|
-
else if (node.kind === "quoted") {
|
|
75
|
-
info = getTokenInfo(ctx, [[`"${node.value}"`, `'${node.value}'`, node.value]], calcAttributeValueStartOffset(node, ctx));
|
|
76
|
-
}
|
|
77
|
-
else if (node.kind === "expression") {
|
|
78
|
-
info = getTokenInfo(ctx, ["{", node.value, "}"], calcAttributeValueStartOffset(node, ctx));
|
|
79
|
-
}
|
|
80
|
-
else if (node.kind === "shorthand") {
|
|
81
|
-
info = getTokenInfo(ctx, ["{", node.name, "}"], node.position.start.offset);
|
|
82
|
-
}
|
|
83
|
-
else if (node.kind === "spread") {
|
|
84
|
-
info = getTokenInfo(ctx, ["{", "...", node.name, "}"], node.position.start.offset);
|
|
85
|
-
}
|
|
86
|
-
else if (node.kind === "template-literal") {
|
|
87
|
-
info = getTokenInfo(ctx, [`\`${node.value}\``], calcAttributeValueStartOffset(node, ctx));
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
|
|
91
|
-
}
|
|
92
|
-
return info.index + info.match.length;
|
|
93
|
-
}
|
|
94
|
-
exports.calcAttributeEndOffset = calcAttributeEndOffset;
|
|
95
|
-
/**
|
|
96
|
-
* Get start offset of attribute value
|
|
97
|
-
*/
|
|
98
|
-
function calcAttributeValueStartOffset(node, ctx) {
|
|
99
|
-
let info;
|
|
100
|
-
if (node.kind === "quoted") {
|
|
101
|
-
info = getTokenInfo(ctx, [node.name, "=", [`"`, `'`, node.value]], node.position.start.offset);
|
|
102
|
-
}
|
|
103
|
-
else if (node.kind === "expression") {
|
|
104
|
-
info = getTokenInfo(ctx, [node.name, "=", "{"], node.position.start.offset);
|
|
105
|
-
}
|
|
106
|
-
else if (node.kind === "template-literal") {
|
|
107
|
-
info = getTokenInfo(ctx, [node.name, "=", "`"], node.position.start.offset);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
|
|
111
|
-
}
|
|
112
|
-
return info.index;
|
|
113
|
-
}
|
|
114
|
-
exports.calcAttributeValueStartOffset = calcAttributeValueStartOffset;
|
|
115
|
-
/**
|
|
116
|
-
* Get end offset of tag
|
|
117
|
-
*/
|
|
118
|
-
function getEndOffset(node, ctx) {
|
|
119
|
-
if (node.position.end?.offset != null) {
|
|
120
|
-
return node.position.end.offset;
|
|
121
|
-
}
|
|
122
|
-
if (isTag(node))
|
|
123
|
-
return calcTagEndOffset(node, ctx);
|
|
124
|
-
if (node.type === "expression")
|
|
125
|
-
return calcExpressionEndOffset(node, ctx);
|
|
126
|
-
if (node.type === "comment")
|
|
127
|
-
return calcCommentEndOffset(node, ctx);
|
|
128
|
-
if (node.type === "frontmatter") {
|
|
129
|
-
const start = node.position.start.offset;
|
|
130
|
-
return ctx.code.indexOf("---", start + 3) + 3;
|
|
131
|
-
}
|
|
132
|
-
if (node.type === "doctype") {
|
|
133
|
-
const start = node.position.start.offset;
|
|
134
|
-
return ctx.code.indexOf(">", start) + 1;
|
|
135
|
-
}
|
|
136
|
-
if (node.type === "text") {
|
|
137
|
-
const start = node.position.start.offset;
|
|
138
|
-
return start + node.value.length;
|
|
139
|
-
}
|
|
140
|
-
if (node.type === "root") {
|
|
141
|
-
return ctx.code.length;
|
|
142
|
-
}
|
|
143
|
-
throw new Error(`unknown type: ${node.type}`);
|
|
144
|
-
}
|
|
145
|
-
exports.getEndOffset = getEndOffset;
|
|
146
|
-
/**
|
|
147
|
-
* Get content end offset
|
|
148
|
-
*/
|
|
149
|
-
function calcContentEndOffset(parent, ctx) {
|
|
150
|
-
const code = ctx.code;
|
|
151
|
-
if (isTag(parent)) {
|
|
152
|
-
const end = getEndOffset(parent, ctx);
|
|
153
|
-
if (code[end - 1] !== ">") {
|
|
154
|
-
return end;
|
|
155
|
-
}
|
|
156
|
-
const index = code.lastIndexOf("</", end - 1);
|
|
157
|
-
if (index >= 0 &&
|
|
158
|
-
code.slice(index + 2, end - 1).trim() === parent.name) {
|
|
159
|
-
return index;
|
|
160
|
-
}
|
|
161
|
-
return end;
|
|
162
|
-
}
|
|
163
|
-
else if (parent.type === "expression") {
|
|
164
|
-
const end = getEndOffset(parent, ctx);
|
|
165
|
-
return code.lastIndexOf("}", end);
|
|
166
|
-
}
|
|
167
|
-
else if (parent.type === "root") {
|
|
168
|
-
return code.length;
|
|
169
|
-
}
|
|
170
|
-
throw new Error(`unknown type: ${parent.type}`);
|
|
171
|
-
}
|
|
172
|
-
exports.calcContentEndOffset = calcContentEndOffset;
|
|
173
|
-
/**
|
|
174
|
-
* If the given tag is a self-close tag, get the self-closing tag.
|
|
175
|
-
*/
|
|
176
|
-
function getSelfClosingTag(node, ctx) {
|
|
177
|
-
if (node.children.length > 0) {
|
|
178
|
-
return null;
|
|
179
|
-
}
|
|
180
|
-
const code = ctx.code;
|
|
181
|
-
const startTagEndOffset = calcStartTagEndOffset(node, ctx);
|
|
182
|
-
if (code.startsWith("/>", startTagEndOffset - 2)) {
|
|
183
|
-
return {
|
|
184
|
-
offset: startTagEndOffset,
|
|
185
|
-
end: "/>",
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
if (code.startsWith(`</${node.name}`, startTagEndOffset)) {
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
return {
|
|
192
|
-
offset: startTagEndOffset,
|
|
193
|
-
end: ">",
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
exports.getSelfClosingTag = getSelfClosingTag;
|
|
197
|
-
/**
|
|
198
|
-
* If the given tag has a end tag, get the end tag.
|
|
199
|
-
*/
|
|
200
|
-
function getEndTag(node, ctx) {
|
|
201
|
-
let beforeIndex;
|
|
202
|
-
if (node.children.length) {
|
|
203
|
-
const lastChild = node.children[node.children.length - 1];
|
|
204
|
-
beforeIndex = getEndOffset(lastChild, ctx);
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
beforeIndex = calcStartTagEndOffset(node, ctx);
|
|
208
|
-
}
|
|
209
|
-
beforeIndex = skipSpaces(ctx.code, beforeIndex);
|
|
210
|
-
if (ctx.code.startsWith(`</${node.name}`, beforeIndex)) {
|
|
211
|
-
const offset = beforeIndex;
|
|
212
|
-
beforeIndex = beforeIndex + 2 + node.name.length;
|
|
213
|
-
const info = getTokenInfo(ctx, [">"], beforeIndex);
|
|
214
|
-
const end = info.index + info.match.length;
|
|
215
|
-
return {
|
|
216
|
-
offset,
|
|
217
|
-
tag: ctx.code.slice(offset, end),
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
return null;
|
|
221
|
-
}
|
|
222
|
-
exports.getEndTag = getEndTag;
|
|
223
|
-
/**
|
|
224
|
-
* Get end offset of comment
|
|
225
|
-
*/
|
|
226
|
-
function calcCommentEndOffset(node, ctx) {
|
|
227
|
-
const info = getTokenInfo(ctx, ["<!--", node.value, "-->"], node.position.start.offset);
|
|
228
|
-
return info.index + info.match.length;
|
|
229
|
-
}
|
|
230
|
-
exports.calcCommentEndOffset = calcCommentEndOffset;
|
|
231
|
-
/**
|
|
232
|
-
* Get end offset of tag
|
|
233
|
-
*/
|
|
234
|
-
function calcTagEndOffset(node, ctx) {
|
|
235
|
-
let beforeIndex;
|
|
236
|
-
if (node.children.length) {
|
|
237
|
-
const lastChild = node.children[node.children.length - 1];
|
|
238
|
-
beforeIndex = getEndOffset(lastChild, ctx);
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
beforeIndex = calcStartTagEndOffset(node, ctx);
|
|
242
|
-
}
|
|
243
|
-
beforeIndex = skipSpaces(ctx.code, beforeIndex);
|
|
244
|
-
if (ctx.code.startsWith(`</${node.name}`, beforeIndex)) {
|
|
245
|
-
beforeIndex = beforeIndex + 2 + node.name.length;
|
|
246
|
-
const info = getTokenInfo(ctx, [">"], beforeIndex);
|
|
247
|
-
return info.index + info.match.length;
|
|
248
|
-
}
|
|
249
|
-
return beforeIndex;
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Get end offset of Expression
|
|
253
|
-
*/
|
|
254
|
-
function calcExpressionEndOffset(node, ctx) {
|
|
255
|
-
if (node.children.length) {
|
|
256
|
-
const lastChild = node.children[node.children.length - 1];
|
|
257
|
-
const beforeIndex = getEndOffset(lastChild, ctx);
|
|
258
|
-
const info = getTokenInfo(ctx, ["}"], beforeIndex);
|
|
259
|
-
return info.index + info.match.length;
|
|
260
|
-
}
|
|
261
|
-
const info = getTokenInfo(ctx, ["{", "}"], node.position.start.offset);
|
|
262
|
-
return info.index + info.match.length;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Get token info
|
|
266
|
-
*/
|
|
267
|
-
function getTokenInfo(ctx, tokens, position) {
|
|
268
|
-
let lastMatch;
|
|
269
|
-
for (const t of tokens) {
|
|
270
|
-
const index = lastMatch
|
|
271
|
-
? lastMatch.index + lastMatch.match.length
|
|
272
|
-
: position;
|
|
273
|
-
const m = typeof t === "string"
|
|
274
|
-
? matchOfStr(t, index)
|
|
275
|
-
: matchOfForMulti(t, index);
|
|
276
|
-
if (m == null) {
|
|
277
|
-
throw new errors_1.ParseError(`Unknown token at ${index}, expected: ${JSON.stringify(t)}, actual: ${JSON.stringify(ctx.code.slice(index, index + 10))}`, index, ctx);
|
|
278
|
-
}
|
|
279
|
-
lastMatch = m;
|
|
280
|
-
}
|
|
281
|
-
return lastMatch;
|
|
282
|
-
/**
|
|
283
|
-
* For string
|
|
284
|
-
*/
|
|
285
|
-
function matchOfStr(search, position) {
|
|
286
|
-
const index = search.trim() === search ? skipSpaces(ctx.code, position) : position;
|
|
287
|
-
if (ctx.code.startsWith(search, index)) {
|
|
288
|
-
return {
|
|
289
|
-
match: search,
|
|
290
|
-
index,
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
return null;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* For multi
|
|
297
|
-
*/
|
|
298
|
-
function matchOfForMulti(search, position) {
|
|
299
|
-
for (const s of search) {
|
|
300
|
-
const m = matchOfStr(s, position);
|
|
301
|
-
if (m) {
|
|
302
|
-
return m;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
return null;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Skip spaces
|
|
310
|
-
*/
|
|
311
|
-
function skipSpaces(string, position) {
|
|
312
|
-
const re = /\s*/g;
|
|
313
|
-
re.lastIndex = position;
|
|
314
|
-
const match = re.exec(string);
|
|
315
|
-
if (match) {
|
|
316
|
-
return match.index + match[0].length;
|
|
317
|
-
}
|
|
318
|
-
return position;
|
|
319
|
-
}
|
|
320
|
-
exports.skipSpaces = skipSpaces;
|
|
321
|
-
/**
|
|
322
|
-
* Get children
|
|
323
|
-
*/
|
|
324
|
-
function getSortedChildren(parent, code) {
|
|
325
|
-
if (parent.type === "root" && parent.children[0]?.type === "frontmatter") {
|
|
326
|
-
// The order of comments and frontmatter may be changed.
|
|
327
|
-
const children = [...parent.children];
|
|
328
|
-
if (children.every((n) => n.position)) {
|
|
329
|
-
return children.sort((a, b) => a.position.start.offset - b.position.start.offset);
|
|
330
|
-
}
|
|
331
|
-
let start = skipSpaces(code, 0);
|
|
332
|
-
if (code.startsWith("<!", start)) {
|
|
333
|
-
const frontmatter = children.shift();
|
|
334
|
-
const before = [];
|
|
335
|
-
let first;
|
|
336
|
-
while ((first = children.shift())) {
|
|
337
|
-
start = skipSpaces(code, start);
|
|
338
|
-
if (first.type === "comment" &&
|
|
339
|
-
code.startsWith("<!--", start)) {
|
|
340
|
-
start = code.indexOf("-->", start + 4) + 3;
|
|
341
|
-
before.push(first);
|
|
342
|
-
}
|
|
343
|
-
else if (first.type === "doctype" &&
|
|
344
|
-
code.startsWith("<!", start)) {
|
|
345
|
-
start = code.indexOf(">", start + 2) + 1;
|
|
346
|
-
before.push(first);
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
children.unshift(first);
|
|
350
|
-
break;
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
return [...before, frontmatter, ...children];
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
return parent.children;
|
|
357
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { ParseResult } from "@astrojs/compiler";
|
|
2
|
-
import type { AttributeNode, Node, ParentNode } from "@astrojs/compiler/types";
|
|
3
|
-
export interface ParseTemplateResult {
|
|
4
|
-
result: ParseResult;
|
|
5
|
-
getEndOffset: (node: Node) => number;
|
|
6
|
-
calcAttributeValueStartOffset: (node: AttributeNode) => number;
|
|
7
|
-
calcAttributeEndOffset: (node: AttributeNode) => number;
|
|
8
|
-
walk: (parent: ParentNode, enter: (n: Node | AttributeNode, parents: ParentNode[]) => void, leave?: (n: Node | AttributeNode, parents: ParentNode[]) => void) => void;
|
|
9
|
-
getLocFromIndex: (index: number) => {
|
|
10
|
-
line: number;
|
|
11
|
-
column: number;
|
|
12
|
-
};
|
|
13
|
-
getIndexFromLoc: (loc: {
|
|
14
|
-
line: number;
|
|
15
|
-
column: number;
|
|
16
|
-
}) => number;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Parse the astro component template.
|
|
20
|
-
*/
|
|
21
|
-
export declare function parseTemplate(code: string): ParseTemplateResult;
|
package/lib/astro-tools/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseTemplate = void 0;
|
|
4
|
-
const template_1 = require("../parser/template");
|
|
5
|
-
const astro_1 = require("../astro");
|
|
6
|
-
/**
|
|
7
|
-
* Parse the astro component template.
|
|
8
|
-
*/
|
|
9
|
-
function parseTemplate(code) {
|
|
10
|
-
const parsed = (0, template_1.parseTemplate)(code);
|
|
11
|
-
return {
|
|
12
|
-
result: parsed.result,
|
|
13
|
-
getEndOffset: (node) => (0, astro_1.getEndOffset)(node, parsed.context),
|
|
14
|
-
calcAttributeValueStartOffset: (node) => (0, astro_1.calcAttributeValueStartOffset)(node, parsed.context),
|
|
15
|
-
calcAttributeEndOffset: (node) => (0, astro_1.calcAttributeEndOffset)(node, parsed.context),
|
|
16
|
-
walk(parent, enter, leave) {
|
|
17
|
-
(0, astro_1.walk)(parent, code, enter, leave ||
|
|
18
|
-
(() => {
|
|
19
|
-
/* noop */
|
|
20
|
-
}));
|
|
21
|
-
},
|
|
22
|
-
getLocFromIndex: (index) => parsed.context.getLocFromIndex(index),
|
|
23
|
-
getIndexFromLoc: (loc) => parsed.context.locs.getIndexFromLoc(loc),
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
exports.parseTemplate = parseTemplate;
|
package/lib/context/index.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { TSESTree } from "@typescript-eslint/types";
|
|
2
|
-
declare type RangeAndLoc = {
|
|
3
|
-
range: TSESTree.Range;
|
|
4
|
-
loc: TSESTree.SourceLocation;
|
|
5
|
-
};
|
|
6
|
-
export declare class Context {
|
|
7
|
-
readonly code: string;
|
|
8
|
-
readonly locs: LinesAndColumns;
|
|
9
|
-
private readonly locsMap;
|
|
10
|
-
private readonly state;
|
|
11
|
-
constructor(code: string);
|
|
12
|
-
getLocFromIndex(index: number): {
|
|
13
|
-
line: number;
|
|
14
|
-
column: number;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Get the location information of the given indexes.
|
|
18
|
-
*/
|
|
19
|
-
getLocations(start: number, end: number): RangeAndLoc;
|
|
20
|
-
/**
|
|
21
|
-
* Build token
|
|
22
|
-
*/
|
|
23
|
-
buildToken(type: TSESTree.Token["type"], range: TSESTree.Range): TSESTree.Token;
|
|
24
|
-
/**
|
|
25
|
-
* get text
|
|
26
|
-
*/
|
|
27
|
-
getText(range: TSESTree.Range): string;
|
|
28
|
-
get originalAST(): any;
|
|
29
|
-
set originalAST(originalAST: any);
|
|
30
|
-
}
|
|
31
|
-
export declare class LinesAndColumns {
|
|
32
|
-
private readonly lineStartIndices;
|
|
33
|
-
private readonly normalizedLineFeed;
|
|
34
|
-
constructor(origCode: string);
|
|
35
|
-
getLocFromIndex(index: number): {
|
|
36
|
-
line: number;
|
|
37
|
-
column: number;
|
|
38
|
-
};
|
|
39
|
-
getIndexFromLoc(loc: {
|
|
40
|
-
line: number;
|
|
41
|
-
column: number;
|
|
42
|
-
}): number;
|
|
43
|
-
getNormalizedLineFeed(): NormalizedLineFeed;
|
|
44
|
-
}
|
|
45
|
-
export declare class NormalizedLineFeed {
|
|
46
|
-
readonly code: string;
|
|
47
|
-
private readonly offsets;
|
|
48
|
-
get needRemap(): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* Remap index
|
|
51
|
-
*/
|
|
52
|
-
readonly remapIndex: (index: number) => number;
|
|
53
|
-
constructor(code: string, offsets: number[]);
|
|
54
|
-
}
|
|
55
|
-
export {};
|
package/lib/context/index.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NormalizedLineFeed = exports.LinesAndColumns = exports.Context = void 0;
|
|
4
|
-
class Context {
|
|
5
|
-
constructor(code) {
|
|
6
|
-
this.locsMap = new Map();
|
|
7
|
-
this.state = {};
|
|
8
|
-
this.locs = new LinesAndColumns(code);
|
|
9
|
-
this.code = code;
|
|
10
|
-
}
|
|
11
|
-
getLocFromIndex(index) {
|
|
12
|
-
let loc = this.locsMap.get(index);
|
|
13
|
-
if (!loc) {
|
|
14
|
-
loc = this.locs.getLocFromIndex(index);
|
|
15
|
-
this.locsMap.set(index, loc);
|
|
16
|
-
}
|
|
17
|
-
return {
|
|
18
|
-
line: loc.line,
|
|
19
|
-
column: loc.column,
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Get the location information of the given indexes.
|
|
24
|
-
*/
|
|
25
|
-
getLocations(start, end) {
|
|
26
|
-
return {
|
|
27
|
-
range: [start, end],
|
|
28
|
-
loc: {
|
|
29
|
-
start: this.getLocFromIndex(start),
|
|
30
|
-
end: this.getLocFromIndex(end),
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Build token
|
|
36
|
-
*/
|
|
37
|
-
buildToken(type, range) {
|
|
38
|
-
return {
|
|
39
|
-
type,
|
|
40
|
-
value: this.getText(range),
|
|
41
|
-
...this.getLocations(...range),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* get text
|
|
46
|
-
*/
|
|
47
|
-
getText(range) {
|
|
48
|
-
return this.code.slice(range[0], range[1]);
|
|
49
|
-
}
|
|
50
|
-
get originalAST() {
|
|
51
|
-
return this.state.originalAST;
|
|
52
|
-
}
|
|
53
|
-
set originalAST(originalAST) {
|
|
54
|
-
this.state.originalAST = originalAST;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.Context = Context;
|
|
58
|
-
class LinesAndColumns {
|
|
59
|
-
constructor(origCode) {
|
|
60
|
-
const len = origCode.length;
|
|
61
|
-
const lineStartIndices = [0];
|
|
62
|
-
const crs = [];
|
|
63
|
-
let normalizedCode = "";
|
|
64
|
-
for (let index = 0; index < len;) {
|
|
65
|
-
const c = origCode[index++];
|
|
66
|
-
if (c === "\r") {
|
|
67
|
-
const next = origCode[index++] || "";
|
|
68
|
-
if (next === "\n") {
|
|
69
|
-
normalizedCode += next;
|
|
70
|
-
crs.push(index - 2);
|
|
71
|
-
lineStartIndices.push(index);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
normalizedCode += `\n${next}`;
|
|
75
|
-
lineStartIndices.push(index - 1);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
normalizedCode += c;
|
|
80
|
-
if (c === "\n") {
|
|
81
|
-
lineStartIndices.push(index);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
this.lineStartIndices = lineStartIndices;
|
|
86
|
-
//
|
|
87
|
-
this.normalizedLineFeed = new NormalizedLineFeed(normalizedCode, crs);
|
|
88
|
-
}
|
|
89
|
-
getLocFromIndex(index) {
|
|
90
|
-
const lineNumber = sortedLastIndex(this.lineStartIndices, index);
|
|
91
|
-
return {
|
|
92
|
-
line: lineNumber,
|
|
93
|
-
column: index - this.lineStartIndices[lineNumber - 1],
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
getIndexFromLoc(loc) {
|
|
97
|
-
const lineStartIndex = this.lineStartIndices[loc.line - 1];
|
|
98
|
-
const positionIndex = lineStartIndex + loc.column;
|
|
99
|
-
return positionIndex;
|
|
100
|
-
}
|
|
101
|
-
getNormalizedLineFeed() {
|
|
102
|
-
return this.normalizedLineFeed;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
exports.LinesAndColumns = LinesAndColumns;
|
|
106
|
-
class NormalizedLineFeed {
|
|
107
|
-
constructor(code, offsets) {
|
|
108
|
-
this.code = code;
|
|
109
|
-
this.offsets = offsets;
|
|
110
|
-
if (offsets.length) {
|
|
111
|
-
const cache = {};
|
|
112
|
-
this.remapIndex = (index) => {
|
|
113
|
-
let result = cache[index];
|
|
114
|
-
if (result != null) {
|
|
115
|
-
return result;
|
|
116
|
-
}
|
|
117
|
-
result = index;
|
|
118
|
-
for (const offset of offsets) {
|
|
119
|
-
if (offset < result) {
|
|
120
|
-
result++;
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
return (cache[index] = result);
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
this.remapIndex = (i) => i;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
get needRemap() {
|
|
134
|
-
return this.offsets.length > 0;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
exports.NormalizedLineFeed = NormalizedLineFeed;
|
|
138
|
-
/**
|
|
139
|
-
* Uses a binary search to determine the highest index at which value should be inserted into array in order to maintain its sort order.
|
|
140
|
-
*/
|
|
141
|
-
function sortedLastIndex(array, value) {
|
|
142
|
-
let lower = 0;
|
|
143
|
-
let upper = array.length;
|
|
144
|
-
while (lower < upper) {
|
|
145
|
-
const mid = Math.floor(lower + (upper - lower) / 2);
|
|
146
|
-
const target = array[mid];
|
|
147
|
-
if (target < value) {
|
|
148
|
-
lower = mid + 1;
|
|
149
|
-
}
|
|
150
|
-
else if (target > value) {
|
|
151
|
-
upper = mid;
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
return mid + 1;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return upper;
|
|
158
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ParserOptionsContext = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const fs_1 = __importDefault(require("fs"));
|
|
9
|
-
const resolve_parser_1 = require("./resolve-parser");
|
|
10
|
-
class ParserOptionsContext {
|
|
11
|
-
constructor(options) {
|
|
12
|
-
this.state = {};
|
|
13
|
-
const parserOptions = {
|
|
14
|
-
ecmaVersion: 2020,
|
|
15
|
-
sourceType: "module",
|
|
16
|
-
loc: true,
|
|
17
|
-
range: true,
|
|
18
|
-
raw: true,
|
|
19
|
-
tokens: true,
|
|
20
|
-
comment: true,
|
|
21
|
-
eslintVisitorKeys: true,
|
|
22
|
-
eslintScopeManager: true,
|
|
23
|
-
...(options || {}),
|
|
24
|
-
};
|
|
25
|
-
parserOptions.ecmaFeatures = {
|
|
26
|
-
...(parserOptions.ecmaFeatures || {}),
|
|
27
|
-
jsx: true,
|
|
28
|
-
};
|
|
29
|
-
parserOptions.sourceType = "module";
|
|
30
|
-
if (parserOptions.ecmaVersion <= 5 ||
|
|
31
|
-
parserOptions.ecmaVersion == null) {
|
|
32
|
-
parserOptions.ecmaVersion = 2015;
|
|
33
|
-
}
|
|
34
|
-
this.parserOptions = parserOptions;
|
|
35
|
-
}
|
|
36
|
-
getParser() {
|
|
37
|
-
return (0, resolve_parser_1.getParser)({}, this.parserOptions.parser);
|
|
38
|
-
}
|
|
39
|
-
isTypeScript() {
|
|
40
|
-
if (this.state.isTypeScript != null) {
|
|
41
|
-
return this.state.isTypeScript;
|
|
42
|
-
}
|
|
43
|
-
const parserName = (0, resolve_parser_1.getParserName)({}, this.parserOptions?.parser);
|
|
44
|
-
if (parserName === "@typescript-eslint/parser") {
|
|
45
|
-
return (this.state.isTypeScript = true);
|
|
46
|
-
}
|
|
47
|
-
if (parserName.includes("@typescript-eslint/parser")) {
|
|
48
|
-
let targetPath = parserName;
|
|
49
|
-
while (targetPath) {
|
|
50
|
-
const pkgPath = path_1.default.join(targetPath, "package.json");
|
|
51
|
-
if (fs_1.default.existsSync(pkgPath)) {
|
|
52
|
-
try {
|
|
53
|
-
return (this.state.isTypeScript =
|
|
54
|
-
JSON.parse(fs_1.default.readFileSync(pkgPath, "utf-8"))
|
|
55
|
-
?.name === "@typescript-eslint/parser");
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
return (this.state.isTypeScript = false);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
const parent = path_1.default.dirname(targetPath);
|
|
62
|
-
if (targetPath === parent) {
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
targetPath = parent;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return (this.state.isTypeScript = false);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.ParserOptionsContext = ParserOptionsContext;
|