Skip to content

Commit 9d67753

Browse files
authored
Merge pull request #68 from sljuka/replace-default-props-implicit-return
feat(replace-default-props): make it work for function components with implicit return
2 parents 00450a4 + 489d0d1 commit 9d67753

5 files changed

Lines changed: 59 additions & 10 deletions

File tree

codemods/react/19/replace-default-props/.codemodrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
33
"name": "react/19/replace-default-props",
4-
"version": "1.0.5",
4+
"version": "1.0.6",
55
"engine": "jscodeshift",
66
"private": false,
77
"arguments": [],
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const Card = ({ user: { name, age } }) => (
2+
<div>
3+
<p>{name}</p>
4+
<p>{age}</p>
5+
</div>
6+
);
7+
8+
Card.defaultProps = {
9+
user: {
10+
name: "Unknown",
11+
age: 0,
12+
},
13+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const cardDefaultPropUser = {
2+
name: "Unknown",
3+
age: 0,
4+
};
5+
6+
const Card = ({ user: { name, age } = cardDefaultPropUser }) => (
7+
<div>
8+
<p>{name}</p>
9+
<p>{age}</p>
10+
</div>
11+
);

codemods/react/19/replace-default-props/src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ export default function transform(
7171
return;
7272
}
7373

74-
const componentFunction = j.functionDeclaration(
75-
j.identifier(componentName),
76-
path.value.params,
77-
path.value.body
78-
);
79-
80-
if (componentFunction === null) {
81-
return;
74+
let componentFunction = null;
75+
const isImplicitReturnComponent = path.value.body.type === "JSXElement";
76+
77+
if (!isImplicitReturnComponent) {
78+
componentFunction = j.functionDeclaration(
79+
j.identifier(componentName),
80+
path.value.params,
81+
path.value.body
82+
);
8283
}
8384

8485
const defaultProps = getComponentStaticPropValue(
@@ -168,7 +169,7 @@ export default function transform(
168169
);
169170
}
170171

171-
if (propsArgName && inlineDefaultProps.length) {
172+
if (componentFunction && propsArgName && inlineDefaultProps.length) {
172173
componentFunction.body.body.unshift(
173174
j.expressionStatement(
174175
j.assignmentExpression(

codemods/react/19/replace-default-props/test/test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,28 @@ describe("react/19/replace-default-props", () => {
262262
OUTPUT.replace(/W/gm, "")
263263
);
264264
});
265+
266+
it("transforms functional components defined via implicit return", async () => {
267+
const INPUT = await readFile(
268+
join(__dirname, "..", "__testfixtures__/implicit-return.input.jsx"),
269+
"utf-8"
270+
);
271+
const OUTPUT = await readFile(
272+
join(__dirname, "..", "__testfixtures__/implicit-return.output.jsx"),
273+
"utf-8"
274+
);
275+
276+
const actualOutput = transform(
277+
{
278+
path: "index.js",
279+
source: INPUT,
280+
},
281+
buildApi("jsx")
282+
);
283+
284+
assert.deepEqual(
285+
actualOutput?.replace(/W/gm, ""),
286+
OUTPUT.replace(/W/gm, "")
287+
);
288+
});
265289
});

0 commit comments

Comments
 (0)