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

Commit 373078a

Browse files
committed
revert: feat(injector): "strict-DI" mode which disables "automatic" function annotation
This reverts commit f5a04f5.
1 parent db07ad2 commit 373078a

File tree

8 files changed

+33
-337
lines changed

8 files changed

+33
-337
lines changed

docs/content/error/$injector/strictdi.ngdoc

-54
This file was deleted.

src/Angular.js

+3-113
Original file line numberDiff line numberDiff line change
@@ -1138,19 +1138,6 @@ function encodeUriQuery(val, pctEncodeSpaces) {
11381138
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
11391139
}
11401140

1141-
var ngAttrPrefixes = ['ng-', 'data-ng-', 'ng:', 'x-ng-'];
1142-
1143-
function getNgAttribute(element, ngAttr) {
1144-
var attr, i, ii = ngAttrPrefixes.length, j, jj;
1145-
element = jqLite(element);
1146-
for (i=0; i<ii; ++i) {
1147-
attr = ngAttrPrefixes[i] + ngAttr;
1148-
if (isString(attr = element.attr(attr))) {
1149-
return attr;
1150-
}
1151-
}
1152-
return null;
1153-
}
11541141

11551142
/**
11561143
* @ngdoc directive
@@ -1160,11 +1147,6 @@ function getNgAttribute(element, ngAttr) {
11601147
* @element ANY
11611148
* @param {angular.Module} ngApp an optional application
11621149
* {@link angular.module module} name to load.
1163-
* @param {boolean=} ngStrictDi if this attribute is present on the app element, the injector will be
1164-
* created in "strict-di" mode. This means that the application will fail to invoke functions which
1165-
* do not use explicit function annotation (and are thus unsuitable for minification), as described
1166-
* in {@link guide/di the Dependency Injection guide}, and useful debugging info will assist in
1167-
* tracking down the root of these bugs.
11681150
*
11691151
* @description
11701152
*
@@ -1202,92 +1184,12 @@ function getNgAttribute(element, ngAttr) {
12021184
</file>
12031185
</example>
12041186
*
1205-
* Using `ngStrictDi`, you would see something like this:
1206-
*
1207-
<example ng-app-included="true">
1208-
<file name="index.html">
1209-
<div ng-app="ngAppStrictDemo" ng-strict-di>
1210-
<div ng-controller="GoodController1">
1211-
I can add: {{a}} + {{b}} = {{ a+b }}
1212-
1213-
<p>This renders because the controller does not fail to
1214-
instantiate, by using explicit annotation style (see
1215-
script.js for details)
1216-
</p>
1217-
</div>
1218-
1219-
<div ng-controller="GoodController2">
1220-
Name: <input ng-model="name"><br />
1221-
Hello, {{name}}!
1222-
1223-
<p>This renders because the controller does not fail to
1224-
instantiate, by using explicit annotation style
1225-
(see script.js for details)
1226-
</p>
1227-
</div>
1228-
1229-
<div ng-controller="BadController">
1230-
I can add: {{a}} + {{b}} = {{ a+b }}
1231-
1232-
<p>The controller could not be instantiated, due to relying
1233-
on automatic function annotations (which are disabled in
1234-
strict mode). As such, the content of this section is not
1235-
interpolated, and there should be an error in your web console.
1236-
</p>
1237-
</div>
1238-
</div>
1239-
</file>
1240-
<file name="script.js">
1241-
angular.module('ngAppStrictDemo', [])
1242-
// BadController will fail to instantiate, due to relying on automatic function annotation,
1243-
// rather than an explicit annotation
1244-
.controller('BadController', function($scope) {
1245-
$scope.a = 1;
1246-
$scope.b = 2;
1247-
})
1248-
// Unlike BadController, GoodController1 and GoodController2 will not fail to be instantiated,
1249-
// due to using explicit annotations using the array style and $inject property, respectively.
1250-
.controller('GoodController1', ['$scope', function($scope) {
1251-
$scope.a = 1;
1252-
$scope.b = 2;
1253-
}])
1254-
.controller('GoodController2', GoodController2);
1255-
function GoodController2($scope) {
1256-
$scope.name = "World";
1257-
}
1258-
GoodController2.$inject = ['$scope'];
1259-
</file>
1260-
<file name="style.css">
1261-
div[ng-controller] {
1262-
margin-bottom: 1em;
1263-
-webkit-border-radius: 4px;
1264-
border-radius: 4px;
1265-
border: 1px solid;
1266-
padding: .5em;
1267-
}
1268-
div[ng-controller^=Good] {
1269-
border-color: #d6e9c6;
1270-
background-color: #dff0d8;
1271-
color: #3c763d;
1272-
}
1273-
div[ng-controller^=Bad] {
1274-
border-color: #ebccd1;
1275-
background-color: #f2dede;
1276-
color: #a94442;
1277-
margin-bottom: 0;
1278-
}
1279-
</file>
1280-
</example>
12811187
*/
12821188
function angularInit(element, bootstrap) {
12831189
var elements = [element],
12841190
appElement,
12851191
module,
1286-
config = {},
12871192
names = ['ng:app', 'ng-app', 'x-ng-app', 'data-ng-app'],
1288-
options = {
1289-
'boolean': ['strict-di']
1290-
},
12911193
NG_APP_CLASS_REGEXP = /\sng[:\-]app(:\s*([\w\d_]+);?)?\s/;
12921194

12931195
function append(element) {
@@ -1323,8 +1225,7 @@ function angularInit(element, bootstrap) {
13231225
}
13241226
});
13251227
if (appElement) {
1326-
config.strictDi = getNgAttribute(appElement, "strict-di") !== null;
1327-
bootstrap(appElement, module ? [module] : [], config);
1228+
bootstrap(appElement, module ? [module] : []);
13281229
}
13291230
}
13301231

