feat(compiler): type TemplateAst values as ASTWithSource (#35892)

TemplateAst values are currently typed as the base class AST, but they
are actually constructed with ASTWithSource. Type them as such, because
ASTWithSource gives more information about the consumed expression AST
to downstream customers (namely, the expression AST source).

Unblocks #35271

PR Close #35892
This commit is contained in:
Ayaz Hafiz 2020-03-05 19:41:47 -08:00 committed by Matias Niemelä
parent 0e2a577b42
commit 11f7e275e6
3 changed files with 18 additions and 15 deletions

View File

@ -691,7 +691,7 @@ export class ParsedEvent {
// Animation events have a phase // Animation events have a phase
constructor( constructor(
public name: string, public targetOrPhase: string, public type: ParsedEventType, public name: string, public targetOrPhase: string, public type: ParsedEventType,
public handler: AST, public sourceSpan: ParseSourceSpan, public handler: ASTWithSource, public sourceSpan: ParseSourceSpan,
public handlerSpan: ParseSourceSpan) {} public handlerSpan: ParseSourceSpan) {}
} }
@ -715,6 +715,6 @@ export const enum BindingType {
export class BoundElementProperty { export class BoundElementProperty {
constructor( constructor(
public name: string, public type: BindingType, public securityContext: SecurityContext, public name: string, public type: BindingType, public securityContext: SecurityContext,
public value: AST, public unit: string|null, public sourceSpan: ParseSourceSpan, public value: ASTWithSource, public unit: string|null, public sourceSpan: ParseSourceSpan,
public valueSpan?: ParseSourceSpan) {} public valueSpan?: ParseSourceSpan) {}
} }

View File

@ -9,7 +9,7 @@
import {AstPath} from '../ast_path'; import {AstPath} from '../ast_path';
import {CompileDirectiveSummary, CompileProviderMetadata, CompileTokenMetadata} from '../compile_metadata'; import {CompileDirectiveSummary, CompileProviderMetadata, CompileTokenMetadata} from '../compile_metadata';
import {SecurityContext} from '../core'; import {SecurityContext} from '../core';
import {AST, BindingType, BoundElementProperty, ParsedEvent, ParsedEventType, ParsedVariable} from '../expression_parser/ast'; import {ASTWithSource, BindingType, BoundElementProperty, ParsedEvent, ParsedEventType, ParsedVariable} from '../expression_parser/ast';
import {LifecycleHooks} from '../lifecycle_reflector'; import {LifecycleHooks} from '../lifecycle_reflector';
import {ParseSourceSpan} from '../parse_util'; import {ParseSourceSpan} from '../parse_util';
@ -44,7 +44,8 @@ export class TextAst implements TemplateAst {
*/ */
export class BoundTextAst implements TemplateAst { export class BoundTextAst implements TemplateAst {
constructor( constructor(
public value: AST, public ngContentIndex: number, public sourceSpan: ParseSourceSpan) {} public value: ASTWithSource, public ngContentIndex: number,
public sourceSpan: ParseSourceSpan) {}
visit(visitor: TemplateAstVisitor, context: any): any { visit(visitor: TemplateAstVisitor, context: any): any {
return visitor.visitBoundText(this, context); return visitor.visitBoundText(this, context);
} }
@ -88,8 +89,8 @@ export class BoundElementPropertyAst implements TemplateAst {
constructor( constructor(
public name: string, public type: PropertyBindingType, public name: string, public type: PropertyBindingType,
public securityContext: SecurityContext, public value: AST, public unit: string|null, public securityContext: SecurityContext, public value: ASTWithSource,
public sourceSpan: ParseSourceSpan) { public unit: string|null, public sourceSpan: ParseSourceSpan) {
this.isAnimation = this.type === PropertyBindingType.Animation; this.isAnimation = this.type === PropertyBindingType.Animation;
} }
@ -114,7 +115,7 @@ export class BoundEventAst implements TemplateAst {
constructor( constructor(
public name: string, public target: string|null, public phase: string|null, public name: string, public target: string|null, public phase: string|null,
public handler: AST, public sourceSpan: ParseSourceSpan, public handler: ASTWithSource, public sourceSpan: ParseSourceSpan,
public handlerSpan: ParseSourceSpan) { public handlerSpan: ParseSourceSpan) {
this.fullName = BoundEventAst.calcFullName(this.name, this.target, this.phase); this.fullName = BoundEventAst.calcFullName(this.name, this.target, this.phase);
this.isAnimation = !!this.phase; this.isAnimation = !!this.phase;
@ -209,7 +210,7 @@ export class EmbeddedTemplateAst implements TemplateAst {
*/ */
export class BoundDirectivePropertyAst implements TemplateAst { export class BoundDirectivePropertyAst implements TemplateAst {
constructor( constructor(
public directiveName: string, public templateName: string, public value: AST, public directiveName: string, public templateName: string, public value: ASTWithSource,
public sourceSpan: ParseSourceSpan) {} public sourceSpan: ParseSourceSpan) {}
visit(visitor: TemplateAstVisitor, context: any): any { visit(visitor: TemplateAstVisitor, context: any): any {
return visitor.visitDirectiveProperty(this, context); return visitor.visitDirectiveProperty(this, context);
@ -350,7 +351,7 @@ export class RecursiveTemplateAstVisitor extends NullTemplateVisitor implements
}); });
} }
protected visitChildren<T extends TemplateAst>( protected visitChildren(
context: any, context: any,
cb: (visit: (<V extends TemplateAst>(children: V[]|undefined) => void)) => void) { cb: (visit: (<V extends TemplateAst>(children: V[]|undefined) => void)) => void) {
let results: any[][] = []; let results: any[][] = [];

View File

@ -9,14 +9,12 @@
import {AotCompilerOptions} from '../aot/compiler_options'; import {AotCompilerOptions} from '../aot/compiler_options';
import {StaticReflector} from '../aot/static_reflector'; import {StaticReflector} from '../aot/static_reflector';
import {StaticSymbol} from '../aot/static_symbol'; import {StaticSymbol} from '../aot/static_symbol';
import {CompileDiDependencyMetadata, CompileDirectiveMetadata, CompilePipeSummary} from '../compile_metadata'; import {CompileDirectiveMetadata, CompilePipeSummary} from '../compile_metadata';
import {BindingForm, BuiltinConverter, EventHandlerVars, LocalResolver, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins} from '../compiler_util/expression_converter'; import {BindingForm, EventHandlerVars, LocalResolver, convertActionBinding, convertPropertyBinding, convertPropertyBindingBuiltins} from '../compiler_util/expression_converter';
import {AST, ASTWithSource, Interpolation} from '../expression_parser/ast'; import {AST, ASTWithSource, Interpolation} from '../expression_parser/ast';
import {Identifiers} from '../identifiers';
import * as o from '../output/output_ast'; import * as o from '../output/output_ast';
import {convertValueToOutputAst} from '../output/value_util';
import {ParseSourceSpan} from '../parse_util'; import {ParseSourceSpan} from '../parse_util';
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 {AttrAst, BoundDirectivePropertyAst, BoundElementPropertyAst, BoundEventAst, BoundTextAst, DirectiveAst, ElementAst, EmbeddedTemplateAst, NgContentAst, ReferenceAst, TemplateAst, TemplateAstVisitor, TextAst, VariableAst, templateVisitAll} from '../template_parser/template_ast';
import {OutputContext} from '../util'; import {OutputContext} from '../util';
@ -133,7 +131,11 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
result.push({ result.push({
guard, guard,
useIf, useIf,
expression: {context: this.component, value: input.value} as Expression expression: {
context: this.component,
value: input.value,
sourceSpan: input.sourceSpan,
},
}); });
} }
} }