astro-eslint-parser 0.0.7 → 0.0.8

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.
@@ -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.8",
4
4
  "description": "Astro parser for ESLint",
5
5
  "main": "lib/index.js",
6
6
  "files": [