X Tutup
Skip to content

Commit 176e559

Browse files
committed
refactor(compiler): support hash syntax for providers.
1 parent 365be6a commit 176e559

File tree

2 files changed

+121
-112
lines changed

2 files changed

+121
-112
lines changed

modules/angular2/src/compiler/static_reflector.ts

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isStringMap,
88
FunctionWrapper
99
} from 'angular2/src/facade/lang';
10+
import {BaseException} from 'angular2/src/facade/exceptions';
1011
import {
1112
AttributeMetadata,
1213
DirectiveMetadata,
@@ -32,8 +33,9 @@ import {
3233
InjectableMetadata,
3334
SelfMetadata,
3435
SkipSelfMetadata,
35-
InjectMetadata
36+
InjectMetadata,
3637
} from "angular2/src/core/di/metadata";
38+
import {OpaqueToken} from 'angular2/src/core/di/opaque_token';
3739

3840
export class ModuleContext {
3941
constructor(public moduleId: string, public filePath: string) {}
@@ -94,7 +96,7 @@ export class StaticReflector implements ReflectorReader {
9496
if (!isPresent(annotations)) {
9597
let classMetadata = this.getTypeMetadata(type);
9698
if (isPresent(classMetadata['decorators'])) {
97-
annotations = this.simplify(type, classMetadata['decorators'], false);
99+
annotations = this.simplify(type, classMetadata['decorators']);
98100
} else {
99101
annotations = [];
100102
}
@@ -111,7 +113,7 @@ export class StaticReflector implements ReflectorReader {
111113
propMetadata = mapStringMap(members, (propData, propName) => {
112114
let prop = (<any[]>propData).find(a => a['__symbolic'] == 'property');
113115
if (isPresent(prop) && isPresent(prop['decorators'])) {
114-
return this.simplify(type, prop['decorators'], false);
116+
return this.simplify(type, prop['decorators']);
115117
} else {
116118
return [];
117119
}
@@ -122,49 +124,53 @@ export class StaticReflector implements ReflectorReader {
122124
}
123125

124126
public parameters(type: StaticSymbol): any[] {
125-
let parameters = this.parameterCache.get(type);
126-
if (!isPresent(parameters)) {
127-
let classMetadata = this.getTypeMetadata(type);
128-
let members = isPresent(classMetadata) ? classMetadata['members'] : null;
129-
let ctorData = isPresent(members) ? members['__ctor__'] : null;
130-
if (isPresent(ctorData)) {
131-
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] == 'constructor');
132-
let parameterTypes = <any[]>this.simplify(type, ctor['parameters'], false);
133-
let parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators'], false);
134-
135-
parameters = [];
136-
ListWrapper.forEachWithIndex(parameterTypes, (paramType, index) => {
137-
let nestedResult = [];
138-
if (isPresent(paramType)) {
139-
nestedResult.push(paramType);
140-
}
141-
let decorators = isPresent(parameterDecorators) ? parameterDecorators[index] : null;
142-
if (isPresent(decorators)) {
143-
ListWrapper.addAll(nestedResult, decorators);
144-
}
145-
parameters.push(nestedResult);
146-
});
147-
}
127+
try {
128+
let parameters = this.parameterCache.get(type);
148129
if (!isPresent(parameters)) {
149-
parameters = [];
130+
let classMetadata = this.getTypeMetadata(type);
131+
let members = isPresent(classMetadata) ? classMetadata['members'] : null;
132+
let ctorData = isPresent(members) ? members['__ctor__'] : null;
133+
if (isPresent(ctorData)) {
134+
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] == 'constructor');
135+
let parameterTypes = <any[]>this.simplify(type, ctor['parameters']);
136+
let parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators']);
137+
138+
parameters = [];
139+
ListWrapper.forEachWithIndex(parameterTypes, (paramType, index) => {
140+
let nestedResult = [];
141+
if (isPresent(paramType)) {
142+
nestedResult.push(paramType);
143+
}
144+
let decorators = isPresent(parameterDecorators) ? parameterDecorators[index] : null;
145+
if (isPresent(decorators)) {
146+
ListWrapper.addAll(nestedResult, decorators);
147+
}
148+
parameters.push(nestedResult);
149+
});
150+
}
151+
if (!isPresent(parameters)) {
152+
parameters = [];
153+
}
154+
this.parameterCache.set(type, parameters);
150155
}
151-
this.parameterCache.set(type, parameters);
156+
return parameters;
157+
} catch (e) {
158+
console.error('Failed on type', type, 'with error', e);
159+
throw e;
152160
}
153-
return parameters;
154161
}
155162

156-
private registerDecoratorOrConstructor(type: StaticSymbol, ctor: any,
157-
crossModuleProps: any[] = []): void {
163+
private registerDecoratorOrConstructor(type: StaticSymbol, ctor: any): void {
158164
this.conversionMap.set(type, (moduleContext: ModuleContext, args: any[]) => {
159165
let argValues = [];
160166
ListWrapper.forEachWithIndex(args, (arg, index) => {
161167
let argValue;
162168
if (isStringMap(arg) && isBlank(arg['__symbolic'])) {
163169
argValue =
164170
mapStringMap(arg, (value, key) => this.simplify(
165-
moduleContext, value, crossModuleProps.indexOf(key) !== -1));
171+
moduleContext, value) );
166172
} else {
167-
argValue = this.simplify(moduleContext, arg, crossModuleProps.indexOf(index) !== -1);
173+
argValue = this.simplify(moduleContext, arg);
168174
}
169175
argValues.push(argValue);
170176
});
@@ -177,6 +183,7 @@ export class StaticReflector implements ReflectorReader {
177183
let diDecorators = 'angular2/src/core/di/decorators';
178184
let diMetadata = 'angular2/src/core/di/metadata';
179185
let provider = 'angular2/src/core/di/provider';
186+
let opaqueToken = 'angular2/src/core/di/opaque_token';
180187
this.registerDecoratorOrConstructor(this.host.findDeclaration(provider, 'Provider'), Provider);
181188

182189
this.registerDecoratorOrConstructor(this.host.findDeclaration(diDecorators, 'Host'),
@@ -216,10 +223,9 @@ export class StaticReflector implements ReflectorReader {
216223
this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'HostListener'),
217224
HostListenerMetadata);
218225
this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'Directive'),
219-
DirectiveMetadata, ['bindings', 'providers']);
226+
DirectiveMetadata);
220227
this.registerDecoratorOrConstructor(this.host.findDeclaration(coreDecorators, 'Component'),
221-
ComponentMetadata,
222-
['bindings', 'providers', 'directives', 'pipes']);
228+
ComponentMetadata);
223229

224230
// Note: Some metadata classes can be used directly with Provider.deps.
225231
this.registerDecoratorOrConstructor(this.host.findDeclaration(diMetadata, 'HostMetadata'),
@@ -230,10 +236,12 @@ export class StaticReflector implements ReflectorReader {
230236
SkipSelfMetadata);
231237
this.registerDecoratorOrConstructor(this.host.findDeclaration(diMetadata, 'OptionalMetadata'),
232238
OptionalMetadata);
239+
this.registerDecoratorOrConstructor(this.host.findDeclaration(opaqueToken, 'OpaqueToken'),
240+
OpaqueToken);
233241
}
234242

235243
/** @internal */
236-
public simplify(moduleContext: ModuleContext, value: any, crossModules: boolean): any {
244+
public simplify(moduleContext: ModuleContext, value: any): any {
237245
let _this = this;
238246

239247
function simplify(expression: any): any {
@@ -328,19 +336,17 @@ export class StaticReflector implements ReflectorReader {
328336
staticSymbol = _this.host.getStaticSymbol(
329337
moduleContext.moduleId, moduleContext.filePath, expression['name']);
330338
}
331-
let result;
332-
if (crossModules || isBlank(expression['module'])) {
333-
let moduleMetadata = _this.getModuleMetadata(staticSymbol.filePath);
334-
let declarationValue = moduleMetadata['metadata'][staticSymbol.name];
339+
let result = staticSymbol;
340+
let moduleMetadata = _this.getModuleMetadata(staticSymbol.filePath);
341+
let declarationValue = isPresent(moduleMetadata) ? moduleMetadata['metadata'][staticSymbol.name] : null;
342+
if (isPresent(declarationValue)) {
335343
if (isClassMetadata(declarationValue)) {
336344
result = staticSymbol;
337345
} else {
338346
let newModuleContext =
339347
new ModuleContext(staticSymbol.moduleId, staticSymbol.filePath);
340-
result = _this.simplify(newModuleContext, declarationValue, crossModules);
348+
result = _this.simplify(newModuleContext, declarationValue);
341349
}
342-
} else {
343-
result = staticSymbol;
344350
}
345351
return result;
346352
case "new":
@@ -349,11 +355,18 @@ export class StaticReflector implements ReflectorReader {
349355
staticSymbol = _this.host.findDeclaration(target['module'], target['name'],
350356
moduleContext.filePath);
351357
let converter = _this.conversionMap.get(staticSymbol);
352-
let args = expression['arguments'];
353-
if (isBlank(args)) {
354-
args = [];
358+
if (isBlank(converter)) {
359+
throw new BaseException(`Cannot convert call/new expression for ${target['name']} in ${moduleContext.filePath}`)
360+
}
361+
if (isPresent(converter)) {
362+
let args = expression['arguments'];
363+
if (isBlank(args)) {
364+
args = [];
365+
}
366+
return converter(moduleContext, args);
367+
} else {
368+
return staticSymbol;
355369
}
356-
return isPresent(converter) ? converter(moduleContext, args) : null;
357370
}
358371
return null;
359372
}

0 commit comments

Comments
 (0)
X Tutup