astro-eslint-parser 0.5.1 → 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.
Files changed (58) hide show
  1. package/lib/index.d.ts +345 -9
  2. package/lib/index.js +1745 -42
  3. package/lib/index.mjs +1719 -0
  4. package/package.json +9 -7
  5. package/lib/ast/astro.d.ts +0 -49
  6. package/lib/ast/astro.js +0 -2
  7. package/lib/ast/base.d.ts +0 -6
  8. package/lib/ast/base.js +0 -2
  9. package/lib/ast/index.d.ts +0 -8
  10. package/lib/ast/index.js +0 -18
  11. package/lib/ast/jsx.d.ts +0 -91
  12. package/lib/ast/jsx.js +0 -2
  13. package/lib/astro/index.d.ts +0 -56
  14. package/lib/astro/index.js +0 -357
  15. package/lib/astro-tools/index.d.ts +0 -21
  16. package/lib/astro-tools/index.js +0 -26
  17. package/lib/context/index.d.ts +0 -55
  18. package/lib/context/index.js +0 -158
  19. package/lib/context/parser-options.d.ts +0 -8
  20. package/lib/context/parser-options.js +0 -77
  21. package/lib/context/resolve-parser/espree.d.ts +0 -6
  22. package/lib/context/resolve-parser/espree.js +0 -41
  23. package/lib/context/resolve-parser/index.d.ts +0 -7
  24. package/lib/context/resolve-parser/index.js +0 -34
  25. package/lib/context/resolve-parser/parser-object.d.ts +0 -33
  26. package/lib/context/resolve-parser/parser-object.js +0 -27
  27. package/lib/context/script.d.ts +0 -34
  28. package/lib/context/script.js +0 -178
  29. package/lib/debug.d.ts +0 -2
  30. package/lib/debug.js +0 -8
  31. package/lib/errors.d.ts +0 -14
  32. package/lib/errors.js +0 -20
  33. package/lib/parser/astro-parser/astrojs-compiler-service.d.ts +0 -5
  34. package/lib/parser/astro-parser/astrojs-compiler-service.js +0 -12
  35. package/lib/parser/astro-parser/astrojs-compiler-worker.d.ts +0 -1
  36. package/lib/parser/astro-parser/astrojs-compiler-worker.js +0 -11
  37. package/lib/parser/astro-parser/parse.d.ts +0 -6
  38. package/lib/parser/astro-parser/parse.js +0 -270
  39. package/lib/parser/index.d.ts +0 -21
  40. package/lib/parser/index.js +0 -72
  41. package/lib/parser/lru-cache.d.ts +0 -7
  42. package/lib/parser/lru-cache.js +0 -32
  43. package/lib/parser/process-template.d.ts +0 -7
  44. package/lib/parser/process-template.js +0 -390
  45. package/lib/parser/script.d.ts +0 -7
  46. package/lib/parser/script.js +0 -44
  47. package/lib/parser/sort.d.ts +0 -6
  48. package/lib/parser/sort.js +0 -15
  49. package/lib/parser/template.d.ts +0 -10
  50. package/lib/parser/template.js +0 -102
  51. package/lib/parser/ts-patch.d.ts +0 -8
  52. package/lib/parser/ts-patch.js +0 -65
  53. package/lib/traverse.d.ts +0 -27
  54. package/lib/traverse.js +0 -93
  55. package/lib/types.d.ts +0 -17
  56. package/lib/types.js +0 -2
  57. package/lib/visitor-keys.d.ts +0 -2
  58. package/lib/visitor-keys.js +0 -14
