This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -273,7 +273,8 @@ function inferInjectionArgs(fn) {
273
273
274
274
275
275
function createInjector ( modulesToLoad ) {
276
- var providerSuffix = 'Provider' ,
276
+ var INSTANTIATING = { } ,
277
+ providerSuffix = 'Provider' ,
277
278
path = [ ] ,
278
279
loadedModules = new HashMap ( ) ,
279
280
providerCache = {
@@ -394,10 +395,14 @@ function createInjector(modulesToLoad) {
394
395
throw Error ( 'Service name expected' ) ;
395
396
}
396
397
if ( cache . hasOwnProperty ( serviceName ) ) {
398
+ if ( cache [ serviceName ] === INSTANTIATING ) {
399
+ throw Error ( 'Circular dependency: ' + path . join ( ' <- ' ) ) ;
400
+ }
397
401
return cache [ serviceName ] ;
398
402
} else {
399
403
try {
400
404
path . unshift ( serviceName ) ;
405
+ cache [ serviceName ] = INSTANTIATING ;
401
406
return cache [ serviceName ] = factory ( serviceName ) ;
402
407
} finally {
403
408
path . shift ( ) ;
Original file line number Diff line number Diff line change @@ -483,6 +483,27 @@ describe('injector', function() {
483
483
createInjector ( [ [ '$injector' , myModule ] ] ) ;
484
484
} ) . toThrow ( 'Unknown provider: $injector from ' + myModule ) ;
485
485
} ) ;
486
+
487
+
488
+ it ( 'should throw error when trying to inject oneself' , function ( ) {
489
+ expect ( function ( ) {
490
+ createInjector ( [ function ( $provide ) {
491
+ $provide . factory ( 'service' , function ( service ) { } ) ;
492
+ return function ( service ) { }
493
+ } ] )
494
+ } ) . toThrow ( 'Circular dependency: service' ) ;
495
+ } ) ;
496
+
497
+
498
+ it ( 'should throw error when trying to inject circular dependency' , function ( ) {
499
+ expect ( function ( ) {
500
+ createInjector ( [ function ( $provide ) {
501
+ $provide . factory ( 'a' , function ( b ) { } ) ;
502
+ $provide . factory ( 'b' , function ( a ) { } ) ;
503
+ return function ( a ) { }
504
+ } ] )
505
+ } ) . toThrow ( 'Circular dependency: b <- a' ) ;
506
+ } ) ;
486
507
} ) ;
487
508
} ) ;
488
509
You can’t perform that action at this time.
0 commit comments