/** * Renderer AST 的节点定义 * * compileToRenderer 不是直接输出具体的代码,而是先输出表示代码的 AST,再用 AST 生成代码。这么做有两个好处: * 1. 在代码生成之前,方便做统一的优化(比如移除一些没用的语句、合并 += 表达式等) * 2. 支持对接不同的代码生成。比如 Renderer AST -> PHP 代码的生成器:https://github.com/searchfe/san-ssr-target-php */ import { ComponentInfo } from '../models/component-info'; import type { ComponentReference } from '../models/component-reference'; /** * SSR 输出代码的 AST 节点 */ export interface SyntaxNode { kind: SyntaxKind; } /** * 包含 body 语句列表的语法构造 */ export interface Block extends SyntaxNode { body: Iterable; } /** * 语法构造的类型 */ export declare enum SyntaxKind { Null = 0, Identifier = 1, BinaryExpression = 2, ComputedCall = 3, UnaryExpression = 4, ImportHelper = 5, FunctionDefinition = 6, VariableDefinition = 7, ReturnStatement = 8, MapLiteral = 9, Literal = 10, AssignmentStatement = 11, BooleanVariable = 12, CreateComponentInstance = 13, NewExpression = 14, If = 15, ComponentRendererReference = 16, FunctionCall = 17, Foreach = 18, ExpressionStatement = 19, ElseIf = 20, Else = 21, MapAssign = 22, ArrayIncludes = 23, ConditionalExpression = 24, FilterCall = 25, EncodeURIComponent = 26, ArrayLiteral = 27, RegexpReplace = 28, JSONStringify = 29, HelperCall = 30, GetRootCtxCall = 31, ComponentReferenceLiteral = 32, SlotRendererDefinition = 33, SlotRenderCall = 34, Undefined = 35, TryStatement = 36, CatchClause = 37, ComponentClassReference = 38, CreateComponentPrototype = 39, Typeof = 40 } export type Expression = Identifier | FunctionDefinition | Literal | BinaryExpression | UnaryExpression | CreateComponentInstance | NewExpression | MapLiteral | ComponentRendererReference | FunctionCall | Null | Undefined | MapAssign | ArrayIncludes | ConditionalExpression | FilterCall | HelperCall | EncodeURIComponent | ArrayLiteral | RegexpReplace | JSONStringify | ComputedCall | GetRootCtxCall | ComponentReferenceLiteral | SlotRendererDefinition | SlotRenderCall | ComponentClassReference | CreateComponentPrototype | Typeof; export type Statement = ReturnStatement | ImportHelper | VariableDefinition | AssignmentStatement | If | ElseIf | Else | Foreach | ExpressionStatement | TryStatement; export type BinaryOperator = '+' | '-' | '*' | '/' | '%' | '.' | '===' | '!==' | '||' | '&&' | '[]' | '+=' | '!=' | '==' | '<' | '<=' | '>' | '>='; export type UnaryOperator = '!' | '~' | '+' | '()' | '-'; export declare class ArrayLiteral implements SyntaxNode { items: [Expression, boolean][]; readonly kind = SyntaxKind.ArrayLiteral; constructor(items: [Expression, boolean][]); } export declare class MapLiteral implements SyntaxNode { readonly kind = SyntaxKind.MapLiteral; items: [Literal | Identifier, Expression, boolean][]; constructor(items: (Literal | Identifier | [Literal | Identifier, Expression, boolean?])[]); } export declare class ComponentRendererReference implements SyntaxNode { value: Expression; tagName: Literal; readonly kind = SyntaxKind.ComponentRendererReference; constructor(value: Expression, tagName: Literal); } export declare class ComponentClassReference implements SyntaxNode { value: Expression; tagName: Literal | Identifier; readonly kind = SyntaxKind.ComponentClassReference; constructor(value: Expression, tagName: Literal | Identifier); } export declare class ComponentReferenceLiteral implements SyntaxNode { value: ComponentReference; readonly kind = SyntaxKind.ComponentReferenceLiteral; constructor(value: ComponentReference); toMapLiteral(): MapLiteral; } export declare class Null implements SyntaxNode { readonly kind = SyntaxKind.Null; private static instance; private constructor(); static create(): Null; } export declare class Undefined implements SyntaxNode { readonly kind = SyntaxKind.Undefined; private static instance; private constructor(); static create(): Undefined; } export declare class TryStatement implements SyntaxNode { block: Statement[]; handler: CatchClause; readonly kind = SyntaxKind.TryStatement; constructor(block: Statement[], handler: CatchClause); } export declare class CatchClause implements SyntaxNode { param: Identifier; body: Statement[]; readonly kind = SyntaxKind.CatchClause; constructor(param: Identifier, body: Statement[]); } export declare class CreateComponentInstance implements SyntaxNode { info: ComponentInfo; readonly kind = SyntaxKind.CreateComponentInstance; constructor(info: ComponentInfo); } export declare class CreateComponentPrototype implements SyntaxNode { info: ComponentInfo; readonly kind = SyntaxKind.CreateComponentPrototype; constructor(info: ComponentInfo); } export declare class NewExpression implements SyntaxNode { name: Expression; args: Expression[]; readonly kind = SyntaxKind.NewExpression; constructor(name: Expression, args: Expression[]); } export declare class BinaryExpression implements SyntaxNode { lhs: Expression; op: BinaryOperator; rhs: Expression; readonly kind = SyntaxKind.BinaryExpression; constructor(lhs: Expression, op: BinaryOperator, rhs: Expression); } export declare class RegexpReplace implements SyntaxNode { original: Expression; pattern: string; replacement: Expression; readonly kind = SyntaxKind.RegexpReplace; constructor(original: Expression, pattern: string, replacement: Expression); } export declare class JSONStringify implements SyntaxNode { value: Expression; readonly kind = SyntaxKind.JSONStringify; constructor(value: Expression); } export declare class ConditionalExpression implements SyntaxNode { cond: Expression; trueValue: Expression; falseValue: Expression; readonly kind = SyntaxKind.ConditionalExpression; constructor(cond: Expression, trueValue: Expression, falseValue: Expression); } export declare class UnaryExpression implements SyntaxNode { op: UnaryOperator; value: Expression; readonly kind = SyntaxKind.UnaryExpression; constructor(op: UnaryOperator, value: Expression); } export declare class ImportHelper implements SyntaxNode { name: string; readonly kind = SyntaxKind.ImportHelper; constructor(name: string); } export declare class VariableDefinition implements SyntaxNode { name: string; initial?: Expression | undefined; readonly kind = SyntaxKind.VariableDefinition; constructor(name: string, initial?: Expression | undefined); } export declare class If implements SyntaxNode { cond: Expression; body: Iterable; readonly kind = SyntaxKind.If; constructor(cond: Expression, body: Iterable); } export declare class ElseIf implements SyntaxNode { cond: Expression; body: Iterable; readonly kind = SyntaxKind.ElseIf; constructor(cond: Expression, body: Iterable); } export declare class Else implements SyntaxNode { body: Iterable; readonly kind = SyntaxKind.Else; constructor(body: Iterable); } export declare class Foreach implements SyntaxNode { key: Identifier; value: Identifier; iterable: Expression; body: Iterable; readonly kind = SyntaxKind.Foreach; constructor(key: Identifier, value: Identifier, iterable: Expression, body: Iterable); } export declare class EncodeURIComponent implements SyntaxNode { value: Expression; readonly kind = SyntaxKind.EncodeURIComponent; constructor(value: Expression); } export declare class ComputedCall implements SyntaxNode { name: string; readonly kind = SyntaxKind.ComputedCall; constructor(name: string); } export declare class FilterCall implements SyntaxNode { name: string; args: Expression[]; readonly kind = SyntaxKind.FilterCall; constructor(name: string, args: Expression[]); } export declare class GetRootCtxCall implements SyntaxNode { args: Expression[]; readonly kind = SyntaxKind.GetRootCtxCall; constructor(args: Expression[]); } export declare class HelperCall implements SyntaxNode { name: 'styleFilter' | 'classFilter' | 'xstyleFilter' | 'xclassFilter' | 'attrFilter' | 'boolAttrFilter' | 'output' | 'escapeHTML'; args: Expression[]; readonly kind = SyntaxKind.HelperCall; constructor(name: 'styleFilter' | 'classFilter' | 'xstyleFilter' | 'xclassFilter' | 'attrFilter' | 'boolAttrFilter' | 'output' | 'escapeHTML', args: Expression[]); } export declare class FunctionCall implements SyntaxNode { fn: Expression; args: Expression[]; readonly kind = SyntaxKind.FunctionCall; constructor(fn: Expression, args: Expression[]); } export declare class SlotRenderCall implements SyntaxNode { fn: Expression; args: Expression[]; readonly kind = SyntaxKind.SlotRenderCall; constructor(fn: Expression, args: Expression[]); } export declare class Identifier implements SyntaxNode { readonly name: string; private static cache; readonly kind = SyntaxKind.Identifier; private constructor(); static create(name: string): Identifier; } export declare class ExpressionStatement implements SyntaxNode { value: Expression; readonly kind = SyntaxKind.ExpressionStatement; constructor(value: Expression); } export declare class ArrayIncludes implements SyntaxNode { arr: Expression; item: Expression; readonly kind = SyntaxKind.ArrayIncludes; constructor(arr: Expression, item: Expression); } export declare class MapAssign implements SyntaxNode { dest: Expression; srcs: Expression[]; readonly kind = SyntaxKind.MapAssign; constructor(dest: Expression, srcs: Expression[]); } export declare class AssignmentStatement implements SyntaxNode { lhs: Expression; rhs: Expression; readonly kind = SyntaxKind.AssignmentStatement; constructor(lhs: Expression, rhs: Expression); } export declare class FunctionDefinition implements SyntaxNode { name: string; args: VariableDefinition[]; body: Iterable; readonly kind = SyntaxKind.FunctionDefinition; constructor(name: string, args: VariableDefinition[], body: Iterable); } export declare class SlotRendererDefinition implements SyntaxNode { name: string; args: VariableDefinition[]; body: Iterable; readonly kind = SyntaxKind.SlotRendererDefinition; constructor(name: string, args: VariableDefinition[], body: Iterable); } export declare class Literal implements SyntaxNode { readonly value: any; readonly kind = SyntaxKind.Literal; private static cache; private constructor(); static create(name: string): Literal; } export declare class ReturnStatement implements SyntaxNode { value: Expression; readonly kind = SyntaxKind.ReturnStatement; constructor(value: Expression); } export declare class Typeof implements SyntaxNode { value: Expression; readonly kind = SyntaxKind.Typeof; constructor(value: Expression); }