-
Notifications
You must be signed in to change notification settings - Fork 5
feat: added codemod for renaming unsafe lifecycles in react #25
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
shubhraagarwal
wants to merge
9
commits into
codemod-com:main
Choose a base branch
from
shubhraagarwal:rename-unsafe-lifecycles
base: main
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
9 commits
Select commit
Hold shift + click to select a range
aac3e6c
Added codemod for renaming unsafe lifecycles
shubhraagarwal 92a2915
fix: readme lines and order
mohab-sameh 5ef5784
fix: replace invalid metafields
mohab-sameh 9fe1b65
fix: remove overriding license
mohab-sameh befaae1
Merge branch 'codemod-com:main' into rename-unsafe-lifecycles
shubhraagarwal ce1ead5
added testfixtures for rename-unsafe-lifecycles
shubhraagarwal de015ef
Added .ts support
shubhraagarwal 569532d
Update fixture1.output.ts
shubhraagarwal d965d65
Update fixture1.input.ts
shubhraagarwal 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json", | ||
"name": "react/rename-unsafe-lifecycles", | ||
"version": "1.0.0", | ||
"description": "Renames unsafe React lifecycle methods to their new names with UNSAFE_ prefix", | ||
"engine": "jscodeshift", | ||
"private": false, | ||
"arguments": [], | ||
"include": [ | ||
"**/*.js", | ||
"**/*.jsx", | ||
"**/*.tsx", | ||
"**/*.ts" | ||
], | ||
"exclude": [ | ||
"node_modules/**", | ||
"dist/**" | ||
], | ||
"meta": { | ||
"tags": [ | ||
"react", | ||
"lifcycle-methods" | ||
], | ||
"git": "https://docs.codemod.com/building-codemods/package-requirements#codemodrc-json-reference" | ||
} | ||
} |
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,2 @@ | ||
node_modules | ||
dist |
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,26 @@ | ||
This codemod is used to rename the unsafe lifecycle methods to their new names. The following table shows the mapping of the old lifecycle methods to the new ones. | ||
|
||
| Old Name | New Name | | ||
|----------|----------| | ||
| componentWillMount | UNSAFE_componentWillMount | | ||
| componentWillReceiveProps | UNSAFE_componentWillReceiveProps | | ||
| componentWillUpdate | UNSAFE_componentWillUpdate | | ||
|
||
## Example | ||
|
||
### Before | ||
|
||
```ts | ||
componentWillMount() { | ||
console.log("hello"); | ||
} | ||
|
||
``` | ||
|
||
### After | ||
|
||
```ts | ||
UNSAFE_componentWillMount() { | ||
console.log("hello"); | ||
} | ||
``` |
19 changes: 19 additions & 0 deletions
19
codemods/react/rename-unsafe-lifecycles/__testfixtures__/fixture1.input.ts
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,19 @@ | ||
class MyComponent extends React.Component { | ||
componentWillMount() { | ||
console.log('component will mount'); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { | ||
console.log('component will receive props', nextProps); | ||
} | ||
|
||
componentWillUpdate(nextProps, nextState) { | ||
console.log('component will update', nextProps, nextState); | ||
} | ||
|
||
render() { | ||
return <div>My Component</div>; | ||
} | ||
} | ||
|
||
export { MyComponent }; |
19 changes: 19 additions & 0 deletions
19
codemods/react/rename-unsafe-lifecycles/__testfixtures__/fixture1.output.ts
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,19 @@ | ||
class MyComponent extends React.Component { | ||
UNSAFE_componentWillMount() { | ||
console.log('component will mount'); | ||
} | ||
|
||
UNSAFE_componentWillReceiveProps(nextProps) { | ||
console.log('component will receive props', nextProps); | ||
} | ||
|
||
UNSAFE_componentWillUpdate(nextProps, nextState) { | ||
console.log('component will update', nextProps, nextState); | ||
} | ||
|
||
render() { | ||
return <div>My Component</div>; | ||
} | ||
} | ||
|
||
export { MyComponent }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,23 @@ | ||
{ | ||
"name": "rename-unsafe-lifecycles", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "20.9.0", | ||
"typescript": "^5.2.2", | ||
"vitest": "^1.0.1", | ||
"@codemod.com/codemod-utils": "*", | ||
"jscodeshift": "^0.15.1", | ||
"@types/jscodeshift": "^0.11.10" | ||
}, | ||
"scripts": { | ||
"test": "vitest run", | ||
"test:watch": "vitest watch" | ||
}, | ||
"files": [ | ||
"README.md", | ||
".codemodrc.json", | ||
"/dist/index.cjs" | ||
], | ||
"type": "module", | ||
"author": "shubhraagarwal" | ||
} |
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,62 @@ | ||
/** | ||
* Copyright 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const DEPRECATED_APIS = Object.create(null); | ||
DEPRECATED_APIS.componentWillMount = 'UNSAFE_componentWillMount'; | ||
DEPRECATED_APIS.componentWillReceiveProps = 'UNSAFE_componentWillReceiveProps'; | ||
DEPRECATED_APIS.componentWillUpdate = 'UNSAFE_componentWillUpdate'; | ||
|
||
export default (file, api, options) => { | ||
const j = api.jscodeshift; | ||
|
||
const printOptions = options.printOptions || { | ||
quote: 'single', | ||
trailingComma: true | ||
}; | ||
|
||
const root = j(file.source); | ||
|
||
let hasModifications = false; | ||
|
||
const renameDeprecatedApis = path => { | ||
const name = path.node.key.name; | ||
|
||
if (DEPRECATED_APIS[name]) { | ||
path.value.key.name = DEPRECATED_APIS[name]; | ||
hasModifications = true; | ||
} | ||
}; | ||
|
||
const renameDeprecatedCallExpressions = path => { | ||
const name = path.node.property.name; | ||
|
||
if (DEPRECATED_APIS[name]) { | ||
path.node.property.name = DEPRECATED_APIS[name]; | ||
hasModifications = true; | ||
} | ||
}; | ||
|
||
// Class methods | ||
root.find(j.MethodDefinition).forEach(renameDeprecatedApis); | ||
|
||
// Class methods - typescript | ||
root.find(j.ClassMethod).forEach(renameDeprecatedApis); | ||
|
||
// Arrow functions | ||
root.find(j.ClassProperty).forEach(renameDeprecatedApis); | ||
|
||
// createReactClass and mixins | ||
root.find(j.Property).forEach(renameDeprecatedApis); | ||
|
||
// Function calls | ||
root.find(j.MemberExpression).forEach(renameDeprecatedCallExpressions); | ||
|
||
return hasModifications ? root.toSource(printOptions) : null; | ||
}; |
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,74 @@ | ||
/** | ||
* Copyright 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const tests = [ | ||
'arrow-functions', | ||
'create-react-class', | ||
'instance-methods', | ||
'manually-calling-lifecycles', | ||
'manually-invoked-mixin-methods', | ||
'one-lifecycle-calls-another', | ||
'standalone-function', | ||
'variable-within-class-method', | ||
]; | ||
|
||
const defineTest = require('jscodeshift/dist/testUtils').defineTest; | ||
|
||
describe('rename-unsafe-lifecycles', () => { | ||
describe('flow', () => { | ||
beforeEach(() => { | ||
jest.mock('../rename-unsafe-lifecycles', () => { | ||
return Object.assign( | ||
require.requireActual('../rename-unsafe-lifecycles'), | ||
{ | ||
parser: 'flow' | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
tests.forEach(test => | ||
defineTest( | ||
__dirname, | ||
'rename-unsafe-lifecycles', | ||
null, | ||
`rename-unsafe-lifecycles/${test}` | ||
) | ||
); | ||
}); | ||
|
||
describe('typescript', () => { | ||
beforeEach(() => { | ||
jest.mock('../rename-unsafe-lifecycles', () => { | ||
return Object.assign( | ||
require.requireActual('../rename-unsafe-lifecycles'), | ||
{ | ||
parser: 'tsx' | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
defineTest( | ||
__dirname, | ||
'rename-unsafe-lifecycles', | ||
null, | ||
'rename-unsafe-lifecycles/typescript/class.tsx' | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add |
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 @@ | ||
import { configDefaults, defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
include: [...configDefaults.include, '**/test/*.ts'], | ||
}, | ||
}); |
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.