Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 03726f7

Browse files
author
Shahar Talmi
committed
fix(injector): support arrow functions with no parenthesis
with arrow functions parenthesis are optional in case you have exactly one argument to the function. the previous regexp assumed function arguments are always inside parenthesis and so it didn't annotate functions like `$http => $http.get(...)` correctly Closes #12890
1 parent 75893ae commit 03726f7

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/auto/injector.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* Implicit module which gets automatically added to each {@link auto.$injector $injector}.
6363
*/
6464

65+
var ARROW_ARG = /^([^\(]+?)=>/;
6566
var FN_ARGS = /^[^\(]*\(\s*([^\)]*)\)/m;
6667
var FN_ARG_SPLIT = /,/;
6768
var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/;
@@ -70,7 +71,7 @@ var $injectorMinErr = minErr('$injector');
7071

7172
function extractArgs(fn) {
7273
var fnText = fn.toString().replace(STRIP_COMMENTS, ''),
73-
args = fnText.match(FN_ARGS);
74+
args = fnText.match(ARROW_ARG) || fnText.match(FN_ARGS);
7475
return args;
7576
}
7677

test/auto/injectorSpec.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,27 @@ describe('injector', function() {
249249

250250
// Only Chrome and Firefox support this syntax.
251251
if (/chrome|firefox/i.test(navigator.userAgent)) {
252-
it('should be possible to annotate functions that are declared using ES6 syntax', function() {
252+
describe('es6', function() {
253253
/*jshint -W061 */
254-
// The function is generated using `eval` as just having the ES6 syntax can break some browsers.
255-
expect(annotate(eval('({ fn(x) { return; } })').fn)).toEqual(['x']);
254+
// The functions are generated using `eval` as just having the ES6 syntax can break some browsers.
255+
it('should be possible to annotate functions that are declared using ES6 syntax', function() {
256+
expect(annotate(eval('({ fn(x) { return; } })').fn)).toEqual(['x']);
257+
});
258+
259+
260+
it('should create $inject for arrow functions', function() {
261+
expect(annotate(eval('(a, b) => a'))).toEqual(['a', 'b']);
262+
});
263+
264+
265+
it('should create $inject for arrow functions with no parenthesis', function() {
266+
expect(annotate(eval('a => a'))).toEqual(['a']);
267+
});
268+
269+
270+
it('should take args before first arrow', function() {
271+
expect(annotate(eval('a => b => b'))).toEqual(['a']);
272+
});
256273
/*jshint +W061 */
257274
});
258275
}

0 commit comments

Comments
 (0)