2023-12-01 23:30:21 +08:00
|
|
|
import type {
|
2023-12-06 00:15:57 +08:00
|
|
|
CompoundExpressionNode,
|
2023-12-05 17:13:25 +08:00
|
|
|
DirectiveNode,
|
2023-12-01 23:30:21 +08:00
|
|
|
RootNode,
|
2023-12-05 17:13:25 +08:00
|
|
|
SimpleExpressionNode,
|
2023-12-01 23:30:21 +08:00
|
|
|
SourceLocation,
|
|
|
|
} from '@vue/compiler-dom'
|
2023-12-03 14:52:11 +08:00
|
|
|
import type { Prettify } from '@vue/shared'
|
|
|
|
import type { DirectiveTransform, NodeTransform } from './transform'
|
2023-11-24 11:07:31 +08:00
|
|
|
|
2023-11-29 22:09:46 +08:00
|
|
|
export enum IRNodeTypes {
|
2023-11-24 11:07:31 +08:00
|
|
|
ROOT,
|
2023-11-26 03:53:47 +08:00
|
|
|
TEMPLATE_FACTORY,
|
|
|
|
FRAGMENT_FACTORY,
|
|
|
|
|
2023-11-24 11:07:31 +08:00
|
|
|
SET_PROP,
|
|
|
|
SET_TEXT,
|
|
|
|
SET_EVENT,
|
2023-11-24 14:44:57 +08:00
|
|
|
SET_HTML,
|
2023-11-24 11:07:31 +08:00
|
|
|
|
|
|
|
INSERT_NODE,
|
2023-11-27 06:22:10 +08:00
|
|
|
PREPEND_NODE,
|
2023-11-27 05:16:21 +08:00
|
|
|
APPEND_NODE,
|
2023-11-26 03:12:02 +08:00
|
|
|
CREATE_TEXT_NODE,
|
2023-12-03 18:36:01 +08:00
|
|
|
|
|
|
|
WITH_DIRECTIVE,
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface BaseIRNode {
|
2023-11-24 11:07:31 +08:00
|
|
|
type: IRNodeTypes
|
|
|
|
loc: SourceLocation
|
|
|
|
}
|
|
|
|
|
2023-12-01 05:18:20 +08:00
|
|
|
// TODO refactor
|
|
|
|
export type VaporHelper = keyof typeof import('../../runtime-vapor/src')
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface RootIRNode extends BaseIRNode {
|
2023-11-24 11:07:31 +08:00
|
|
|
type: IRNodeTypes.ROOT
|
2023-12-01 22:12:19 +08:00
|
|
|
source: string
|
2023-12-01 23:30:21 +08:00
|
|
|
node: RootNode
|
2023-11-26 03:53:47 +08:00
|
|
|
template: Array<TemplateFactoryIRNode | FragmentFactoryIRNode>
|
2023-12-01 23:30:21 +08:00
|
|
|
dynamic: IRDynamicInfo
|
|
|
|
effect: IREffect[]
|
2023-11-24 15:02:47 +08:00
|
|
|
operation: OperationNode[]
|
2023-11-24 11:07:31 +08:00
|
|
|
helpers: Set<string>
|
2023-12-01 05:18:20 +08:00
|
|
|
vaporHelpers: Set<VaporHelper>
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface TemplateFactoryIRNode extends BaseIRNode {
|
2023-11-26 03:53:47 +08:00
|
|
|
type: IRNodeTypes.TEMPLATE_FACTORY
|
2023-11-24 11:07:31 +08:00
|
|
|
template: string
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface FragmentFactoryIRNode extends BaseIRNode {
|
2023-11-26 03:53:47 +08:00
|
|
|
type: IRNodeTypes.FRAGMENT_FACTORY
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface SetPropIRNode extends BaseIRNode {
|
2023-11-24 11:07:31 +08:00
|
|
|
type: IRNodeTypes.SET_PROP
|
|
|
|
element: number
|
2023-12-09 04:06:46 +08:00
|
|
|
key: IRExpression
|
2023-12-01 23:30:21 +08:00
|
|
|
value: IRExpression
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface SetTextIRNode extends BaseIRNode {
|
2023-11-24 11:07:31 +08:00
|
|
|
type: IRNodeTypes.SET_TEXT
|
|
|
|
element: number
|
2023-12-01 23:30:21 +08:00
|
|
|
value: IRExpression
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface SetEventIRNode extends BaseIRNode {
|
2023-11-24 11:07:31 +08:00
|
|
|
type: IRNodeTypes.SET_EVENT
|
|
|
|
element: number
|
2023-12-09 04:06:46 +08:00
|
|
|
key: IRExpression
|
2023-12-01 23:30:21 +08:00
|
|
|
value: IRExpression
|
2023-12-03 03:49:44 +08:00
|
|
|
modifiers: {
|
|
|
|
// modifiers for addEventListener() options, e.g. .passive & .capture
|
|
|
|
options: string[]
|
|
|
|
// modifiers that needs runtime guards, withKeys
|
|
|
|
keys: string[]
|
|
|
|
// modifiers that needs runtime guards, withModifiers
|
|
|
|
nonKeys: string[]
|
|
|
|
}
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface SetHtmlIRNode extends BaseIRNode {
|
2023-11-24 14:44:57 +08:00
|
|
|
type: IRNodeTypes.SET_HTML
|
|
|
|
element: number
|
2023-12-01 23:30:21 +08:00
|
|
|
value: IRExpression
|
2023-11-24 14:44:57 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface CreateTextNodeIRNode extends BaseIRNode {
|
2023-11-26 03:12:02 +08:00
|
|
|
type: IRNodeTypes.CREATE_TEXT_NODE
|
2023-11-24 11:07:31 +08:00
|
|
|
id: number
|
2023-12-01 23:30:21 +08:00
|
|
|
value: IRExpression
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface InsertNodeIRNode extends BaseIRNode {
|
2023-11-24 11:07:31 +08:00
|
|
|
type: IRNodeTypes.INSERT_NODE
|
2023-11-27 06:22:10 +08:00
|
|
|
element: number | number[]
|
|
|
|
parent: number
|
|
|
|
anchor: number
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface PrependNodeIRNode extends BaseIRNode {
|
2023-11-27 06:22:10 +08:00
|
|
|
type: IRNodeTypes.PREPEND_NODE
|
|
|
|
elements: number[]
|
2023-11-24 11:07:31 +08:00
|
|
|
parent: number
|
2023-11-27 05:16:21 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface AppendNodeIRNode extends BaseIRNode {
|
2023-11-27 05:16:21 +08:00
|
|
|
type: IRNodeTypes.APPEND_NODE
|
|
|
|
elements: number[]
|
|
|
|
parent: number
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-03 18:36:01 +08:00
|
|
|
export interface WithDirectiveIRNode extends BaseIRNode {
|
|
|
|
type: IRNodeTypes.WITH_DIRECTIVE
|
|
|
|
element: number
|
2023-12-07 01:41:17 +08:00
|
|
|
dir: VaporDirectiveNode
|
2023-12-03 18:36:01 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export type IRNode =
|
|
|
|
| OperationNode
|
|
|
|
| RootIRNode
|
|
|
|
| TemplateFactoryIRNode
|
|
|
|
| FragmentFactoryIRNode
|
2023-11-24 15:25:34 +08:00
|
|
|
export type OperationNode =
|
|
|
|
| SetPropIRNode
|
|
|
|
| SetTextIRNode
|
|
|
|
| SetEventIRNode
|
|
|
|
| SetHtmlIRNode
|
2023-11-26 03:12:02 +08:00
|
|
|
| CreateTextNodeIRNode
|
2023-11-24 15:25:34 +08:00
|
|
|
| InsertNodeIRNode
|
2023-11-27 06:22:10 +08:00
|
|
|
| PrependNodeIRNode
|
2023-11-27 05:16:21 +08:00
|
|
|
| AppendNodeIRNode
|
2023-12-03 18:36:01 +08:00
|
|
|
| WithDirectiveIRNode
|
2023-11-24 11:07:31 +08:00
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface IRDynamicInfo {
|
2023-11-24 11:07:31 +08:00
|
|
|
id: number | null
|
2023-11-27 05:16:21 +08:00
|
|
|
referenced: boolean
|
|
|
|
/** created by DOM API */
|
2023-11-26 03:08:35 +08:00
|
|
|
ghost: boolean
|
2023-11-27 05:16:21 +08:00
|
|
|
placeholder: number | null
|
2023-12-01 23:30:21 +08:00
|
|
|
children: IRDynamicChildren
|
|
|
|
}
|
|
|
|
export type IRDynamicChildren = Record<number, IRDynamicInfo>
|
|
|
|
|
2023-12-05 17:13:25 +08:00
|
|
|
export type IRExpression = SimpleExpressionNode | string
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface IREffect {
|
|
|
|
// TODO multi-expression effect
|
|
|
|
expressions: IRExpression[]
|
|
|
|
operations: OperationNode[]
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
2023-12-03 14:52:11 +08:00
|
|
|
|
|
|
|
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> &
|
|
|
|
Pick<U, Extract<keyof U, keyof T>>
|
|
|
|
|
|
|
|
export type HackOptions<T> = Prettify<
|
|
|
|
Overwrite<
|
|
|
|
T,
|
|
|
|
{
|
|
|
|
nodeTransforms?: NodeTransform[]
|
|
|
|
directiveTransforms?: Record<string, DirectiveTransform | undefined>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
>
|
2023-12-05 17:13:25 +08:00
|
|
|
|
2023-12-06 00:15:57 +08:00
|
|
|
export type VaporDirectiveNode = Overwrite<
|
2023-12-05 17:13:25 +08:00
|
|
|
DirectiveNode,
|
|
|
|
{
|
2023-12-06 00:15:57 +08:00
|
|
|
exp: Exclude<DirectiveNode['exp'], CompoundExpressionNode>
|
|
|
|
arg: Exclude<DirectiveNode['arg'], CompoundExpressionNode>
|
2023-12-05 17:13:25 +08:00
|
|
|
}
|
|
|
|
>
|