refactor: remove facade/collection (#14837)

This commit is contained in:
Miško Hevery
2017-03-01 14:10:59 -08:00
committed by Chuck Jazdzewski
parent 4fe0b90948
commit b0e0839075
27 changed files with 149 additions and 244 deletions

View File

@ -6,9 +6,8 @@
* found in the LICENSE file at https://angular.io/license
*/
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompileProviderMetadata, componentFactoryName, createHostComponentMeta, identifierName} from '../compile_metadata';
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompileProviderMetadata, componentFactoryName, createHostComponentMeta, flatten, identifierName} from '../compile_metadata';
import {CompilerConfig} from '../config';
import {ListWrapper} from '../facade/collection';
import {Identifiers, createIdentifier, createIdentifierToken} from '../identifiers';
import {CompileMetadataResolver} from '../metadata_resolver';
import {NgModuleCompiler} from '../ng_module_compiler';
@ -51,7 +50,7 @@ export class AotCompiler {
file => this._compileSrcFile(
file.srcUrl, ngModuleByPipeOrDirective, file.directives, file.pipes,
file.ngModules, file.injectables));
return ListWrapper.flatten(sourceModules);
return flatten(sourceModules);
});
}

View File

@ -7,9 +7,7 @@
*/
import {ChangeDetectionStrategy, ComponentFactory, RendererTypeV2, SchemaMetadata, Type, ViewEncapsulation, ɵLifecycleHooks, ɵreflector} from '@angular/core';
import {StaticSymbol} from './aot/static_symbol';
import {ListWrapper} from './facade/collection';
import {isPresent, stringify} from './facade/lang';
import {CssSelector} from './selector';
import {splitAtColon} from './util';
@ -272,7 +270,7 @@ export class CompileTemplateMetadata {
this.styles = _normalizeArray(styles);
this.styleUrls = _normalizeArray(styleUrls);
this.externalStylesheets = _normalizeArray(externalStylesheets);
this.animations = animations ? ListWrapper.flatten(animations) : [];
this.animations = animations ? flatten(animations) : [];
this.ngContentSelectors = ngContentSelectors || [];
if (interpolation && interpolation.length != 2) {
throw new Error(`'interpolation' should have a start and an end symbol.`);
@ -735,3 +733,10 @@ export class ProviderMeta {
this.multi = !!multi;
}
}
export function flatten<T>(list: Array<T|T[]>): T[] {
return list.reduce((flat: any[], item: T | T[]): T[] => {
const flatItem = Array.isArray(item) ? flatten(item) : item;
return (<T[]>flat).concat(flatItem);
}, []);
}

View File

@ -6,13 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Component, Directive, HostBinding, HostListener, Input, Output, Query, Type, resolveForwardRef, ɵReflectorReader, ɵreflector} from '@angular/core';
import {Component, Directive, HostBinding, HostListener, Input, Output, Query, Type, resolveForwardRef, ɵReflectorReader, ɵmerge as merge, ɵreflector} from '@angular/core';
import {ListWrapper, StringMapWrapper} from './facade/collection';
import {stringify} from './facade/lang';
import {CompilerInjectable} from './injectable';
import {splitAtColon} from './util';
/*
* Resolve a `Type` for {@link Directive}.
*
@ -35,7 +35,7 @@ export class DirectiveResolver {
resolve(type: Type<any>, throwIfNotFound = true): Directive {
const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
if (typeMetadata) {
const metadata = ListWrapper.findLast(typeMetadata, isDirectiveMetadata);
const metadata = findLast(typeMetadata, isDirectiveMetadata);
if (metadata) {
const propertyMetadata = this._reflector.propMetadata(type);
return this._mergeWithPropertyMetadata(metadata, propertyMetadata, type);
@ -58,7 +58,7 @@ export class DirectiveResolver {
const queries: {[key: string]: any} = {};
Object.keys(propertyMetadata).forEach((propName: string) => {
const input = ListWrapper.findLast(propertyMetadata[propName], (a) => a instanceof Input);
const input = findLast(propertyMetadata[propName], (a) => a instanceof Input);
if (input) {
if (input.bindingPropertyName) {
inputs.push(`${propName}: ${input.bindingPropertyName}`);
@ -66,7 +66,7 @@ export class DirectiveResolver {
inputs.push(propName);
}
}
const output = ListWrapper.findLast(propertyMetadata[propName], (a) => a instanceof Output);
const output = findLast(propertyMetadata[propName], (a) => a instanceof Output);
if (output) {
if (output.bindingPropertyName) {
outputs.push(`${propName}: ${output.bindingPropertyName}`);
@ -94,7 +94,7 @@ export class DirectiveResolver {
const args = hostListener.args || [];
host[`(${hostListener.eventName})`] = `${propName}(${args.join(',')})`;
});
const query = ListWrapper.findLast(propertyMetadata[propName], (a) => a instanceof Query);
const query = findLast(propertyMetadata[propName], (a) => a instanceof Query);
if (query) {
queries[propName] = query;
}
@ -126,9 +126,8 @@ export class DirectiveResolver {
this._dedupeBindings(directive.inputs ? directive.inputs.concat(inputs) : inputs);
const mergedOutputs =
this._dedupeBindings(directive.outputs ? directive.outputs.concat(outputs) : outputs);
const mergedHost = directive.host ? StringMapWrapper.merge(directive.host, host) : host;
const mergedQueries =
directive.queries ? StringMapWrapper.merge(directive.queries, queries) : queries;
const mergedHost = directive.host ? merge(directive.host, host) : host;
const mergedQueries = directive.queries ? merge(directive.queries, queries) : queries;
if (directive instanceof Component) {
return new Component({
@ -168,3 +167,12 @@ export class DirectiveResolver {
function isDirectiveMetadata(type: any): type is Directive {
return type instanceof Directive;
}
export function findLast<T>(arr: T[], condition: (value: T) => boolean): T {
for (let i = arr.length - 1; i >= 0; i--) {
if (condition(arr[i])) {
return arr[i];
}
}
return null;
}

View File

@ -7,8 +7,7 @@
*/
import {NgModule, Type, ɵReflectorReader, ɵreflector} from '@angular/core';
import {ListWrapper} from './facade/collection';
import {findLast} from './directive_resolver';
import {stringify} from './facade/lang';
import {CompilerInjectable} from './injectable';
@ -26,8 +25,7 @@ export class NgModuleResolver {
isNgModule(type: any) { return this._reflector.annotations(type).some(_isNgModuleMetadata); }
resolve(type: Type<any>, throwIfNotFound = true): NgModule {
const ngModuleMeta: NgModule =
ListWrapper.findLast(this._reflector.annotations(type), _isNgModuleMetadata);
const ngModuleMeta: NgModule = findLast(this._reflector.annotations(type), _isNgModuleMetadata);
if (ngModuleMeta) {
return ngModuleMeta;

View File

@ -7,8 +7,7 @@
*/
import {Pipe, Type, resolveForwardRef, ɵReflectorReader, ɵreflector} from '@angular/core';
import {ListWrapper} from './facade/collection';
import {findLast} from './directive_resolver';
import {stringify} from './facade/lang';
import {CompilerInjectable} from './injectable';
@ -38,7 +37,7 @@ export class PipeResolver {
resolve(type: Type<any>, throwIfNotFound = true): Pipe {
const metas = this._reflector.annotations(resolveForwardRef(type));
if (metas) {
const annotation = ListWrapper.findLast(metas, _isPipeMetadata);
const annotation = findLast(metas, _isPipeMetadata);
if (annotation) {
return annotation;
}