refactor(compiler): misc minor refactor / cleanup (#19189)

PR Close #19189
This commit is contained in:
Victor Berchet 2017-08-16 16:29:39 -07:00 committed by Igor Minar
parent 473a577e34
commit 34fdb91899
6 changed files with 46 additions and 96 deletions

View File

@ -81,7 +81,7 @@ export function createAotCompiler(compilerHost: AotCompilerHost, options: AotCom
new DirectiveResolver(staticReflector), new PipeResolver(staticReflector), summaryResolver, new DirectiveResolver(staticReflector), new PipeResolver(staticReflector), summaryResolver,
elementSchemaRegistry, normalizer, console, symbolCache, staticReflector); elementSchemaRegistry, normalizer, console, symbolCache, staticReflector);
// TODO(vicb): do not pass options.i18nFormat here // TODO(vicb): do not pass options.i18nFormat here
const viewCompiler = new ViewCompiler(config, staticReflector, elementSchemaRegistry); const viewCompiler = new ViewCompiler(staticReflector);
const typeCheckCompiler = new TypeCheckCompiler(options, staticReflector); const typeCheckCompiler = new TypeCheckCompiler(options, staticReflector);
const compiler = new AotCompiler( const compiler = new AotCompiler(
config, compilerHost, staticReflector, resolver, tmplParser, new StyleCompiler(urlResolver), config, compilerHost, staticReflector, resolver, tmplParser, new StyleCompiler(urlResolver),

View File

@ -6,9 +6,8 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {CompileIdentifierMetadata, CompileTokenMetadata} from './compile_metadata'; import {CompileTokenMetadata} from './compile_metadata';
import {CompileReflector} from './compile_reflector'; import {CompileReflector} from './compile_reflector';
import {Attribute, Component, Directive, HostBinding, HostListener, Inject, Input, NgModule, Output, Pipe, Query} from './core';
import * as o from './output/output_ast'; import * as o from './output/output_ast';
const CORE = '@angular/core'; const CORE = '@angular/core';

View File

@ -6,11 +6,10 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {CompileDiDependencyMetadata, CompileDirectiveMetadata, CompilePipeSummary, CompileProviderMetadata, CompileTokenMetadata, CompileTypeMetadata, rendererTypeName, tokenReference, viewClassName} from '../compile_metadata'; import {CompileDirectiveMetadata, CompilePipeSummary, rendererTypeName, tokenReference, viewClassName} from '../compile_metadata';
import {CompileReflector} from '../compile_reflector'; import {CompileReflector} from '../compile_reflector';
import {BuiltinConverter, EventHandlerVars, LocalResolver, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins} from '../compiler_util/expression_converter'; import {BuiltinConverter, EventHandlerVars, LocalResolver, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins} from '../compiler_util/expression_converter';
import {CompilerConfig} from '../config'; import {ArgumentType, BindingFlags, ChangeDetectionStrategy, NodeFlags, QueryBindingType, QueryValueType, ViewFlags} from '../core';
import {ArgumentType, BindingFlags, ChangeDetectionStrategy, DepFlags, NodeFlags, QueryBindingType, QueryValueType, ViewFlags} from '../core';
import {AST, ASTWithSource, Interpolation} from '../expression_parser/ast'; import {AST, ASTWithSource, Interpolation} from '../expression_parser/ast';
import {Identifiers} from '../identifiers'; import {Identifiers} from '../identifiers';
import {LifecycleHooks} from '../lifecycle_reflector'; import {LifecycleHooks} from '../lifecycle_reflector';
@ -18,8 +17,7 @@ import {isNgContainer} from '../ml_parser/tags';
import * as o from '../output/output_ast'; import * as o from '../output/output_ast';
import {convertValueToOutputAst} from '../output/value_util'; import {convertValueToOutputAst} from '../output/value_util';
import {ParseSourceSpan} from '../parse_util'; import {ParseSourceSpan} from '../parse_util';
import {ElementSchemaRegistry} from '../schema/element_schema_registry'; import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, PropertyBindingType, ProviderAst, QueryMatch, ReferenceAst, TemplateAst, TemplateAstVisitor, TextAst, VariableAst, templateVisitAll} from '../template_parser/template_ast';
import {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, PropertyBindingType, ProviderAst, ProviderAstType, QueryMatch, ReferenceAst, TemplateAst, TemplateAstVisitor, TextAst, VariableAst, templateVisitAll} from '../template_parser/template_ast';
import {OutputContext} from '../util'; import {OutputContext} from '../util';
import {componentFactoryResolverProviderDef, depDef, lifecycleHookToNodeFlag, providerDef} from './provider_compiler'; import {componentFactoryResolverProviderDef, depDef, lifecycleHookToNodeFlag, providerDef} from './provider_compiler';
@ -33,9 +31,7 @@ export class ViewCompileResult {
} }
export class ViewCompiler { export class ViewCompiler {
constructor( constructor(private _reflector: CompileReflector) {}
private _config: CompilerConfig, private _reflector: CompileReflector,
private _schemaRegistry: ElementSchemaRegistry) {}
compileComponent( compileComponent(
outputCtx: OutputContext, component: CompileDirectiveMetadata, template: TemplateAst[], outputCtx: OutputContext, component: CompileDirectiveMetadata, template: TemplateAst[],
@ -684,7 +680,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
return null; return null;
} }
createLiteralArrayConverter(sourceSpan: ParseSourceSpan, argCount: number): BuiltinConverter { private _createLiteralArrayConverter(sourceSpan: ParseSourceSpan, argCount: number):
BuiltinConverter {
if (argCount === 0) { if (argCount === 0) {
const valueExpr = o.importExpr(Identifiers.EMPTY_ARRAY); const valueExpr = o.importExpr(Identifiers.EMPTY_ARRAY);
return () => valueExpr; return () => valueExpr;
@ -701,8 +698,8 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
return (args: o.Expression[]) => callCheckStmt(nodeIndex, args); return (args: o.Expression[]) => callCheckStmt(nodeIndex, args);
} }
createLiteralMapConverter(sourceSpan: ParseSourceSpan, keys: {key: string, quoted: boolean}[]): private _createLiteralMapConverter(
BuiltinConverter { sourceSpan: ParseSourceSpan, keys: {key: string, quoted: boolean}[]): BuiltinConverter {
if (keys.length === 0) { if (keys.length === 0) {
const valueExpr = o.importExpr(Identifiers.EMPTY_MAP); const valueExpr = o.importExpr(Identifiers.EMPTY_MAP);
return () => valueExpr; return () => valueExpr;
@ -720,7 +717,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
return (args: o.Expression[]) => callCheckStmt(nodeIndex, args); return (args: o.Expression[]) => callCheckStmt(nodeIndex, args);
} }
createPipeConverter(expression: UpdateExpression, name: string, argCount: number): private _createPipeConverter(expression: UpdateExpression, name: string, argCount: number):
BuiltinConverter { BuiltinConverter {
const pipe = this.usedPipes.find((pipeSummary) => pipeSummary.name === name) !; const pipe = this.usedPipes.find((pipeSummary) => pipeSummary.name === name) !;
if (pipe.pure) { if (pipe.pure) {
@ -781,7 +778,13 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
return nodeIndex; return nodeIndex;
} }
// Attention: This might create new nodeDefs (for pipes and literal arrays and literal maps)! /**
* For the AST in `UpdateExpression.value`:
* - create nodes for pipes, literal arrays and, literal maps,
* - update the AST to replace pipes, literal arrays and, literal maps with calls to check fn.
*
* WARNING: This might create new nodeDefs (for pipes and literal arrays and literal maps)!
*/
private _preprocessUpdateExpression(expression: UpdateExpression): UpdateExpression { private _preprocessUpdateExpression(expression: UpdateExpression): UpdateExpression {
return { return {
nodeIndex: expression.nodeIndex, nodeIndex: expression.nodeIndex,
@ -790,13 +793,13 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
context: expression.context, context: expression.context,
value: convertPropertyBindingBuiltins( value: convertPropertyBindingBuiltins(
{ {
createLiteralArrayConverter: (argCount: number) => this.createLiteralArrayConverter( createLiteralArrayConverter: (argCount: number) => this._createLiteralArrayConverter(
expression.sourceSpan, argCount), expression.sourceSpan, argCount),
createLiteralMapConverter: createLiteralMapConverter:
(keys: {key: string, quoted: boolean}[]) => (keys: {key: string, quoted: boolean}[]) =>
this.createLiteralMapConverter(expression.sourceSpan, keys), this._createLiteralMapConverter(expression.sourceSpan, keys),
createPipeConverter: (name: string, argCount: number) => createPipeConverter: (name: string, argCount: number) =>
this.createPipeConverter(expression, name, argCount) this._createPipeConverter(expression, name, argCount)
}, },
expression.value) expression.value)
}; };

View File

@ -7,21 +7,21 @@
*/ */
import {BindingDef, BindingFlags, NodeDef, NodeFlags, TextData, ViewData, asTextData} from './types'; import {BindingDef, BindingFlags, NodeDef, NodeFlags, TextData, ViewData, asTextData} from './types';
import {calcBindingFlags, checkAndUpdateBinding, getParentRenderElement} from './util'; import {checkAndUpdateBinding, getParentRenderElement} from './util';
export function textDef(ngContentIndex: number, constants: string[]): NodeDef { export function textDef(ngContentIndex: number, staticText: string[]): NodeDef {
const bindings: BindingDef[] = new Array(constants.length - 1); const bindings: BindingDef[] = new Array(staticText.length - 1);
for (let i = 1; i < constants.length; i++) { for (let i = 1; i < staticText.length; i++) {
bindings[i - 1] = { bindings[i - 1] = {
flags: BindingFlags.TypeProperty, flags: BindingFlags.TypeProperty,
name: null, name: null,
ns: null, ns: null,
nonMinifiedName: null, nonMinifiedName: null,
securityContext: null, securityContext: null,
suffix: constants[i] suffix: staticText[i],
}; };
} }
const flags = NodeFlags.TypeText;
return { return {
// will bet set by the view definition // will bet set by the view definition
index: -1, index: -1,
@ -30,7 +30,7 @@ export function textDef(ngContentIndex: number, constants: string[]): NodeDef {
bindingIndex: -1, bindingIndex: -1,
outputIndex: -1, outputIndex: -1,
// regular values // regular values
flags, flags: NodeFlags.TypeText,
childFlags: 0, childFlags: 0,
directChildFlags: 0, directChildFlags: 0,
childMatchedQueries: 0, childMatchedQueries: 0,
@ -38,13 +38,13 @@ export function textDef(ngContentIndex: number, constants: string[]): NodeDef {
matchedQueryIds: 0, matchedQueryIds: 0,
references: {}, ngContentIndex, references: {}, ngContentIndex,
childCount: 0, bindings, childCount: 0, bindings,
bindingFlags: calcBindingFlags(bindings), bindingFlags: BindingFlags.TypeProperty,
outputs: [], outputs: [],
element: null, element: null,
provider: null, provider: null,
text: {prefix: constants[0]}, text: {prefix: staticText[0]},
query: null, query: null,
ngContent: null ngContent: null,
}; };
} }

View File

@ -137,7 +137,7 @@ export const COMPILER_PROVIDERS = <StaticProvider[]>[
[Optional, ERROR_COLLECTOR_TOKEN]]}, [Optional, ERROR_COLLECTOR_TOKEN]]},
DEFAULT_PACKAGE_URL_PROVIDER, DEFAULT_PACKAGE_URL_PROVIDER,
{ provide: StyleCompiler, deps: [UrlResolver]}, { provide: StyleCompiler, deps: [UrlResolver]},
{ provide: ViewCompiler, deps: [CompilerConfig, CompileReflector, ElementSchemaRegistry]}, { provide: ViewCompiler, deps: [CompileReflector]},
{ provide: NgModuleCompiler, deps: [CompileReflector] }, { provide: NgModuleCompiler, deps: [CompileReflector] },
{ provide: CompilerConfig, useValue: new CompilerConfig()}, { provide: CompilerConfig, useValue: new CompilerConfig()},
{ provide: Compiler, useClass: CompilerImpl, deps: [Injector, CompileMetadataResolver, { provide: Compiler, useClass: CompilerImpl, deps: [Injector, CompileMetadataResolver,

View File

@ -7,7 +7,7 @@
*/ */
import {CompileReflector, ExternalReference, Identifiers, getUrlScheme, syntaxError} from '@angular/compiler'; import {CompileReflector, ExternalReference, Identifiers, getUrlScheme, syntaxError} from '@angular/compiler';
import {ANALYZE_FOR_ENTRY_COMPONENTS, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, ElementRef, Inject, InjectionToken, Injector, LOCALE_ID, NgModuleFactory, NgModuleRef, Optional, PACKAGE_ROOT_URL, PlatformRef, QueryList, Renderer, SecurityContext, StaticProvider, TRANSLATIONS, TRANSLATIONS_FORMAT, TemplateRef, Type, ViewContainerRef, ViewEncapsulation, createPlatformFactory, isDevMode, platformCore, ɵCodegenComponentFactoryResolver, ɵConsole as Console, ɵEMPTY_ARRAY, ɵEMPTY_MAP, ɵReflectionCapabilities as ReflectionCapabilities, ɵand, ɵccf, ɵcmf, ɵcrt, ɵdid, ɵeld, ɵinlineInterpolate, ɵinterpolate, ɵmod, ɵmpd, ɵncd, ɵnov, ɵpad, ɵpid, ɵpod, ɵppd, ɵprd, ɵqud, ɵregisterModuleFactory, ɵstringify as stringify, ɵted, ɵunv, ɵvid} from '@angular/core'; import {ANALYZE_FOR_ENTRY_COMPONENTS, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, ElementRef, Injector, LOCALE_ID, NgModuleFactory, NgModuleRef, QueryList, Renderer, SecurityContext, TRANSLATIONS_FORMAT, TemplateRef, ViewContainerRef, ViewEncapsulation, ɵCodegenComponentFactoryResolver, ɵEMPTY_ARRAY, ɵEMPTY_MAP, ɵReflectionCapabilities as ReflectionCapabilities, ɵand, ɵccf, ɵcmf, ɵcrt, ɵdid, ɵeld, ɵinlineInterpolate, ɵinterpolate, ɵmod, ɵmpd, ɵncd, ɵnov, ɵpad, ɵpid, ɵpod, ɵppd, ɵprd, ɵqud, ɵregisterModuleFactory, ɵstringify as stringify, ɵted, ɵunv, ɵvid} from '@angular/core';
export const MODULE_SUFFIX = ''; export const MODULE_SUFFIX = '';
const builtinExternalReferences = createBuiltinExternalReferencesMap(); const builtinExternalReferences = createBuiltinExternalReferencesMap();
@ -50,81 +50,29 @@ export class JitReflector implements CompileReflector {
function createBuiltinExternalReferencesMap() { function createBuiltinExternalReferencesMap() {
const map = new Map<ExternalReference, any>(); const map = new Map<ExternalReference, any>();
map.set( map.set(Identifiers.ANALYZE_FOR_ENTRY_COMPONENTS, ANALYZE_FOR_ENTRY_COMPONENTS);
Identifiers.ANALYZE_FOR_ENTRY_COMPONENTS,
ANALYZE_FOR_ENTRY_COMPONENTS);
map.set(Identifiers.ElementRef, ElementRef); map.set(Identifiers.ElementRef, ElementRef);
map.set(Identifiers.NgModuleRef, NgModuleRef); map.set(Identifiers.NgModuleRef, NgModuleRef);
map.set(Identifiers.ViewContainerRef, ViewContainerRef); map.set(Identifiers.ViewContainerRef, ViewContainerRef);
map.set( map.set(Identifiers.ChangeDetectorRef, ChangeDetectorRef);
Identifiers.ChangeDetectorRef,
ChangeDetectorRef);
map.set(Identifiers.QueryList, QueryList); map.set(Identifiers.QueryList, QueryList);
map.set(Identifiers.TemplateRef, TemplateRef); map.set(Identifiers.TemplateRef, TemplateRef);
map.set( map.set(Identifiers.CodegenComponentFactoryResolver, ɵCodegenComponentFactoryResolver);
Identifiers.CodegenComponentFactoryResolver, map.set(Identifiers.ComponentFactoryResolver, ComponentFactoryResolver);
ɵCodegenComponentFactoryResolver);
map.set(
Identifiers.ComponentFactoryResolver,
ComponentFactoryResolver);
map.set(Identifiers.ComponentFactory, ComponentFactory); map.set(Identifiers.ComponentFactory, ComponentFactory);
map.set(Identifiers.ComponentRef, ComponentRef); map.set(Identifiers.ComponentRef, ComponentRef);
map.set(Identifiers.NgModuleFactory, NgModuleFactory); map.set(Identifiers.NgModuleFactory, NgModuleFactory);
map.set( map.set(Identifiers.createModuleFactory, ɵcmf);
Identifiers.createModuleFactory, map.set(Identifiers.moduleDef, ɵmod);
map.set(Identifiers.moduleProviderDef, ɵmpd);
map.set(Identifiers.RegisterModuleFactoryFn, ɵregisterModuleFactory);
ɵcmf, );
map.set(
Identifiers.moduleDef,
ɵmod, );
map.set(
Identifiers.moduleProviderDef,
ɵmpd, );
map.set(
Identifiers.RegisterModuleFactoryFn,
ɵregisterModuleFactory, );
map.set(Identifiers.Injector, Injector); map.set(Identifiers.Injector, Injector);
map.set( map.set(Identifiers.ViewEncapsulation, ViewEncapsulation);
Identifiers.ViewEncapsulation, map.set(Identifiers.ChangeDetectionStrategy, ChangeDetectionStrategy);
map.set(Identifiers.SecurityContext, SecurityContext);
ViewEncapsulation);
map.set(
Identifiers.ChangeDetectionStrategy,
ChangeDetectionStrategy);
map.set(
Identifiers.SecurityContext,
SecurityContext, );
map.set(Identifiers.LOCALE_ID, LOCALE_ID); map.set(Identifiers.LOCALE_ID, LOCALE_ID);
map.set( map.set(Identifiers.TRANSLATIONS_FORMAT, TRANSLATIONS_FORMAT);
Identifiers.TRANSLATIONS_FORMAT, map.set(Identifiers.inlineInterpolate, ɵinlineInterpolate);
TRANSLATIONS_FORMAT);
map.set(
Identifiers.inlineInterpolate,
ɵinlineInterpolate);
map.set(Identifiers.interpolate, ɵinterpolate); map.set(Identifiers.interpolate, ɵinterpolate);
map.set(Identifiers.EMPTY_ARRAY, ɵEMPTY_ARRAY); map.set(Identifiers.EMPTY_ARRAY, ɵEMPTY_ARRAY);
map.set(Identifiers.EMPTY_MAP, ɵEMPTY_MAP); map.set(Identifiers.EMPTY_MAP, ɵEMPTY_MAP);