Giter Club home page Giter Club logo

eject-enum's Introduction

eject-enum

Eject enums from your TypeScript codebases.

What is this?

eject-enum is an automatic code rewriting tool for TypeScript codebases that rewrites each TypeScript enum in your codes to the safer alternative.

Before rewriting:

/**
 * Signals of traffic light.
 */
export enum TrafficLight {
    /** Stop. */
    Red,
    /** Stop unless you can't do so safely. */
    Yellow,
    /** Go. */
    Green,
}

After rewriting:

/**
 * Signals of traffic light.
 */
export const TrafficLight = {
    /** Stop. */
    Red: 0,
    /** Stop unless you can't do so safely. */
    Yellow: 1,
    /** Go. */
    Green: 2,
} as const;

export type TrafficLight = typeof TrafficLight[keyof typeof TrafficLight];

Usage

Installation

# global
npm install -g eject-enum
yarn global add eject-enum

# local
npm install --save-dev eject-enum
yarn add --dev eject-enum

Execution

# if you installed locally, prepend `npx` or `yarn`.

# rewrite all files in projects specified by TS configs.
eject-enum path/to/tsconfig.json path/to/tsconfig2.json

# rewrite all TS files under the `src` and `test` directories,
# except files under the `src/foo` directory.
eject-enum "src/**/*.ts" "test/**/*.ts" --exclude "src/foo/**/*.ts"

You can execute eject-enum from scripts as well.

/* ejectEnum.ts */
import { ejectEnum, EjectEnumTarget } from 'eject-enum';

// rewrite all files in projects specified by TS configs.
ejectEnum(EjectEnumTarget.tsConfig(["path/to/tsconfig.json", "path/to/tsconfig2.json"]));

// rewrite all TS files under the `src` and `test` directories
// except files under the `src/foo` directory.
ejectEnum(
    EjectEnumTarget.paths({ 
        include: ["src/**/*.ts", "test/**/*.ts"], 
        exclude: ["src/foo/**/*.ts"] 
    }),
);
# execute the script with ts-node
npx ts-node ejectEnum.ts
# or using esbuild-register
node -r esbuild-register ejectEnum.ts

Note

It is recommended to run code formatting tools after rewriting by eject-enum, as it doesn't consider any code formatting configurations of your project when rewriting.

Features (to come)

  • Rewrite enums in the top-level as well as nested in functions, namespaces and body of controll flows (if, while, switch).
  • Rewrite enums that have constant enum expressions as member's value.
  • Preserve comments as much as possible.
  • Preserve original expressions of enum members in the original code as comments.
  • Rewrite an enum member used as a type.
  • Deno support.

Limitations

eject-enum have some limitations about code rewriting. They originate from limitations of the TS Compiler API/ts-morph.

  • Can't rewrite enums that have computed enum members.
    • e.g. refering variables, members of other enums (even constant members)
  • Can't preserve original trailing comments of enum members.

eject-enum's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar jiftechnify avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.