babel-plugin-implicit-return
Implicitly return function result
Examples
// Before { 314;} // After { return 314;}
// Before { if n > 0 1; else if n < 0 -1; else 0; } // After { if n > 0 return 1; else if n < 0 return -1; else return 0; }
// Before { let result = 0; for let i = 0; i < arraylength; i++ result += arrayi } // After { var _ret; let result = 0; for let i = 0; i < arraylength; i++ _ret = result += arrayi; return _ret;}
// Before { { super; } { 1; } static { {}; } } // After { return { return super; } { return 1; } static { return {}; } }
Requirements
Your compiling enviroment must have support for:
Installation
$ npm install --save-dev babel-plugin-implicit-return
Usage
.babelrc
(Recommended)
Via .babelrc
Via CLI
$ babel --plugins implicit-return script.js
Via Node API
;
Quirks
Due to JavaScript syntax limitations this plugin can't make a function to implicitly return:
Object literal
This won't work:
{ key1: value1 key2: value2 ; // Syntax Error}
There're two workarounds:
// first - assign an object to a variable/constant { const obj = key1: value1 key2: value2 ;} // second - wrap an object literal with braces { key1: value1 key2: value2 ;}
Function expression
This won't work:
{ {}; // Syntax Error}
There're four workarounds:
// first - use arrow function { {};} // second - give your function a name { {};} // third - assign a function to a variable/constant { const fn = {};} // fourth - wrap a function with braces { {};}
License
MIT