diff --git a/lib/autoInject.js b/lib/autoInject.js index 03f534eee..8d0033336 100644 --- a/lib/autoInject.js +++ b/lib/autoInject.js @@ -2,8 +2,8 @@ import auto from './auto'; import wrapAsync from './internal/wrapAsync'; import { isAsync } from './internal/wrapAsync'; -var FN_ARGS = /^(?:async\s+)?(?:function)?\s*[^(]*\(\s*([^)]+)\s*\)(?:\s*{)/m; -var ARROW_FN_ARGS = /^(?:async\s+)?(?:function\s+)?\(?\s*([^)^=]+)\s*\)?(?:\s*=>)/m; +var FN_ARGS = /^(?:async\s+)?(?:function)?\s*\w*\s*\(\s*([^)]+)\s*\)(?:\s*{)/; +var ARROW_FN_ARGS = /^(?:async\s+)?\(?\s*([^)=]+)\s*\)?(?:\s*=>)/; var FN_ARG_SPLIT = /,/; var FN_ARG = /(=.+)?(\s*)$/; var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; diff --git a/test/autoInject.js b/test/autoInject.js index 224783f29..3088faa60 100644 --- a/test/autoInject.js +++ b/test/autoInject.js @@ -148,4 +148,80 @@ describe('autoInject', () => { done(); }, 25); }); + + it('should work with complicated functions', done => { + async.autoInject({ + one: (cb) => cb(null, 1), + two: (cb) => cb(null, 2), + three: (cb) => cb(null, 3), + result: (one, two, three, cb) => { + if (!one || !two || !three) { + return cb('fail') + } + function add (a, b, c) { + return a + b + c + } + add(one, two, three) + cb(null, 1 + 2 + 3) + } + }, (err, results) => { + expect(results).to.eql({ one: 1, two: 2, three: 3, result: 6 }) + done() + }) + }) + + it('should work with functions with args on multiple lines', done => { + async.autoInject({ + one: (cb) => cb(null, 1), + two: (cb) => cb(null, 2), + three: (cb) => cb(null, 3), + result: function ( + one, + two, + three, + cb + ) { + cb(null, 1 + 2 + 3) + } + }, (err, results) => { + expect(results).to.eql({ one: 1, two: 2, three: 3, result: 6 }) + done() + }) + }) + + it('should work with methods with args on multiple lines', done => { + async.autoInject({ + one: (cb) => cb(null, 1), + two: (cb) => cb(null, 2), + three: (cb) => cb(null, 3), + result ( + one, + two, + three, + cb + ) { + cb(null, 1 + 2 + 3) + } + }, (err, results) => { + expect(results).to.eql({ one: 1, two: 2, three: 3, result: 6 }) + done() + }) + }) + + it('should work with arrow functions with args on multiple lines', done => { + async.autoInject({ + one: (cb) => cb(null, 1), + two: (cb) => cb(null, 2), + three: (cb) => cb(null, 3), + result: ( + one, + two, + three, + cb + ) => cb(null, 1 + 2 + 3) + }, (err, results) => { + expect(results).to.eql({ one: 1, two: 2, three: 3, result: 6 }) + done() + }) + }) });