- 
                Notifications
    You must be signed in to change notification settings 
- Fork 300
feat: add React 19 replace default props transform #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            amirabbas-gh
  wants to merge
  17
  commits into
  reactjs:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
amirabbas-gh:replace-default-props-transform
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            17 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      cd05eb8
              
                feat: add codemod tsconfig file
              
              
                amirabbas-gh 82ad3a4
              
                feat: add codemod-utils and typescript jscodeshift package
              
              
                amirabbas-gh 7b1901c
              
                fix: fix new codemod tsconfig jest config
              
              
                amirabbas-gh 1d8befd
              
                feat: add react-19-replace-default-props transform
              
              
                amirabbas-gh 0bcf600
              
                fix: fix EOL at the end of button-jsx-example-input test fixture
              
              
                amirabbas-gh 98ca275
              
                fix: fix import
              
              
                amirabbas-gh 0d7dc9f
              
                fix: fix tsconfig.codemod config file format
              
              
                amirabbas-gh eff3a37
              
                fix: prevent unnecessary re-renders by hoisting default object values
              
              
                amirabbas-gh 5dabf5b
              
                fix: fix formatting of react-19-replace-default-props transform
              
              
                amirabbas-gh 121afe2
              
                fix: fix replace-default-props transform in not destructured cases
              
              
                amirabbas-gh 13e3d84
              
                fix: fix replace-default-props transform in rest element cases
              
              
                amirabbas-gh 1ff6996
              
                update lock files
              
              
                amirabbas-gh 40d28ac
              
                feat: add node next into tsconfig
              
              
                amirabbas-gh 58d7e23
              
                feat: add module name mapper for codemod utils package in jest config
              
              
                amirabbas-gh 88b5b16
              
                fix: fix moduleResolution in tsconfig
              
              
                amirabbas-gh 611d84e
              
                update codemod-utils
              
              
                amirabbas-gh c752f35
              
                fix: fix lockfile
              
              
                amirabbas-gh File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .DS_STORE | ||
