diff options
-rw-r--r-- | lib/autoInject.js | 19 | ||||
-rw-r--r-- | mocha_test/autoInject.js | 46 |
2 files changed, 61 insertions, 4 deletions
diff --git a/lib/autoInject.js b/lib/autoInject.js index 40a250d..96922ba 100644 --- a/lib/autoInject.js +++ b/lib/autoInject.js @@ -5,10 +5,19 @@ import clone from 'lodash/_copyArray'; import isArray from 'lodash/isArray'; import trim from 'lodash/trim'; -var argsRegex = /^(function[^\(]*)?\(?\s*([^\)=]*)/m; +var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m; +var FN_ARG_SPLIT = /,/; +var FN_ARG = /(=.+)?(\s*)$/; +var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; function parseParams(func) { - return trim(func.toString().match(argsRegex)[2]).split(/\s*\,\s*/); + func = func.toString().replace(STRIP_COMMENTS, ''); + func = func.match(FN_ARGS)[2].replace(' ', ''); + func = func ? func.split(FN_ARG_SPLIT) : []; + func = func.map(function (arg){ + return trim(arg.replace(FN_ARG, '')); + }); + return func; } /** @@ -108,13 +117,15 @@ export default function autoInject(tasks, callback) { taskFn = params.pop(); newTasks[key] = params.concat(params.length > 0 ? newTask : taskFn); - } else if (taskFn.length === 0) { - throw new Error("autoInject task functions require explicit parameters."); } else if (taskFn.length === 1) { // no dependencies, use the function as-is newTasks[key] = taskFn; } else { params = parseParams(taskFn); + if (taskFn.length === 0 && params.length === 0) { + throw new Error("autoInject task functions require explicit parameters."); + } + params.pop(); newTasks[key] = params.concat(newTask); diff --git a/mocha_test/autoInject.js b/mocha_test/autoInject.js index d699453..9c2d1fb 100644 --- a/mocha_test/autoInject.js +++ b/mocha_test/autoInject.js @@ -85,6 +85,20 @@ describe('autoInject', function () { }, done); }); + it('should throw error for function without explicit parameters', function (done) { + try { + async.autoInject({ + a: function (){} + }); + } catch (e) { + // It's ok. It detected a void function + return done(); + } + + // If didn't catch error, then it's a failed test + done(true) + }); + var arrowSupport = true; try { new Function('x => x'); @@ -111,4 +125,36 @@ describe('autoInject', function () { "}) " )(); } + + + var defaultSupport = true; + try { + eval('function x(y = 1){ return y }'); + }catch (e){ + defaultSupport = false; + } + + if(arrowSupport && defaultSupport){ + // Needs to be run on ES6 only + + /* eslint {no-eval: 0}*/ + eval("(function() { " + + " it('should work with es6 obj method syntax', function (done) { " + + " async.autoInject({ " + + " task1 (cb){ cb(null, 1) }, " + + " task2 ( task3 , cb ) { cb(null, 2) }, " + + " task3 (cb) { cb(null, 3) }, " + + " task4 ( task2 , cb ) { cb(null) }, " + + " task5 ( task4 = 4 , cb ) { cb(null, task4 + 1) } " + + " }, (err, results) => { " + + " expect(results.task1).to.equal(1); " + + " expect(results.task3).to.equal(3); " + + " expect(results.task4).to.equal(undefined); " + + " expect(results.task5).to.equal(5); " + + " done(); " + + " }); " + + " }); " + + "}) " + )(); + } }); |