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

some-codemod

v0.2.3

Published

This repository contains codemod script(s) for use with [JSCodeshift](https://github.com/facebook/jscodeshift) and a command line tool to trigger refactor by selecting specific codemod to run.

Downloads

2

Readme

some-codemod

This repository contains codemod script(s) for use with JSCodeshift and a command line tool to trigger refactor by selecting specific codemod to run.

Installation & Usage

To install the tool, run:

npm install -g some-codemod

After installation, you can run the following command to select codemod to refactor your code:

refactor

Above example shows global installation. In production, it's recommended to use local installation instead. In this way, it's recommended to use npm-scripts to run refactor, as it will help find the correct location of command line tool.

Several CLI flag you can use:

  • --folder / -f: folder of source code where transform should be applied, default value is ./src;
  • --transforms / -t: transforms to use, use comma to separate; if no transform is provided, CLI will ask to choose manually. Example: -t iconfont,no-autobind;
  • --yes / -y: use default config directly; if this flag is not provided, CLI will tries to find .refactorrc file locally for configured config; if still not found, CLI will ask to confirm config manually;
  • --all: apply all transforms in once;
  • --verbose / -v: same as jscodeshift verbose flag, show more information about the transform process, possible values are 0, 1 and 2, default value is 0.

Built-in Codemods

iconfont

This codemod will transform <i className="iconfont icon-xxx" /> to <Icon type="xxx" /> and import Icon component to file if not imported already.

This codemod will be useful when migrating iconfont to SVG. Following illustrates a series of possible steps:

  1. Create a React component that will render iconfont, example:
const Icon: React.FC<{ type: string }> = props => {
  const { className, type, ...rest } = props;
  return (
    <i className={classnames('iconfont', `icon-${props.type}`, className)} {...rest} />
  );
};
  1. Run this codemod to transform all usages of iconfont, use component above instead.
  2. Refactor component above to use SVG instead.

Options required:

  • iconName: The name of component that will be used, such as Icon
  • importPath: The path of component, which will be used to insert import statement (only if component not imported already)

autobind

This codemod will transform class definition to use autobind decorator instead of using class property for this bind. Example:

Input the following class definition:

class Component extends React.Component {
  onClick = () => { };
  render() {
    return null;
  }
}

Transform result will be:

import { autobind } from 'core-decorators';

@autobind
class Component extends React.Component {
  onClick() { }
  render() {
    return null;
  }
}

Possible configs for this codemod are:

  • decoratorName: Name of decorator, default is autobind
  • decoratorPath: Path of decorator library, default is core-decorators
  • isDefault: Whether should use default import for decorator, default is false

Known Issue:

  • When transforming class function property with type definition, it's type definition will be lost. Example
class Component extends React.Component {
  SomeItem: React.FunctionComponent<{}> = (props) => <div {...props} />;
  render() {
    return null;
  }
}

If transformed, will result in:

@autobind
class Component extends React.Component {
  SomeItem(props) {
    return <div {...props} />;
  }
  render() {
    return null;
  }
}

To keep the type definition, this scenario will be ignored and not transformed.

no-autobind

Reverse version of autobind codemod. This codemod will transform class definition to remove usage of autobind decorator. Instead, it will change class method to arrow function for automatic this bind. Example:

Input the following class definition (with decorator on class method):

import { autobind } from 'core-decorators';
class Component extends React.Component {
  @autobind
  onClick() { }
  render() {
    return null;
  }
}

Transform result will be:

class Component extends React.Component {
  onClick = () => { };
  render() {
    return null;
  }
}

If decorator is on class, all methods inside will be switched to arrow function:

import { autobind } from 'core-decorators';

@autobind
class Component extends React.Component {
  onClick() { }
  render() {
    return null;
  }
}

Transform result will be:

class Component extends React.Component {
  onClick = () => { };
  render = () => {
    return null;
  }
}

Notice that render function is also transformed, though it might not be necessary for React Component.

Possible configs for this codemod are:

  • decoratorName: Name of decorator, default is autobind
  • decoratorPath: Path of decorator library, default is core-decorators
  • isDefault: Whether should use default import for decorator, default is false

Notice

autobind and arrow function are not by definition 100% equal. There are tiny difference. Following is an example:

class A {
  ref = {
    onClick: this.onClick,
  };
  @autobind
  onClick() {
    console.log('clicked');
  }
}

Above code is correct, but changing autobind directly to arrow function will cause error, as this.onClick is actually used before onClick = () => { } is defined.

To make things work, transform will be made with order modification as well:

class A {
  onClick = () => {
    console.log('clicked');
  };
  ref = {
    onClick: this.onClick,
  };
}