| node_modules | ||
| npm-debug.log | ||
| package-lock.json | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            8 changes: 8 additions & 0 deletions
          
          8 
        
  transforms/__testfixtures__/react-19-replace-default-props/button-jsx-example-input.jsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const Button = ({ size, color }) => { | ||
| return <button style={{ color, fontSize: size }}>Click me</button>; | ||
| }; | ||
|  | ||
| Button.defaultProps = { | ||
| size: "16px", | ||
| color: "blue", | ||
| }; | 
        
          
          
            3 changes: 3 additions & 0 deletions
          
          3 
        
  transforms/__testfixtures__/react-19-replace-default-props/button-jsx-example-output.jsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const Button = ({ size = "16px", color = "blue" }) => { | ||
| return <button style={{ color, fontSize: size }}>Click me</button>; | ||
| }; | 
        
          
          
            8 changes: 8 additions & 0 deletions
          
          8 
        
  transforms/__testfixtures__/react-19-replace-default-props/multiple-default-props.input.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const Button = ({ size, color }) => { | ||
| return <button style={{ color, fontSize: size }}>Click me</button>; | ||
| }; | ||
|  | ||
| Button.defaultProps = { | ||
| size: "16px", | ||
| color: "blue", | ||
| }; | 
        
          
          
            3 changes: 3 additions & 0 deletions
          
          3 
        
  transforms/__testfixtures__/react-19-replace-default-props/multiple-default-props.output.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const Button = ({ size = "16px", color = "blue" }) => { | ||
| return <button style={{ color, fontSize: size }}>Click me</button>; | ||
| }; | 
        
          
          
            15 changes: 15 additions & 0 deletions
          
          15 
        
  transforms/__testfixtures__/react-19-replace-default-props/nested-destructuring.input.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const Card = ({ user: { name, age } }) => { | ||
| return ( | ||
| <div> | ||
| <p>{name}</p> | ||
| <p>{age}</p> | ||
| </div> | ||
| ); | ||
| }; | ||
|  | ||
| Card.defaultProps = { | ||
| user: { | ||
| name: "Unknown", | ||
| age: 0, | ||
| }, | ||
| }; | 
        
          
          
            13 changes: 13 additions & 0 deletions
          
          13 
        
  transforms/__testfixtures__/react-19-replace-default-props/nested-destructuring.output.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const cardDefaultPropUser = { | ||
| name: "Unknown", | ||
| age: 0, | ||
| }; | ||
|  | ||
| const Card = ({ user: { name, age } = cardDefaultPropUser }) => { | ||
| return ( | ||
| <div> | ||
| <p>{name}</p> | ||
| <p>{age}</p> | ||
| </div> | ||
| ); | ||
| }; | 
        
          
          
            7 changes: 7 additions & 0 deletions
          
          7 
        
  transforms/__testfixtures__/react-19-replace-default-props/partial-default-props.input.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const Span = ({ fontSize, children }) => { | ||
| return <span style={{ fontSize }}>{children}</span>; | ||
| }; | ||
|  | ||
| Span.defaultProps = { | ||
| fontSize: "20px", | ||
| }; | 
        
          
          
            3 changes: 3 additions & 0 deletions
          
          3 
        
  transforms/__testfixtures__/react-19-replace-default-props/partial-default-props.output.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const Span = ({ fontSize = "20px", children }) => { | ||
| return <span style={{ fontSize }}>{children}</span>; | ||
| }; | 
        
          
          
            10 changes: 10 additions & 0 deletions
          
          10 
        
  transforms/__testfixtures__/react-19-replace-default-props/props-not-destructured.input.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| const C = (props) => { | ||
| console.log(props.helloWorld); | ||
| return <>{props.text}</>; | ||
| }; | ||
|  | ||
| C.defaultProps = { | ||
| text: "Hello", | ||
| test: 2, | ||
| helloWorld: true, | ||
| }; | 
        
          
          
            11 changes: 11 additions & 0 deletions
          
          11 
        
  transforms/__testfixtures__/react-19-replace-default-props/props-not-destructured.output.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const C = (props) => { | ||
| props = { | ||
| ...props, | ||
| text: typeof props.text === "undefined" ? "Hello" : props.text, | ||
| test: typeof props.test === "undefined" ? 2 : props.test, | ||
| helloWorld: typeof props.helloWorld === "undefined" ? true : props.helloWorld | ||
| }; | ||
|  | ||
| console.log(props.helloWorld); | ||
| return <>{props.text}</>; | ||
| }; | 
        
          
          
            5 changes: 5 additions & 0 deletions
          
          5 
        
  transforms/__testfixtures__/react-19-replace-default-props/rest-element-example.input.jsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const MyComp = ({foo, ...props}) => { | ||
| console.log(props.bar) | ||
| } | ||
|  | ||
| MyComp.defaultProps = { foo: "hello", bar: "bye", test: 2 }; | 
        
          
          
            9 changes: 9 additions & 0 deletions
          
          9 
        
  transforms/__testfixtures__/react-19-replace-default-props/rest-element-example.output.jsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const MyComp = ({foo = "hello", ...props}) => { | ||
| props = { | ||
| ...props, | ||
| bar: typeof props.bar === "undefined" ? "bye" : props.bar, | ||
| test: typeof props.test === "undefined" ? 2 : props.test | ||
| }; | ||
|  | ||
| console.log(props.bar) | ||
| } | 
        
          
          
            7 changes: 7 additions & 0 deletions
          
          7 
        
  transforms/__testfixtures__/react-19-replace-default-props/single-default-prop.input.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const C = ({ text }) => { | ||
| return <>{text}</>; | ||
| }; | ||
|  | ||
| C.defaultProps = { | ||
| text: "test", | ||
| }; | 
        
          
          
            3 changes: 3 additions & 0 deletions
          
          3 
        
  transforms/__testfixtures__/react-19-replace-default-props/single-default-prop.output.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const C = ({ text = "test" }) => { | ||
| return <>{text}</>; | ||
| }; | 
        
          
          
            8 changes: 8 additions & 0 deletions
          
          8 
        
  transforms/__testfixtures__/react-19-replace-default-props/with-functions.input.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const List = ({ items, renderItem }) => { | ||
| return <ul>{items.map(renderItem)}</ul>; | ||
| }; | ||
|  | ||
| List.defaultProps = { | ||
| items: [], | ||
| renderItem: (item) => <li key={item}>{item}</li>, | ||
| }; | 
        
          
          
            5 changes: 5 additions & 0 deletions
          
          5 
        
  transforms/__testfixtures__/react-19-replace-default-props/with-functions.output.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const listDefaultPropItems = []; | ||
| const listDefaultPropRenderItem = (item) => <li key={item}>{item}</li>; | ||
| const List = ({ items = listDefaultPropItems, renderItem = listDefaultPropRenderItem }) => { | ||
| return <ul>{items.map(renderItem)}</ul>; | ||
| }; | 
        
          
          
            12 changes: 12 additions & 0 deletions
          
          12 
        
  transforms/__testfixtures__/react-19-replace-default-props/with-rest-props.input.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const Link = ({ href, children, ...props }) => { | ||
| return ( | ||
| <a href={href} {...props}> | ||
| {children} | ||
| </a> | ||
| ); | ||
| }; | ||
|  | ||
| Link.defaultProps = { | ||
| href: "#", | ||
| children: "Click here", | ||
| }; | ||
        
          
          
            7 changes: 7 additions & 0 deletions
          
          7 
        
  transforms/__testfixtures__/react-19-replace-default-props/with-rest-props.output.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const Link = ({ href = "#", children = "Click here", ...props }) => { | ||
| return ( | ||
| <a href={href} {...props}> | ||
| {children} | ||
| </a> | ||
| ); | ||
| }; | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if
hreforchildrenare not named here e.g.{ href, ...props}and<a href={href} {...props} />?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the example you provided, it only considers href and produces the following output:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eps1lon
We reviewed this case with @mohebifar 's help, and I understood your point. What do you think about doing something similar to the solution I suggested for the previous case?
Before:
After:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eps1lon
I fix it in this commit:
13e3d842e861a8f68c99a07b034dbd83a40cc96c