refactor(core): introduce interfaces for constructor arguments of decorators

For @Directive, @Component, @Pipe, @NgModule
This commit is contained in:
Tobias Bosch 2016-07-29 02:10:30 -07:00
parent 4ad6bcce54
commit 3dbc66c1ac
4 changed files with 105 additions and 148 deletions

View File

@ -37,7 +37,8 @@ export class CompileMetadataResolver {
constructor(
private _ngModuleResolver: NgModuleResolver, private _directiveResolver: DirectiveResolver,
private _pipeResolver: PipeResolver, private _config: CompilerConfig,
private _console: Console, private _schemaRegistry: ElementSchemaRegistry, private _reflector: ReflectorReader = reflector) {}
private _console: Console, private _schemaRegistry: ElementSchemaRegistry,
private _reflector: ReflectorReader = reflector) {}
private sanitizeTokenName(token: any): string {
let identifier = stringify(token);

View File

@ -15,14 +15,14 @@ import {ChangeDetectionStrategy} from '../src/change_detection/change_detection'
import {AnimationEntryMetadata} from './animation/metadata';
import {AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di';
import {ComponentMetadata, DirectiveMetadata, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata} from './metadata/directives';
import {ModuleWithProviders, NgModuleMetadata, SchemaMetadata} from './metadata/ng_module';
import {ComponentMetadata, ComponentMetadataType, DirectiveMetadata, DirectiveMetadataType, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata, PipeMetadataType} from './metadata/directives';
import {ModuleWithProviders, NgModuleMetadata, NgModuleMetadataType, SchemaMetadata} from './metadata/ng_module';
import {ViewEncapsulation} from './metadata/view';
export {ANALYZE_FOR_ENTRY_COMPONENTS, AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di';
export {ComponentMetadata, DirectiveMetadata, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata} from './metadata/directives';
export {ComponentMetadata, ComponentMetadataType, DirectiveMetadata, DirectiveMetadataType, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata, PipeMetadataType} from './metadata/directives';
export {AfterContentChecked, AfterContentInit, AfterViewChecked, AfterViewInit, DoCheck, OnChanges, OnDestroy, OnInit} from './metadata/lifecycle_hooks';
export {CUSTOM_ELEMENTS_SCHEMA, ModuleWithProviders, NgModuleMetadata, SchemaMetadata} from './metadata/ng_module';
export {CUSTOM_ELEMENTS_SCHEMA, ModuleWithProviders, NgModuleMetadata, NgModuleMetadataType, SchemaMetadata} from './metadata/ng_module';
export {ViewEncapsulation, ViewMetadata} from './metadata/view';
import {makeDecorator, makeParamDecorator, makePropDecorator, TypeDecorator,} from './util/decorators';
@ -90,28 +90,8 @@ export interface NgModuleDecorator extends TypeDecorator {}
* @stable
*/
export interface DirectiveMetadataFactory {
(obj: {
selector?: string,
inputs?: string[],
outputs?: string[],
properties?: string[],
events?: string[],
host?: {[key: string]: string},
providers?: any[],
exportAs?: string,
queries?: {[key: string]: any}
}): DirectiveDecorator;
new (obj: {
selector?: string,
inputs?: string[],
outputs?: string[],
properties?: string[],
events?: string[],
host?: {[key: string]: string},
providers?: any[],
exportAs?: string,
queries?: {[key: string]: any}
}): DirectiveMetadata;
(obj: DirectiveMetadataType): DirectiveDecorator;
new (obj: DirectiveMetadataType): DirectiveMetadata;
}
/**
@ -148,54 +128,8 @@ export interface DirectiveMetadataFactory {
* @stable
*/
export interface ComponentMetadataFactory {
(obj: {
selector?: string,
inputs?: string[],
outputs?: string[],
properties?: string[],
events?: string[],
host?: {[key: string]: string},
providers?: any[],
exportAs?: string,
moduleId?: string,
queries?: {[key: string]: any},
viewProviders?: any[],
changeDetection?: ChangeDetectionStrategy,
templateUrl?: string,
template?: string,
styleUrls?: string[],
styles?: string[],
animations?: AnimationEntryMetadata[],
directives?: Array<Type|any[]>,
pipes?: Array<Type|any[]>,
encapsulation?: ViewEncapsulation,
interpolation?: [string, string],
entryComponents?: Array<Type|any[]>
}): ComponentDecorator;
new (obj: {
selector?: string,
inputs?: string[],
outputs?: string[],
properties?: string[],
events?: string[],
host?: {[key: string]: string},
providers?: any[],
exportAs?: string,
moduleId?: string,
queries?: {[key: string]: any},
viewProviders?: any[],
changeDetection?: ChangeDetectionStrategy,
templateUrl?: string,
template?: string,
styleUrls?: string[],
styles?: string[],
animations?: AnimationEntryMetadata[],
directives?: Array<Type|any[]>,
pipes?: Array<Type|any[]>,
encapsulation?: ViewEncapsulation,
interpolation?: [string, string],
entryComponents?: Array<Type|any[]>
}): ComponentMetadata;
(obj: ComponentMetadataType): ComponentDecorator;
new (obj: ComponentMetadataType): ComponentMetadata;
}
/**
@ -337,8 +271,8 @@ export interface ViewChildMetadataFactory {
* @stable
*/
export interface PipeMetadataFactory {
(obj: {name: string, pure?: boolean}): any;
new (obj: {name: string, pure?: boolean}): any;
(obj: PipeMetadataType): any;
new (obj: PipeMetadataType): any;
}
/**
@ -387,22 +321,8 @@ export interface HostListenerMetadataFactory {
* @experimental
*/
export interface NgModuleMetadataFactory {
(obj?: {
providers?: any[],
declarations?: Array<Type|any[]>,
imports?: Array<Type|ModuleWithProviders|any[]>,
exports?: Array<Type|any[]>,
entryComponents?: Array<Type|any[]>,
schemas?: Array<SchemaMetadata|any[]>
}): NgModuleDecorator;
new (obj?: {
providers?: any[],
declarations?: Array<Type|any[]>,
imports?: Array<Type|any[]>,
exports?: Array<Type|any[]>,
entryComponents?: Array<Type|any[]>,
schemas?: Array<SchemaMetadata|any[]>
}): NgModuleMetadata;
(obj?: NgModuleMetadataType): NgModuleDecorator;
new (obj?: NgModuleMetadataType): NgModuleMetadata;
}
// TODO(alexeagle): remove the duplication of this doc. It is copied from ComponentMetadata.

View File

@ -13,6 +13,21 @@ import {Type, isPresent} from '../facade/lang';
import {ViewEncapsulation} from './view';
/**
* Interface for creating {@link DirectiveMetadata}
* @experimental
*/
export interface DirectiveMetadataType {
selector?: string;
properties?: string[];
inputs?: string[];
events?: string[];
outputs?: string[];
host?: {[key: string]: string};
providers?: any[];
exportAs?: string;
queries?: {[key: string]: any};
}
/**
* Directives allow you to attach behavior to elements in the DOM.
@ -396,7 +411,7 @@ import {ViewEncapsulation} from './view';
* @ts2dart_const
* @stable
*/
export class DirectiveMetadata extends InjectableMetadata {
export class DirectiveMetadata extends InjectableMetadata implements DirectiveMetadataType {
/**
* The CSS selector that triggers the instantiation of a directive.
*
@ -749,17 +764,9 @@ export class DirectiveMetadata extends InjectableMetadata {
*/
queries: {[key: string]: any};
constructor({selector, inputs, outputs, properties, events, host, providers, exportAs, queries}: {
selector?: string,
inputs?: string[],
outputs?: string[],
/** @deprecated */ properties?: string[],
/** @deprecated */ events?: string[],
host?: {[key: string]: string},
providers?: any[],
exportAs?: string,
queries?: {[key: string]: any}
} = {}) {
constructor(
{selector, inputs, outputs, properties, events, host, providers, exportAs,
queries}: DirectiveMetadataType = {}) {
super();
this.selector = selector;
this._inputs = inputs;
@ -773,6 +780,26 @@ export class DirectiveMetadata extends InjectableMetadata {
}
}
/**
* Interface for creating {@link ComponentMetadataType}
* @experimental
*/
export interface ComponentMetadataType extends DirectiveMetadataType {
changeDetection?: ChangeDetectionStrategy;
viewProviders?: any[];
moduleId?: string;
templateUrl?: string;
template?: string;
styleUrls?: string[];
styles?: string[];
animations?: AnimationEntryMetadata[];
directives?: Array<Type|any[]>;
pipes?: Array<Type|any[]>;
encapsulation?: ViewEncapsulation;
interpolation?: [string, string];
entryComponents?: Array<Type|any[]>;
}
/**
* Declare reusable UI building blocks for an application.
*
@ -800,7 +827,7 @@ export class DirectiveMetadata extends InjectableMetadata {
* @ts2dart_const
* @stable
*/
export class ComponentMetadata extends DirectiveMetadata {
export class ComponentMetadata extends DirectiveMetadata implements ComponentMetadataType {
/**
* Defines the used change detection strategy.
*
@ -999,36 +1026,28 @@ export class ComponentMetadata extends DirectiveMetadata {
*/
entryComponents: Array<Type|any[]>;
constructor(
{selector, inputs, outputs, properties, events, host, exportAs, moduleId, providers,
viewProviders, changeDetection = ChangeDetectionStrategy.Default, queries, templateUrl,
template, styleUrls, styles, animations, directives, pipes, encapsulation, interpolation,
/** @deprecated use entryComponents instead! */
precompile, entryComponents}: {
selector?: string,
inputs?: string[],
outputs?: string[],
/** @deprecated */ properties?: string[],
/** @deprecated */ events?: string[],
host?: {[key: string]: string},
providers?: any[],
exportAs?: string,
moduleId?: string,
viewProviders?: any[],
queries?: {[key: string]: any},
changeDetection?: ChangeDetectionStrategy,
templateUrl?: string,
template?: string,
styleUrls?: string[],
styles?: string[],
animations?: AnimationEntryMetadata[],
directives?: Array<Type|any[]>,
pipes?: Array<Type|any[]>,
encapsulation?: ViewEncapsulation,
interpolation?: [string, string],
precompile?: Array<Type|any[]>,
entryComponents?: Array<Type|any[]>
} = {}) {
constructor({selector,
inputs,
outputs,
properties,
events,
host,
exportAs,
moduleId,
providers,
viewProviders,
changeDetection = ChangeDetectionStrategy.Default,
queries,
templateUrl,
template,
styleUrls,
styles,
animations,
directives,
pipes,
encapsulation,
interpolation,
entryComponents}: ComponentMetadataType = {}) {
super({
selector: selector,
inputs: inputs,
@ -1053,10 +1072,19 @@ export class ComponentMetadata extends DirectiveMetadata {
this.moduleId = moduleId;
this.animations = animations;
this.interpolation = interpolation;
this.entryComponents = precompile ? precompile : entryComponents;
this.entryComponents = entryComponents;
}
}
/**
* Interface for creating {@link PipeMetadata}
* @experimental
*/
export interface PipeMetadataType {
name: string;
pure?: boolean;
}
/**
* Declare reusable pipe function.
*
@ -1070,12 +1098,12 @@ export class ComponentMetadata extends DirectiveMetadata {
* @ts2dart_const
* @stable
*/
export class PipeMetadata extends InjectableMetadata {
export class PipeMetadata extends InjectableMetadata implements PipeMetadataType {
name: string;
/** @internal */
_pure: boolean;
constructor({name, pure}: {name: string, pure?: boolean}) {
constructor({name, pure}: PipeMetadataType) {
super();
this.name = name;
this._pure = pure;

View File

@ -36,11 +36,24 @@ export const CUSTOM_ELEMENTS_SCHEMA: SchemaMetadata = {
name: 'custom-elements'
};
/**
* Interface for creating {@link NgModuleMetadata}
* @experimental
*/
export interface NgModuleMetadataType {
providers?: any[];
declarations?: Array<Type|any[]>;
imports?: Array<Type|ModuleWithProviders|any[]>;
exports?: Array<Type|any[]>;
entryComponents?: Array<Type|any[]>;
schemas?: Array<SchemaMetadata|any[]>;
}
/**
* Declares an Angular Module.
* @experimental
*/
export class NgModuleMetadata extends InjectableMetadata {
export class NgModuleMetadata extends InjectableMetadata implements NgModuleMetadataType {
/**
* Defines the set of injectable objects that are available in the injector
* of this module.
@ -133,14 +146,9 @@ export class NgModuleMetadata extends InjectableMetadata {
schemas: Array<SchemaMetadata|any[]>;
constructor({providers, declarations, imports, exports, entryComponents, schemas}: {
providers?: any[],
declarations?: Array<Type|any[]>,
imports?: Array<Type|any[]>,
exports?: Array<Type|any[]>,
entryComponents?: Array<Type|any[]>,
schemas?: Array<SchemaMetadata|any[]>
} = {}) {
constructor(
{providers, declarations, imports, exports, entryComponents,
schemas}: NgModuleMetadataType = {}) {
super();
this._providers = providers;
this.declarations = declarations;