Skip to content

Commit 4e9809b

Browse files
committed
feat(transformers): changes transformers to collect information about providers and resolve identifiers during linking
Closes angular#7380
1 parent bd8a421 commit 4e9809b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2417
-972
lines changed

modules/angular2/src/compiler/directive_metadata.ts

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import {
22
isPresent,
33
isBlank,
4+
isNumber,
5+
isBoolean,
46
normalizeBool,
57
normalizeBlank,
68
serializeEnum,
79
Type,
810
isString,
911
RegExpWrapper,
10-
StringWrapper
12+
StringWrapper,
13+
isArray
1114
} from 'angular2/src/facade/lang';
1215
import {unimplemented} from 'angular2/src/facade/exceptions';
1316
import {StringMapWrapper} from 'angular2/src/facade/collection';
@@ -25,64 +28,70 @@ import {LifecycleHooks, LIFECYCLE_HOOKS_VALUES} from 'angular2/src/core/linker/i
2528
var HOST_REG_EXP = /^(?:(?:\[([^\]]+)\])|(?:\(([^\)]+)\)))$/g;
2629

2730
export abstract class CompileMetadataWithIdentifier {
28-
static fromJson(data: {[key: string]: any}): CompileMetadataWithIdentifier {
29-
return _COMPILE_METADATA_FROM_JSON[data['class']](data);
30-
}
31-
3231
abstract toJson(): {[key: string]: any};
3332

3433
get identifier(): CompileIdentifierMetadata { return <CompileIdentifierMetadata>unimplemented(); }
3534
}
3635

3736
export abstract class CompileMetadataWithType extends CompileMetadataWithIdentifier {
38-
static fromJson(data: {[key: string]: any}): CompileMetadataWithType {
39-
return _COMPILE_METADATA_FROM_JSON[data['class']](data);
40-
}
41-
4237
abstract toJson(): {[key: string]: any};
4338

4439
get type(): CompileTypeMetadata { return <CompileTypeMetadata>unimplemented(); }
4540

4641
get identifier(): CompileIdentifierMetadata { return <CompileIdentifierMetadata>unimplemented(); }
4742
}
4843

