astro-eslint-parser 0.0.8 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,7 @@
1
- import type { ParseOptions, ParseResult } from "@astrojs/compiler";
1
+ import type { ParseOptions } from "@astrojs/compiler";
2
2
  /**
3
3
  * Parse code by `@astrojs/compiler`
4
4
  */
5
- export declare function parse(code: string, options: ParseOptions): ParseResult;
5
+ export declare function parse(code: string, options: ParseOptions): {
6
+ ast: string;
7
+ };
@@ -16,7 +16,6 @@ const service = globalThis["@astrojs/compiler"];
16
16
  * Parse code by `@astrojs/compiler`
17
17
  */
18
18
  function parse(code, options) {
19
- const ast = JSON.parse(service.parse(code, options).ast);
20
- return { ast };
19
+ return service.parse(code, options);
21
20
  }
22
21
  exports.parse = parse;
@@ -31,18 +31,95 @@ const errors_1 = require("../../errors");
31
31
  * Parse code by `@astrojs/compiler`
32
32
  */
33
33
  function parse(code, ctx) {
34
- const ast = service.parse(code, { position: true }).ast;
34
+ const ast = parseByService(code).ast;
35
+ const htmlElement = ast.children.find((n) => n.type === "element" && n.name === "html");
36
+ if (htmlElement) {
37
+ adjustHTML(ast, htmlElement, ctx);
38
+ }
35
39
  fixLocations(ast, ctx);
36
40
  return { ast };
37
41
  }
38
42
  exports.parse = parse;
43
+ /**
44
+ * Parse code by `@astrojs/compiler`
45
+ */
46
+ function parseByService(code) {
47
+ const jsonAst = service.parse(code, { position: true }).ast;
48
+ try {
49
+ const ast = JSON.parse(jsonAst);
50
+ return { ast };
51
+ }
52
+ catch (_a) {
53
+ // Adjust because may get the wrong escape as JSON.
54
+ const ast = JSON.parse(jsonAst.replace(/\\./gu, (m) => {
55
+ try {
56
+ JSON.parse(`"${m}"`);
57
+ return m;
58
+ }
59
+ catch (_a) {
60
+ return `\\${m}`;
61
+ }
62
+ }));
63
+ return { ast };
64
+ }
65
+ }
66
+ /**
67
+ * Adjust <html> element node
68
+ */
69
+ function adjustHTML(ast, htmlElement, ctx) {
70
+ var _a;
71
+ const htmlEnd = ctx.code.indexOf("</html");
72
+ if (htmlEnd == null) {
73
+ return;
74
+ }
75
+ const children = [...htmlElement.children];
76
+ for (const child of children) {
77
+ const offset = (_a = child.position) === null || _a === void 0 ? void 0 : _a.start.offset;
78
+ if (offset != null) {
79
+ if (htmlEnd <= offset) {
80
+ htmlElement.children.splice(htmlElement.children.indexOf(child), 1);
81
+ ast.children.push(child);
82
+ }
83
+ }
84
+ if (child.type === "element" && child.name === "body") {
85
+ adjustHTMLBody(ast, htmlElement, htmlEnd, child, ctx);
86
+ }
87
+ }
88
+ }
89
+ /**
90
+ * Adjust <body> element node
91
+ */
92
+ function adjustHTMLBody(ast, htmlElement, htmlEnd, bodyElement, ctx) {
93
+ var _a;
94
+ const bodyEnd = ctx.code.indexOf("</body");
95
+ if (bodyEnd == null) {
96
+ return;
97
+ }
98
+ const children = [...bodyElement.children];
99
+ for (const child of children) {
100
+ const offset = (_a = child.position) === null || _a === void 0 ? void 0 : _a.start.offset;
101
+ if (offset != null) {
102
+ if (bodyEnd <= offset) {
103
+ bodyElement.children.splice(bodyElement.children.indexOf(child), 1);
104
+ if (htmlEnd <= offset) {
105
+ ast.children.push(child);
106
+ }
107
+ else {
108
+ htmlElement.children.push(child);
109
+ }
110
+ }
111
+ }
112
+ }
113
+ }
39
114
  /**
40
115
  * Fix locations
41
116
  */
42
117
  function fixLocations(node, ctx) {
43
118
  // FIXME: Adjust because the parser does not return the correct location.
44
119
  let start = 0;
45
- (0, astro_1.walk)(node, ctx.code, (node) => {
120
+ (0, astro_1.walk)(node, ctx.code,
121
+ // eslint-disable-next-line complexity -- X(
122
+ (node, parent) => {
46
123
  if (node.type === "frontmatter") {
47
124
  start = node.position.start.offset = tokenIndex(ctx, "---", start);
48
125
  start = node.position.end.offset =
@@ -71,8 +148,20 @@ function fixLocations(node, ctx) {
71
148
  start = (0, astro_1.getCommentEndOffset)(node, ctx);
72
149
  }
73
150
  else if (node.type === "text") {
74
- start = node.position.start.offset = tokenIndex(ctx, node.value, start);
75
- start += node.value.length;
151
+ if (parent.type === "element" &&
152
+ (parent.name === "script" || parent.name === "style")) {
153
+ node.position.start.offset = start;
154
+ start = ctx.code.indexOf(`</${parent.name}`, start);
155
+ if (start < 0) {
156
+ start = ctx.code.length;
157
+ }
158
+ // Workaround for escape bugs
159
+ node.value = ctx.code.slice(node.position.start.offset, start);
160
+ }
161
+ else {
162
+ start = node.position.start.offset = tokenIndex(ctx, node.value, start);
163
+ start += node.value.length;
164
+ }
76
165
  }
77
166
  else if (node.type === "expression") {
78
167
  start = node.position.start.offset = tokenIndex(ctx, "{", start);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-eslint-parser",
3
- "version": "0.0.8",
3
+ "version": "0.0.11",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [