Skip to content

Commit

Permalink
fix(autoInject): tighten up regex for parsing args. Closes #1663
Browse files Browse the repository at this point in the history
  • Loading branch information
aearly committed Jun 23, 2019
1 parent 76f5693 commit e1045be
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/autoInject.js
Expand Up @@ -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;
Expand Down
76 changes: 76 additions & 0 deletions test/autoInject.js
Expand Up @@ -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()
})
})
});

0 comments on commit e1045be

Please sign in to comment.