astro-eslint-parser 0.0.9 → 0.0.12

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,7 +31,7 @@ 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
35
  const htmlElement = ast.children.find((n) => n.type === "element" && n.name === "html");
36
36
  if (htmlElement) {
37
37
  adjustHTML(ast, htmlElement, ctx);
@@ -40,6 +40,29 @@ function parse(code, ctx) {
40
40
  return { ast };
41
41
  }
42
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
+ }
43
66
  /**
44
67
  * Adjust <html> element node
45
68
  */
@@ -94,7 +117,9 @@ function adjustHTMLBody(ast, htmlElement, htmlEnd, bodyElement, ctx) {
94
117
  function fixLocations(node, ctx) {
95
118
  // FIXME: Adjust because the parser does not return the correct location.
96
119
  let start = 0;
97
- (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) => {
98
123
  if (node.type === "frontmatter") {
99
124
  start = node.position.start.offset = tokenIndex(ctx, "---", start);
100
125
  start = node.position.end.offset =
@@ -123,8 +148,46 @@ function fixLocations(node, ctx) {
123
148
  start = (0, astro_1.getCommentEndOffset)(node, ctx);
124
149
  }
125
150
  else if (node.type === "text") {
126
- start = node.position.start.offset = tokenIndex(ctx, node.value, start);
127
- 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
+ const index = tokenIndexSafe(ctx.code, node.value, start);
163
+ if (index != null) {
164
+ start = node.position.start.offset = index;
165
+ start += node.value.length;
166
+ }
167
+ else {
168
+ // Workaround for escape bugs
169
+ node.position.start.offset = start;
170
+ for (const token of node.value.split(/\s+/u)) {
171
+ const index = tokenIndexSafe(ctx.code, token, start);
172
+ if (index != null) {
173
+ start = index + token.length;
174
+ continue;
175
+ }
176
+ start = (0, astro_1.skipSpaces)(ctx.code, start);
177
+ let t = token;
178
+ if (ctx.code.startsWith("\\", start)) {
179
+ const char = JSON.parse(`"\\${ctx.code[start + 1]}"`);
180
+ if (char.trim()) {
181
+ t = t.slice(1);
182
+ }
183
+ start += 2;
184
+ }
185
+ start = tokenIndex(ctx, t, start) + t.length;
186
+ }
187
+ start = (0, astro_1.skipSpaces)(ctx.code, start);
188
+ node.value = ctx.code.slice(node.position.start.offset, start);
189
+ }
190
+ }
128
191
  }
129
192
  else if (node.type === "expression") {
130
193
  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.9",
3
+ "version": "0.0.12",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -20,6 +20,7 @@
20
20
  "debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
21
21
  "preversion": "npm run lint && npm test",
22
22
  "update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts",
23
+ "debug-parser": "ts-node --transpile-only ./tools/parser-test.ts",
23
24
  "eslint-playground": "eslint tests/fixtures --ext .astro --config .eslintrc-for-playground.js --format codeframe",
24
25
  "benchmark": "ts-node --transpile-only benchmark/index.ts"
25
26
  },