@idomatic/parser-js 1.0.22 → 1.0.24

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 +17 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,8 +6,15 @@ import { createRequire } from "module";
6
6
  const require = createRequire(import.meta.url);
7
7
  const b = types.builders;
8
8
 
9
- export default function parseJS(filePath, config) {
9
+ /**
10
+ * @param {string} filePath - Full path to file
11
+ * @param {object} config - iDomatic config
12
+ * @param {boolean} dry - Whether to skip writing to disk
13
+ * @returns {boolean} - True if the file would be (or was) updated
14
+ */
15
+ export default function parseJS(filePath, config, dry = false) {
10
16
  const code = fs.readFileSync(filePath, "utf-8");
17
+ let changed = false;
11
18
 
12
19
  const ast = parse(code, {
13
20
  parser: require("recast/parsers/babel-ts"),
@@ -17,10 +24,10 @@ export default function parseJS(filePath, config) {
17
24
  visitJSXOpeningElement(path) {
18
25
  const tag = path.node.name.name;
19
26
  const tagsToExclude = config.excludeTags || [];
27
+
20
28
  if (tagsToExclude.includes(tag)) return false;
21
29
 
22
- // For MVP, attributeName is hardcoded as "id"
23
- const attributeName = "id";
30
+ const attributeName = config.attributeName;
24
31
 
25
32
  const hasId = path.node.attributes.some(
26
33
  (attr) => attr.name?.name === attributeName
@@ -34,12 +41,17 @@ export default function parseJS(filePath, config) {
34
41
  b.stringLiteral(idValue)
35
42
  )
36
43
  );
44
+ changed = true;
37
45
  }
38
46
 
39
47
  return false;
40
48
  },
41
49
  });
42
50
 
43
- const output = print(ast).code;
44
- fs.writeFileSync(filePath, output);
51
+ if (changed && !dry) {
52
+ const output = print(ast).code;
53
+ fs.writeFileSync(filePath, output);
54
+ }
55
+
56
+ return changed;
45
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idomatic/parser-js",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "JS/TSX parser for idomatic CLI",
5
5
  "type": "module",
6
6
  "main": "index.js",