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( constructor(
private _ngModuleResolver: NgModuleResolver, private _directiveResolver: DirectiveResolver, private _ngModuleResolver: NgModuleResolver, private _directiveResolver: DirectiveResolver,
private _pipeResolver: PipeResolver, private _config: CompilerConfig, 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 { private sanitizeTokenName(token: any): string {
let identifier = stringify(token); let identifier = stringify(token);

View File

@ -15,14 +15,14 @@ import {ChangeDetectionStrategy} from '../src/change_detection/change_detection'
import {AnimationEntryMetadata} from './animation/metadata'; import {AnimationEntryMetadata} from './animation/metadata';
import {AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di'; import {AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di';
import {ComponentMetadata, DirectiveMetadata, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata} from './metadata/directives'; import {ComponentMetadata, ComponentMetadataType, DirectiveMetadata, DirectiveMetadataType, HostBindingMetadata, HostListenerMetadata, InputMetadata, OutputMetadata, PipeMetadata, PipeMetadataType} from './metadata/directives';
import {ModuleWithProviders, NgModuleMetadata, SchemaMetadata} from './metadata/ng_module'; import {ModuleWithProviders, NgModuleMetadata, NgModuleMetadataType, SchemaMetadata} from './metadata/ng_module';
import {ViewEncapsulation} from './metadata/view'; import {ViewEncapsulation} from './metadata/view';
export {ANALYZE_FOR_ENTRY_COMPONENTS, AttributeMetadata, ContentChildMetadata, ContentChildrenMetadata, QueryMetadata, ViewChildMetadata, ViewChildrenMetadata, ViewQueryMetadata} from './metadata/di'; 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 {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'; export {ViewEncapsulation, ViewMetadata} from './metadata/view';
import {makeDecorator, makeParamDecorator, makePropDecorator, TypeDecorator,} from './util/decorators'; import {makeDecorator, makeParamDecorator, makePropDecorator, TypeDecorator,} from './util/decorators';
@ -90,28 +90,8 @@ export interface NgModuleDecorator extends TypeDecorator {}
* @stable * @stable
*/ */
export interface DirectiveMetadataFactory { export interface DirectiveMetadataFactory {
(obj: { (obj: DirectiveMetadataType): DirectiveDecorator;
selector?: string, new (obj: DirectiveMetadataType): DirectiveMetadata;
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;
} }
/** /**
@ -148,54 +128,8 @@ export interface DirectiveMetadataFactory {
* @stable * @stable
*/ */
export interface ComponentMetadataFactory { export interface ComponentMetadataFactory {
(obj: { (obj: ComponentMetadataType): ComponentDecorator;
selector?: string, new (obj: ComponentMetadataType): ComponentMetadata;
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;
} }
/** /**
@ -337,8 +271,8 @@ export interface ViewChildMetadataFactory {
* @stable * @stable
*/ */
export interface PipeMetadataFactory { export interface PipeMetadataFactory {
(obj: {name: string, pure?: boolean}): any; (obj: PipeMetadataType): any;
new (obj: {name: string, pure?: boolean}): any; new (obj: PipeMetadataType): any;
} }
/** /**
@ -387,22 +321,8 @@ export interface HostListenerMetadataFactory {
* @experimental * @experimental
*/ */
export interface NgModuleMetadataFactory { export interface NgModuleMetadataFactory {
(obj?: { (obj?: NgModuleMetadataType): NgModuleDecorator;
providers?: any[], new (obj?: NgModuleMetadataType): NgModuleMetadata;
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;
} }
// TODO(alexeagle): remove the duplication of this doc. It is copied from ComponentMetadata. // 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'; 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. * Directives allow you to attach behavior to elements in the DOM.
@ -396,7 +411,7 @@ import {ViewEncapsulation} from './view';
* @ts2dart_const * @ts2dart_const
* @stable * @stable
*/ */
export class DirectiveMetadata extends InjectableMetadata { export class DirectiveMetadata extends InjectableMetadata implements DirectiveMetadataType {
/** /**
* The CSS selector that triggers the instantiation of a directive. * The CSS selector that triggers the instantiation of a directive.
* *
@ -749,17 +764,9 @@ export class DirectiveMetadata extends InjectableMetadata {
*/ */
queries: {[key: string]: any}; queries: {[key: string]: any};
constructor({selector, inputs, outputs, properties, events, host, providers, exportAs, queries}: { constructor(
selector?: string, {selector, inputs, outputs, properties, events, host, providers, exportAs,
inputs?: string[], queries}: DirectiveMetadataType = {}) {
outputs?: string[],
/** @deprecated */ properties?: string[],
/** @deprecated */ events?: string[],
host?: {[key: string]: string},
providers?: any[],
exportAs?: string,
queries?: {[key: string]: any}
} = {}) {
super(); super();
this.selector = selector; this.selector = selector;
this._inputs = inputs; 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. * Declare reusable UI building blocks for an application.
* *
@ -800,7 +827,7 @@ export class DirectiveMetadata extends InjectableMetadata {
* @ts2dart_const * @ts2dart_const
* @stable * @stable
*/ */
export class ComponentMetadata extends DirectiveMetadata { export class ComponentMetadata extends DirectiveMetadata implements ComponentMetadataType {
/** /**
* Defines the used change detection strategy. * Defines the used change detection strategy.
* *
@ -999,36 +1026,28 @@ export class ComponentMetadata extends DirectiveMetadata {
*/ */
entryComponents: Array<Type|any[]>; entryComponents: Array<Type|any[]>;
constructor( constructor({selector,
{selector, inputs, outputs, properties, events, host, exportAs, moduleId, providers, inputs,
viewProviders, changeDetection = ChangeDetectionStrategy.Default, queries, templateUrl, outputs,
template, styleUrls, styles, animations, directives, pipes, encapsulation, interpolation, properties,
/** @deprecated use entryComponents instead! */ events,
precompile, entryComponents}: { host,
selector?: string, exportAs,
inputs?: string[], moduleId,
outputs?: string[], providers,
/** @deprecated */ properties?: string[], viewProviders,
/** @deprecated */ events?: string[], changeDetection = ChangeDetectionStrategy.Default,
host?: {[key: string]: string}, queries,
providers?: any[], templateUrl,
exportAs?: string, template,
moduleId?: string, styleUrls,
viewProviders?: any[], styles,
queries?: {[key: string]: any}, animations,
changeDetection?: ChangeDetectionStrategy, directives,
templateUrl?: string, pipes,
template?: string, encapsulation,
styleUrls?: string[], interpolation,
styles?: string[], entryComponents}: ComponentMetadataType = {}) {
animations?: AnimationEntryMetadata[],
directives?: Array<Type|any[]>,
pipes?: Array<Type|any[]>,
encapsulation?: ViewEncapsulation,
interpolation?: [string, string],
precompile?: Array<Type|any[]>,
entryComponents?: Array<Type|any[]>
} = {}) {
super({ super({
selector: selector, selector: selector,
inputs: inputs, inputs: inputs,
@ -1053,10 +1072,19 @@ export class ComponentMetadata extends DirectiveMetadata {
this.moduleId = moduleId; this.moduleId = moduleId;
this.animations = animations; this.animations = animations;
this.interpolation = interpolation; 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. * Declare reusable pipe function.
* *
@ -1070,12 +1098,12 @@ export class ComponentMetadata extends DirectiveMetadata {
* @ts2dart_const * @ts2dart_const
* @stable * @stable
*/ */
export class PipeMetadata extends InjectableMetadata { export class PipeMetadata extends InjectableMetadata implements PipeMetadataType {
name: string; name: string;
/** @internal */ /** @internal */
_pure: boolean; _pure: boolean;
constructor({name, pure}: {name: string, pure?: boolean}) { constructor({name, pure}: PipeMetadataType) {
super(); super();
this.name = name; this.name = name;
this._pure = pure; this._pure = pure;

View File

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