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
This commit is contained in:
Georgii Dolzhykov 2021-03-28 22:52:09 +03:00 committed by Alex Rickabaugh
parent 4d7294d9f7
commit 209768a570
1 changed files with 6 additions and 4 deletions

View File

@ -10,12 +10,14 @@ import {AstPath} from '../ast_path';
import {I18nMeta} from '../i18n/i18n_ast'; import {I18nMeta} from '../i18n/i18n_ast';
import {ParseSourceSpan} from '../parse_util'; import {ParseSourceSpan} from '../parse_util';
export interface Node { interface BaseNode {
sourceSpan: ParseSourceSpan; sourceSpan: ParseSourceSpan;
visit(visitor: Visitor, context: any): any; 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) {} constructor(public sourceSpan: ParseSourceSpan, public i18n?: I18nMeta) {}
abstract visit(visitor: Visitor, context: any): any; 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( constructor(
public value: string, public expression: Node[], public sourceSpan: ParseSourceSpan, public value: string, public expression: Node[], public sourceSpan: ParseSourceSpan,
public valueSourceSpan: ParseSourceSpan, public expSourceSpan: 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) {} constructor(public value: string|null, public sourceSpan: ParseSourceSpan) {}
visit(visitor: Visitor, context: any): any { visit(visitor: Visitor, context: any): any {
return visitor.visitComment(this, context); return visitor.visitComment(this, context);