44+
export function metadataFromJson(data: {[key: string]: any}): any {
45+
return _COMPILE_METADATA_FROM_JSON[data['class']](data);
46+
}
47+
4948
export class CompileIdentifierMetadata implements CompileMetadataWithIdentifier {
5049
runtime: any;
5150
name: string;
5251
prefix: string;
5352
moduleUrl: string;
5453
constConstructor: boolean;
55-
constructor({runtime, name, moduleUrl, prefix, constConstructor}: {
54+
value: any;
55+
56+
constructor({runtime, name, moduleUrl, prefix, constConstructor, value}: {
5657
runtime?: any,
5758
name?: string,
59+
staticMethodName?: string,
5860
moduleUrl?: string,
5961
prefix?: string,
60-
constConstructor?: boolean
62+
constConstructor?: boolean,
63+
value?: any
6164
} = {}) {
6265
this.runtime = runtime;
6366
this.name = name;
6467
this.prefix = prefix;
6568
this.moduleUrl = moduleUrl;
6669
this.constConstructor = constConstructor;
70+
this.value = value;
6771
}
6872

6973
static fromJson(data: {[key: string]: any}): CompileIdentifierMetadata {
74+
let value = isArray(data['value']) ? arrayFromJson(data['value'], metadataFromJson) :
75+
objFromJson(data['value'], metadataFromJson);
7076
return new CompileIdentifierMetadata({
7177
name: data['name'],
7278
prefix: data['prefix'],
7379
moduleUrl: data['moduleUrl'],
74-
constConstructor: data['constConstructor']
80+
constConstructor: data['constConstructor'],
81+
value: value
7582
});
7683
}
7784

7885
toJson(): {[key: string]: any} {
86+
let value = isArray(this.value) ? arrayToJson(this.value) : objToJson(this.value);
7987
return {
8088
// Note: Runtime type can't be serialized...
8189
'class': 'Identifier',
8290
'name': this.name,
8391
'moduleUrl': this.moduleUrl,
8492
'prefix': this.prefix,
85-
'constConstructor': this.constConstructor
93+
'constConstructor': this.constConstructor,
94+
'value': value
8695
};
8796
}
8897

@@ -177,44 +186,78 @@ export class CompileProviderMetadata {
177186
static fromJson(data: {[key: string]: any}): CompileProviderMetadata {
178187
return new CompileProviderMetadata({
179188
token: objFromJson(data['token'], CompileIdentifierMetadata.fromJson),
180-
useClass: objFromJson(data['useClass'], CompileTypeMetadata.fromJson)
189+
useClass: objFromJson(data['useClass'], CompileTypeMetadata.fromJson),
190+
useExisting: objFromJson(data['useExisting'], CompileIdentifierMetadata.fromJson),
191+
useValue: objFromJson(data['useValue'], CompileIdentifierMetadata.fromJson),
192+
useFactory: objFromJson(data['useFactory'], CompileFactoryMetadata.fromJson)
181193
});
182194
}
183195

184196
toJson(): {[key: string]: any} {
185197
return {
186198
// Note: Runtime type can't be serialized...
199+
'class': 'Provider',
187200
'token': objToJson(this.token),
188-
'useClass': objToJson(this.useClass)
201+
'useClass': objToJson(this.useClass),
202+
'useExisting': objToJson(this.useExisting),
203+
'useValue': objToJson(this.useValue),
204+
'useFactory': objToJson(this.useFactory)
189205
};
190206
}
191207
}
192208

193-
export class CompileFactoryMetadata implements CompileIdentifierMetadata {
209+
export class CompileFactoryMetadata implements CompileIdentifierMetadata,
210+
CompileMetadataWithIdentifier {
194211
runtime: Function;
195212
name: string;
196213
prefix: string;
197214
moduleUrl: string;
198215
constConstructor: boolean;
216+
value: any;
199217
diDeps: CompileDiDependencyMetadata[];
200218

201-
constructor({runtime, name, moduleUrl, constConstructor, diDeps}: {
219+
constructor({runtime, name, moduleUrl, prefix, constConstructor, diDeps, value}: {
202220
runtime?: Function,
203221
name?: string,
222+
prefix?: string,
204223
moduleUrl?: string,
205224
constConstructor?: boolean,
225+
value?: boolean,
206226
diDeps?: CompileDiDependencyMetadata[]
207227
}) {
208228
this.runtime = runtime;
209229
this.name = name;
230+
this.prefix = prefix;
210231
this.moduleUrl = moduleUrl;
211232
this.diDeps = diDeps;
212233
this.constConstructor = constConstructor;
234+
this.value = value;
213235
}
214236

215237
get identifier(): CompileIdentifierMetadata { return this; }
216238

217-
toJson() { return null; }
239+
static fromJson(data: {[key: string]: any}): CompileFactoryMetadata {
240+
return new CompileFactoryMetadata({
241+
name: data['name'],
242+
prefix: data['prefix'],
243+
moduleUrl: data['moduleUrl'],
244+
constConstructor: data['constConstructor'],
245+
value: data['value'],
246+
diDeps: arrayFromJson(data['diDeps'], CompileDiDependencyMetadata.fromJson)
247+
});
248+
}
249+
250+
toJson(): {[key: string]: any} {
251+
return {
252+
'class': 'Factory',
253+
'name': this.name,
254+
'prefix': this.prefix,
255+
'moduleUrl': this.moduleUrl,
256+
'constConstructor': this.constConstructor,
257+
'value': this.value,
258+
'diDeps': arrayToJson(this.diDeps)
259+
};
260+
}
218261
}
219262

220263
/**
@@ -227,15 +270,17 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
227270
moduleUrl: string;
228271
isHost: boolean;
229272
constConstructor: boolean;
273+
value: any;
230274
diDeps: CompileDiDependencyMetadata[];
231275

232-
constructor({runtime, name, moduleUrl, prefix, isHost, constConstructor, diDeps}: {
276+
constructor({runtime, name, moduleUrl, prefix, isHost, constConstructor, value, diDeps}: {
233277
runtime?: Type,
234278
name?: string,
235279
moduleUrl?: string,
236280
prefix?: string,
237281
isHost?: boolean,
238282
constConstructor?: boolean,
283+
value?: any,
239284
diDeps?: CompileDiDependencyMetadata[]
240285
} = {}) {
241286
this.runtime = runtime;
@@ -244,6 +289,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
244289
this.prefix = prefix;
245290
this.isHost = normalizeBool(isHost);
246291
this.constConstructor = constConstructor;
292+
this.value = value;
247293
this.diDeps = normalizeBlank(diDeps);
248294
}
249295

@@ -254,6 +300,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
254300
prefix: data['prefix'],
255301
isHost: data['isHost'],
256302
constConstructor: data['constConstructor'],
303+
value: data['value'],
257304
diDeps: arrayFromJson(data['diDeps'], CompileDiDependencyMetadata.fromJson)
258305
});
259306
}
@@ -270,6 +317,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
270317
'prefix': this.prefix,
271318
'isHost': this.isHost,
272319
'constConstructor': this.constConstructor,
320+
'value': this.value,
273321
'diDeps': arrayToJson(this.diDeps)
274322
};
275323
}
@@ -382,8 +430,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
382430
outputs?: string[],
383431
host?: {[key: string]: string},
384432
lifecycleHooks?: LifecycleHooks[],
385-
providers?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
386-
viewProviders?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
433+
providers?:
434+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
435+
viewProviders?:
436+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
387437
queries?: CompileQueryMetadata[],
388438
viewQueries?: CompileQueryMetadata[],
389439
template?: CompileTemplateMetadata
@@ -474,8 +524,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
474524
hostProperties?: {[key: string]: string},
475525
hostAttributes?: {[key: string]: string},
476526
lifecycleHooks?: LifecycleHooks[],
477-
providers?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
478-
viewProviders?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
527+
providers?:
528+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
529+
viewProviders?:
530+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
479531
queries?: CompileQueryMetadata[],
480532
viewQueries?: CompileQueryMetadata[],
481533
template?: CompileTemplateMetadata
@@ -494,8 +546,8 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
494546
this.lifecycleHooks = lifecycleHooks;
495547
this.providers = normalizeBlank(providers);
496548
this.viewProviders = normalizeBlank(viewProviders);
497-
this.queries = queries;
498-
this.viewQueries = viewQueries;
549+
this.queries = normalizeBlank(queries);
550+
this.viewQueries = normalizeBlank(viewQueries);
499551
this.template = template;
500552
}
501553

@@ -520,7 +572,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
520572
(<any[]>data['lifecycleHooks']).map(hookValue => LIFECYCLE_HOOKS_VALUES[hookValue]),
521573
template: isPresent(data['template']) ? CompileTemplateMetadata.fromJson(data['template']) :
522574
data['template'],
523-
providers: arrayFromJson(data['providers'], CompileProviderMetadata.fromJson)
575+
providers: arrayFromJson(data['providers'], metadataFromJson),
576+
viewProviders: arrayFromJson(data['viewProviders'], metadataFromJson),
577+
queries: arrayFromJson(data['queries'], CompileQueryMetadata.fromJson),
578+
viewQueries: arrayFromJson(data['viewQueries'], CompileQueryMetadata.fromJson)
524579
});
525580
}
526581

@@ -541,7 +596,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
541596
'hostAttributes': this.hostAttributes,
542597
'lifecycleHooks': this.lifecycleHooks.map(hook => serializeEnum(hook)),
543598
'template': isPresent(this.template) ? this.template.toJson() : this.template,
544-
'providers': arrayToJson(this.providers)
599+
'providers': arrayToJson(this.providers),
600+
'viewProviders': arrayToJson(this.viewProviders),
601+
'queries': arrayToJson(this.queries),
602+
'viewQueries': arrayToJson(this.viewQueries)
545603
};
546604
}
547605
}
@@ -611,7 +669,9 @@ var _COMPILE_METADATA_FROM_JSON = {
611669
'Directive': CompileDirectiveMetadata.fromJson,
612670
'Pipe': CompilePipeMetadata.fromJson,
613671
'Type': CompileTypeMetadata.fromJson,
614-
'Identifier': CompileIdentifierMetadata.fromJson
672+
'Provider': CompileProviderMetadata.fromJson,
673+
'Identifier': CompileIdentifierMetadata.fromJson,
674+
'Factory': CompileFactoryMetadata.fromJson
615675
};
616676

617677
function arrayFromJson(obj: any[], fn: (a: {[key: string]: any}) => any): any {
@@ -623,9 +683,9 @@ function arrayToJson(obj: any[]): string | {[key: string]: any} {
623683
}
624684

625685
function objFromJson(obj: any, fn: (a: {[key: string]: any}) => any): any {
626-
return (isString(obj) || isBlank(obj)) ? obj : fn(obj);
686+
return (isString(obj) || isBlank(obj) || isBoolean(obj) || isNumber(obj)) ? obj : fn(obj);
627687
}
628688

629689
function objToJson(obj: any): string | {[key: string]: any} {
630-
return (isString(obj) || isBlank(obj)) ? obj : obj.toJson();
690+
return (isString(obj) || isBlank(obj) || isBoolean(obj) || isNumber(obj)) ? obj : obj.toJson();
631691
}

modules/angular2/src/compiler/template_compiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ export class TemplateCompiler {
110110
hostAttributes: directive.hostAttributes,
111111
lifecycleHooks: directive.lifecycleHooks,
112112
providers: directive.providers,
113+
viewProviders: directive.viewProviders,
114+
queries: directive.queries,
115+
viewQueries: directive.viewQueries,
113116
template: normalizedTemplate
114117
}));
115118
}

modules/angular2/src/facade/lang.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ bool isStringMap(Object obj) => obj is Map;
3333
bool isArray(Object obj) => obj is List;
3434
bool isPromise(Object obj) => obj is Future;
3535
bool isNumber(Object obj) => obj is num;
36+
bool isBoolean(Object obj) => obj is bool;
3637
bool isDate(Object obj) => obj is DateTime;
3738

3839
String stringify(obj) {

modules/angular2/src/facade/lang.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ export function isBlank(obj: any): boolean {
124124
return obj === undefined || obj === null;
125125
}
126126

127+
export function isBoolean(obj: any): boolean {
128+
return typeof obj === "boolean";
129+
}
130+
131+
export function isNumber(obj: any): boolean {
132+
return typeof obj === "number";
133+
}
134+
127135
export function isString(obj: any): boolean {
128136
return typeof obj === "string";
129137
}
@@ -148,10 +156,6 @@ export function isArray(obj: any): boolean {
148156
return Array.isArray(obj);
149157
}
150158

151-
export function isNumber(obj): boolean {
152-
return typeof obj === 'number';
153-
}
154-
155159
export function isDate(obj): boolean {
156160
return obj instanceof Date && !isNaN(obj.valueOf());
157161
}

0 commit comments

Comments
 (0)