Code Monkey home page Code Monkey logo

babel-plugin-implicit-return's Introduction

babel-plugin-implicit-return

Implicitly return function result

Examples

// Before
function Pi() {
  3.14;
}

// After
function Pi() {
  return 3.14;
}
// Before
function abs(n) {
  if (n > 0) {
    1;
  } else if (n < 0) {
    -1;
  } else {
    0;
  }
}

// After
function abs(n) {
  if (n > 0) {
    return 1;
  } else if (n < 0) {
    return -1;
  } else {
    return 0;
  }
}
// Before
function sum(array) {
  let result = 0;
  for (let i = 0; i < array.length; i++) {
    result += array[i]
  }
}

// After
function sum(array) {
  var _ret;

  let result = 0;
  for (let i = 0; i < array.length; i++) {
    _ret = result += array[i];
  }
  return _ret;
}
// Before
function classFactory() {
  class Thingy extends Thing {
    constructor() {
      super();
    }

    fn() {
      () => 1;
    }

    static fn() {
      class Other {};
    }
  }
}

// After
function classFactory() {
  return class Thingy extends Thing {
    constructor() {
      return super();
    }

    fn() {
      return () => 1;
    }

    static fn() {
      return class Other {};
    }
  }
}

Requirements

Your compiling enviroment must have support for:

Installation

$ npm install --save-dev babel-plugin-implicit-return

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["implicit-return"]
}

Via CLI

$ babel --plugins implicit-return script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["implicit-return"]
});

Quirks

Due to JavaScript syntax limitations this plugin can't make a function to implicitly return:

Object literal

This won't work:

function getObject() {
  { key1: value1, key2: value2 }; // Syntax Error
}

There're two workarounds:

// first - assign an object to a variable/constant
function getObject() {
  const obj = { key1: value1, key2: value2 };
}

// second - wrap an object literal with braces
function getObject() {
  ({ key1: value1, key2: value2 });
}

Function expression

This won't work:

function getFunction() {
  function() {}; // Syntax Error
}

There're four workarounds:

// first - use arrow function
function getFunction() {
  () => {};
}

// second - give your function a name
function getObject() {
  function fn() {};
}

// third - assign a function to a variable/constant
function getObject() {
  const fn = function() {};
}

// fourth - wrap a function with braces
function getObject() {
  (function() {});
}

License

MIT

babel-plugin-implicit-return's People

Contributors

miraks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

babel-plugin-implicit-return's Issues

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.