X Tutup
Skip to content

Commit 3b60503

Browse files
committed
feat(transformers): changes transformers to collect information about providers and resolve identifiers during linking
1 parent 3c2473b commit 3b60503

Some content is hidden

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

46 files changed

+2466
-971
lines changed

modules/angular2/src/compiler/directive_metadata.ts

Lines changed: 93 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,69 @@ 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,
5859
moduleUrl?: string,
5960
prefix?: string,
60-
constConstructor?: boolean
61+
constConstructor?: boolean,
62+
value?: any
6163
} = {}) {
6264
this.runtime = runtime;
6365
this.name = name;
6466
this.prefix = prefix;
6567
this.moduleUrl = moduleUrl;
6668
this.constConstructor = constConstructor;
69+
this.value = value;
6770
}
6871

6972
static fromJson(data: {[key: string]: any}): CompileIdentifierMetadata {
73+
let value = isArray(data['value']) ? arrayFromJson(data['value'], metadataFromJson) :
74+
objFromJson(data['value'], metadataFromJson);
7075
return new CompileIdentifierMetadata({
7176
name: data['name'],
7277
prefix: data['prefix'],
7378
moduleUrl: data['moduleUrl'],
74-
constConstructor: data['constConstructor']
79+
constConstructor: data['constConstructor'],
80+
value: value
7581
});
7682
}
7783

7884
toJson(): {[key: string]: any} {
85+
let value = isArray(this.value) ? arrayToJson(this.value) : objToJson(this.value);
7986
return {
8087
// Note: Runtime type can't be serialized...
8188
'class': 'Identifier',
8289
'name': this.name,
8390
'moduleUrl': this.moduleUrl,
8491
'prefix': this.prefix,
85-
'constConstructor': this.constConstructor
92+
'constConstructor': this.constConstructor,
93+
'value': value
8694
};
8795
}
8896

@@ -177,44 +185,78 @@ export class CompileProviderMetadata {
177185
static fromJson(data: {[key: string]: any}): CompileProviderMetadata {
178186
return new CompileProviderMetadata({
179187
token: objFromJson(data['token'], CompileIdentifierMetadata.fromJson),
180-
useClass: objFromJson(data['useClass'], CompileTypeMetadata.fromJson)
188+
useClass: objFromJson(data['useClass'], CompileTypeMetadata.fromJson),
189+
useExisting: objFromJson(data['useExisting'], CompileIdentifierMetadata.fromJson),
190+
useValue: objFromJson(data['useValue'], CompileIdentifierMetadata.fromJson),
191+
useFactory: objFromJson(data['useFactory'], CompileFactoryMetadata.fromJson)
181192
});
182193
}
183194

184195
toJson(): {[key: string]: any} {
185196
return {
186197
// Note: Runtime type can't be serialized...
198+
'class': 'Provider',
187199
'token': objToJson(this.token),
188-
'useClass': objToJson(this.useClass)
200+
'useClass': objToJson(this.useClass),
201+
'useExisting': objToJson(this.useExisting),
202+
'useValue': objToJson(this.useValue),
203+
'useFactory': objToJson(this.useFactory)
189204
};
190205
}
191206
}
192207

