Skip to content

Commit 925c1c5

Browse files
committed
fix($injector): add workaround for class stringification in Chrome v50/51
Partially fixes angular#14240.
1 parent 3dcc016 commit 925c1c5

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/auto/injector.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/;
7070
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
7171
var $injectorMinErr = minErr('$injector');
7272

73-
function extractArgs(fn) {
73+
function stringifyFn(fn) {
7474
// Support: Chrome 50-51 only
7575
// Creating a new string by adding `' '` at the end, to hack around some bug in Chrome v50/51
7676
// (See https://github.com/angular/angular.js/issues/14487.)
7777
// TODO (gkalpak): Remove workaround when Chrome v52 is released
78-
var fnText = Function.prototype.toString.call(fn).replace(STRIP_COMMENTS, '') + ' ',
78+
return Function.prototype.toString.call(fn) + ' ';
79+
}
80+
81+
function extractArgs(fn) {
82+
var fnText = stringifyFn(fn).replace(STRIP_COMMENTS, ''),
7983
args = fnText.match(ARROW_ARG) || fnText.match(FN_ARGS);
8084
return args;
8185
}
@@ -849,7 +853,7 @@ function createInjector(modulesToLoad, strictDi) {
849853
if (!isBoolean(result)) {
850854
// Workaround for MS Edge.
851855
// Check https://connect.microsoft.com/IE/Feedback/Details/2211653
852-
result = func.$$ngIsClass = /^(?:class\s|constructor\()/.test(Function.prototype.toString.call(func));
856+
result = func.$$ngIsClass = /^(?:class\s|constructor\()/.test(stringifyFn(func));
853857
}
854858
return result;
855859
}

test/auto/injectorSpec.js

+21
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ describe('injector', function() {
283283
it('should take args before first arrow', function() {
284284
expect(annotate(eval('a => b => b'))).toEqual(['a']);
285285
});
286+
287+
// Support: Chrome 50-51 only
288+
// TODO (gkalpak): Remove when Chrome v52 is relased.
289+
// it('should be able to inject fat-arrow function', function() {
290+
// inject(($injector) => {
291+
// expect($injector).toBeDefined();
292+
// });
293+
// });
286294
}
287295

288296
if (support.classes) {
@@ -293,6 +301,19 @@ describe('injector', function() {
293301
expect(instance).toEqual(new Clazz('a-value'));
294302
expect(instance.aVal()).toEqual('a-value');
295303
});
304+
305+
// Support: Chrome 50-51 only
306+
// TODO (gkalpak): Remove when Chrome v52 is relased.
307+
// it('should be able to invoke classes', function() {
308+
// class Test {
309+
// constructor($injector) {
310+
// this.$injector = $injector;
311+
// }
312+
// }
313+
// var instance = injector.invoke(Test, null, null, 'Test');
314+
315+
// expect(instance.$injector).toBe(injector);
316+
// });
296317
}
297318
/*jshint +W061 */
298319
});

0 commit comments

Comments
 (0)