-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New codemod to help migrating React components "defaultProps" usage (#…
…3681) * feat(codemod): add codemod for migrating react components default props * refactor(codemod): component props refactoring * refactor(codemod): first finished version of the default props migration codemod * chore: add changeset * docs(codemod): updated readme to include the new codemod * chore: update changeset Co-authored-by: Tobias Deekens <[email protected]> * chore: update changeset Co-authored-by: Tobias Deekens <[email protected]> * refactor(codemod): add better component props ts type resolution * refactor(codemod): improve component default props extraction and transformation * refactor(codemod): improve default props transformation and substitution * refactor(codemod): improve components body adjustments and signature refactor * fix(codemod): fix cli ignore patterns --------- Co-authored-by: Tobias Deekens <[email protected]>
- Loading branch information
1 parent
9504631
commit 1e9e84a
Showing
5 changed files
with
623 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,40 @@ | ||
--- | ||
'@commercetools-frontend/codemod': minor | ||
--- | ||
|
||
Introduces a new codemod which helps migrating away from React's `defaultProps` to `prop` destructuring. | ||
|
||
This is how the change looks like: | ||
|
||
```ts | ||
// BEFORE | ||
type TMyComponentProps = { | ||
message: string; | ||
size: string; | ||
} | ||
|
||
function MyComponent(props: TMyComponentProps) { | ||
... | ||
} | ||
|
||
MyComponent.defaultProps = { | ||
size: 'big' | ||
} | ||
|
||
|
||
// AFTER | ||
type TMyComponentProps = { | ||
message: string; | ||
size?: string; // <--- Note this property is now defined as optional | ||
} | ||
|
||
function MyComponent({ size = 'big', ...props }: TMyComponentProps) { | ||
... | ||
} | ||
``` | ||
|
||
And here is how the new codemod can be run: | ||
|
||
``` | ||
$ npx @commercetools-frontend/codemod@latest react-default-props-migration 'src/**/*.{jsx,tsx}' | ||
``` |
This file contains 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
This file contains 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
Oops, something went wrong.