@idomatic/parser-js 1.0.21 → 1.0.23
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.
- package/index.js +10 -30
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,13 +2,13 @@ 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";
|
|
6
5
|
|
|
7
6
|
const require = createRequire(import.meta.url);
|
|
8
7
|
const b = types.builders;
|
|
9
8
|
|
|
10
9
|
export default function parseJS(filePath, config) {
|
|
11
10
|
const code = fs.readFileSync(filePath, "utf-8");
|
|
11
|
+
let changed = false;
|
|
12
12
|
|
|
13
13
|
const ast = parse(code, {
|
|
14
14
|
parser: require("recast/parsers/babel-ts"),
|
|
@@ -18,10 +18,10 @@ export default function parseJS(filePath, config) {
|
|
|
18
18
|
visitJSXOpeningElement(path) {
|
|
19
19
|
const tag = path.node.name.name;
|
|
20
20
|
const tagsToExclude = config.excludeTags || [];
|
|
21
|
+
|
|
21
22
|
if (tagsToExclude.includes(tag)) return false;
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
const attributeName = "id";
|
|
24
|
+
const attributeName = config.attributeName;
|
|
25
25
|
|
|
26
26
|
const hasId = path.node.attributes.some(
|
|
27
27
|
(attr) => attr.name?.name === attributeName
|
|
@@ -35,37 +35,17 @@ export default function parseJS(filePath, config) {
|
|
|
35
35
|
b.stringLiteral(idValue)
|
|
36
36
|
)
|
|
37
37
|
);
|
|
38
|
+
changed = true;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
return false;
|
|
41
42
|
},
|
|
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
|
-
},
|
|
67
43
|
});
|
|
68
44
|
|
|
69
|
-
|
|
70
|
-
|
|
45
|
+
if (changed) {
|
|
46
|
+
const output = print(ast).code;
|
|
47
|
+
fs.writeFileSync(filePath, output);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return changed;
|
|
71
51
|
}
|