@@ -1,178 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ScriptContext = void 0;
4
- const traverse_1 = require("../traverse");
5
- class RestoreNodeProcessContext {
6
- constructor(result, parentMap) {
7
- this.removeTokens = new Set();
8
- this.result = result;
9
- this.parentMap = parentMap;
10
- }
11
- addRemoveToken(test) {
12
- this.removeTokens.add(test);
13
- }
14
- getParent(node) {
15
- return this.parentMap.get(node) || null;
16
- }
17
- }
18
- class ScriptContext {
19
- constructor(ctx) {
20
- this.script = "";
21
- this.consumedIndex = 0;
22
- this.offsets = [];
23
- this.fragments = [];
24
- this.tokens = [];
25
- this.restoreNodeProcesses = [];
26
- this.ctx = ctx;
27
- }
28
- get originalCode() {
29
- return this.ctx.code;
30
- }
31
- skipOriginalOffset(offset) {
32
- this.consumedIndex += offset;
33
- }
34
- appendOriginal(index) {
35
- if (this.consumedIndex >= index) {
36
- return;
37
- }
38
- this.offsets.push({
39
- original: this.consumedIndex,
40
- script: this.script.length,
41
- });
42
- this.script += this.ctx.code.slice(this.consumedIndex, index);
43
- this.consumedIndex = index;
44
- }
45
- appendScript(fragment) {
46
- const start = this.script.length;
47
- this.script += fragment;
48
- this.fragments.push({ start, end: this.script.length });
49
- }
50
- addToken(type, range) {
51
- if (range[0] >= range[1]) {
52
- return;
53
- }
54
- this.tokens.push(this.ctx.buildToken(type, range));
55
- }
56
- addRestoreNodeProcess(process) {
57
- this.restoreNodeProcesses.push(process);
58
- }
59
- /**
60
- * Restore AST nodes
61
- */
62
- restore(result) {
63
- // remap locations
64
- const traversed = new Map();
65
- (0, traverse_1.traverseNodes)(result.ast, {
66
- visitorKeys: result.visitorKeys,
67
- enterNode: (node, p) => {
68
- if (!traversed.has(node)) {
69
- traversed.set(node, p);
70
- this.remapLocation(node);
71
- }
72
- },
73
- leaveNode: (_node) => {
74
- // noop
75
- },
76
- });
77
- const tokens = [...this.tokens];
78
- for (const token of result.ast.tokens || []) {
79
- if (this.fragments.some((f) => f.start <= token.range[0] && token.range[1] <= f.end)) {
80
- continue;
81
- }
82
- this.remapLocation(token);
83
- tokens.push(token);
84
- }
85
- result.ast.tokens = tokens;
86
- for (const token of result.ast.comments || []) {
87
- this.remapLocation(token);
88
- }
89
- const context = new RestoreNodeProcessContext(result, traversed);
90
- let restoreNodeProcesses = this.restoreNodeProcesses;
91
- for (const [node, parent] of traversed) {
92
- if (!parent)
93
- continue;
94
- restoreNodeProcesses = restoreNodeProcesses.filter((proc) => !proc(node, context));
95
- }
96
- if (context.removeTokens.size) {
97
- const tokens = result.ast.tokens || [];
98
- for (let index = tokens.length - 1; index >= 0; index--) {
99
- const token = tokens[index];
100
- for (const rt of context.removeTokens) {
101
- if (rt(token)) {
102
- tokens.splice(index, 1);
103
- context.removeTokens.delete(rt);
104
- if (!context.removeTokens.size) {
105
- break;
106
- }
107
- }
108
- }
109
- }
110
- }
111
- // Adjust program node location
112
- const firstOffset = Math.min(...[
113
- result.ast.body[0],
114
- result.ast.tokens?.[0],
115
- result.ast.comments?.[0],
116
- ]
117
- .filter(Boolean)
118
- .map((t) => t.range[0]));
119
- if (firstOffset < result.ast.range[0]) {
120
- result.ast.range[0] = firstOffset;
121
- result.ast.loc.start = this.ctx.getLocFromIndex(firstOffset);
122
- }
123
- }
124
- remapLocation(node) {
125
- let [start, end] = node.range;
126
- const startFragment = this.fragments.find((f) => f.start <= start && start < f.end);
127
- if (startFragment) {
128
- start = startFragment.end;
129
- }
130
- const endFragment = this.fragments.find((f) => f.start < end && end <= f.end);
131
- if (endFragment) {
132
- end = endFragment.start;
133
- }
134
- if (end < start) {
135
- const w = start;
136
- start = end;
137
- end = w;
138
- }
139
- const locs = this.ctx.getLocations(...this.getRemapRange(start, end));
140
- node.loc = locs.loc;
141
- node.range = locs.range;
142
- if (node.start != null) {
143
- delete node.start;
144
- }
145
- if (node.end != null) {
146
- delete node.end;
147
- }
148
- }
149
- getRemapRange(start, end) {
150
- if (!this.offsets.length) {
151
- return [start, end];
152
- }
153
- let lastStart = this.offsets[0];
154
- let lastEnd = this.offsets[0];
155
- for (const offset of this.offsets) {
156
- if (offset.script <= start) {
157
- lastStart = offset;
158
- }
159
- if (offset.script < end) {
160
- lastEnd = offset;
161
- }
162
- else {
163
- if (offset.script === end) {
164
- const remapStart = lastStart.original + (start - lastStart.script);
165
- if (this.tokens.some((t) => t.range[0] <= remapStart &&
166
- offset.original <= t.range[1])) {
167
- lastEnd = offset;
168
- }
169
- }
170
- break;
171
- }
172
- }
173
- const remapStart = lastStart.original + (start - lastStart.script);
174
- const remapEnd = lastEnd.original + (end - lastEnd.script);
175
- return [remapStart, remapEnd];
176
- }
177
- }
178
- exports.ScriptContext = ScriptContext;
package/lib/debug.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import debugFactory from "debug";
2
- export declare const debug: debugFactory.Debugger;
package/lib/debug.js DELETED
@@ -1,8 +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.debug = void 0;
7
- const debug_1 = __importDefault(require("debug"));
8
- exports.debug = (0, debug_1.default)("astro-eslint-parser");
package/lib/errors.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { Context } from "./context";
2
- /**
3
- * Astro parse errors.
4
- */
5
- export declare class ParseError extends SyntaxError {
6
- index: number;
7
- lineNumber: number;
8
- column: number;
9
- originalAST: any;
10
- /**
11
- * Initialize this ParseError instance.
12
- */
13
- constructor(message: string, offset: number, ctx: Context);
14
- }
package/lib/errors.js DELETED
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ParseError = void 0;
4
- /**
5
- * Astro parse errors.
6
- */
7
- class ParseError extends SyntaxError {
8
- /**
9
- * Initialize this ParseError instance.
10
- */
11
- constructor(message, offset, ctx) {
12
- super(message);
13
- this.index = offset;
14
- const loc = ctx.getLocFromIndex(offset);
15
- this.lineNumber = loc.line;
16
- this.column = loc.column;
17
- this.originalAST = ctx.originalAST;
18
- }
19
- }
20
- exports.ParseError = ParseError;
@@ -1,5 +0,0 @@
1
- import type { ParseOptions, ParseResult } from "@astrojs/compiler";
2
- /**
3
- * Parse code by `@astrojs/compiler`
4
- */
5
- export declare function parse(code: string, options: ParseOptions): ParseResult;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parse = void 0;
4
- const synckit_1 = require("synckit");
5
- const parseSync = (0, synckit_1.createSyncFn)(require.resolve("./astrojs-compiler-worker"));
6
- /**
7
- * Parse code by `@astrojs/compiler`
8
- */
9
- function parse(code, options) {
10
- return parseSync(code, options);
11
- }
12
- exports.parse = parse;
@@ -1 +0,0 @@
1
- export {};
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const synckit_1 = require("synckit");
4
- const dynamicImport = new Function("m", "return import(m)");
5
- (0, synckit_1.runAsWorker)(async (source) => {
6
- const { parse } = await dynamicImport("@astrojs/compiler");
7
- // console.time("parse")
8
- const result = parse(source);
9
- // console.timeEnd("parse")
10
- return result;
11
- });
@@ -1,6 +0,0 @@
1
- import type { ParseResult } from "@astrojs/compiler";
2
- import type { Context } from "../../context";
3
- /**
4
- * Parse code by `@astrojs/compiler`
5
- */
6
- export declare function parse(code: string, ctx: Context): ParseResult;
@@ -1,270 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.parse = void 0;
27
- const service = __importStar(require("./astrojs-compiler-service"));
28
- const astro_1 = require("../../astro");
29
- const errors_1 = require("../../errors");
30
- /**
31
- * Parse code by `@astrojs/compiler`
32
- */
33
- function parse(code, ctx) {
34
- const ast = service.parse(code, { position: true }).ast;
35
- if (!ast.children) {
36
- // If the source code is empty, the children property may not be available.
37
- ast.children = [];
38
- }
39
- const htmlElement = ast.children.find((n) => n.type === "element" && n.name === "html");
40
- if (htmlElement) {
41
- adjustHTML(ast, htmlElement, ctx);
42
- }
43
- fixLocations(ast, ctx);
44
- return { ast };
45
- }
46
- exports.parse = parse;
47
- /**
48
- * Adjust <html> element node
49
- */
50
- function adjustHTML(ast, htmlElement, ctx) {
51
- const htmlEnd = ctx.code.indexOf("</html");
52
- if (htmlEnd == null) {
53
- return;
54
- }
55
- const hasTokenAfter = Boolean(ctx.code.slice(htmlEnd + 7).trim());
56
- const children = [...htmlElement.children];
57
- for (const child of children) {
58
- const offset = child.position?.start.offset;
59
- if (hasTokenAfter && offset != null) {
60
- if (htmlEnd <= offset) {
61
- htmlElement.children.splice(htmlElement.children.indexOf(child), 1);
62
- ast.children.push(child);
63
- }
64
- }
65
- if (child.type === "element" && child.name === "body") {
66
- adjustHTMLBody(ast, htmlElement, htmlEnd, hasTokenAfter, child, ctx);
67
- }
68
- }
69
- }
70
- /**
71
- * Adjust <body> element node
72
- */
73
- function adjustHTMLBody(ast, htmlElement, htmlEnd, hasTokenAfterHtmlEnd, bodyElement, ctx) {
74
- const bodyEnd = ctx.code.indexOf("</body");
75
- if (bodyEnd == null) {
76
- return;
77
- }
78
- const hasTokenAfter = Boolean(ctx.code.slice(bodyEnd + 7, htmlEnd).trim());
79
- if (!hasTokenAfter && !hasTokenAfterHtmlEnd) {
80
- return;
81
- }
82
- const children = [...bodyElement.children];
83
- for (const child of children) {
84
- const offset = child.position?.start.offset;
85
- if (offset != null) {
86
- if (bodyEnd <= offset) {
87
- if (hasTokenAfterHtmlEnd && htmlEnd <= offset) {
88
- bodyElement.children.splice(bodyElement.children.indexOf(child), 1);
89
- ast.children.push(child);
90
- }
91
- else if (hasTokenAfter) {
92
- bodyElement.children.splice(bodyElement.children.indexOf(child), 1);
93
- htmlElement.children.push(child);
94
- }
95
- }
96
- }
97
- }
98
- }
99
- /**
100
- * Fix locations
101
- */
102
- function fixLocations(node, ctx) {
103
- // FIXME: Adjust because the parser does not return the correct location.
104
- let start = 0;
105
- (0, astro_1.walk)(node, ctx.code,
106
- // eslint-disable-next-line complexity -- X(
107
- (node, [parent]) => {
108
- if (node.type === "frontmatter") {
109
- start = node.position.start.offset = tokenIndex(ctx, "---", start);
110
- if (!node.position.end) {
111
- node.position.end = {};
112
- }
113
- start = node.position.end.offset =
114
- tokenIndex(ctx, "---", start + 3 + node.value.length) + 3;
115
- }
116
- else if (node.type === "fragment" ||
117
- node.type === "element" ||
118
- node.type === "component" ||
119
- node.type === "custom-element") {
120
- if (!node.position) {
121
- node.position = { start: {}, end: {} };
122
- }
123
- start = node.position.start.offset = tokenIndex(ctx, "<", start);
124
- start += 1;
125
- start += node.name.length;
126
- if (!node.attributes.length) {
127
- start = (0, astro_1.calcStartTagEndOffset)(node, ctx);
128
- }
129
- }
130
- else if (node.type === "attribute") {
131
- fixLocationForAttr(node, ctx, start);
132
- start = (0, astro_1.calcAttributeEndOffset)(node, ctx);
133
- if (node.position.end) {
134
- node.position.end.offset = start;
135
- }
136
- }
137
- else if (node.type === "comment") {
138
- node.position.start.offset = tokenIndex(ctx, "<!--", start);
139
- start = (0, astro_1.calcCommentEndOffset)(node, ctx);
140
- if (node.position.end) {
141
- node.position.end.offset = start;
142
- }
143
- }
144
- else if (node.type === "text") {
145
- if (parent.type === "element" &&
146
- (parent.name === "script" || parent.name === "style")) {
147
- node.position.start.offset = start;
148
- start = ctx.code.indexOf(`</${parent.name}`, start);
149
- if (start < 0) {
150
- start = ctx.code.length;
151
- }
152
- }
153
- else {
154
- const index = tokenIndexSafe(ctx.code, node.value, start);
155
- if (index != null) {
156
- start = node.position.start.offset = index;
157
- start += node.value.length;
158
- }
159
- else {
160
- // FIXME: Some white space may be removed.
161
- node.position.start.offset = start;
162
- const value = node.value.replace(/\s+/gu, "");
163
- for (const char of value) {
164
- const index = tokenIndex(ctx, char, start);
165
- start = index + 1;
166
- }
167
- start = (0, astro_1.skipSpaces)(ctx.code, start);
168
- node.value = ctx.code.slice(node.position.start.offset, start);
169
- }
170
- }
171
- if (node.position.end) {
172
- node.position.end.offset = start;
173
- }
174
- }
175
- else if (node.type === "expression") {
176
- start = node.position.start.offset = tokenIndex(ctx, "{", start);
177
- start += 1;
178
- }
179
- else if (node.type === "doctype") {
180
- if (!node.position) {
181
- node.position = { start: {}, end: {} };
182
- }
183
- if (!node.position.end) {
184
- node.position.end = {};
185
- }
186
- start = node.position.start.offset = tokenIndex(ctx, "<!", start);
187
- start += 2;
188
- start = node.position.end.offset =
189
- ctx.code.indexOf(">", start) + 1;
190
- }
191
- else if (node.type === "root") {
192
- // noop
193
- }
194
- }, (node, [parent]) => {
195
- if (node.type === "attribute") {
196
- const attributes = parent.attributes;
197
- if (attributes[attributes.length - 1] === node) {
198
- start = (0, astro_1.calcStartTagEndOffset)(parent, ctx);
199
- }
200
- }
201
- else if (node.type === "expression") {
202
- start = tokenIndex(ctx, "}", start) + 1;
203
- }
204
- else if (node.type === "fragment" ||
205
- node.type === "element" ||
206
- node.type === "component" ||
207
- node.type === "custom-element") {
208
- if (!(0, astro_1.getSelfClosingTag)(node, ctx)) {
209
- const closeTagStart = tokenIndexSafe(ctx.code, `</${node.name}`, start);
210
- if (closeTagStart != null) {
211
- start = closeTagStart + 2 + node.name.length;
212
- start = tokenIndex(ctx, ">", start) + 1;
213
- }
214
- }
215
- }
216
- else {
217
- return;
218
- }
219
- if (node.position.end) {
220
- node.position.end.offset = start;
221
- }
222
- });
223
- }
224
- /**
225
- * Fix locations
226
- */
227
- function fixLocationForAttr(node, ctx, start) {
228
- if (node.kind === "empty") {
229
- node.position.start.offset = tokenIndex(ctx, node.name, start);
230
- }
231
- else if (node.kind === "quoted") {
232
- node.position.start.offset = tokenIndex(ctx, node.name, start);
233
- }
234
- else if (node.kind === "expression") {
235
- node.position.start.offset = tokenIndex(ctx, node.name, start);
236
- }
237
- else if (node.kind === "shorthand") {
238
- node.position.start.offset = tokenIndex(ctx, "{", start);
239
- }
240
- else if (node.kind === "spread") {
241
- node.position.start.offset = tokenIndex(ctx, "{", start);
242
- }
243
- else if (node.kind === "template-literal") {
244
- node.position.start.offset = tokenIndex(ctx, node.name, start);
245
- }
246
- else {
247
- throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
248
- }
249
- }
250
- /**
251
- * Get token index
252
- */
253
- function tokenIndex(ctx, token, position) {
254
- const index = tokenIndexSafe(ctx.code, token, position);
255
- if (index == null) {
256
- const start = token.trim() === token ? (0, astro_1.skipSpaces)(ctx.code, position) : position;
257
- throw new errors_1.ParseError(`Unknown token at ${start}, expected: ${JSON.stringify(token)}, actual: ${JSON.stringify(ctx.code.slice(start, start + 10))}`, start, ctx);
258
- }
259
- return index;
260
- }
261
- /**
262
- * Get token index
263
- */
264
- function tokenIndexSafe(string, token, position) {
265
- const index = token.trim() === token ? (0, astro_1.skipSpaces)(string, position) : position;
266
- if (string.startsWith(token, index)) {
267
- return index;
268
- }
269
- return null;
270
- }
@@ -1,21 +0,0 @@
1
- import type { Context } from "../context";
2
- import type { AstroProgram } from "../ast";
3
- import type { ScopeManager } from "eslint-scope";
4
- import type { ParseResult } from "@astrojs/compiler";
5
- import type { ESLintExtendedProgram } from "../types";
6
- /**
7
- * Parse source code
8
- */
9
- export declare function parseForESLint(code: string, options?: any): {
10
- ast: AstroProgram;
11
- services: Record<string, any> & {
12
- isAstro: true;
13
- getAstroAst: () => ParseResult;
14
- };
15
- visitorKeys: {
16
- [type: string]: string[];
17
- };
18
- scopeManager: ScopeManager;
19
- };
20
- /** Extract tokens */
21
- export declare function extractTokens(ast: ESLintExtendedProgram, ctx: Context): void;
@@ -1,72 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.extractTokens = exports.parseForESLint = void 0;
4
- const visitor_keys_1 = require("../visitor-keys");
5
- const types_1 = require("@typescript-eslint/types");
6
- const script_1 = require("./script");
7
- const sort_1 = require("./sort");
8
- const process_template_1 = require("./process-template");
9
- const template_1 = require("./template");
10
- const parser_options_1 = require("../context/parser-options");
11
- /**
12
- * Parse source code
13
- */
14
- function parseForESLint(code, options) {
15
- const { result: resultTemplate, context: ctx } = (0, template_1.parseTemplate)(code);
16
- const scriptContext = (0, process_template_1.processTemplate)(ctx, resultTemplate);
17
- const parserOptions = new parser_options_1.ParserOptionsContext(options);
18
- const resultScript = (0, script_1.parseScript)(scriptContext.script, ctx, parserOptions);
19
- scriptContext.restore(resultScript);
20
- (0, sort_1.sort)(resultScript.ast.comments);
21
- (0, sort_1.sort)(resultScript.ast.tokens);
22
- extractTokens(resultScript, ctx);
23
- resultScript.services = Object.assign(resultScript.services || {}, {
24
- isAstro: true,
25
- getAstroAst() {
26
- return resultTemplate.ast;
27
- },
28
- });
29
- resultScript.visitorKeys = Object.assign({}, visitor_keys_1.KEYS, resultScript.visitorKeys);
30
- return resultScript;
31
- }
32
- exports.parseForESLint = parseForESLint;
33
- /** Extract tokens */
34
- function extractTokens(ast, ctx) {
35
- if (!ast.ast.tokens) {
36
- return;
37
- }
38
- const useRanges = (0, sort_1.sort)([
39
- ...ast.ast.tokens,
40
- ...(ast.ast.comments || []),
41
- ]).map((t) => t.range);
42
- let range = useRanges.shift();
43
- for (let index = 0; index < ctx.code.length; index++) {
44
- while (range && range[1] <= index) {
45
- range = useRanges.shift();
46
- }
47
- if (range && range[0] <= index) {
48
- index = range[1] - 1;
49
- continue;
50
- }
51
- const c = ctx.code[index];
52
- if (!c.trim()) {
53
- continue;
54
- }
55
- if (isPunctuator(c)) {
56
- ast.ast.tokens.push(ctx.buildToken(types_1.AST_TOKEN_TYPES.Punctuator, [index, index + 1]));
57
- }
58
- else {
59
- // unknown
60
- // It is may be a bug.
61
- ast.ast.tokens.push(ctx.buildToken(types_1.AST_TOKEN_TYPES.Identifier, [index, index + 1]));
62
- }
63
- }
64
- (0, sort_1.sort)(ast.ast.tokens);
65
- /**
66
- * Checks if the given char is punctuator
67
- */
68
- function isPunctuator(c) {
69
- return /^[^\w$]$/iu.test(c);
70
- }
71
- }
72
- exports.extractTokens = extractTokens;
@@ -1,7 +0,0 @@
1
- export declare class LruCache<K, V> extends Map<K, V> {
2
- private readonly capacity;
3
- constructor(capacity: number);
4
- get(key: K): V | undefined;
5
- set(key: K, value: V): this;
6
- private deleteOldestEntry;
7
- }
@@ -1,32 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LruCache = void 0;
4
- class LruCache extends Map {
5
- constructor(capacity) {
6
- super();
7
- this.capacity = capacity;
8
- }
9
- get(key) {
10
- if (!this.has(key)) {
11
- return undefined;
12
- }
13
- const value = super.get(key);
14
- this.set(key, value);
15
- return value;
16
- }
17
- set(key, value) {
18
- this.delete(key);
19
- super.set(key, value);
20
- if (this.size > this.capacity) {
21
- this.deleteOldestEntry();
22
- }
23
- return this;
24
- }
25
- deleteOldestEntry() {
26
- for (const entry of this) {
27
- this.delete(entry[0]);
28
- return;
29
- }
30
- }
31
- }
32
- exports.LruCache = LruCache;
@@ -1,7 +0,0 @@
1
- import type { ParseResult } from "@astrojs/compiler";
2
- import type { Context } from "../context";
3
- import { ScriptContext } from "../context/script";
4
- /**
5
- * Process the template to generate a ScriptContext.
6
- */
7
- export declare function processTemplate(ctx: Context, resultTemplate: ParseResult): ScriptContext;