Warn on injection by name that will fail when minified #4504
Description
It would be awesome to be able to set some sort of configuration to cause angular to throw warnings (or errors) when it is injecting by function argument name.
I'm working on a project that has it's javascript minified in production and therefore need to specify injection arguments explicitly using either the array syntax:
["$scope", function($scope) { ... }]
or the $inject syntax:
controllerFunction.$inject = ["$scope"]
I'm using those (almost) everywhere in my project, but one of them snuck by me into the minified javascript and was a huge pain to find. I just got errors like
Error: [$injector:unpr] Unknown provider: eProvider <- e
I did eventually find it, but only by trial and error and a lot of searching through the code.
I've modified the angular source a bit to help me find these down the road. I've modified the annotate
function as follows:
function annotate(fn) {
var $inject,
fnText,
argDecl,
last;
if (typeof fn == 'function') {
if (!($inject = fn.$inject)) {
$inject = [];
if (fn.length) {
fnText = fn.toString().replace(STRIP_COMMENTS, '');
throw 'Using injection by name, should explicitly define ' +
'requirements using $inject or an array! Function text:' + fnText;
argDecl = fnText.match(FN_ARGS);
forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){
arg.replace(FN_ARG, function(all, underscore, name){
$inject.push(name);
});
});
}
fn.$inject = $inject;
}
} else if (isArray(fn)) {
last = fn.length - 1;
assertArgFn(fn[last], 'fn');
$inject = fn.slice(0, last);
} else {
assertArgFn(fn, 'fn', true);
}
return $inject;
}
That way I am warned in development mode if my code won't work when minified. I know this isn't the right way to do it because it prevents injection by name from working at all (which is why this isn't a pull request), but maybe there is a way we can figure out to make this a configurable option.
Or if we can't maybe angular should always throw a warning (not exception) because shouldn't we all be planning to minify our javascript in production?