npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js2tsx

v1.0.0

Published

migrate js based react-project to typescript

Downloads

4

Readme

React to TSX Code

A toolkit provide some codemod scripts based on jscodeshift to migrating react code base to typesceipt.

Getting Started

NOTE Please make sure a stable and modern verison of node and npm packages installed!

git clone $this-repo
yarn install
# or npm install

Usage

All npm scripts can be run in jscodeshift's dry mode, which will never really change the files' content.

jscodeshift -t ./transforms/$some-provided-codemod.js $react-code-base-path --extension js --parser babylon

If you want to rename all js/jsx files to tsx/ts files, a simple rename file is provied to do this job.

npm run rename $code-base-path js tsx

add-import.js

Add a import statement to each file match the path pattern specified;

react-to-tsx.js

Add type annotaions to react composite components based on exsiting propTypes defination, turn from

...
class Counter extends Component {
    static propTypes = {
        active: PropTypes.bool,
        name: PropTypes.string,
        count: PropTypes.number,
        unit: PropTypes.node,
        lowerLimit: PropTypes.number,
        upperLimit: PropTypes.number,
        onCountChange: PropTypes.func,
    };
    ...
}
...

to:

...
type CounterState = {};
interface CounterProps extends BaseProps {
  active?: boolean,
  name?: string,
  count?: number,
  unit?: any,
  lowerLimit?: number,
  upperLimit?: number,
  onCountChange?: any,
}
class Counter extends Component<CounterProps, CounterState> {
    static propTypes = {
        active: PropTypes.bool,
        name: PropTypes.string,
        count: PropTypes.number,
        unit: PropTypes.node,
        lowerLimit: PropTypes.number,
        upperLimit: PropTypes.number,
        onCountChange: PropTypes.func,
    };
    ...
}
...

sfc-to-tsx.js

Add type annotaions to stateless function components based on exsiting propTypes defination, turn from

...
const Counter = ({ unit, count, lowerLimit, onCountChange}) => {
    return (
        <div className="fake-counter">
            <span className="operation" onClick={this.onCountChange.bind(this, 'minus')}>
                -
            </span>
            <span className="count">
                {count}
                {unit}
            </span>
            <span className="operation" onClick={this.onCountChange.bind(this, 'add')}>
                +
            </span>
        </div>
    );
};

Counter.propTypes = {
    active: PropTypes.bool,
    name: PropTypes.string,
    count: PropTypes.number,
    unit: PropTypes.node,
    lowerLimit: PropTypes.number,
    upperLimit: PropTypes.number,
    onCountChange: PropTypes.func,
};
...

to:

...
const Counter: React.SFC<CounterProps> = ({ unit, count, lowerLimit, onCountChange}) => {
    return (
        <div className="fake-counter">
            <span className="operation" onClick={this.onCountChange.bind(this, 'minus')}>
                -
            </span>
            <span className="count">
                {count}
                {unit}
            </span>
            <span className="operation" onClick={this.onCountChange.bind(this, 'add')}>
                +
            </span>
        </div>
    );
};

interface CounterProps extends BaseProps {
  active?: boolean,
  name?: string,
  count?: number,
  unit?: any,
  lowerLimit?: number,
  upperLimit?: number,
  onCountChange?: any,
}
...

Debug

VS Code debug tool + nodejs debugger is heavilly recommended; sample config: .vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch via NPM",
            "runtimeExecutable": "npm",
            "runtimeArgs": ["run", "dev"],
            "port": 9229
        }
    ]
}

More Info @jscodeshift

Alt Codemods

  • react-codemod - React codemod scripts to update React APIs.
  • js-codemod - Codemod scripts to transform code to next generation JS.
  • js-transforms - Some documented codemod experiments to help you learn.

Support

jscodeshift: https://github.com/facebook/jscodeshift

recast: https://github.com/benjamn/recast

ast-types: https://github.com/benjamn/ast-types

ast-explorer: http://astexplorer.net/