@@ -1380,20 +1281,9 @@ function angularInit(element, bootstrap) {
13801281
* Each item in the array should be the name of a predefined module or a (DI annotated)
13811282
* function that will be invoked by the injector as a run block.
13821283
* See: {@link angular.module modules}
1383-
* @param {Object=} config an object for defining configuration options for the application. The
1384-
* following keys are supported:
1385-
*
1386-
* - `strictDi`: disable automatic function annotation for the application. This is meant to
1387-
* assist in finding bugs which break minified code.
1388-
*
13891284
* @returns {auto.$injector} Returns the newly created injector for this app.
13901285
*/
1391-
function bootstrap(element, modules, config) {
1392-
if (!isObject(config)) config = {};
1393-
var defaultConfig = {
1394-
strictDi: false
1395-
};
1396-
config = extend(defaultConfig, config);
1286+
function bootstrap(element, modules) {
13971287
var doBootstrap = function() {
13981288
element = jqLite(element);
13991289

@@ -1407,7 +1297,7 @@ function bootstrap(element, modules, config) {
14071297
$provide.value('$rootElement', element);
14081298
}]);
14091299
modules.unshift('ng');
1410-
var injector = createInjector(modules, config.strictDi);
1300+
var injector = createInjector(modules);
14111301
injector.invoke(['$rootScope', '$rootElement', '$compile', '$injector', '$animate',
14121302
function(scope, element, compile, injector, animate) {
14131303
scope.$apply(function() {

src/auto/injector.js

+9-35
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,7 @@ var FN_ARG_SPLIT = /,/;
6666
var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/;
6767
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
6868
var $injectorMinErr = minErr('$injector');
69-
70-
function anonFn(fn) {
71-
// For anonymous functions, showing at the very least the function signature can help in
72-
// debugging.
73-
var fnText = fn.toString().replace(STRIP_COMMENTS, ''),
74-
args = fnText.match(FN_ARGS);
75-
if (args) {
76-
return 'function(' + (args[1] || '').replace(/[\s\r\n]+/, ' ') + ')';
77-
}
78-
return 'fn';
79-
}
80-
81-
function annotate(fn, strictDi, name) {
69+
function annotate(fn) {
8270
var $inject,
8371
fnText,
8472
argDecl,
@@ -88,13 +76,6 @@ function annotate(fn, strictDi, name) {
8876
if (!($inject = fn.$inject)) {
8977
$inject = [];
9078
if (fn.length) {
91-
if (strictDi) {
92-
if (!isString(name) || !name) {
93-
name = fn.name || anonFn(fn);
94-
}
95-
throw $injectorMinErr('strictdi',
96-
'{0} is not using explicit annotation and cannot be invoked in strict mode', name);
97-
}
9879
fnText = fn.toString().replace(STRIP_COMMENTS, '');
9980
argDecl = fnText.match(FN_ARGS);
10081
forEach(argDecl[1].split(FN_ARG_SPLIT), function(arg){
@@ -606,8 +587,7 @@ function annotate(fn, strictDi, name) {
606587
*/
607588

608589

609-
function createInjector(modulesToLoad, strictDi) {
610-
strictDi = (strictDi === true);
590+
function createInjector(modulesToLoad) {
611591
var INSTANTIATING = {},
612592
providerSuffix = 'Provider',
613593
path = [],
@@ -625,13 +605,13 @@ function createInjector(modulesToLoad, strictDi) {
625605
providerInjector = (providerCache.$injector =
626606
createInternalInjector(providerCache, function() {
627607
throw $injectorMinErr('unpr', "Unknown provider: {0}", path.join(' <- '));
628-
}, strictDi)),
608+
})),
629609
instanceCache = {},
630610
instanceInjector = (instanceCache.$injector =
631611
createInternalInjector(instanceCache, function(servicename) {
632612
var provider = providerInjector.get(servicename + providerSuffix);
633-
return instanceInjector.invoke(provider.$get, provider, undefined, servicename);
634-
}, strictDi));
613+
return instanceInjector.invoke(provider.$get, provider);
614+
}));
635615

636616

637617
forEach(loadModules(modulesToLoad), function(fn) { instanceInjector.invoke(fn || noop); });
@@ -763,14 +743,9 @@ function createInjector(modulesToLoad, strictDi) {
763743
}
764744
}
765745

766-
function invoke(fn, self, locals, serviceName){
767-
if (typeof locals === 'string') {
768-
serviceName = locals;
769-
locals = null;
770-
}
771-
746+
function invoke(fn, self, locals){
772747
var args = [],
773-
$inject = annotate(fn, strictDi, serviceName),
748+
$inject = annotate(fn),
774749
length, i,
775750
key;
776751

@@ -796,15 +771,15 @@ function createInjector(modulesToLoad, strictDi) {
796771
return fn.apply(self, args);
797772
}
798773

799-
function instantiate(Type, locals, serviceName) {
774+
function instantiate(Type, locals) {
800775
var Constructor = function() {},
801776
instance, returnedValue;
802777

803778
// Check if Type is annotated and use just the given function at n-1 as parameter
804779
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
805780
Constructor.prototype = (isArray(Type) ? Type[Type.length - 1] : Type).prototype;
806781
instance = new Constructor();
807-
returnedValue = invoke(Type, instance, locals, serviceName);
782+
returnedValue = invoke(Type, instance, locals);
808783

809784
return isObject(returnedValue) || isFunction(returnedValue) ? returnedValue : instance;
810785
}
@@ -821,4 +796,3 @@ function createInjector(modulesToLoad, strictDi) {
821796
}
822797
}
823798

824-
createInjector.$$annotate = annotate;

src/ng/controller.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function $ControllerProvider() {
7171
assertArgFn(expression, constructor, true);
7272
}
7373

74-
instance = $injector.instantiate(expression, locals, constructor);
74+
instance = $injector.instantiate(expression, locals);
7575

7676
if (identifier) {
7777
if (!(locals && typeof locals.$scope == 'object')) {

0 commit comments

Comments
 (0)