193-
export class CompileFactoryMetadata implements CompileIdentifierMetadata {
208+
export class CompileFactoryMetadata implements CompileIdentifierMetadata,
209+
CompileMetadataWithIdentifier {
194210
runtime: Function;
195211
name: string;
196212
prefix: string;
197213
moduleUrl: string;
198214
constConstructor: boolean;
215+
value: any;
199216
diDeps: CompileDiDependencyMetadata[];
200217

201-
constructor({runtime, name, moduleUrl, constConstructor, diDeps}: {
218+
constructor({runtime, name, moduleUrl, prefix, constConstructor, diDeps, value}: {
202219
runtime?: Function,
203220
name?: string,
221+
prefix?: string,
204222
moduleUrl?: string,
205223
constConstructor?: boolean,
224+
value?: boolean,
206225
diDeps?: CompileDiDependencyMetadata[]
207226
}) {
208227
this.runtime = runtime;
209228
this.name = name;
229+
this.prefix = prefix;
210230
this.moduleUrl = moduleUrl;
211231
this.diDeps = diDeps;
212232
this.constConstructor = constConstructor;
233+
this.value = value;
213234
}
214235

215236
get identifier(): CompileIdentifierMetadata { return this; }
216237

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

220262
/**
@@ -227,15 +269,17 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
227269
moduleUrl: string;
228270
isHost: boolean;
229271
constConstructor: boolean;
272+
value: any;
230273
diDeps: CompileDiDependencyMetadata[];
231274

232-
constructor({runtime, name, moduleUrl, prefix, isHost, constConstructor, diDeps}: {
275+
constructor({runtime, name, moduleUrl, prefix, isHost, constConstructor, value, diDeps}: {
233276
runtime?: Type,
234277
name?: string,
235278
moduleUrl?: string,
236279
prefix?: string,
237280
isHost?: boolean,
238281
constConstructor?: boolean,
282+
value?: any,
239283
diDeps?: CompileDiDependencyMetadata[]
240284
} = {}) {
241285
this.runtime = runtime;
@@ -244,6 +288,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
244288
this.prefix = prefix;
245289
this.isHost = normalizeBool(isHost);
246290
this.constConstructor = constConstructor;
291+
this.value = value;
247292
this.diDeps = normalizeBlank(diDeps);
248293
}
249294

@@ -254,6 +299,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
254299
prefix: data['prefix'],
255300
isHost: data['isHost'],
256301
constConstructor: data['constConstructor'],
302+
value: data['value'],
257303
diDeps: arrayFromJson(data['diDeps'], CompileDiDependencyMetadata.fromJson)
258304
});
259305
}
@@ -270,6 +316,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
270316
'prefix': this.prefix,
271317
'isHost': this.isHost,
272318
'constConstructor': this.constConstructor,
319+
'value': this.value,
273320
'diDeps': arrayToJson(this.diDeps)
274321
};
275322
}
@@ -382,8 +429,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
382429
outputs?: string[],
383430
host?: {[key: string]: string},
384431
lifecycleHooks?: LifecycleHooks[],
385-
providers?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
386-
viewProviders?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
432+
providers?:
433+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
434+
viewProviders?:
435+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
387436
queries?: CompileQueryMetadata[],
388437
viewQueries?: CompileQueryMetadata[],
389438
template?: CompileTemplateMetadata
@@ -474,8 +523,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
474523
hostProperties?: {[key: string]: string},
475524
hostAttributes?: {[key: string]: string},
476525
lifecycleHooks?: LifecycleHooks[],
477-
providers?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
478-
viewProviders?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
526+
providers?:
527+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
528+
viewProviders?:
529+
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
479530
queries?: CompileQueryMetadata[],
480531
viewQueries?: CompileQueryMetadata[],
481532
template?: CompileTemplateMetadata
@@ -494,8 +545,8 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
494545
this.lifecycleHooks = lifecycleHooks;
495546
this.providers = normalizeBlank(providers);
496547
this.viewProviders = normalizeBlank(viewProviders);
497-
this.queries = queries;
498-
this.viewQueries = viewQueries;
548+
this.queries = normalizeBlank(queries);
549+
this.viewQueries = normalizeBlank(viewQueries);
499550
this.template = template;
500551
}
501552

@@ -520,7 +571,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
520571
(<any[]>data['lifecycleHooks']).map(hookValue => LIFECYCLE_HOOKS_VALUES[hookValue]),
521572
template: isPresent(data['template']) ? CompileTemplateMetadata.fromJson(data['template']) :
522573
data['template'],
523-
providers: arrayFromJson(data['providers'], CompileProviderMetadata.fromJson)
574+
providers: arrayFromJson(data['providers'], metadataFromJson),
575+
viewProviders: arrayFromJson(data['viewProviders'], metadataFromJson),
576+
queries: arrayFromJson(data['queries'], CompileQueryMetadata.fromJson),
577+
viewQueries: arrayFromJson(data['viewQueries'], CompileQueryMetadata.fromJson)
524578
});
525579
}
526580

@@ -541,7 +595,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
541595
'hostAttributes': this.hostAttributes,
542596
'lifecycleHooks': this.lifecycleHooks.map(hook => serializeEnum(hook)),
543597
'template': isPresent(this.template) ? this.template.toJson() : this.template,
544-
'providers': arrayToJson(this.providers)
598+
'providers': arrayToJson(this.providers),
599+
'viewProviders': arrayToJson(this.viewProviders),
600+
'queries': arrayToJson(this.queries),
601+
'viewQueries': arrayToJson(this.viewQueries)
545602
};
546603
}
547604
}
@@ -611,7 +668,9 @@ var _COMPILE_METADATA_FROM_JSON = {
611668
'Directive': CompileDirectiveMetadata.fromJson,
612669
'Pipe': CompilePipeMetadata.fromJson,
613670
'Type': CompileTypeMetadata.fromJson,
614-
'Identifier': CompileIdentifierMetadata.fromJson
671+
'Provider': CompileProviderMetadata.fromJson,
672+
'Identifier': CompileIdentifierMetadata.fromJson,
673+
'Factory': CompileFactoryMetadata.fromJson
615674
};
616675

617676
function arrayFromJson(obj: any[], fn: (a: {[key: string]: any}) => any): any {
@@ -623,9 +682,13 @@ function arrayToJson(obj: any[]): string | {[key: string]: any} {
623682
}
624683

625684
function objFromJson(obj: any, fn: (a: {[key: string]: any}) => any): any {
626-
return (isString(obj) || isBlank(obj)) ? obj : fn(obj);
685+
if (isArray(obj)) return arrayFromJson(obj, fn);
686+
if (isString(obj) || isBlank(obj) || isBoolean(obj) || isNumber(obj)) return obj;
687+
return fn(obj);
627688
}
628689

629690
function objToJson(obj: any): string | {[key: string]: any} {
630-
return (isString(obj) || isBlank(obj)) ? obj : obj.toJson();
691+
if (isArray(obj)) return arrayToJson(obj);
692+
if (isString(obj) || isBlank(obj) || isBoolean(obj) || isNumber(obj)) return obj;
693+
return obj.toJson();
631694
}

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)
X Tutup