🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

try-catch.macro

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

try-catch.macro

A Babel macro that removes console prints in production

latest
Source
npmnpm
Version
0.0.1
Version published
Maintainers
1
Created
Source

try-catch.macro

在项目开发中经常会遇到一些错误没有抛出,导致无法定位问题。希望通过一种简单的方法来对函数进行try catch包装。

通过babel-plugin-marcos来进行按需的转换,而不是通过babel的插件进行全局的替换。

装饰器语法对于单纯的函数没法处理,所以这里就选用了flow的语法。使用flow的语法来对需要转换的方法进行标记,在语法方法可能不太符合flow的标准。

Installation

npm install --save-dev babel-plugin-macros try-catch.macro @babel/preset-flow 
yarn add babel-plugin-macros try-catch.macro @babel/preset-flow --dev

因为使用了flow的语法进行函数标记,所以需要安装@babel/preset-flow来对flow的语法进行处理

Usage


// babel.config.js add babel-plugin-macros and @babel/preset-flow

plugins = ['babel-plugin-macros'];
presets = ['@babel/preset-flow'];

import { TryCatch } from 'try-catch.macro';

var a = (b): TryCatch => {
  return b;
}

class Test {

  print = (): TryCatch => {
    const data = this.getData();

    return data;
  }
}

      ↓ ↓ ↓ ↓ ↓ ↓

var a = (b) => {
  try {
    return b;
  } catch(e) {
    console.error(e);
  }
}

class Test {

  print = () => {
    try {
      const data = this.getData();

      return data;
    } catch(e) {
      console.log(e);
    }
  }
}

import { TryCatch } from 'try-catch.macro';

var a = (b): TryCatch => {
  try {
    return b;
  } catch(e) {
    console.log(e);
  }
}

      ↓ ↓ ↓ ↓ ↓ ↓

var a = (b) => {
  try {
    return b;
  } catch(e) {
    console.log(e);
  }
}

对于非函数或者函数已经被try catch包裹了一层,不会进行处理

Keywords

try-catch

FAQs

Package last updated on 30 Dec 2019

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