astro-eslint-parser 0.0.7 → 0.0.10

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,11 +31,86 @@ 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 you 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
  */
@@ -8,6 +8,8 @@ const script_1 = require("../context/script");
8
8
  * Process the template to generate a ScriptContext.
9
9
  */
10
10
  function processTemplate(ctx, resultTemplate) {
11
+ let uniqueIdSeq = 0;
12
+ const usedUniqueIds = new Set();
11
13
  const script = new script_1.ScriptContext(ctx);
12
14
  const frontmatter = resultTemplate.ast.children.find((n) => n.type === "frontmatter");
13
15
  let fragmentOpened = false;
@@ -99,13 +101,19 @@ function processTemplate(ctx, resultTemplate) {
99
101
  if (attr.kind === "shorthand") {
100
102
  const start = attr.position.start.offset;
101
103
  script.appendOriginal(start);
102
- script.appendScript(`${attr.name}=`);
104
+ const jsxName = /[\s"'[\]{}]/u.test(attr.name)
105
+ ? generateUniqueId(attr.name)
106
+ : attr.name;
107
+ script.appendScript(`${jsxName}=`);
103
108
  script.addRestoreNodeProcess((scriptNode) => {
104
109
  if (scriptNode.type === types_1.AST_NODE_TYPES.JSXAttribute &&
105
110
  scriptNode.range[0] === start) {
106
111
  const attrNode = scriptNode;
107
112
  attrNode.type = "AstroShorthandAttribute";
108
113
  const locs = ctx.getLocations(...attrNode.value.expression.range);
114
+ if (jsxName !== attr.name) {
115
+ attrNode.name.name = attr.name;
116
+ }
109
117
  attrNode.name.range = locs.range;
110
118
  attrNode.name.loc = locs.loc;
111
119
  return true;
@@ -228,6 +236,17 @@ function processTemplate(ctx, resultTemplate) {
228
236
  script.appendOriginal(ctx.code.length);
229
237
  script.appendScript("</>");
230
238
  return script;
239
+ /**
240
+ * Generate unique id
241
+ */
242
+ function generateUniqueId(base) {
243
+ let candidate = `$_${base.replace(/\W/g, "_")}${uniqueIdSeq++}`;
244
+ while (usedUniqueIds.has(candidate) || ctx.code.includes(candidate)) {
245
+ candidate = `$_${base.replace(/\W/g, "_")}${uniqueIdSeq++}`;
246
+ }
247
+ usedUniqueIds.add(candidate);
248
+ return candidate;
249
+ }
231
250
  }
232
251
  exports.processTemplate = processTemplate;
233
252
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-eslint-parser",
3
- "version": "0.0.7",
3
+ "version": "0.0.10",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [