From 209768a5702c3a18c72cfa7bd0c104099c8234b4 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Sun, 28 Mar 2021 22:52:09 +0300 Subject: [PATCH] refactor(compiler): stricter types for HTML AST (#41360) A Node can only be an instance of one of the six classes. This relation can be accurately expressed using a union type. PR Close #41360 --- packages/compiler/src/ml_parser/ast.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/compiler/src/ml_parser/ast.ts b/packages/compiler/src/ml_parser/ast.ts index c962e04a9d..274163561a 100644 --- a/packages/compiler/src/ml_parser/ast.ts +++ b/packages/compiler/src/ml_parser/ast.ts @@ -10,12 +10,14 @@ import {AstPath} from '../ast_path'; import {I18nMeta} from '../i18n/i18n_ast'; import {ParseSourceSpan} from '../parse_util'; -export interface Node { +interface BaseNode { sourceSpan: ParseSourceSpan; visit(visitor: Visitor, context: any): any; } -export abstract class NodeWithI18n implements Node { +export type Node = Attribute|Comment|Element|Expansion|ExpansionCase|Text; + +export abstract class NodeWithI18n implements BaseNode { constructor(public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta) {} abstract visit(visitor: Visitor, context: any): any; } @@ -40,7 +42,7 @@ export class Expansion extends NodeWithI18n { } } -export class ExpansionCase implements Node { +export class ExpansionCase implements BaseNode { constructor( public value: string, public expression: Node[], public sourceSpan: ParseSourceSpan, public valueSourceSpan: ParseSourceSpan, public expSourceSpan: ParseSourceSpan) {} @@ -74,7 +76,7 @@ export class Element extends NodeWithI18n { } } -export class Comment implements Node { +export class Comment implements BaseNode { constructor(public value: string|null, public sourceSpan: ParseSourceSpan) {} visit(visitor: Visitor, context: any): any { return visitor.visitComment(this, context);