diff --git a/packages/compiler/src/compiler.ts b/packages/compiler/src/compiler.ts index 5ff2bb48e4df4a..9f33082aaec30a 100644 --- a/packages/compiler/src/compiler.ts +++ b/packages/compiler/src/compiler.ts @@ -121,46 +121,7 @@ export {SourceMap} from './output/source_map'; export * from './injectable_compiler_2'; export * from './render3/partial/api'; export * from './render3/view/api'; -export { - visitAll as tmplAstVisitAll, - BlockNode as TmplAstBlockNode, - BoundAttribute as TmplAstBoundAttribute, - BoundEvent as TmplAstBoundEvent, - BoundText as TmplAstBoundText, - Content as TmplAstContent, - Element as TmplAstElement, - Icu as TmplAstIcu, - Node as TmplAstNode, - Visitor as TmplAstVisitor, - RecursiveVisitor as TmplAstRecursiveVisitor, - Reference as TmplAstReference, - Template as TmplAstTemplate, - Text as TmplAstText, - TextAttribute as TmplAstTextAttribute, - Variable as TmplAstVariable, - DeferredBlock as TmplAstDeferredBlock, - DeferredBlockPlaceholder as TmplAstDeferredBlockPlaceholder, - DeferredBlockLoading as TmplAstDeferredBlockLoading, - DeferredBlockError as TmplAstDeferredBlockError, - DeferredTrigger as TmplAstDeferredTrigger, - BoundDeferredTrigger as TmplAstBoundDeferredTrigger, - IdleDeferredTrigger as TmplAstIdleDeferredTrigger, - ImmediateDeferredTrigger as TmplAstImmediateDeferredTrigger, - HoverDeferredTrigger as TmplAstHoverDeferredTrigger, - TimerDeferredTrigger as TmplAstTimerDeferredTrigger, - InteractionDeferredTrigger as TmplAstInteractionDeferredTrigger, - ViewportDeferredTrigger as TmplAstViewportDeferredTrigger, - NeverDeferredTrigger as TmplAstNeverDeferredTrigger, - SwitchBlock as TmplAstSwitchBlock, - SwitchBlockCase as TmplAstSwitchBlockCase, - ForLoopBlock as TmplAstForLoopBlock, - ForLoopBlockEmpty as TmplAstForLoopBlockEmpty, - IfBlock as TmplAstIfBlock, - IfBlockBranch as TmplAstIfBlockBranch, - DeferredBlockTriggers as TmplAstDeferredBlockTriggers, - UnknownBlock as TmplAstUnknownBlock, - LetDeclaration as TmplAstLetDeclaration, -} from './render3/r3_ast'; +export * from './compiler_ast'; export * from './render3/view/t2_api'; export * from './render3/view/t2_binder'; export {createCssSelectorFromNode} from './render3/view/util'; diff --git a/packages/compiler/src/compiler_ast.ts b/packages/compiler/src/compiler_ast.ts new file mode 100644 index 00000000000000..f5e923bf0b6b63 --- /dev/null +++ b/packages/compiler/src/compiler_ast.ts @@ -0,0 +1,50 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +export { + BlockNode as TmplAstBlockNode, + BoundAttribute as TmplAstBoundAttribute, + BoundDeferredTrigger as TmplAstBoundDeferredTrigger, + BoundEvent as TmplAstBoundEvent, + BoundText as TmplAstBoundText, + Content as TmplAstContent, + Comment as TmplAstComment, + DeferredBlock as TmplAstDeferredBlock, + DeferredBlockError as TmplAstDeferredBlockError, + DeferredBlockLoading as TmplAstDeferredBlockLoading, + DeferredBlockPlaceholder as TmplAstDeferredBlockPlaceholder, + DeferredBlockTriggers as TmplAstDeferredBlockTriggers, + DeferredTrigger as TmplAstDeferredTrigger, + Element as TmplAstElement, + ForLoopBlock as TmplAstForLoopBlock, + ForLoopBlockEmpty as TmplAstForLoopBlockEmpty, + HoverDeferredTrigger as TmplAstHoverDeferredTrigger, + Icu as TmplAstIcu, + IdleDeferredTrigger as TmplAstIdleDeferredTrigger, + IfBlock as TmplAstIfBlock, + IfBlockBranch as TmplAstIfBlockBranch, + ImmediateDeferredTrigger as TmplAstImmediateDeferredTrigger, + InteractionDeferredTrigger as TmplAstInteractionDeferredTrigger, + LetDeclaration as TmplAstLetDeclaration, + NeverDeferredTrigger as TmplAstNeverDeferredTrigger, + Node as TmplAstNode, + RecursiveVisitor as TmplAstRecursiveVisitor, + Reference as TmplAstReference, + SwitchBlock as TmplAstSwitchBlock, + SwitchBlockCase as TmplAstSwitchBlockCase, + Template as TmplAstTemplate, + Text as TmplAstText, + TextAttribute as TmplAstTextAttribute, + TimerDeferredTrigger as TmplAstTimerDeferredTrigger, + UnknownBlock as TmplAstUnknownBlock, + Variable as TmplAstVariable, + ViewportDeferredTrigger as TmplAstViewportDeferredTrigger, + Visitor as TmplAstVisitor, + NodeKind as TmplAstNodeKind, + visitAll as tmplAstVisitAll, +} from './render3/r3_ast'; diff --git a/packages/compiler/src/render3/r3_ast.ts b/packages/compiler/src/render3/r3_ast.ts index 39a64498e5fa64..4fc0e3b81046b6 100644 --- a/packages/compiler/src/render3/r3_ast.ts +++ b/packages/compiler/src/render3/r3_ast.ts @@ -19,10 +19,46 @@ import {I18nMeta} from '../i18n/i18n_ast'; import {ParseSourceSpan} from '../parse_util'; export interface Node { + readonly kind: NodeKind; sourceSpan: ParseSourceSpan; visit(visitor: Visitor): Result; } +export enum NodeKind { + BoundText = 'BoundText', + TextAttribute = 'TextAttribute', + BoundAttribute = 'BoundAttribute', + BoundEvent = 'BoundEvent', + Comment = 'Comment', + Element = 'Element', + BoundDeferredTrigger = 'BoundDeferredTrigger', + NeverDeferredTrigger = 'NeverDeferredTrigger', + IdleDeferredTrigger = 'IdleDeferredTrigger', + ImmediateDeferredTrigger = 'ImmediateDeferredTrigger', + HoverDeferredTrigger = 'HoverDeferredTrigger', + TimerDeferredTrigger = 'TimerDeferredTrigger', + InteractionDeferredTrigger = 'InteractionDeferredTrigger', + ViewportDeferredTrigger = 'ViewportDeferredTrigger', + DeferredBlockPlaceholder = 'DeferredBlockPlaceholder', + DeferredBlockLoading = 'DeferredBlockLoading', + DeferredBlockError = 'DeferredBlockError', + DeferredBlock = 'DeferredBlock', + SwitchBlock = 'SwitchBlock', + SwitchBlockCase = 'SwitchBlockCase', + ForLoopBlock = 'ForLoopBlock', + ForLoopBlockEmpty = 'ForLoopBlockEmpty', + IfBlock = 'IfBlock', + IfBlockBranch = 'IfBlockBranch', + UnknownBlock = 'UnknownBlock', + LetDeclaration = 'LetDeclaration', + Template = 'Template', + Text = 'Text', + Content = 'Content', + Variable = 'Variable', + Reference = 'Reference', + Icu = 'Icu', +} + /** * This is an R3 `Node`-like wrapper for a raw `html.Comment` node. We do not currently * require the implementation of a visitor for Comments as they are only collected at @@ -34,6 +70,9 @@ export class Comment implements Node { public value: string, public sourceSpan: ParseSourceSpan, ) {} + + readonly kind = NodeKind.Comment; + visit(_visitor: Visitor): Result { throw new Error('visit() not implemented for Comment'); } @@ -44,6 +83,9 @@ export class Text implements Node { public value: string, public sourceSpan: ParseSourceSpan, ) {} + + readonly kind = NodeKind.Text; + visit(visitor: Visitor): Result { return visitor.visitText(this); } @@ -55,6 +97,9 @@ export class BoundText implements Node { public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta, ) {} + + readonly kind = NodeKind.BoundText; + visit(visitor: Visitor): Result { return visitor.visitBoundText(this); } @@ -75,6 +120,9 @@ export class TextAttribute implements Node { public valueSpan?: ParseSourceSpan, public i18n?: I18nMeta, ) {} + + readonly kind = NodeKind.TextAttribute; + visit(visitor: Visitor): Result { return visitor.visitTextAttribute(this); } @@ -93,6 +141,8 @@ export class BoundAttribute implements Node { public i18n: I18nMeta | undefined, ) {} + readonly kind = NodeKind.BoundAttribute; + static fromBoundElementProperty(prop: BoundElementProperty, i18n?: I18nMeta): BoundAttribute { if (prop.keySpan === undefined) { throw new Error( @@ -129,6 +179,8 @@ export class BoundEvent implements Node { readonly keySpan: ParseSourceSpan, ) {} + readonly kind = NodeKind.BoundEvent; + static fromParsedEvent(event: ParsedEvent) { const target: string | null = event.type === ParsedEventType.Regular ? event.targetOrPhase : null; @@ -169,6 +221,9 @@ export class Element implements Node { public endSourceSpan: ParseSourceSpan | null, public i18n?: I18nMeta, ) {} + + readonly kind = NodeKind.Element; + visit(visitor: Visitor): Result { return visitor.visitElement(this); } @@ -183,6 +238,8 @@ export abstract class DeferredTrigger implements Node { public hydrateSpan: ParseSourceSpan | null, ) {} + abstract readonly kind: NodeKind; + visit(visitor: Visitor): Result { return visitor.visitDeferredTrigger(this); } @@ -200,13 +257,21 @@ export class BoundDeferredTrigger extends DeferredTrigger { // nameSpan. Trigger names are the built in event triggers like hover, interaction, etc. super(/** nameSpan */ null, sourceSpan, prefetchSpan, whenSourceSpan, hydrateSpan); } + + override readonly kind = NodeKind.BoundDeferredTrigger; } -export class NeverDeferredTrigger extends DeferredTrigger {} +export class NeverDeferredTrigger extends DeferredTrigger { + override readonly kind = NodeKind.NeverDeferredTrigger; +} -export class IdleDeferredTrigger extends DeferredTrigger {} +export class IdleDeferredTrigger extends DeferredTrigger { + override readonly kind = NodeKind.IdleDeferredTrigger; +} -export class ImmediateDeferredTrigger extends DeferredTrigger {} +export class ImmediateDeferredTrigger extends DeferredTrigger { + override readonly kind = NodeKind.ImmediateDeferredTrigger; +} export class HoverDeferredTrigger extends DeferredTrigger { constructor( @@ -219,6 +284,8 @@ export class HoverDeferredTrigger extends DeferredTrigger { ) { super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan); } + + override readonly kind = NodeKind.HoverDeferredTrigger; } export class TimerDeferredTrigger extends DeferredTrigger { @@ -232,6 +299,8 @@ export class TimerDeferredTrigger extends DeferredTrigger { ) { super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan); } + + override readonly kind = NodeKind.TimerDeferredTrigger; } export class InteractionDeferredTrigger extends DeferredTrigger { @@ -245,6 +314,8 @@ export class InteractionDeferredTrigger extends DeferredTrigger { ) { super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan); } + + override readonly kind = NodeKind.InteractionDeferredTrigger; } export class ViewportDeferredTrigger extends DeferredTrigger { @@ -258,6 +329,8 @@ export class ViewportDeferredTrigger extends DeferredTrigger { ) { super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan); } + + override readonly kind = NodeKind.ViewportDeferredTrigger; } export class BlockNode { @@ -267,6 +340,8 @@ export class BlockNode { public startSourceSpan: ParseSourceSpan, public endSourceSpan: ParseSourceSpan | null, ) {} + + readonly kind: string = 'BlockNode'; } export class DeferredBlockPlaceholder extends BlockNode implements Node { @@ -285,6 +360,8 @@ export class DeferredBlockPlaceholder extends BlockNode implements Node { visit(visitor: Visitor): Result { return visitor.visitDeferredBlockPlaceholder(this); } + + override readonly kind = NodeKind.DeferredBlockPlaceholder; } export class DeferredBlockLoading extends BlockNode implements Node { @@ -304,6 +381,8 @@ export class DeferredBlockLoading extends BlockNode implements Node { visit(visitor: Visitor): Result { return visitor.visitDeferredBlockLoading(this); } + + override readonly kind = NodeKind.DeferredBlockLoading; } export class DeferredBlockError extends BlockNode implements Node { @@ -318,6 +397,8 @@ export class DeferredBlockError extends BlockNode implements Node { super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan); } + override readonly kind = NodeKind.DeferredBlockError; + visit(visitor: Visitor): Result { return visitor.visitDeferredBlockError(this); } @@ -368,6 +449,8 @@ export class DeferredBlock extends BlockNode implements Node { this.definedHydrateTriggers = Object.keys(hydrateTriggers) as (keyof DeferredBlockTriggers)[]; } + override readonly kind = NodeKind.DeferredBlock; + visit(visitor: Visitor): Result { return visitor.visitDeferredBlock(this); } @@ -413,6 +496,8 @@ export class SwitchBlock extends BlockNode implements Node { super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan); } + override readonly kind = NodeKind.SwitchBlock; + visit(visitor: Visitor): Result { return visitor.visitSwitchBlock(this); } @@ -431,6 +516,8 @@ export class SwitchBlockCase extends BlockNode implements Node { super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan); } + override readonly kind = NodeKind.SwitchBlockCase; + visit(visitor: Visitor): Result { return visitor.visitSwitchBlockCase(this); } @@ -455,6 +542,8 @@ export class ForLoopBlock extends BlockNode implements Node { super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan); } + override readonly kind = NodeKind.ForLoopBlock; + visit(visitor: Visitor): Result { return visitor.visitForLoopBlock(this); } @@ -472,6 +561,8 @@ export class ForLoopBlockEmpty extends BlockNode implements Node { super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan); } + override readonly kind = NodeKind.ForLoopBlockEmpty; + visit(visitor: Visitor): Result { return visitor.visitForLoopBlockEmpty(this); } @@ -488,6 +579,8 @@ export class IfBlock extends BlockNode implements Node { super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan); } + override readonly kind = NodeKind.IfBlock; + visit(visitor: Visitor): Result { return visitor.visitIfBlock(this); } @@ -507,6 +600,8 @@ export class IfBlockBranch extends BlockNode implements Node { super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan); } + override readonly kind = NodeKind.IfBlockBranch; + visit(visitor: Visitor): Result { return visitor.visitIfBlockBranch(this); } @@ -519,6 +614,8 @@ export class UnknownBlock implements Node { public nameSpan: ParseSourceSpan, ) {} + readonly kind = NodeKind.UnknownBlock; + visit(visitor: Visitor): Result { return visitor.visitUnknownBlock(this); } @@ -533,6 +630,8 @@ export class LetDeclaration implements Node { public valueSpan: ParseSourceSpan, ) {} + readonly kind = NodeKind.LetDeclaration; + visit(visitor: Visitor): Result { return visitor.visitLetDeclaration(this); } @@ -557,6 +656,9 @@ export class Template implements Node { public endSourceSpan: ParseSourceSpan | null, public i18n?: I18nMeta, ) {} + + readonly kind = NodeKind.Template; + visit(visitor: Visitor): Result { return visitor.visitTemplate(this); } @@ -572,6 +674,9 @@ export class Content implements Node { public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta, ) {} + + readonly kind = NodeKind.Content; + visit(visitor: Visitor): Result { return visitor.visitContent(this); } @@ -585,6 +690,9 @@ export class Variable implements Node { readonly keySpan: ParseSourceSpan, public valueSpan?: ParseSourceSpan, ) {} + + readonly kind = NodeKind.Variable; + visit(visitor: Visitor): Result { return visitor.visitVariable(this); } @@ -598,6 +706,9 @@ export class Reference implements Node { readonly keySpan: ParseSourceSpan, public valueSpan?: ParseSourceSpan, ) {} + + readonly kind = NodeKind.Reference; + visit(visitor: Visitor): Result { return visitor.visitReference(this); } @@ -610,6 +721,9 @@ export class Icu implements Node { public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta, ) {} + + readonly kind = NodeKind.Icu; + visit(visitor: Visitor): Result { return visitor.visitIcu(this); }