8
8
9
9
import { Type } from '../type' ;
10
10
import { stringify } from '../util' ;
11
-
12
11
import { resolveForwardRef } from './forward_ref' ;
13
12
import { InjectionToken } from './injection_token' ;
14
13
import { Inject , Optional , Self , SkipSelf } from './metadata' ;
15
14
import { ConstructorProvider , ExistingProvider , FactoryProvider , StaticClassProvider , StaticProvider , ValueProvider } from './provider' ;
16
15
16
+ export const SOURCE = '__source' ;
17
17
const _THROW_IF_NOT_FOUND = new Object ( ) ;
18
18
export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND ;
19
19
@@ -64,15 +64,28 @@ export abstract class Injector {
64
64
*/
65
65
abstract get ( token : any , notFoundValue ?: any ) : any ;
66
66
67
+ /**
68
+ * @deprecated from v5 use the new signature Injector.create(options)
69
+ */
70
+ static create ( providers : StaticProvider [ ] , parent ?: Injector ) : Injector ;
71
+
72
+ static create ( options : { providers : StaticProvider [ ] , parent ?: Injector , name ?: string } ) : Injector ;
73
+
67
74
/**
68
75
* Create a new Injector which is configure using `StaticProvider`s.
69
76
*
70
77
* ### Example
71
78
*
72
79
* {@example core/di/ts/provider_spec.ts region='ConstructorProvider'}
73
80
*/
74
- static create ( providers : StaticProvider [ ] , parent ?: Injector ) : Injector {
75
- return new StaticInjector ( providers , parent ) ;
81
+ static create (
82
+ options : StaticProvider [ ] | { providers : StaticProvider [ ] , parent ?: Injector , name ?: string } ,
83
+ parent ?: Injector ) : Injector {
84
+ if ( Array . isArray ( options ) ) {
85
+ return new StaticInjector ( options , parent ) ;
86
+ } else {
87
+ return new StaticInjector ( options . providers , options . parent , options . name || null ) ;
88
+ }
76
89
}
77
90
}
78
91
@@ -103,11 +116,14 @@ const NO_NEW_LINE = 'ɵ';
103
116
104
117
export class StaticInjector implements Injector {
105
118
readonly parent : Injector ;
119
+ readonly source : string | null ;
106
120
107
121
private _records : Map < any , Record > ;
108
122
109
- constructor ( providers : StaticProvider [ ] , parent : Injector = NULL_INJECTOR ) {
123
+ constructor (
124
+ providers : StaticProvider [ ] , parent : Injector = NULL_INJECTOR , source : string | null = null ) {
110
125
this . parent = parent ;
126
+ this . source = source ;
111
127
const records = this . _records = new Map < any , Record > ( ) ;
112
128
records . set (
113
129
Injector , < Record > { token : Injector , fn : IDENT , deps : EMPTY , value : this , useNew : false } ) ;
@@ -122,7 +138,10 @@ export class StaticInjector implements Injector {
122
138
return tryResolveToken ( token , record , this . _records , this . parent , notFoundValue ) ;
123
139
} catch ( e ) {
124
140
const tokenPath : any [ ] = e [ NG_TEMP_TOKEN_PATH ] ;
125
- e . message = formatError ( '\n' + e . message , tokenPath ) ;
141
+ if ( token [ SOURCE ] ) {
142
+ tokenPath . unshift ( token [ SOURCE ] ) ;
143
+ }
144
+ e . message = formatError ( '\n' + e . message , tokenPath , this . source ) ;
126
145
e [ NG_TOKEN_PATH ] = tokenPath ;
127
146
e [ NG_TEMP_TOKEN_PATH ] = null ;
128
147
throw e ;
@@ -336,7 +355,7 @@ function computeDeps(provider: StaticProvider): DependencyRecord[] {
336
355
return deps ;
337
356
}
338
357
339
- function formatError ( text : string , obj : any ) : string {
358
+ function formatError ( text : string , obj : any , source : string | null = null ) : string {
340
359
text = text && text . charAt ( 0 ) === '\n' && text . charAt ( 1 ) == NO_NEW_LINE ? text . substr ( 2 ) : text ;
341
360
let context = stringify ( obj ) ;
342
361
if ( obj instanceof Array ) {
@@ -352,7 +371,7 @@ function formatError(text: string, obj: any): string {
352
371
}
353
372
context = `{${ parts . join ( ', ' ) } }` ;
354
373
}
355
- return `StaticInjectorError[${ context } ]: ${ text . replace ( NEW_LINE , '\n ' ) } ` ;
374
+ return `StaticInjectorError${ source ? '(' + source + ')' : '' } [${ context } ]: ${ text . replace ( NEW_LINE , '\n ' ) } ` ;
356
375
}
357
376
358
377
function staticError ( text : string , obj : any ) : Error {
0 commit comments