react-codemod icon indicating copy to clipboard operation
react-codemod copied to clipboard

feat: add React 19 replace default props transform

Open amirabbas-gh opened this issue 11 months ago โ€ข 5 comments

๐Ÿ“š 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.

  1. Added Codemod Utils Package

    • Added @codemod.com/codemod-utils package as a dependency to facilitate codemod development.
  2. Added Custom TypeScript Configuration for Codemods

"Introduced tsconfig.codemod.json to provide a dedicated TypeScript configuration for transforms from the Codemod repository.
A dedicated tsconfig.codemod.json has been added to support the new transform from the Codemod repository and ensure it has the required TypeScript configuration.

  1. New Transform: React 19 Replace Default Props
    • Added a new transform react-19-replace-default-props to 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

๐Ÿงช Test Plan

The following steps were taken to ensure the changes function as intended:

  1. Ran the test suite to validate existing functionality and new transforms.
  2. Verified type resolution and TypeScript transform compatibility.
  3. To confirm accuracy, the react-19-replace-default-props transform was executed against test cases.

amirabbas-gh avatar Jan 31 '25 20:01 amirabbas-gh

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

alexbit-codemod avatar Jan 31 '25 20:01 alexbit-codemod

How does this handle more complex types like objects? One issue with migrating from .defaultProps to 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

amirabbas-gh avatar Feb 15 '25 17:02 amirabbas-gh

Looks good. I'd recommend running this on some actual codebases to verify. I don't think we have any more defaultProps but I can test it on our larger codebases internally as a sanity check. Can you ping me once npx codemod react/19/replace-default-props is 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! ๐Ÿ˜Š

amirabbas-gh avatar Feb 19 '25 21:02 amirabbas-gh

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

alexbit-codemod avatar Feb 19 '25 21:02 alexbit-codemod

@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.

amirabbas-gh avatar Apr 01 '25 18:04 amirabbas-gh