{"id":13533325,"url":"https://github.com/sjy/js2tsx","last_synced_at":"2025-07-29T12:38:01.763Z","repository":{"id":69059360,"uuid":"111188368","full_name":"sjy/js2tsx","owner":"sjy","description":"A toolkit provide some codemod scripts based on jscodeshift to migrating react code base to typesceipt","archived":false,"fork":false,"pushed_at":"2018-04-25T15:14:33.000Z","size":40,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T16:22:27.614Z","etag":null,"topics":["codemod","jscodeshift","react","refactory","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sjy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-11-18T08:16:35.000Z","updated_at":"2023-12-07T14:59:00.000Z","dependencies_parsed_at":"2023-07-06T21:00:41.668Z","dependency_job_id":null,"html_url":"https://github.com/sjy/js2tsx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjy%2Fjs2tsx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjy%2Fjs2tsx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjy%2Fjs2tsx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjy%2Fjs2tsx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sjy","download_url":"https://codeload.github.com/sjy/js2tsx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250264983,"owners_count":21402016,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codemod","jscodeshift","react","refactory","typescript"],"created_at":"2024-08-01T07:01:18.741Z","updated_at":"2025-04-22T15:22:04.881Z","avatar_url":"https://github.com/sjy.png","language":"JavaScript","funding_links":[],"categories":["Frameworks"],"sub_categories":["React.js"],"readme":"# React to TSX Code\n\n\u003cp align=\"center\"\u003e\n\n[![npm](https://img.shields.io/npm/v/js2tsx.svg)](https://www.npmjs.com/package/js2tsx)\n[![npm](https://img.shields.io/npm/l/express.svg)](https://www.npmjs.com/package/js2tsx)\n\n\u003c/p\u003e\n\n\u003e A toolkit provide some codemod scripts based on [jscodeshift](https://github.com/facebook/jscodeshift) to migrating react code base to typesceipt.\n\n## Getting Started\n\n\u003e **NOTE** Please make sure a stable and modern verison of node and npm packages installed!\n\n```bash\ngit clone $this-repo\nyarn install\n# or npm install\n```\n\n## Usage\n\nAll npm scripts can be run in `jscodeshift's dry` mode, which will never really change the files' content.\n\n```bash\njscodeshift -t ./transforms/$some-provided-codemod.js $react-code-base-path --extension js --parser babylon\n```\n\nIf you want to rename all js/jsx files to tsx/ts files, a simple rename file is provied to do this job.\n\n```bash\nnpm run rename $code-base-path js tsx\n```\n\n### add-import.js\n\nAdd a import statement to each file match the path pattern specified;\n\n### react-to-tsx.js\n\nAdd type annotaions to react composite components based on exsiting propTypes defination, turn from\n\n```jsx\n...\nclass Counter extends Component {\n    static propTypes = {\n        active: PropTypes.bool,\n        name: PropTypes.string,\n        count: PropTypes.number,\n        unit: PropTypes.node,\n        lowerLimit: PropTypes.number,\n        upperLimit: PropTypes.number,\n        onCountChange: PropTypes.func,\n    };\n    ...\n}\n...\n```\n\nto:\n\n```jsx\n...\ntype CounterState = {};\ninterface CounterProps extends BaseProps {\n  active?: boolean,\n  name?: string,\n  count?: number,\n  unit?: any,\n  lowerLimit?: number,\n  upperLimit?: number,\n  onCountChange?: any,\n}\nclass Counter extends Component\u003cCounterProps, CounterState\u003e {\n    static propTypes = {\n        active: PropTypes.bool,\n        name: PropTypes.string,\n        count: PropTypes.number,\n        unit: PropTypes.node,\n        lowerLimit: PropTypes.number,\n        upperLimit: PropTypes.number,\n        onCountChange: PropTypes.func,\n    };\n    ...\n}\n...\n```\n\n### sfc-to-tsx.js\n\nAdd type annotaions to stateless function components based on exsiting propTypes defination, turn from\n\n```jsx\n...\nconst Counter = ({ unit, count, lowerLimit, onCountChange}) =\u003e {\n    return (\n        \u003cdiv className=\"fake-counter\"\u003e\n            \u003cspan className=\"operation\" onClick={this.onCountChange.bind(this, 'minus')}\u003e\n                -\n            \u003c/span\u003e\n            \u003cspan className=\"count\"\u003e\n                {count}\n                {unit}\n            \u003c/span\u003e\n            \u003cspan className=\"operation\" onClick={this.onCountChange.bind(this, 'add')}\u003e\n                +\n            \u003c/span\u003e\n        \u003c/div\u003e\n    );\n};\n\nCounter.propTypes = {\n    active: PropTypes.bool,\n    name: PropTypes.string,\n    count: PropTypes.number,\n    unit: PropTypes.node,\n    lowerLimit: PropTypes.number,\n    upperLimit: PropTypes.number,\n    onCountChange: PropTypes.func,\n};\n...\n```\n\nto:\n\n```jsx\n...\nconst Counter: React.SFC\u003cCounterProps\u003e = ({ unit, count, lowerLimit, onCountChange}) =\u003e {\n    return (\n        \u003cdiv className=\"fake-counter\"\u003e\n            \u003cspan className=\"operation\" onClick={this.onCountChange.bind(this, 'minus')}\u003e\n                -\n            \u003c/span\u003e\n            \u003cspan className=\"count\"\u003e\n                {count}\n                {unit}\n            \u003c/span\u003e\n            \u003cspan className=\"operation\" onClick={this.onCountChange.bind(this, 'add')}\u003e\n                +\n            \u003c/span\u003e\n        \u003c/div\u003e\n    );\n};\n\ninterface CounterProps extends BaseProps {\n  active?: boolean,\n  name?: string,\n  count?: number,\n  unit?: any,\n  lowerLimit?: number,\n  upperLimit?: number,\n  onCountChange?: any,\n}\n...\n```\n\n## Debug\n\n\u003e VS Code debug tool + nodejs debugger is heavilly recommended;\n\u003e sample config: .vscode/launch.json\n\n```json\n{\n  \"version\": \"0.2.0\",\n  \"configurations\": [\n    {\n      \"type\": \"node\",\n      \"request\": \"launch\",\n      \"name\": \"Launch via NPM\",\n      \"runtimeExecutable\": \"npm\",\n      \"runtimeArgs\": [\"run\", \"dev\"],\n      \"port\": 9229\n    }\n  ]\n}\n```\n\n[More Info @jscodeshift](https://github.com/facebook/jscodeshift/issues/223)\n\n## Alt Codemods\n\n* [react-codemod](https://github.com/reactjs/react-codemod) - React codemod scripts to update React APIs.\n* [js-codemod](https://github.com/cpojer/js-codemod/) - Codemod scripts to transform code to next generation JS.\n* [js-transforms](https://github.com/jhgg/js-transforms) - Some documented codemod experiments to help you learn.\n\n## Support\n\njscodeshift: [https://github.com/facebook/jscodeshift](https://github.com/facebook/jscodeshift)\n\nrecast: [https://github.com/benjamn/recast](https://github.com/benjamn/recast)\n\nast-types: [https://github.com/benjamn/ast-types](https://github.com/benjamn/ast-types)\n\nast-explorer: [http://astexplorer.net/](http://astexplorer.net/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjy%2Fjs2tsx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsjy%2Fjs2tsx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjy%2Fjs2tsx/lists"}