Skip to content

Commit e1fbab6

Browse files
committed
feat: add replace string icon props
1 parent 96b9f27 commit e1fbab6

File tree

6 files changed

+66
-20
lines changed

6 files changed

+66
-20
lines changed

Diff for: .prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__testfixtures__
1+
# __testfixtures__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Button, IconButton, Box } from "@chakra-ui/core";
2+
3+
function Example(props) {
4+
return (
5+
<Box>
6+
<Button leftIcon="phone">Click me</Button>
7+
<IconButton icon="email" />
8+
</Box>
9+
);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Button, IconButton, Box } from "@chakra-ui/core";
2+
3+
import { EmailIcon, PhoneIcon } from "@chakra-ui/icons";
4+
5+
function Example(props) {
6+
return (
7+
<Box>
8+
<Button leftIcon={<PhoneIcon />}>Click me</Button>
9+
<IconButton icon={<EmailIcon />} />
10+
</Box>
11+
);
12+
}

Diff for: transforms/replace-string-icon-props.ts

+38-14
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,58 @@
1010
*/
1111

1212
import { JSCodeshift, Transform } from "jscodeshift";
13-
import { findJSXElementsByModuleName } from "utils/jsx";
14-
import { prepare } from "utils/shared";
15-
16-
const hasProps = (j: JSCodeshift, props: string[]) => {
17-
const keys = props.reduce((acc, val) => {
18-
acc[val] = () => true;
19-
return acc;
20-
}, {});
21-
return j.filters.JSXElement.hasAttributes(keys);
13+
import { createJSXElement, findJSXElementsByModuleName } from "utils/jsx";
14+
import { insertOrCreateSubmoduleImport } from "utils/module";
15+
import { capitalize, prepare } from "utils/shared";
16+
17+
const hasJSXAttribute = (j: JSCodeshift, prop: string) => {
18+
return j.filters.JSXElement.hasAttributes({ [prop]: () => true });
2219
};
2320

21+
const iconsPkgName = "@chakra-ui/icons";
22+
2423
/**
2524
* IconButton: Check for `icon` prop
2625
* Button: Check for `leftIcon` and `rightIcon` prop
2726
*/
2827

2928
const transformer: Transform = (file, api) => {
3029
const config = prepare(file, api);
31-
const { done, j } = config;
30+
const { done, j, root } = config;
3231

3332
findJSXElementsByModuleName({
3433
config,
3534
moduleName: "@chakra-ui/core",
36-
selector: "Button",
35+
selector: "Button|IconButton",
3736
})
38-
.filter(hasProps(j, ["leftIcon", "rightIcon"]))
39-
.forEach((node) => {
40-
console.log(node.value);
37+
.filter((node) => {
38+
return (
39+
hasJSXAttribute(j, "leftIcon")(node) ||
40+
hasJSXAttribute(j, "rightIcon")(node) ||
41+
hasJSXAttribute(j, "icon")(node)
42+
);
43+
})
44+
.find(j.JSXAttribute, (n) =>
45+
["leftIcon", "rightIcon", "icon"].includes(n.name.name),
46+
)
47+
.replaceWith((node) => {
48+
const attrValue =
49+
j.StringLiteral.check(node.value.value) && node.value.value.value;
50+
const attrName =
51+
j.JSXIdentifier.check(node.value.name) && node.value.name.name;
52+
53+
const v1IconName = `${capitalize(attrValue)}Icon`;
54+
const newJSXAttribute = j.jsxAttribute(
55+
j.jsxIdentifier(attrName),
56+
j.jsxExpressionContainer(createJSXElement(j, v1IconName)),
57+
);
58+
59+
insertOrCreateSubmoduleImport(j, root, {
60+
importedName: v1IconName,
61+
moduleName: iconsPkgName,
62+
});
63+
64+
return newJSXAttribute;
4165
});
4266

4367
return done();

Diff for: transforms/v0-icon-to-v1-icon.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import {
44
insertOrCreateSubmoduleImport,
55
removeModuleImport,
66
} from "utils/module";
7-
import { prepare } from "utils/shared";
8-
9-
function capitalize(str: string) {
10-
return str.charAt(0).toUpperCase() + str.slice(1);
11-
}
7+
import { capitalize, prepare } from "utils/shared";
128

139
const iconsPkgName = "@chakra-ui/icons";
1410

Diff for: utils/shared.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ export function prepare(file: FileInfo, api: API) {
1818

1919
return { root, j, done };
2020
}
21+
22+
export function capitalize(str: string) {
23+
return str.charAt(0).toUpperCase() + str.slice(1);
24+
}

0 commit comments

Comments
 (0)