@idomatic/parser-js 1.0.1 → 1.0.21

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 (2) hide show
  1. package/index.js +34 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -2,6 +2,7 @@ import fs from "fs";
2
2
  import { parse, print, types } from "recast";
3
3
  import { randomUUID } from "crypto";
4
4
  import { createRequire } from "module";
5
+ import cheerio from "cheerio";
5
6
 
6
7
  const require = createRequire(import.meta.url);
7
8
  const b = types.builders;
@@ -16,27 +17,55 @@ export default function parseJS(filePath, config) {
16
17
  types.visit(ast, {
17
18
  visitJSXOpeningElement(path) {
18
19
  const tag = path.node.name.name;
20
+ const tagsToExclude = config.excludeTags || [];
21
+ if (tagsToExclude.includes(tag)) return false;
19
22
 
20
- if (config.skipTags.includes(tag)) return false;
23
+ // For MVP, attributeName is hardcoded as "id"
24
+ const attributeName = "id";
21
25
 
22
26
  const hasId = path.node.attributes.some(
23
- (attr) => attr.name?.name === "id"
27
+ (attr) => attr.name?.name === attributeName
24
28
  );
25
29
 
26
30
  if (!hasId) {
31
+ const idValue = `${config.prefix}${randomUUID()}`;
27
32
  path.node.attributes.push(
28
33
  b.jsxAttribute(
29
- b.jsxIdentifier("id"),
30
- b.stringLiteral(`${config.prefix}${randomUUID()}`)
34
+ b.jsxIdentifier(attributeName),
35
+ b.stringLiteral(idValue)
31
36
  )
32
37
  );
33
38
  }
34
39
 
35
40
  return false;
36
41
  },
42
+ // Process Angular inline templates defined in TS files
43
+ visitObjectProperty(path) {
44
+ if (
45
+ path.node.key.name === "template" &&
46
+ path.node.value.type === "StringLiteral"
47
+ ) {
48
+ const templateValue = path.node.value.value;
49
+
50
+ const $ = cheerio.load(templateValue, { xmlMode: false });
51
+
52
+ $("*").each((i, elem) => {
53
+ const tagName = elem.tagName.toLowerCase();
54
+ const tagsToExclude = config.excludeTags || [];
55
+ if (tagsToExclude.includes(tagName)) return;
56
+ if (!$(elem).attr("id")) {
57
+ const idValue = `${config.prefix}${randomUUID()}`;
58
+ $(elem).attr("id", idValue);
59
+ }
60
+ });
61
+
62
+ const newTemplate = $.html();
63
+ path.node.value.value = newTemplate;
64
+ }
65
+ this.traverse(path);
66
+ },
37
67
  });
38
68
 
39
69
  const output = print(ast).code;
40
-
41
70
  fs.writeFileSync(filePath, output);
42
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-js",
3
- "version": "1.0.1",
3
+ "version": "1.0.21",
4
4
  "description": "JS/TSX parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",