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

Commit 25a62b5

Browse files
mheveryIgorMinar
authored andcommitted
refactor(injection) infer injection args in ng:controller only
Because only controllers don't have currying, we can infer its arguments, all other APIs needing currying, automatic inference complicates the matters unecessary.
1 parent 97e3ec4 commit 25a62b5

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

src/Compiler.js

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ Template.prototype = {
4040

4141
addLinkFn:function(linkingFn) {
4242
if (linkingFn) {
43-
if (!linkingFn.$inject)
44-
linkingFn.$inject = [];
4543
this.linkFns.push(linkingFn);
4644
}
4745
},

src/Injector.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ function createInjector(factoryScope, factories, instanceCache) {
6060
if (!(value in instanceCache)) {
6161
var factory = factories[value];
6262
if (!factory) throw Error("Unknown provider for '"+value+"'.");
63+
inferInjectionArgs(factory);
6364
instanceCache[value] = invoke(factoryScope, factory);
6465
}
6566
return instanceCache[value];
6667
}
6768

6869
function invoke(self, fn, args){
6970
args = args || [];
70-
var injectNames = injectionArgs(fn);
71+
var injectNames = fn.$inject || [];
7172
var i = injectNames.length;
7273
while(i--) {
7374
args.unshift(injector(injectNames[i]));
@@ -133,7 +134,7 @@ var FN_ARGS = /^function\s*[^\(]*\(([^\)]*)\)/m;
133134
var FN_ARG_SPLIT = /,/;
134135
var FN_ARG = /^\s*(.+?)\s*$/;
135136
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
136-
function injectionArgs(fn) {
137+
function inferInjectionArgs(fn) {
137138
assertArgFn(fn);
138139
if (!fn.$inject) {
139140
var args = fn.$inject = [];

src/directives.js

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ angularDirective("ng:controller", function(expression){
174174
getter(scope, expression, true) ||
175175
getter(window, expression, true);
176176
assertArgFn(Controller, expression);
177+
inferInjectionArgs(Controller);
177178
return Controller;
178179
});
179180
return noop;

test/InjectorSpec.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ describe('injector', function(){
8585
it('should return $inject', function(){
8686
function fn(){}
8787
fn.$inject = ['a'];
88-
expect(injectionArgs(fn)).toBe(fn.$inject);
89-
expect(injectionArgs(function(){})).toEqual([]);
90-
expect(injectionArgs(function (){})).toEqual([]);
91-
expect(injectionArgs(function (){})).toEqual([]);
92-
expect(injectionArgs(function /* */ (){})).toEqual([]);
88+
expect(inferInjectionArgs(fn)).toBe(fn.$inject);
89+
expect(inferInjectionArgs(function(){})).toEqual([]);
90+
expect(inferInjectionArgs(function (){})).toEqual([]);
91+
expect(inferInjectionArgs(function (){})).toEqual([]);
92+
expect(inferInjectionArgs(function /* */ (){})).toEqual([]);
9393
});
9494

9595
it('should create $inject', function(){
@@ -103,28 +103,35 @@ describe('injector', function(){
103103
*/
104104
_c,
105105
/* {some type} */ d){ extraParans();}
106-
expect(injectionArgs($f_n0)).toEqual(['$a', 'b_', '_c', 'd']);
106+
expect(inferInjectionArgs($f_n0)).toEqual(['$a', 'b_', '_c', 'd']);
107107
expect($f_n0.$inject).toEqual(['$a', 'b_', '_c', 'd']);
108108
});
109109

110110
it('should handle no arg functions', function(){
111111
function $f_n0(){}
112-
expect(injectionArgs($f_n0)).toEqual([]);
112+
expect(inferInjectionArgs($f_n0)).toEqual([]);
113113
expect($f_n0.$inject).toEqual([]);
114114
});
115115

116116
it('should handle args with both $ and _', function(){
117117
function $f_n0($a_){}
118-
expect(injectionArgs($f_n0)).toEqual(['$a_']);
118+
expect(inferInjectionArgs($f_n0)).toEqual(['$a_']);
119119
expect($f_n0.$inject).toEqual(['$a_']);
120120
});
121121

122122
it('should throw on non function arg', function(){
123123
expect(function(){
124-
injectionArgs({});
124+
inferInjectionArgs({});
125125
}).toThrow();
126126
});
127127

128+
it('should infer injection on services', function(){
129+
var scope = angular.scope({
130+
a: function(){ return 'a';},
131+
b: function(a){ return a + 'b';}
132+
});
133+
expect(scope.$service('b')).toEqual('ab');
134+
});
128135
});
129136

130137
describe('inject', function(){

test/directivesSpec.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -469,17 +469,21 @@ describe("directive", function() {
469469
expect(scope.$element.text()).toEqual('hey dude!');
470470
});
471471

472+
it('should infer injection arguments', function(){
473+
temp.MyController = function($xhr){
474+
this.$root.someService = $xhr;
475+
};
476+
var scope = compile('<div ng:controller="temp.MyController"></div>');
477+
expect(scope.someService).toBe(scope.$service('$xhr'));
478+
});
472479
});
473480

474481
describe('ng:cloak', function() {
475482

476483
it('should get removed when an element is compiled', function() {
477484
var element = jqLite('<div ng:cloak></div>');
478-
479485
expect(element.attr('ng:cloak')).toBe('');
480-
481486
angular.compile(element);
482-
483487
expect(element.attr('ng:cloak')).toBeUndefined();
484488
});
485489

0 commit comments

Comments
 (0)