feat: add React 19 replace default props transform
๐ Description
This PR is created to update the react-codemod repository with the updates for the react/19/replace-default-props codemod and to align the open-source content of this codemod in react-codemod similar to the Codemod repository.
-
Added Codemod Utils Package
- Added
@codemod.com/codemod-utilspackage as a dependency to facilitate codemod development.
- Added
-
Added Custom TypeScript Configuration for Codemods
"Introduced
tsconfig.codemod.jsonto provide a dedicated TypeScript configuration for transforms from the Codemod repository.
A dedicatedtsconfig.codemod.jsonhas been added to support the new transform from the Codemod repository and ensure it has the required TypeScript configuration.
-
New Transform: React 19 Replace Default Props
- Added a new transform
react-19-replace-default-propsto handle default props migration for React 19. - Comprehensive tests were included to ensure reliability and coverage.
- Available will using
npx codemod react/19/replace-default-props
- Added a new transform
๐งช Test Plan
The following steps were taken to ensure the changes function as intended:
- Ran the test suite to validate existing functionality and new transforms.
- Verified type resolution and TypeScript transform compatibility.
- To confirm accuracy, the
react-19-replace-default-propstransform was executed against test cases.
thanks @amirabbas-gh (and @mohebifar)!
@rickhanlonii, this is the first of many PRs from Codemod. appreciate your review! excited about taking this repo to a whole new level one PR at a time! :D
How does this handle more complex types like objects? One issue with migrating from
.defaultPropsto function default parameters is that creating an object in the default parameter would invalidate memoization too often.E.g.
function Component({ options = {} }) {}is not equivalent to
function Component({ options }) {} Component.defaultProps = { options: {} }you'd have to to hoist those values out e.g.
const defaultOptions = {} function Component({ options = defaultOptions }) {}
@eps1lon
I fixed the memory issue when using ObjectExpression, ArrayExpression, or ArrowFunctionExpression by creating separate variables for them per your suggestion.
In this commit
Looks good. I'd recommend running this on some actual codebases to verify. I don't think we have any more
defaultPropsbut I can test it on our larger codebases internally as a sanity check. Can you ping me oncenpx codemod react/19/replace-default-propsis available?
Final version published on Codemod registry! ๐
The final version has been published! All updates are now available on the Codemod registry.
You can now run:
npx codemod react/19/replace-default-props
to execute the codemod with the latest improvements.
@eps1lon Let me know if you need anything! ๐
great job @amirabbas-gh and thank you @eps1lon for code review. i also tested the codemod and all lgtm.
this can be merged now... cc @rickhanlonii , @mohebifar
@eps1lon There were some issues in the PR as well as in some of Codemod's packages, which I have fixed. Please pull changes, reinstall the packages, and make sure to run the tests again.