feat(transformers): changes transformers to collect information about providers and resolve identifiers during linking
This commit is contained in:
@ -1,13 +1,16 @@
|
||||
import {
|
||||
isPresent,
|
||||
isBlank,
|
||||
isNumber,
|
||||
isBoolean,
|
||||
normalizeBool,
|
||||
normalizeBlank,
|
||||
serializeEnum,
|
||||
Type,
|
||||
isString,
|
||||
RegExpWrapper,
|
||||
StringWrapper
|
||||
StringWrapper,
|
||||
isArray
|
||||
} from 'angular2/src/facade/lang';
|
||||
import {unimplemented} from 'angular2/src/facade/exceptions';
|
||||
import {StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
@ -25,20 +28,12 @@ import {LifecycleHooks, LIFECYCLE_HOOKS_VALUES} from 'angular2/src/core/linker/i
|
||||
var HOST_REG_EXP = /^(?:(?:\[([^\]]+)\])|(?:\(([^\)]+)\)))$/g;
|
||||
|
||||
export abstract class CompileMetadataWithIdentifier {
|
||||
static fromJson(data: {[key: string]: any}): CompileMetadataWithIdentifier {
|
||||
return _COMPILE_METADATA_FROM_JSON[data['class']](data);
|
||||
}
|
||||
|
||||
abstract toJson(): {[key: string]: any};
|
||||
|
||||
get identifier(): CompileIdentifierMetadata { return <CompileIdentifierMetadata>unimplemented(); }
|
||||
}
|
||||
|
||||
export abstract class CompileMetadataWithType extends CompileMetadataWithIdentifier {
|
||||
static fromJson(data: {[key: string]: any}): CompileMetadataWithType {
|
||||
return _COMPILE_METADATA_FROM_JSON[data['class']](data);
|
||||
}
|
||||
|
||||
abstract toJson(): {[key: string]: any};
|
||||
|
||||
get type(): CompileTypeMetadata { return <CompileTypeMetadata>unimplemented(); }
|
||||
@ -46,43 +41,56 @@ export abstract class CompileMetadataWithType extends CompileMetadataWithIdentif
|
||||
get identifier(): CompileIdentifierMetadata { return <CompileIdentifierMetadata>unimplemented(); }
|
||||
}
|
||||
|
||||
export function metadataFromJson(data: {[key: string]: any}): any {
|
||||
return _COMPILE_METADATA_FROM_JSON[data['class']](data);
|
||||
}
|
||||
|
||||
export class CompileIdentifierMetadata implements CompileMetadataWithIdentifier {
|
||||
runtime: any;
|
||||
name: string;
|
||||
prefix: string;
|
||||
moduleUrl: string;
|
||||
constConstructor: boolean;
|
||||
constructor({runtime, name, moduleUrl, prefix, constConstructor}: {
|
||||
value: any;
|
||||
|
||||
constructor({runtime, name, moduleUrl, prefix, constConstructor, value}: {
|
||||
runtime?: any,
|
||||
name?: string,
|
||||
moduleUrl?: string,
|
||||
prefix?: string,
|
||||
constConstructor?: boolean
|
||||
constConstructor?: boolean,
|
||||
value?: any
|
||||
} = {}) {
|
||||
this.runtime = runtime;
|
||||
this.name = name;
|
||||
this.prefix = prefix;
|
||||
this.moduleUrl = moduleUrl;
|
||||
this.constConstructor = constConstructor;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
static fromJson(data: {[key: string]: any}): CompileIdentifierMetadata {
|
||||
let value = isArray(data['value']) ? arrayFromJson(data['value'], metadataFromJson) :
|
||||
objFromJson(data['value'], metadataFromJson);
|
||||
return new CompileIdentifierMetadata({
|
||||
name: data['name'],
|
||||
prefix: data['prefix'],
|
||||
moduleUrl: data['moduleUrl'],
|
||||
constConstructor: data['constConstructor']
|
||||
constConstructor: data['constConstructor'],
|
||||
value: value
|
||||
});
|
||||
}
|
||||
|
||||
toJson(): {[key: string]: any} {
|
||||
let value = isArray(this.value) ? arrayToJson(this.value) : objToJson(this.value);
|
||||
return {
|
||||
// Note: Runtime type can't be serialized...
|
||||
'class': 'Identifier',
|
||||
'name': this.name,
|
||||
'moduleUrl': this.moduleUrl,
|
||||
'prefix': this.prefix,
|
||||
'constConstructor': this.constConstructor
|
||||
'constConstructor': this.constConstructor,
|
||||
'value': value
|
||||
};
|
||||
}
|
||||
|
||||
@ -177,44 +185,78 @@ export class CompileProviderMetadata {
|
||||
static fromJson(data: {[key: string]: any}): CompileProviderMetadata {
|
||||
return new CompileProviderMetadata({
|
||||
token: objFromJson(data['token'], CompileIdentifierMetadata.fromJson),
|
||||
useClass: objFromJson(data['useClass'], CompileTypeMetadata.fromJson)
|
||||
useClass: objFromJson(data['useClass'], CompileTypeMetadata.fromJson),
|
||||
useExisting: objFromJson(data['useExisting'], CompileIdentifierMetadata.fromJson),
|
||||
useValue: objFromJson(data['useValue'], CompileIdentifierMetadata.fromJson),
|
||||
useFactory: objFromJson(data['useFactory'], CompileFactoryMetadata.fromJson)
|
||||
});
|
||||
}
|
||||
|
||||
toJson(): {[key: string]: any} {
|
||||
return {
|
||||
// Note: Runtime type can't be serialized...
|
||||
'class': 'Provider',
|
||||
'token': objToJson(this.token),
|
||||
'useClass': objToJson(this.useClass)
|
||||
'useClass': objToJson(this.useClass),
|
||||
'useExisting': objToJson(this.useExisting),
|
||||
'useValue': objToJson(this.useValue),
|
||||
'useFactory': objToJson(this.useFactory)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class CompileFactoryMetadata implements CompileIdentifierMetadata {
|
||||
export class CompileFactoryMetadata implements CompileIdentifierMetadata,
|
||||
CompileMetadataWithIdentifier {
|
||||
runtime: Function;
|
||||
name: string;
|
||||
prefix: string;
|
||||
moduleUrl: string;
|
||||
constConstructor: boolean;
|
||||
value: any;
|
||||
diDeps: CompileDiDependencyMetadata[];
|
||||
|
||||
constructor({runtime, name, moduleUrl, constConstructor, diDeps}: {
|
||||
constructor({runtime, name, moduleUrl, prefix, constConstructor, diDeps, value}: {
|
||||
runtime?: Function,
|
||||
name?: string,
|
||||
prefix?: string,
|
||||
moduleUrl?: string,
|
||||
constConstructor?: boolean,
|
||||
value?: boolean,
|
||||
diDeps?: CompileDiDependencyMetadata[]
|
||||
}) {
|
||||
this.runtime = runtime;
|
||||
this.name = name;
|
||||
this.prefix = prefix;
|
||||
this.moduleUrl = moduleUrl;
|
||||
this.diDeps = diDeps;
|
||||
this.constConstructor = constConstructor;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
get identifier(): CompileIdentifierMetadata { return this; }
|
||||
|
||||
toJson(): {[key: string]: any} { return null; }
|
||||
static fromJson(data: {[key: string]: any}): CompileFactoryMetadata {
|
||||
return new CompileFactoryMetadata({
|
||||
name: data['name'],
|
||||
prefix: data['prefix'],
|
||||
moduleUrl: data['moduleUrl'],
|
||||
constConstructor: data['constConstructor'],
|
||||
value: data['value'],
|
||||
diDeps: arrayFromJson(data['diDeps'], CompileDiDependencyMetadata.fromJson)
|
||||
});
|
||||
}
|
||||
|
||||
toJson(): {[key: string]: any} {
|
||||
return {
|
||||
'class': 'Factory',
|
||||
'name': this.name,
|
||||
'prefix': this.prefix,
|
||||
'moduleUrl': this.moduleUrl,
|
||||
'constConstructor': this.constConstructor,
|
||||
'value': this.value,
|
||||
'diDeps': arrayToJson(this.diDeps)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -227,15 +269,17 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
|
||||
moduleUrl: string;
|
||||
isHost: boolean;
|
||||
constConstructor: boolean;
|
||||
value: any;
|
||||
diDeps: CompileDiDependencyMetadata[];
|
||||
|
||||
constructor({runtime, name, moduleUrl, prefix, isHost, constConstructor, diDeps}: {
|
||||
constructor({runtime, name, moduleUrl, prefix, isHost, constConstructor, value, diDeps}: {
|
||||
runtime?: Type,
|
||||
name?: string,
|
||||
moduleUrl?: string,
|
||||
prefix?: string,
|
||||
isHost?: boolean,
|
||||
constConstructor?: boolean,
|
||||
value?: any,
|
||||
diDeps?: CompileDiDependencyMetadata[]
|
||||
} = {}) {
|
||||
this.runtime = runtime;
|
||||
@ -244,6 +288,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
|
||||
this.prefix = prefix;
|
||||
this.isHost = normalizeBool(isHost);
|
||||
this.constConstructor = constConstructor;
|
||||
this.value = value;
|
||||
this.diDeps = normalizeBlank(diDeps);
|
||||
}
|
||||
|
||||
@ -254,6 +299,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
|
||||
prefix: data['prefix'],
|
||||
isHost: data['isHost'],
|
||||
constConstructor: data['constConstructor'],
|
||||
value: data['value'],
|
||||
diDeps: arrayFromJson(data['diDeps'], CompileDiDependencyMetadata.fromJson)
|
||||
});
|
||||
}
|
||||
@ -270,6 +316,7 @@ export class CompileTypeMetadata implements CompileIdentifierMetadata, CompileMe
|
||||
'prefix': this.prefix,
|
||||
'isHost': this.isHost,
|
||||
'constConstructor': this.constConstructor,
|
||||
'value': this.value,
|
||||
'diDeps': arrayToJson(this.diDeps)
|
||||
};
|
||||
}
|
||||
@ -382,8 +429,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
|
||||
outputs?: string[],
|
||||
host?: {[key: string]: string},
|
||||
lifecycleHooks?: LifecycleHooks[],
|
||||
providers?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
|
||||
viewProviders?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
|
||||
providers?:
|
||||
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
|
||||
viewProviders?:
|
||||
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
|
||||
queries?: CompileQueryMetadata[],
|
||||
viewQueries?: CompileQueryMetadata[],
|
||||
template?: CompileTemplateMetadata
|
||||
@ -474,8 +523,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
|
||||
hostProperties?: {[key: string]: string},
|
||||
hostAttributes?: {[key: string]: string},
|
||||
lifecycleHooks?: LifecycleHooks[],
|
||||
providers?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
|
||||
viewProviders?: Array<CompileProviderMetadata | CompileTypeMetadata | any[]>,
|
||||
providers?:
|
||||
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
|
||||
viewProviders?:
|
||||
Array<CompileProviderMetadata | CompileTypeMetadata | CompileIdentifierMetadata | any[]>,
|
||||
queries?: CompileQueryMetadata[],
|
||||
viewQueries?: CompileQueryMetadata[],
|
||||
template?: CompileTemplateMetadata
|
||||
@ -494,8 +545,8 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
|
||||
this.lifecycleHooks = lifecycleHooks;
|
||||
this.providers = normalizeBlank(providers);
|
||||
this.viewProviders = normalizeBlank(viewProviders);
|
||||
this.queries = queries;
|
||||
this.viewQueries = viewQueries;
|
||||
this.queries = normalizeBlank(queries);
|
||||
this.viewQueries = normalizeBlank(viewQueries);
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@ -520,7 +571,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
|
||||
(<any[]>data['lifecycleHooks']).map(hookValue => LIFECYCLE_HOOKS_VALUES[hookValue]),
|
||||
template: isPresent(data['template']) ? CompileTemplateMetadata.fromJson(data['template']) :
|
||||
data['template'],
|
||||
providers: arrayFromJson(data['providers'], CompileProviderMetadata.fromJson)
|
||||
providers: arrayFromJson(data['providers'], metadataFromJson),
|
||||
viewProviders: arrayFromJson(data['viewProviders'], metadataFromJson),
|
||||
queries: arrayFromJson(data['queries'], CompileQueryMetadata.fromJson),
|
||||
viewQueries: arrayFromJson(data['viewQueries'], CompileQueryMetadata.fromJson)
|
||||
});
|
||||
}
|
||||
|
||||
@ -541,7 +595,10 @@ export class CompileDirectiveMetadata implements CompileMetadataWithType {
|
||||
'hostAttributes': this.hostAttributes,
|
||||
'lifecycleHooks': this.lifecycleHooks.map(hook => serializeEnum(hook)),
|
||||
'template': isPresent(this.template) ? this.template.toJson() : this.template,
|
||||
'providers': arrayToJson(this.providers)
|
||||
'providers': arrayToJson(this.providers),
|
||||
'viewProviders': arrayToJson(this.viewProviders),
|
||||
'queries': arrayToJson(this.queries),
|
||||
'viewQueries': arrayToJson(this.viewQueries)
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -611,7 +668,9 @@ var _COMPILE_METADATA_FROM_JSON = {
|
||||
'Directive': CompileDirectiveMetadata.fromJson,
|
||||
'Pipe': CompilePipeMetadata.fromJson,
|
||||
'Type': CompileTypeMetadata.fromJson,
|
||||
'Identifier': CompileIdentifierMetadata.fromJson
|
||||
'Provider': CompileProviderMetadata.fromJson,
|
||||
'Identifier': CompileIdentifierMetadata.fromJson,
|
||||
'Factory': CompileFactoryMetadata.fromJson
|
||||
};
|
||||
|
||||
function arrayFromJson(obj: any[], fn: (a: {[key: string]: any}) => any): any {
|
||||
@ -623,9 +682,13 @@ function arrayToJson(obj: any[]): string | {[key: string]: any} {
|
||||
}
|
||||
|
||||
function objFromJson(obj: any, fn: (a: {[key: string]: any}) => any): any {
|
||||
return (isString(obj) || isBlank(obj)) ? obj : fn(obj);
|
||||
if (isArray(obj)) return arrayFromJson(obj, fn);
|
||||
if (isString(obj) || isBlank(obj) || isBoolean(obj) || isNumber(obj)) return obj;
|
||||
return fn(obj);
|
||||
}
|
||||
|
||||
function objToJson(obj: any): string | {[key: string]: any} {
|
||||
return (isString(obj) || isBlank(obj)) ? obj : obj.toJson();
|
||||
if (isArray(obj)) return arrayToJson(obj);
|
||||
if (isString(obj) || isBlank(obj) || isBoolean(obj) || isNumber(obj)) return obj;
|
||||
return obj.toJson();
|
||||
}
|
||||
|
@ -110,6 +110,9 @@ export class TemplateCompiler {
|
||||
hostAttributes: directive.hostAttributes,
|
||||
lifecycleHooks: directive.lifecycleHooks,
|
||||
providers: directive.providers,
|
||||
viewProviders: directive.viewProviders,
|
||||
queries: directive.queries,
|
||||
viewQueries: directive.viewQueries,
|
||||
template: normalizedTemplate
|
||||
}));
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ bool isStringMap(Object obj) => obj is Map;
|
||||
bool isArray(Object obj) => obj is List;
|
||||
bool isPromise(Object obj) => obj is Future;
|
||||
bool isNumber(Object obj) => obj is num;
|
||||
bool isBoolean(Object obj) => obj is bool;
|
||||
bool isDate(Object obj) => obj is DateTime;
|
||||
|
||||
String stringify(obj) {
|
||||
|
@ -124,6 +124,14 @@ export function isBlank(obj: any): boolean {
|
||||
return obj === undefined || obj === null;
|
||||
}
|
||||
|
||||
export function isBoolean(obj: any): boolean {
|
||||
return typeof obj === "boolean";
|
||||
}
|
||||
|
||||
export function isNumber(obj: any): boolean {
|
||||
return typeof obj === "number";
|
||||
}
|
||||
|
||||
export function isString(obj: any): boolean {
|
||||
return typeof obj === "string";
|
||||
}
|
||||
@ -148,10 +156,6 @@ export function isArray(obj: any): boolean {
|
||||
return Array.isArray(obj);
|
||||
}
|
||||
|
||||
export function isNumber(obj): boolean {
|
||||
return typeof obj === 'number';
|
||||
}
|
||||
|
||||
export function isDate(obj): boolean {
|
||||
return obj instanceof Date && !isNaN(obj.valueOf());
|
||||
}
|
||||
|
@ -18,7 +18,9 @@ import {
|
||||
CompileTemplateMetadata,
|
||||
CompileProviderMetadata,
|
||||
CompileDiDependencyMetadata,
|
||||
CompileQueryMetadata
|
||||
CompileQueryMetadata,
|
||||
CompileIdentifierMetadata,
|
||||
CompileFactoryMetadata
|
||||
} from 'angular2/src/compiler/directive_metadata';
|
||||
import {ViewEncapsulation} from 'angular2/src/core/metadata/view';
|
||||
import {ChangeDetectionStrategy} from 'angular2/src/core/change_detection';
|
||||
@ -31,25 +33,21 @@ export function main() {
|
||||
var fullDirectiveMeta: CompileDirectiveMetadata;
|
||||
|
||||
beforeEach(() => {
|
||||
fullTypeMeta = new CompileTypeMetadata({
|
||||
name: 'SomeType',
|
||||
moduleUrl: 'someUrl',
|
||||
var diDep = new CompileDiDependencyMetadata({
|
||||
isAttribute: true,
|
||||
isSelf: true,
|
||||
isHost: true,
|
||||
diDeps: [
|
||||
new CompileDiDependencyMetadata({
|
||||
isAttribute: true,
|
||||
isSelf: true,
|
||||
isHost: true,
|
||||
isSkipSelf: true,
|
||||
isOptional: true,
|
||||
token: 'someToken',
|
||||
query: new CompileQueryMetadata(
|
||||
{selectors: ['one'], descendants: true, first: true, propertyName: 'one'}),
|
||||
viewQuery: new CompileQueryMetadata(
|
||||
{selectors: ['one'], descendants: true, first: true, propertyName: 'one'})
|
||||
})
|
||||
]
|
||||
isSkipSelf: true,
|
||||
isOptional: true,
|
||||
token: 'someToken',
|
||||
query: new CompileQueryMetadata(
|
||||
{selectors: ['one'], descendants: true, first: true, propertyName: 'one'}),
|
||||
viewQuery: new CompileQueryMetadata(
|
||||
{selectors: ['one'], descendants: true, first: true, propertyName: 'one'})
|
||||
});
|
||||
|
||||
fullTypeMeta = new CompileTypeMetadata(
|
||||
{name: 'SomeType', moduleUrl: 'someUrl', isHost: true, diDeps: [diDep]});
|
||||
fullTemplateMeta = new CompileTemplateMetadata({
|
||||
encapsulation: ViewEncapsulation.Emulated,
|
||||
template: '<a></a>',
|
||||
@ -69,11 +67,49 @@ export function main() {
|
||||
outputs: ['someEvent'],
|
||||
host: {'(event1)': 'handler1', '[prop1]': 'expr1', 'attr1': 'attrValue2'},
|
||||
lifecycleHooks: [LifecycleHooks.OnChanges],
|
||||
providers: [new CompileProviderMetadata({token: 'token', useClass: fullTypeMeta})]
|
||||
providers: [
|
||||
new CompileProviderMetadata({
|
||||
token: 'token',
|
||||
useClass: fullTypeMeta,
|
||||
useExisting: new CompileIdentifierMetadata({name: 'someName'}),
|
||||
useFactory: new CompileFactoryMetadata({name: 'someName', diDeps: [diDep]}),
|
||||
useValue: 'someValue',
|
||||
})
|
||||
],
|
||||
viewProviders: [
|
||||
new CompileProviderMetadata({
|
||||
token: 'token',
|
||||
useClass: fullTypeMeta,
|
||||
useExisting: new CompileIdentifierMetadata({name: 'someName'}),
|
||||
useFactory: new CompileFactoryMetadata({name: 'someName', diDeps: [diDep]}),
|
||||
useValue: 'someValue',
|
||||
})
|
||||
],
|
||||
queries: [
|
||||
new CompileQueryMetadata(
|
||||
{selectors: ['selector'], descendants: true, first: false, propertyName: 'prop'})
|
||||
],
|
||||
viewQueries: [
|
||||
new CompileQueryMetadata(
|
||||
{selectors: ['selector'], descendants: true, first: false, propertyName: 'prop'})
|
||||
]
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('CompileIdentifierMetadata', () => {
|
||||
it('should serialize with full data', () => {
|
||||
let full = new CompileIdentifierMetadata(
|
||||
{name: 'name', moduleUrl: 'module', constConstructor: true, value: ['one', ['two']]});
|
||||
expect(CompileIdentifierMetadata.fromJson(full.toJson())).toEqual(full);
|
||||
});
|
||||
|
||||
it('should serialize with no data', () => {
|
||||
let empty = new CompileIdentifierMetadata();
|
||||
expect(CompileIdentifierMetadata.fromJson(empty.toJson())).toEqual(empty);
|
||||
});
|
||||
});
|
||||
|
||||
describe('DirectiveMetadata', () => {
|
||||
it('should serialize with full data', () => {
|
||||
expect(CompileDirectiveMetadata.fromJson(fullDirectiveMeta.toJson()))
|
||||
|
Reference in New Issue
Block a user