🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

babel-plugin-typescript-const-enum

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-typescript-const-enum

A babel plugin to replace typescript const enum expression as TS does.

latest
npmnpm
Version
1.0.8
Version published
Weekly downloads
17
-10.53%
Maintainers
1
Weekly downloads
 
Created
Source

babel-plugin-typescript-const-enum

https://www.npmjs.com/package/babel-plugin-typescript-const-enum

Description

This plugin is to resolve typescript const enum declaration to hard code.

Example

some.d.ts

declare namespace N.S {
  export const enum STATUS {
    success = 0,
    fail = 1,
  }
}

declare const enum OUT_NS_STATUS {
  success = 0,
  fail = 1,
}

index.ts

import NS2 = NS;
import NS = N.S;
import ONS = OUT_NS_STATUS;

export function foo() {
  const a = N.S.STATUS.fail;  // => 1
  const b = NS.STATUS.success;  // => 0
  const c = NS2.STATUS.fail;  // => 1
  const d = ONS.success;  // => 0
  function baz1() {
    const NS = { STATUS: { success: 'success', fail: 'fail' } };
    return NS.STATUS.success; // keep
  }
  function baz2() {
    const ONS = { success: 'success', fail: 'fail' };
    return ONS.success; // keep
  }
  function baz3() {
    const N = { S: { STATUS: { success: 'success', fail: 'fail' } } };
    function bas3Closure() {
      return N.S.STATUS.fail; // keep
    }
    return bas3Closure();
  }
  function haha() {
    return function () {
      return NS.STATUS.success; // => 0
    };
  }
  return;
}

How to Use

Add plugin in babel config

babel.config.json

{
  ...
  "plugins": [
    [
      "babel-plugin-typescript-const-enum",
      {
        "enumFile": "./my-const-enum.json",
        "enums": {
          "a.b.c.xxx": "xxx"
        }
      }
    ]
  ]
  ...
}

Options

  • enumFile string

    a pathname to the enums json data

  • enums

    an object of enums map like follows:

    {
      "my.namespace.student.grade.LEVEL_ENUM.one": "一年级",
      "my.namespace.student.grade.LEVEL_ENUM.two": "二年级",
      "my.namespace.student.grade.LEVEL_ENUM.three": "三年级",
      "real.world.common.sense.DAY.SUNDAY": 7,
      "real.world.common.sense.DAY.MONDAY": 1,
      "real.world.common.sense.DAY.TUSEDAY": 2,
    }
    

    the upon json may be from some idl, and there should be some d.ts file as following to ensure TS Language Server works OK:

    declare namespace my.namespace.student {
      export namespace grade {
        export const enum LEVEL_ENUM {
          one = '一年级',
          two = '二年级',
          three = '三年级'
        }
      }
    }
    
    declare namespace real.world.common.sense {
      export const enum DAY {
        SUNDAY = 7,
        MONDAY = 1,
        TUSEDAY = 2
      }
    }
    

Keywords

typescript

FAQs

Package last updated on 29 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts