Skip to content

Commit 3e11422

Browse files
committed
refactor(core): support importUri in StaticReflector
Closes angular#8195
1 parent 6103aa0 commit 3e11422

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

modules/angular2/src/compiler/runtime_metadata.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,27 @@ import {
3939
SkipSelfMetadata
4040
} from 'angular2/src/core/di/metadata';
4141
import {AttributeMetadata} from 'angular2/src/core/metadata/di';
42+
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
4243

4344
@Injectable()
4445
export class RuntimeMetadataResolver {
4546
private _directiveCache = new Map<Type, cpl.CompileDirectiveMetadata>();
4647
private _pipeCache = new Map<Type, cpl.CompilePipeMetadata>();
4748
private _anonymousTypes = new Map<Object, number>();
4849
private _anonymousTypeIndex = 0;
50+
private _reflector: ReflectorReader;
4951

5052
constructor(private _directiveResolver: DirectiveResolver, private _pipeResolver: PipeResolver,
5153
private _viewResolver: ViewResolver,
5254
@Optional() @Inject(PLATFORM_DIRECTIVES) private _platformDirectives: Type[],
53-
@Optional() @Inject(PLATFORM_PIPES) private _platformPipes: Type[]) {}
55+
@Optional() @Inject(PLATFORM_PIPES) private _platformPipes: Type[],
56+
_reflector?: ReflectorReader) {
57+
if (isPresent(_reflector)) {
58+
this._reflector = _reflector;
59+
} else {
60+
this._reflector = reflector;
61+
}
62+
}
5463

5564
private sanitizeTokenName(token: any): string {
5665
let identifier = stringify(token);
@@ -78,7 +87,7 @@ export class RuntimeMetadataResolver {
7887
if (dirMeta instanceof md.ComponentMetadata) {
7988
assertArrayOfStrings('styles', dirMeta.styles);
8089
var cmpMeta = <md.ComponentMetadata>dirMeta;
81-
moduleUrl = calcModuleUrl(directiveType, cmpMeta);
90+
moduleUrl = calcModuleUrl(this._reflector, directiveType, cmpMeta);
8291
var viewMeta = this._viewResolver.resolve(directiveType);
8392
assertArrayOfStrings('styles', viewMeta.styles);
8493
templateMeta = new cpl.CompileTemplateMetadata({
@@ -148,7 +157,7 @@ export class RuntimeMetadataResolver {
148157
var meta = this._pipeCache.get(pipeType);
149158
if (isBlank(meta)) {
150159
var pipeMeta = this._pipeResolver.resolve(pipeType);
151-
var moduleUrl = reflector.importUri(pipeType);
160+
var moduleUrl = this._reflector.importUri(pipeType);
152161
meta = new cpl.CompilePipeMetadata({
153162
type: this.getTypeMetadata(pipeType, moduleUrl),
154163
name: pipeMeta.name,
@@ -341,7 +350,8 @@ function isValidType(value: Type): boolean {
341350
return isPresent(value) && (value instanceof Type);
342351
}
343352

344-
function calcModuleUrl(type: Type, cmpMetadata: md.ComponentMetadata): string {
353+
function calcModuleUrl(reflector: ReflectorReader, type: Type,
354+
cmpMetadata: md.ComponentMetadata): string {
345355
var moduleId = cmpMetadata.moduleId;
346356
if (isPresent(moduleId)) {
347357
var scheme = getUrlScheme(moduleId);

modules/angular2/src/compiler/static_reflector.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ViewQueryMetadata,
2626
QueryMetadata,
2727
} from 'angular2/src/core/metadata';
28+
import {ReflectorReader} from 'angular2/src/core/reflection/reflector_reader';
2829

2930
/**
3031
* The host of the static resolver is expected to be able to provide module metadata in the form of
@@ -56,15 +57,16 @@ export class StaticType {
5657
* A static reflector implements enough of the Reflector API that is necessary to compile
5758
* templates statically.
5859
*/
59-
export class StaticReflector {
60+
export class StaticReflector implements ReflectorReader {
6061
private typeCache = new Map<string, StaticType>();
6162
private annotationCache = new Map<StaticType, any[]>();
6263
private propertyCache = new Map<StaticType, {[key: string]: any}>();
6364
private parameterCache = new Map<StaticType, any[]>();
6465
private metadataCache = new Map<string, {[key: string]: any}>();
65-
6666
constructor(private host: StaticReflectorHost) { this.initializeConversionMap(); }
6767

68+
importUri(typeOrFunc: any): string { return (<StaticType>typeOrFunc).moduleId; }
69+
6870
/**
6971
* getStatictype produces a Type whose metadata is known but whose implementation is not loaded.
7072
* All types passed to the StaticResolver should be pseudo-types returned by this method.
@@ -438,7 +440,7 @@ export class StaticReflector {
438440
return simplify(value);
439441
}
440442

441-
private getModuleMetadata(module: string): {[key: string]: any} {
443+
public getModuleMetadata(module: string): {[key: string]: any} {
442444
let moduleMetadata = this.metadataCache.get(module);
443445
if (!isPresent(moduleMetadata)) {
444446
moduleMetadata = this.host.getMetadataFor(module);

modules/angular2/src/core/reflection/reflector_reader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export abstract class ReflectorReader {
66
abstract parameters(typeOrFunc: /*Type*/ any): any[][];
77
abstract annotations(typeOrFunc: /*Type*/ any): any[];
88
abstract propMetadata(typeOrFunc: /*Type*/ any): {[key: string]: any[]};
9-
}
9+
abstract importUri(typeOrFunc: /*Type*/ any): string;
10+
}

0 commit comments

Comments
 (0)