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.
Files changed (69) hide show
  1. package/README.md +8 -10
  2. package/lib/index.d.ts +345 -10
  3. package/lib/index.js +1744 -47
  4. package/lib/index.mjs +1719 -0
  5. package/package.json +22 -17
  6. package/lib/ast/astro.d.ts +0 -49
  7. package/lib/ast/astro.js +0 -2
  8. package/lib/ast/base.d.ts +0 -6
  9. package/lib/ast/base.js +0 -2
  10. package/lib/ast/index.d.ts +0 -8
  11. package/lib/ast/index.js +0 -18
  12. package/lib/ast/jsx.d.ts +0 -91
  13. package/lib/ast/jsx.js +0 -2
  14. package/lib/astro/index.d.ts +0 -56
  15. package/lib/astro/index.js +0 -357
  16. package/lib/astro-tools/index.d.ts +0 -21
  17. package/lib/astro-tools/index.js +0 -26
  18. package/lib/context/index.d.ts +0 -55
  19. package/lib/context/index.js +0 -158
  20. package/lib/context/parser-options.d.ts +0 -8
  21. package/lib/context/parser-options.js +0 -71
  22. package/lib/context/resolve-parser/espree.d.ts +0 -6
  23. package/lib/context/resolve-parser/espree.js +0 -41
  24. package/lib/context/resolve-parser/index.d.ts +0 -5
  25. package/lib/context/resolve-parser/index.js +0 -30
  26. package/lib/context/script.d.ts +0 -34
  27. package/lib/context/script.js +0 -178
  28. package/lib/debug.d.ts +0 -2
  29. package/lib/debug.js +0 -8
  30. package/lib/errors.d.ts +0 -14
  31. package/lib/errors.js +0 -20
  32. package/lib/markdown/frontmatter.d.ts +0 -9
  33. package/lib/markdown/frontmatter.js +0 -53
  34. package/lib/markdown/index.d.ts +0 -19
  35. package/lib/markdown/index.js +0 -60
  36. package/lib/markdown/mdast-util-from-markdown-service.d.ts +0 -5
  37. package/lib/markdown/mdast-util-from-markdown-service.js +0 -12
  38. package/lib/markdown/mdast-util-from-markdown-worker.d.ts +0 -2
  39. package/lib/markdown/mdast-util-from-markdown-worker.js +0 -8
  40. package/lib/markdown/process-markdown.d.ts +0 -6
  41. package/lib/markdown/process-markdown.js +0 -82
  42. package/lib/markdown/yaml.d.ts +0 -9
  43. package/lib/markdown/yaml.js +0 -86
  44. package/lib/parser/astro-parser/astrojs-compiler-service.d.ts +0 -5
  45. package/lib/parser/astro-parser/astrojs-compiler-service.js +0 -12
  46. package/lib/parser/astro-parser/astrojs-compiler-worker.d.ts +0 -1
  47. package/lib/parser/astro-parser/astrojs-compiler-worker.js +0 -11
  48. package/lib/parser/astro-parser/parse.d.ts +0 -6
  49. package/lib/parser/astro-parser/parse.js +0 -267
  50. package/lib/parser/index.d.ts +0 -21
  51. package/lib/parser/index.js +0 -72
  52. package/lib/parser/lru-cache.d.ts +0 -7
  53. package/lib/parser/lru-cache.js +0 -32
  54. package/lib/parser/process-template.d.ts +0 -7
  55. package/lib/parser/process-template.js +0 -381
  56. package/lib/parser/script.d.ts +0 -7
  57. package/lib/parser/script.js +0 -42
  58. package/lib/parser/sort.d.ts +0 -6
  59. package/lib/parser/sort.js +0 -15
  60. package/lib/parser/template.d.ts +0 -10
  61. package/lib/parser/template.js +0 -102
  62. package/lib/parser/ts-patch.d.ts +0 -8
  63. package/lib/parser/ts-patch.js +0 -65
  64. package/lib/traverse.d.ts +0 -27
  65. package/lib/traverse.js +0 -93
  66. package/lib/types.d.ts +0 -21
  67. package/lib/types.js +0 -2
  68. package/lib/visitor-keys.d.ts +0 -2
  69. package/lib/visitor-keys.js +0 -14
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseYaml = void 0;
4
- const yaml_1 = require("yaml");
5
- /** Parse for yaml */
6
- function parseYaml(frontmatter) {
7
- const doc = (0, yaml_1.parseDocument)(frontmatter.value, { keepSourceTokens: true });
8
- const range = getSetupRange(doc);
9
- if (!range) {
10
- return null;
11
- }
12
- let before = range[0];
13
- let after = range[1];
14
- while (isSpace(frontmatter.value[before - 1])) {
15
- before--;
16
- }
17
- while (isSpace(frontmatter.value[after])) {
18
- after++;
19
- }
20
- return {
21
- before: before + frontmatter.range[0],
22
- setupValueRange: [
23
- range[0] + frontmatter.range[0],
24
- range[1] + frontmatter.range[0],
25
- ],
26
- after: after + frontmatter.range[0],
27
- getYamlValue: () => doc.toJS(),
28
- };
29
- }
30
- exports.parseYaml = parseYaml;
31
- /** Checks whether the given char is spaces, or not */
32
- function isSpace(c) {
33
- return c && !c.trim();
34
- }
35
- /** Get setup range */
36
- function getSetupRange(ast) {
37
- if (!ast.contents) {
38
- return null;
39
- }
40
- if (!(0, yaml_1.isMap)(ast.contents)) {
41
- return null;
42
- }
43
- for (const item of ast.contents.items) {
44
- if (!(0, yaml_1.isScalar)(item.key) || !(0, yaml_1.isScalar)(item.value)) {
45
- continue;
46
- }
47
- if (item.key.value !== "setup") {
48
- continue;
49
- }
50
- if (item.value.type === "PLAIN") {
51
- return [item.value.range[0], item.value.range[1]];
52
- }
53
- if (item.value.type === "QUOTE_DOUBLE" ||
54
- item.value.type === "QUOTE_SINGLE") {
55
- return [item.value.range[0] + 1, item.value.range[1] - 1];
56
- }
57
- if (item.value.type === "BLOCK_FOLDED" ||
58
- item.value.type === "BLOCK_LITERAL") {
59
- const cst = item.value.srcToken;
60
- let blockStart = cst.offset;
61
- for (const token of cst.props) {
62
- if (isCommentOrSpace(token) ||
63
- token.type === "block-scalar-header") {
64
- blockStart = token.offset + token.source.length;
65
- continue;
66
- }
67
- /* istanbul ignore next */
68
- throw new Error(`Unknown token:${token.type}`);
69
- }
70
- return [blockStart, item.value.range[1]];
71
- }
72
- break;
73
- }
74
- return null;
75
- }
76
- /**
77
- * Checks whether the given cst is comments, spaces, or not
78
- */
79
- function isCommentOrSpace(node) {
80
- if (node.type === "space" ||
81
- node.type === "newline" ||
82
- node.type === "comment") {
83
- return true;
84
- }
85
- return false;
86
- }
@@ -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,267 +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
- start = node.position.end.offset =
111
- tokenIndex(ctx, "---", start + 3 + node.value.length) + 3;
112
- }
113
- else if (node.type === "fragment" ||
114
- node.type === "element" ||
115
- node.type === "component" ||
116
- node.type === "custom-element") {
117
- if (!node.position) {
118
- node.position = { start: {}, end: {} };
119
- }
120
- start = node.position.start.offset = tokenIndex(ctx, "<", start);
121
- start += 1;
122
- start += node.name.length;
123
- if (!node.attributes.length) {
124
- start = (0, astro_1.calcStartTagEndOffset)(node, ctx);
125
- }
126
- }
127
- else if (node.type === "attribute") {
128
- fixLocationForAttr(node, ctx, start);
129
- start = (0, astro_1.calcAttributeEndOffset)(node, ctx);
130
- if (node.position.end) {
131
- node.position.end.offset = start;
132
- }
133
- }
134
- else if (node.type === "comment") {
135
- node.position.start.offset = tokenIndex(ctx, "<!--", start);
136
- start = (0, astro_1.calcCommentEndOffset)(node, ctx);
137
- if (node.position.end) {
138
- node.position.end.offset = start;
139
- }
140
- }
141
- else if (node.type === "text") {
142
- if (parent.type === "element" &&
143
- (parent.name === "script" || parent.name === "style")) {
144
- node.position.start.offset = start;
145
- start = ctx.code.indexOf(`</${parent.name}`, start);
146
- if (start < 0) {
147
- start = ctx.code.length;
148
- }
149
- }
150
- else {
151
- const index = tokenIndexSafe(ctx.code, node.value, start);
152
- if (index != null) {
153
- start = node.position.start.offset = index;
154
- start += node.value.length;
155
- }
156
- else {
157
- // FIXME: Some white space may be removed.
158
- node.position.start.offset = start;
159
- const value = node.value.replace(/\s+/gu, "");
160
- for (const char of value) {
161
- const index = tokenIndex(ctx, char, start);
162
- start = index + 1;
163
- }
164
- start = (0, astro_1.skipSpaces)(ctx.code, start);
165
- node.value = ctx.code.slice(node.position.start.offset, start);
166
- }
167
- }
168
- if (node.position.end) {
169
- node.position.end.offset = start;
170
- }
171
- }
172
- else if (node.type === "expression") {
173
- start = node.position.start.offset = tokenIndex(ctx, "{", start);
174
- start += 1;
175
- }
176
- else if (node.type === "doctype") {
177
- if (!node.position) {
178
- node.position = { start: {}, end: {} };
179
- }
180
- if (!node.position.end) {
181
- node.position.end = {};
182
- }
183
- start = node.position.start.offset = tokenIndex(ctx, "<!", start);
184
- start += 2;
185
- start = node.position.end.offset =
186
- ctx.code.indexOf(">", start) + 1;
187
- }
188
- else if (node.type === "root") {
189
- // noop
190
- }
191
- }, (node, [parent]) => {
192
- if (node.type === "attribute") {
193
- const attributes = parent.attributes;
194
- if (attributes[attributes.length - 1] === node) {
195
- start = (0, astro_1.calcStartTagEndOffset)(parent, ctx);
196
- }
197
- }
198
- else if (node.type === "expression") {
199
- start = tokenIndex(ctx, "}", start) + 1;
200
- }
201
- else if (node.type === "fragment" ||
202
- node.type === "element" ||
203
- node.type === "component" ||
204
- node.type === "custom-element") {
205
- if (!(0, astro_1.getSelfClosingTag)(node, ctx)) {
206
- const closeTagStart = tokenIndexSafe(ctx.code, `</${node.name}`, start);
207
- if (closeTagStart != null) {
208
- start = closeTagStart + 2 + node.name.length;
209
- start = tokenIndex(ctx, ">", start) + 1;
210
- }
211
- }
212
- }
213
- else {
214
- return;
215
- }
216
- if (node.position.end) {
217
- node.position.end.offset = start;
218
- }
219
- });
220
- }
221
- /**
222
- * Fix locations
223
- */
224
- function fixLocationForAttr(node, ctx, start) {
225
- if (node.kind === "empty") {
226
- node.position.start.offset = tokenIndex(ctx, node.name, start);
227
- }
228
- else if (node.kind === "quoted") {
229
- node.position.start.offset = tokenIndex(ctx, node.name, start);
230
- }
231
- else if (node.kind === "expression") {
232
- node.position.start.offset = tokenIndex(ctx, node.name, start);
233
- }
234
- else if (node.kind === "shorthand") {
235
- node.position.start.offset = tokenIndex(ctx, "{", start);
236
- }
237
- else if (node.kind === "spread") {
238
- node.position.start.offset = tokenIndex(ctx, "{", start);
239
- }
240
- else if (node.kind === "template-literal") {
241
- node.position.start.offset = tokenIndex(ctx, node.name, start);
242
- }
243
- else {
244
- throw new errors_1.ParseError(`Unknown attr kind: ${node.kind}`, node.position.start.offset, ctx);
245
- }
246
- }
247
- /**
248
- * Get token index
249
- */
250
- function tokenIndex(ctx, token, position) {
251
- const index = tokenIndexSafe(ctx.code, token, position);
252
- if (index == null) {
253
- const start = token.trim() === token ? (0, astro_1.skipSpaces)(ctx.code, position) : position;
254
- throw new errors_1.ParseError(`Unknown token at ${start}, expected: ${JSON.stringify(token)}, actual: ${JSON.stringify(ctx.code.slice(start, start + 10))}`, start, ctx);
255
- }
256
- return index;
257
- }
258
- /**
259
- * Get token index
260
- */
261
- function tokenIndexSafe(string, token, position) {
262
- const index = token.trim() === token ? (0, astro_1.skipSpaces)(string, position) : position;
263
- if (string.startsWith(token, index)) {
264
- return index;
265
- }
266
- return null;
267
- }
@@ -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;