forked from ResearchHub/researchhub-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iconsCodeMod.js
34 lines (30 loc) · 919 Bytes
/
iconsCodeMod.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import React from "react";
import icons from "./config/themes/icons.json";
module.exports = function (fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);
// Find all instances of icons.____
root
.find(j.MemberExpression, {
object: {
name: "icons",
},
})
.replaceWith((path) => {
// Get the property name
const propertyName = path.node.property.name;
// Get the value from the icons object
const value = icons[propertyName];
if (value === undefined) {
// If value is undefined, return original node
return path.node;
} else if (React.isValidElement(value)) {
// If value is a React element, return it directly
return value;
} else {
// Otherwise, replace with a literal containing the value
return j.literal(value);
}
});
return root.toSource();
};