astro-eslint-parser 0.5.0 → 0.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/index.d.ts +345 -9
- package/lib/index.js +1745 -42
- package/lib/index.mjs +1719 -0
- package/package.json +12 -9
- 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 -77
- 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 -7
- package/lib/context/resolve-parser/index.js +0 -34
- package/lib/context/resolve-parser/parser-object.d.ts +0 -33
- package/lib/context/resolve-parser/parser-object.js +0 -27
- 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/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 -270
- 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 -44
- 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 -17
- package/lib/types.js +0 -2
- package/lib/visitor-keys.d.ts +0 -2
- package/lib/visitor-keys.js +0 -14
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.processTemplate = void 0;
|
|
4
|
-
const types_1 = require("@typescript-eslint/types");
|
|
5
|
-
const astro_1 = require("../astro");
|
|
6
|
-
const script_1 = require("../context/script");
|
|
7
|
-
/**
|
|
8
|
-
* Process the template to generate a ScriptContext.
|
|
9
|
-
*/
|
|
10
|
-
function processTemplate(ctx, resultTemplate) {
|
|
11
|
-
let uniqueIdSeq = 0;
|
|
12
|
-
const usedUniqueIds = new Set();
|
|
13
|
-
const script = new script_1.ScriptContext(ctx);
|
|
14
|
-
let fragmentOpened = false;
|
|
15
|
-
/** Open astro root fragment */
|
|
16
|
-
function openRootFragment(startOffset) {
|
|
17
|
-
script.appendScript("<>");
|
|
18
|
-
fragmentOpened = true;
|
|
19
|
-
script.addRestoreNodeProcess((scriptNode, { result }) => {
|
|
20
|
-
if (scriptNode.type === types_1.AST_NODE_TYPES.ExpressionStatement &&
|
|
21
|
-
scriptNode.expression.type === types_1.AST_NODE_TYPES.JSXFragment &&
|
|
22
|
-
scriptNode.range[0] === startOffset &&
|
|
23
|
-
result.ast.body.includes(scriptNode)) {
|
|
24
|
-
const index = result.ast.body.indexOf(scriptNode);
|
|
25
|
-
const rootFragment = (result.ast.body[index] =
|
|
26
|
-
scriptNode.expression);
|
|
27
|
-
delete rootFragment.closingFragment;
|
|
28
|
-
delete rootFragment.openingFragment;
|
|
29
|
-
rootFragment.type = "AstroFragment";
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
return false;
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
(0, astro_1.walkElements)(resultTemplate.ast, ctx.code,
|
|
36
|
-
// eslint-disable-next-line complexity -- X(
|
|
37
|
-
(node, [parent]) => {
|
|
38
|
-
if (node.type === "frontmatter") {
|
|
39
|
-
const start = node.position.start.offset;
|
|
40
|
-
if (fragmentOpened) {
|
|
41
|
-
script.appendScript("</>;");
|
|
42
|
-
fragmentOpened = false;
|
|
43
|
-
}
|
|
44
|
-
script.appendOriginal(start);
|
|
45
|
-
script.skipOriginalOffset(3);
|
|
46
|
-
const end = (0, astro_1.getEndOffset)(node, ctx);
|
|
47
|
-
script.appendOriginal(end - 3);
|
|
48
|
-
script.appendScript(";");
|
|
49
|
-
script.skipOriginalOffset(3);
|
|
50
|
-
script.addRestoreNodeProcess((_scriptNode, { result }) => {
|
|
51
|
-
for (let index = 0; index < result.ast.body.length; index++) {
|
|
52
|
-
const st = result.ast.body[index];
|
|
53
|
-
if (st.type === types_1.AST_NODE_TYPES.EmptyStatement) {
|
|
54
|
-
if (st.range[0] === end - 3 && st.range[1] <= end) {
|
|
55
|
-
result.ast.body.splice(index, 1);
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return true;
|
|
61
|
-
});
|
|
62
|
-
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [
|
|
63
|
-
node.position.start.offset,
|
|
64
|
-
node.position.start.offset + 3,
|
|
65
|
-
]);
|
|
66
|
-
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [end - 3, end]);
|
|
67
|
-
}
|
|
68
|
-
else if ((0, astro_1.isTag)(node)) {
|
|
69
|
-
// Process for multiple tag
|
|
70
|
-
if (parent.type === "expression") {
|
|
71
|
-
const index = parent.children.indexOf(node);
|
|
72
|
-
const before = parent.children[index - 1];
|
|
73
|
-
if (!before || !(0, astro_1.isTag)(before)) {
|
|
74
|
-
const after = parent.children[index + 1];
|
|
75
|
-
if (after &&
|
|
76
|
-
((0, astro_1.isTag)(after) || after.type === "comment")) {
|
|
77
|
-
const start = node.position.start.offset;
|
|
78
|
-
script.appendOriginal(start);
|
|
79
|
-
script.appendScript("<>");
|
|
80
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
81
|
-
if (scriptNode.range[0] === start &&
|
|
82
|
-
scriptNode.type ===
|
|
83
|
-
types_1.AST_NODE_TYPES.JSXFragment) {
|
|
84
|
-
delete scriptNode.openingFragment;
|
|
85
|
-
delete scriptNode.closingFragment;
|
|
86
|
-
const fragmentNode = scriptNode;
|
|
87
|
-
fragmentNode.type = "AstroFragment";
|
|
88
|
-
const last = fragmentNode.children[fragmentNode.children.length - 1];
|
|
89
|
-
if (fragmentNode.range[1] < last.range[1]) {
|
|
90
|
-
fragmentNode.range[1] = last.range[1];
|
|
91
|
-
fragmentNode.loc.end =
|
|
92
|
-
ctx.getLocFromIndex(fragmentNode.range[1]);
|
|
93
|
-
}
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
return false;
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
const start = node.position.start.offset;
|
|
102
|
-
script.appendOriginal(start);
|
|
103
|
-
if (!fragmentOpened) {
|
|
104
|
-
openRootFragment(start);
|
|
105
|
-
}
|
|
106
|
-
// Process for attributes
|
|
107
|
-
for (const attr of node.attributes) {
|
|
108
|
-
if ((node.type === "component" ||
|
|
109
|
-
node.type === "fragment") &&
|
|
110
|
-
(attr.kind === "quoted" ||
|
|
111
|
-
attr.kind === "empty" ||
|
|
112
|
-
attr.kind === "expression" ||
|
|
113
|
-
attr.kind === "template-literal")) {
|
|
114
|
-
const colonIndex = attr.name.indexOf(":");
|
|
115
|
-
if (colonIndex >= 0) {
|
|
116
|
-
const start = attr.position.start.offset;
|
|
117
|
-
script.appendOriginal(start + colonIndex);
|
|
118
|
-
script.skipOriginalOffset(1);
|
|
119
|
-
script.appendScript(`_`);
|
|
120
|
-
script.addToken(types_1.AST_TOKEN_TYPES.JSXIdentifier, [
|
|
121
|
-
start,
|
|
122
|
-
start + colonIndex,
|
|
123
|
-
]);
|
|
124
|
-
script.addToken(types_1.AST_TOKEN_TYPES.Punctuator, [
|
|
125
|
-
start + colonIndex,
|
|
126
|
-
start + colonIndex + 1,
|
|
127
|
-
]);
|
|
128
|
-
script.addToken(types_1.AST_TOKEN_TYPES.JSXIdentifier, [
|
|
129
|
-
start + colonIndex + 1,
|
|
130
|
-
start + attr.name.length,
|
|
131
|
-
]);
|
|
132
|
-
script.addRestoreNodeProcess((scriptNode, context) => {
|
|
133
|
-
if (scriptNode.type ===
|
|
134
|
-
types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
135
|
-
scriptNode.range[0] === start) {
|
|
136
|
-
const baseNameNode = scriptNode.name;
|
|
137
|
-
const nsn = {
|
|
138
|
-
...baseNameNode,
|
|
139
|
-
type: types_1.AST_NODE_TYPES.JSXNamespacedName,
|
|
140
|
-
namespace: {
|
|
141
|
-
type: types_1.AST_NODE_TYPES.JSXIdentifier,
|
|
142
|
-
name: attr.name.slice(0, colonIndex),
|
|
143
|
-
...ctx.getLocations(baseNameNode.range[0], baseNameNode.range[0] +
|
|
144
|
-
colonIndex),
|
|
145
|
-
},
|
|
146
|
-
name: {
|
|
147
|
-
type: types_1.AST_NODE_TYPES.JSXIdentifier,
|
|
148
|
-
name: attr.name.slice(colonIndex + 1),
|
|
149
|
-
...ctx.getLocations(baseNameNode.range[0] +
|
|
150
|
-
colonIndex +
|
|
151
|
-
1, baseNameNode.range[1]),
|
|
152
|
-
},
|
|
153
|
-
};
|
|
154
|
-
scriptNode.name = nsn;
|
|
155
|
-
nsn.namespace.parent = nsn;
|
|
156
|
-
nsn.name.parent = nsn;
|
|
157
|
-
context.addRemoveToken((token) => token.range[0] ===
|
|
158
|
-
baseNameNode.range[0] &&
|
|
159
|
-
token.range[1] ===
|
|
160
|
-
baseNameNode.range[1]);
|
|
161
|
-
return true;
|
|
162
|
-
}
|
|
163
|
-
return false;
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (attr.kind === "shorthand") {
|
|
168
|
-
const start = attr.position.start.offset;
|
|
169
|
-
script.appendOriginal(start);
|
|
170
|
-
const jsxName = /[\s"'[\]{}]/u.test(attr.name)
|
|
171
|
-
? generateUniqueId(attr.name)
|
|
172
|
-
: attr.name;
|
|
173
|
-
script.appendScript(`${jsxName}=`);
|
|
174
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
175
|
-
if (scriptNode.type ===
|
|
176
|
-
types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
177
|
-
scriptNode.range[0] === start) {
|
|
178
|
-
const attrNode = scriptNode;
|
|
179
|
-
attrNode.type = "AstroShorthandAttribute";
|
|
180
|
-
const locs = ctx.getLocations(...attrNode.value.expression.range);
|
|
181
|
-
if (jsxName !== attr.name) {
|
|
182
|
-
attrNode.name.name = attr.name;
|
|
183
|
-
}
|
|
184
|
-
attrNode.name.range = locs.range;
|
|
185
|
-
attrNode.name.loc = locs.loc;
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
return false;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
else if (attr.kind === "template-literal") {
|
|
192
|
-
const attrStart = attr.position.start.offset;
|
|
193
|
-
const start = (0, astro_1.calcAttributeValueStartOffset)(attr, ctx);
|
|
194
|
-
const end = (0, astro_1.calcAttributeEndOffset)(attr, ctx);
|
|
195
|
-
script.appendOriginal(start);
|
|
196
|
-
script.appendScript("{");
|
|
197
|
-
script.appendOriginal(end);
|
|
198
|
-
script.appendScript("}");
|
|
199
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
200
|
-
if (scriptNode.type ===
|
|
201
|
-
types_1.AST_NODE_TYPES.JSXAttribute &&
|
|
202
|
-
scriptNode.range[0] === attrStart) {
|
|
203
|
-
const attrNode = scriptNode;
|
|
204
|
-
attrNode.type = "AstroTemplateLiteralAttribute";
|
|
205
|
-
return true;
|
|
206
|
-
}
|
|
207
|
-
return false;
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
// Process for start tag close
|
|
212
|
-
const closing = (0, astro_1.getSelfClosingTag)(node, ctx);
|
|
213
|
-
if (closing && closing.end === ">") {
|
|
214
|
-
script.appendOriginal(closing.offset - 1);
|
|
215
|
-
script.appendScript("/");
|
|
216
|
-
}
|
|
217
|
-
// Process for raw text
|
|
218
|
-
if (node.name === "script" || node.name === "style") {
|
|
219
|
-
const text = node.children[0];
|
|
220
|
-
if (text && text.type === "text") {
|
|
221
|
-
const styleNodeStart = node.position.start.offset;
|
|
222
|
-
const start = text.position.start.offset;
|
|
223
|
-
script.appendOriginal(start);
|
|
224
|
-
script.skipOriginalOffset(text.value.length);
|
|
225
|
-
script.addRestoreNodeProcess((scriptNode) => {
|
|
226
|
-
if (scriptNode.type === types_1.AST_NODE_TYPES.JSXElement &&
|
|
227
|
-
scriptNode.range[0] === styleNodeStart) {
|
|
228
|
-
const textNode = {
|
|
229
|
-
type: "AstroRawText",
|
|
230
|
-
value: text.value,
|
|
231
|
-
raw: text.value,
|
|
232
|
-
parent: scriptNode,
|
|
233
|
-
...ctx.getLocations(start, start + text.value.length),
|
|
234
|
-
};
|
|
235
|
-
scriptNode.children = [
|
|
236
|
-
textNode,
|
|
237
|
-
];
|
|
238
|
-
return true;
|
|
239
|
-
}
|
|
240
|
-
return false;
|
|
241
|
-
});
|
|
242
|
-
script.addToken(types_1.AST_TOKEN_TYPES.JSXText, [
|
|
243
|
-
start,
|
|
244
|
-
start + text.value.length,
|
|
245
|
-
]);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
else if (node.type === "comment") {
|
|
250
|
-
const start = node.position.start.offset;
|
|
251
|
-
const end = (0, astro_1.getEndOffset)(node, ctx);
|
|
252
|
-
const length = end - start;
|
|
253
|
-
script.appendOriginal(start);
|
|
254
|
-
if (!fragmentOpened) {
|
|
255
|
-
openRootFragment(start);
|
|
256
|
-
}
|
|
257
|
-
script.appendOriginal(start + 1);
|
|
258
|
-
script.appendScript(`></`);
|
|
259
|
-
script.skipOriginalOffset(length - 2);
|
|
260
|
-
script.appendOriginal(end);
|
|
261
|
-
script.addRestoreNodeProcess((scriptNode, context) => {
|
|
262
|
-
if (scriptNode.range[0] === start &&
|
|
263
|
-
scriptNode.type === types_1.AST_NODE_TYPES.JSXFragment) {
|
|
264
|
-
delete scriptNode.children;
|
|
265
|
-
delete scriptNode.openingFragment;
|
|
266
|
-
delete scriptNode.closingFragment;
|
|
267
|
-
delete scriptNode.expression;
|
|
268
|
-
const commentNode = scriptNode;
|
|
269
|
-
commentNode.type = "AstroHTMLComment";
|
|
270
|
-
commentNode.value = node.value;
|
|
271
|
-
context.addRemoveToken((token) => token.value === "<" &&
|
|
272
|
-
token.range[0] === scriptNode.range[0]);
|
|
273
|
-
context.addRemoveToken((token) => token.value === ">" &&
|
|
274
|
-
token.range[1] === scriptNode.range[1]);
|
|
275
|
-
return true;
|
|
276
|
-
}
|
|
277
|
-
return false;
|
|
278
|
-
});
|
|
279
|
-
script.addToken("HTMLComment", [
|
|
280
|
-
start,
|
|
281
|
-
start + length,
|
|
282
|
-
]);
|
|
283
|
-
}
|
|
284
|
-
else if (node.type === "doctype") {
|
|
285
|
-
const start = node.position.start.offset;
|
|
286
|
-
const end = (0, astro_1.getEndOffset)(node, ctx);
|
|
287
|
-
const length = end - start;
|
|
288
|
-
script.appendOriginal(start);
|
|
289
|
-
if (!fragmentOpened) {
|
|
290
|
-
openRootFragment(start);
|
|
291
|
-
}
|
|
292
|
-
script.appendOriginal(start + 1);
|
|
293
|
-
script.appendScript(`></`);
|
|
294
|
-
script.skipOriginalOffset(length - 2);
|
|
295
|
-
script.appendOriginal(end);
|
|
296
|
-
script.addRestoreNodeProcess((scriptNode, context) => {
|
|
297
|
-
if (scriptNode.range[0] === start &&
|
|
298
|
-
scriptNode.type === types_1.AST_NODE_TYPES.JSXFragment) {
|
|
299
|
-
delete scriptNode.children;
|
|
300
|
-
delete scriptNode.openingFragment;
|
|
301
|
-
delete scriptNode.closingFragment;
|
|
302
|
-
delete scriptNode.expression;
|
|
303
|
-
const doctypeNode = scriptNode;
|
|
304
|
-
doctypeNode.type = "AstroDoctype";
|
|
305
|
-
context.addRemoveToken((token) => token.value === "<" &&
|
|
306
|
-
token.range[0] === scriptNode.range[0]);
|
|
307
|
-
context.addRemoveToken((token) => token.value === ">" &&
|
|
308
|
-
token.range[1] === scriptNode.range[1]);
|
|
309
|
-
return true;
|
|
310
|
-
}
|
|
311
|
-
return false;
|
|
312
|
-
});
|
|
313
|
-
script.addToken("HTMLDocType", [start, end]);
|
|
314
|
-
}
|
|
315
|
-
else {
|
|
316
|
-
const start = node.position.start.offset;
|
|
317
|
-
script.appendOriginal(start);
|
|
318
|
-
if (!fragmentOpened) {
|
|
319
|
-
openRootFragment(start);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}, (node, [parent]) => {
|
|
323
|
-
if ((0, astro_1.isTag)(node)) {
|
|
324
|
-
const closing = (0, astro_1.getSelfClosingTag)(node, ctx);
|
|
325
|
-
if (!closing) {
|
|
326
|
-
const end = (0, astro_1.getEndTag)(node, ctx);
|
|
327
|
-
if (!end) {
|
|
328
|
-
const offset = (0, astro_1.calcContentEndOffset)(node, ctx);
|
|
329
|
-
script.appendOriginal(offset);
|
|
330
|
-
script.appendScript(`</${node.name}>`);
|
|
331
|
-
script.addRestoreNodeProcess((scriptNode, context) => {
|
|
332
|
-
const parent = context.getParent(scriptNode);
|
|
333
|
-
if (scriptNode.range[0] === offset &&
|
|
334
|
-
scriptNode.type ===
|
|
335
|
-
types_1.AST_NODE_TYPES.JSXClosingElement &&
|
|
336
|
-
parent.type === types_1.AST_NODE_TYPES.JSXElement) {
|
|
337
|
-
parent.closingElement = null;
|
|
338
|
-
return true;
|
|
339
|
-
}
|
|
340
|
-
return false;
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
// Process for multiple tag
|
|
346
|
-
if (((0, astro_1.isTag)(node) || node.type === "comment") &&
|
|
347
|
-
parent.type === "expression") {
|
|
348
|
-
const index = parent.children.indexOf(node);
|
|
349
|
-
const after = parent.children[index + 1];
|
|
350
|
-
if (!after || (!(0, astro_1.isTag)(after) && after.type !== "comment")) {
|
|
351
|
-
const before = parent.children[index - 1];
|
|
352
|
-
if (before &&
|
|
353
|
-
((0, astro_1.isTag)(before) || before.type === "comment")) {
|
|
354
|
-
const end = (0, astro_1.getEndOffset)(node, ctx);
|
|
355
|
-
script.appendOriginal(end);
|
|
356
|
-
script.appendScript("</>");
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
|
-
if (fragmentOpened) {
|
|
362
|
-
const last = resultTemplate.ast.children[resultTemplate.ast.children.length - 1];
|
|
363
|
-
const end = (0, astro_1.getEndOffset)(last, ctx);
|
|
364
|
-
script.appendOriginal(end);
|
|
365
|
-
script.appendScript("</>");
|
|
366
|
-
}
|
|
367
|
-
script.appendOriginal(ctx.code.length);
|
|
368
|
-
return script;
|
|
369
|
-
/**
|
|
370
|
-
* Generate unique id
|
|
371
|
-
*/
|
|
372
|
-
function generateUniqueId(base) {
|
|
373
|
-
let candidate = `$_${base.replace(/\W/g, "_")}${uniqueIdSeq++}`;
|
|
374
|
-
while (usedUniqueIds.has(candidate) || ctx.code.includes(candidate)) {
|
|
375
|
-
candidate = `$_${base.replace(/\W/g, "_")}${uniqueIdSeq++}`;
|
|
376
|
-
}
|
|
377
|
-
usedUniqueIds.add(candidate);
|
|
378
|
-
return candidate;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
exports.processTemplate = processTemplate;
|
package/lib/parser/script.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Context } from "../context";
|
|
2
|
-
import type { ParserOptionsContext } from "../context/parser-options";
|
|
3
|
-
import type { ESLintExtendedProgram } from "../types";
|
|
4
|
-
/**
|
|
5
|
-
* Parse for script
|
|
6
|
-
*/
|
|
7
|
-
export declare function parseScript(code: string, _ctx: Context, parserOptions: ParserOptionsContext): ESLintExtendedProgram;
|
package/lib/parser/script.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseScript = void 0;
|
|
4
|
-
const debug_1 = require("../debug");
|
|
5
|
-
const ts_patch_1 = require("./ts-patch");
|
|
6
|
-
const parser_object_1 = require("../context/resolve-parser/parser-object");
|
|
7
|
-
/**
|
|
8
|
-
* Parse for script
|
|
9
|
-
*/
|
|
10
|
-
function parseScript(code, _ctx, parserOptions) {
|
|
11
|
-
const parser = parserOptions.getParser();
|
|
12
|
-
let patchResult;
|
|
13
|
-
try {
|
|
14
|
-
const scriptParserOptions = {
|
|
15
|
-
...parserOptions.parserOptions,
|
|
16
|
-
};
|
|
17
|
-
scriptParserOptions.ecmaFeatures = {
|
|
18
|
-
...(scriptParserOptions.ecmaFeatures || {}),
|
|
19
|
-
jsx: true,
|
|
20
|
-
};
|
|
21
|
-
if (parserOptions.isTypeScript() &&
|
|
22
|
-
scriptParserOptions.filePath &&
|
|
23
|
-
scriptParserOptions.project) {
|
|
24
|
-
patchResult = (0, ts_patch_1.tsPatch)(scriptParserOptions);
|
|
25
|
-
}
|
|
26
|
-
const result = (0, parser_object_1.isEnhancedParserObject)(parser)
|
|
27
|
-
? parser.parseForESLint(code, scriptParserOptions)
|
|
28
|
-
: parser.parse(code, scriptParserOptions);
|
|
29
|
-
if ("ast" in result && result.ast != null) {
|
|
30
|
-
return result;
|
|
31
|
-
}
|
|
32
|
-
return { ast: result };
|
|
33
|
-
}
|
|
34
|
-
catch (e) {
|
|
35
|
-
(0, debug_1.debug)("[script] parsing error:", e.message, `@ ${JSON.stringify(code)}
|
|
36
|
-
|
|
37
|
-
${code}`);
|
|
38
|
-
throw e;
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
patchResult?.terminate();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.parseScript = parseScript;
|
package/lib/parser/sort.d.ts
DELETED
package/lib/parser/sort.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sort = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Sort tokens
|
|
6
|
-
*/
|
|
7
|
-
function sort(tokens) {
|
|
8
|
-
return tokens.sort((a, b) => {
|
|
9
|
-
if (a.range[0] !== b.range[0]) {
|
|
10
|
-
return a.range[0] - b.range[0];
|
|
11
|
-
}
|
|
12
|
-
return a.range[1] - b.range[1];
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
exports.sort = sort;
|
package/lib/parser/template.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { ParseResult } from "@astrojs/compiler";
|
|
2
|
-
import { Context } from "../context";
|
|
3
|
-
export declare type TemplateResult = {
|
|
4
|
-
result: ParseResult;
|
|
5
|
-
context: Context;
|
|
6
|
-
};
|
|
7
|
-
/**
|
|
8
|
-
* Parse the astro component template.
|
|
9
|
-
*/
|
|
10
|
-
export declare function parseTemplate(code: string): TemplateResult;
|
package/lib/parser/template.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseTemplate = void 0;
|
|
4
|
-
const astro_1 = require("../astro");
|
|
5
|
-
const context_1 = require("../context");
|
|
6
|
-
const errors_1 = require("../errors");
|
|
7
|
-
const parse_1 = require("./astro-parser/parse");
|
|
8
|
-
const lru_cache_1 = require("./lru-cache");
|
|
9
|
-
const lruCache = new lru_cache_1.LruCache(5);
|
|
10
|
-
/**
|
|
11
|
-
* Parse the astro component template.
|
|
12
|
-
*/
|
|
13
|
-
function parseTemplate(code) {
|
|
14
|
-
const cache = lruCache.get(code);
|
|
15
|
-
if (cache) {
|
|
16
|
-
return cache;
|
|
17
|
-
}
|
|
18
|
-
const ctx = new context_1.Context(code);
|
|
19
|
-
const normalized = ctx.locs.getNormalizedLineFeed();
|
|
20
|
-
const ctxForAstro = normalized.needRemap
|
|
21
|
-
? new context_1.Context(normalized.code)
|
|
22
|
-
: ctx;
|
|
23
|
-
try {
|
|
24
|
-
const result = (0, parse_1.parse)(normalized?.code ?? code, ctxForAstro);
|
|
25
|
-
if (normalized.needRemap) {
|
|
26
|
-
remap(result, normalized, code, ctxForAstro);
|
|
27
|
-
ctx.originalAST = ctxForAstro.originalAST;
|
|
28
|
-
}
|
|
29
|
-
const templateResult = {
|
|
30
|
-
result,
|
|
31
|
-
context: ctx,
|
|
32
|
-
};
|
|
33
|
-
lruCache.set(code, templateResult);
|
|
34
|
-
return templateResult;
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
if (typeof e.pos === "number") {
|
|
38
|
-
const err = new errors_1.ParseError(e.message, normalized?.remapIndex(e.pos), ctx);
|
|
39
|
-
err.astroCompilerError = e;
|
|
40
|
-
throw err;
|
|
41
|
-
}
|
|
42
|
-
throw e;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
exports.parseTemplate = parseTemplate;
|
|
46
|
-
/** Remap */
|
|
47
|
-
function remap(result, normalized, originalCode, ctxForAstro) {
|
|
48
|
-
const remapDataMap = new Map();
|
|
49
|
-
(0, astro_1.walk)(result.ast, normalized.code, (node) => {
|
|
50
|
-
const start = normalized.remapIndex(node.position.start.offset);
|
|
51
|
-
let end, value;
|
|
52
|
-
if (node.position.end) {
|
|
53
|
-
end = normalized.remapIndex(node.position.end.offset);
|
|
54
|
-
if (node.position.start.offset === start &&
|
|
55
|
-
node.position.end.offset === end) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
if (node.type === "text") {
|
|
60
|
-
value = originalCode.slice(start, normalized.remapIndex((0, astro_1.getEndOffset)(node, ctxForAstro)));
|
|
61
|
-
}
|
|
62
|
-
else if (node.type === "comment") {
|
|
63
|
-
value = originalCode.slice(start + 4, normalized.remapIndex((0, astro_1.getEndOffset)(node, ctxForAstro)) - 3);
|
|
64
|
-
}
|
|
65
|
-
else if (node.type === "attribute") {
|
|
66
|
-
if (node.kind !== "empty" &&
|
|
67
|
-
node.kind !== "shorthand" &&
|
|
68
|
-
node.kind !== "spread") {
|
|
69
|
-
let valueStart = normalized.remapIndex((0, astro_1.calcAttributeValueStartOffset)(node, ctxForAstro));
|
|
70
|
-
let valueEnd = normalized.remapIndex((0, astro_1.calcAttributeEndOffset)(node, ctxForAstro));
|
|
71
|
-
if (node.kind !== "quoted" ||
|
|
72
|
-
originalCode[valueStart] === '"' ||
|
|
73
|
-
originalCode[valueStart] === "'") {
|
|
74
|
-
valueStart++;
|
|
75
|
-
valueEnd--;
|
|
76
|
-
}
|
|
77
|
-
value = originalCode.slice(valueStart, valueEnd);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
remapDataMap.set(node, {
|
|
81
|
-
start,
|
|
82
|
-
end,
|
|
83
|
-
value,
|
|
84
|
-
});
|
|
85
|
-
}, (_node) => {
|
|
86
|
-
/* noop */
|
|
87
|
-
});
|
|
88
|
-
for (const [node, remapData] of remapDataMap) {
|
|
89
|
-
node.position.start.offset = remapData.start;
|
|
90
|
-
if (node.position.end) {
|
|
91
|
-
node.position.end.offset = remapData.end;
|
|
92
|
-
}
|
|
93
|
-
if (node.type === "text" ||
|
|
94
|
-
node.type === "comment" ||
|
|
95
|
-
(node.type === "attribute" &&
|
|
96
|
-
node.kind !== "empty" &&
|
|
97
|
-
node.kind !== "shorthand" &&
|
|
98
|
-
node.kind !== "spread")) {
|
|
99
|
-
node.value = remapData.value;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
package/lib/parser/ts-patch.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { ParserOptions } from "@typescript-eslint/types";
|
|
2
|
-
export declare type PatchTerminate = {
|
|
3
|
-
terminate: () => void;
|
|
4
|
-
};
|
|
5
|
-
/**
|
|
6
|
-
* Apply a patch to parse .astro files as TSX.
|
|
7
|
-
*/
|
|
8
|
-
export declare function tsPatch(scriptParserOptions: ParserOptions): PatchTerminate | null;
|
package/lib/parser/ts-patch.js
DELETED
|
@@ -1,65 +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.tsPatch = void 0;
|
|
7
|
-
const module_1 = require("module");
|
|
8
|
-
const path_1 = __importDefault(require("path"));
|
|
9
|
-
const fs_1 = __importDefault(require("fs"));
|
|
10
|
-
/**
|
|
11
|
-
* Apply a patch to parse .astro files as TSX.
|
|
12
|
-
*/
|
|
13
|
-
function tsPatch(scriptParserOptions) {
|
|
14
|
-
let targetExt = ".astro";
|
|
15
|
-
if (scriptParserOptions.filePath) {
|
|
16
|
-
const ext = path_1.default.extname(scriptParserOptions.filePath);
|
|
17
|
-
if (ext) {
|
|
18
|
-
targetExt = ext;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
try {
|
|
22
|
-
// Apply a patch to parse .astro files as TSX.
|
|
23
|
-
const cwd = process.cwd();
|
|
24
|
-
const relativeTo = path_1.default.join(cwd, "__placeholder__.js");
|
|
25
|
-
const ts = (0, module_1.createRequire)(relativeTo)("typescript");
|
|
26
|
-
const { ensureScriptKind, getScriptKindFromFileName } = ts;
|
|
27
|
-
if (typeof ensureScriptKind === "function" &&
|
|
28
|
-
typeof getScriptKindFromFileName === "function") {
|
|
29
|
-
ts.ensureScriptKind = function (fileName, ...args) {
|
|
30
|
-
if (fileName.endsWith(targetExt)) {
|
|
31
|
-
return ts.ScriptKind.TSX;
|
|
32
|
-
}
|
|
33
|
-
return ensureScriptKind.call(this, fileName, ...args);
|
|
34
|
-
};
|
|
35
|
-
ts.getScriptKindFromFileName = function (fileName, ...args) {
|
|
36
|
-
if (fileName.endsWith(targetExt)) {
|
|
37
|
-
return ts.ScriptKind.TSX;
|
|
38
|
-
}
|
|
39
|
-
return getScriptKindFromFileName.call(this, fileName, ...args);
|
|
40
|
-
};
|
|
41
|
-
return {
|
|
42
|
-
terminate() {
|
|
43
|
-
ts.ensureScriptKind = ensureScriptKind;
|
|
44
|
-
ts.getScriptKindFromFileName = getScriptKindFromFileName;
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
catch {
|
|
50
|
-
// ignore
|
|
51
|
-
}
|
|
52
|
-
// If the patch cannot be applied, create a tsx file and parse it.
|
|
53
|
-
const tsxFilePath = `${scriptParserOptions.filePath}.tsx`;
|
|
54
|
-
scriptParserOptions.filePath = tsxFilePath;
|
|
55
|
-
if (!fs_1.default.existsSync(tsxFilePath)) {
|
|
56
|
-
fs_1.default.writeFileSync(tsxFilePath, "/* temp for astro-eslint-parser */");
|
|
57
|
-
return {
|
|
58
|
-
terminate() {
|
|
59
|
-
fs_1.default.unlinkSync(tsxFilePath);
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
exports.tsPatch = tsPatch;
|
package/lib/traverse.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { VisitorKeys } from "eslint-visitor-keys";
|
|
2
|
-
import type { TSESTree } from "@typescript-eslint/types";
|
|
3
|
-
import type { AstroNode } from "./ast";
|
|
4
|
-
/**
|
|
5
|
-
* Get the keys of the given node to traverse it.
|
|
6
|
-
* @param node The node to get.
|
|
7
|
-
* @returns The keys to traverse.
|
|
8
|
-
*/
|
|
9
|
-
export declare function getFallbackKeys(node: any): string[];
|
|
10
|
-
/**
|
|
11
|
-
* Get the keys of the given node to traverse it.
|
|
12
|
-
* @param node The node to get.
|
|
13
|
-
* @returns The keys to traverse.
|
|
14
|
-
*/
|
|
15
|
-
export declare function getKeys(node: any, visitorKeys?: VisitorKeys): string[];
|
|
16
|
-
/**
|
|
17
|
-
* Get the nodes of the given node.
|
|
18
|
-
* @param node The node to get.
|
|
19
|
-
*/
|
|
20
|
-
export declare function getNodes(node: any, key: string): IterableIterator<AstroNode | TSESTree.Node>;
|
|
21
|
-
export interface Visitor<N> {
|
|
22
|
-
visitorKeys?: VisitorKeys;
|
|
23
|
-
enterNode(node: N, parent: N | null): void;
|
|
24
|
-
leaveNode(node: N, parent: N | null): void;
|
|
25
|
-
}
|
|
26
|
-
export declare function traverseNodes(node: AstroNode, visitor: Visitor<AstroNode | TSESTree.Node>): void;
|
|
27
|
-
export declare function traverseNodes(node: TSESTree.Node, visitor: Visitor<TSESTree.Node>): void;
|