2023-12-01 23:30:21 +08:00
|
|
|
import type {
|
2024-01-21 13:43:23 +08:00
|
|
|
BindingTypes,
|
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,
|
2024-01-28 01:31:20 +08:00
|
|
|
TemplateChildNode,
|
2023-12-01 23:30:21 +08:00
|
|
|
} from '@vue/compiler-dom'
|
2023-12-03 14:52:11 +08:00
|
|
|
import type { Prettify } from '@vue/shared'
|
2024-02-05 02:44:30 +08:00
|
|
|
import type {
|
|
|
|
DirectiveTransform,
|
|
|
|
DirectiveTransformResult,
|
|
|
|
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,
|
2024-01-31 17:00:19 +08:00
|
|
|
BLOCK_FUNCTION,
|
|
|
|
|
2023-11-26 03:53:47 +08:00
|
|
|
TEMPLATE_FACTORY,
|
|
|
|
|
2023-11-24 11:07:31 +08:00
|
|
|
SET_PROP,
|
2024-02-05 02:44:30 +08:00
|
|
|
SET_DYNAMIC_PROPS,
|
2023-11-24 11:07:31 +08:00
|
|
|
SET_TEXT,
|
|
|
|
SET_EVENT,
|
2023-11-24 14:44:57 +08:00
|
|
|
SET_HTML,
|
2024-01-20 23:48:10 +08:00
|
|
|
SET_REF,
|
2024-01-21 02:16:30 +08:00
|
|
|
SET_MODEL_VALUE,
|
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,
|
2024-01-28 01:31:20 +08:00
|
|
|
|
|
|
|
IF,
|
2024-01-31 17:00:19 +08:00
|
|
|
FOR,
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-31 17:26:07 +08:00
|
|
|
export type VaporHelper = keyof typeof import('@vue/runtime-vapor')
|
2023-12-01 05:18:20 +08:00
|
|
|
|
2024-01-28 01:31:20 +08:00
|
|
|
export interface BlockFunctionIRNode extends BaseIRNode {
|
|
|
|
type: IRNodeTypes.BLOCK_FUNCTION
|
|
|
|
node: RootNode | TemplateChildNode
|
|
|
|
templateIndex: number
|
2023-12-01 23:30:21 +08:00
|
|
|
dynamic: IRDynamicInfo
|
|
|
|
effect: IREffect[]
|
2023-11-24 15:02:47 +08:00
|
|
|
operation: OperationNode[]
|
2024-02-07 17:29:04 +08:00
|
|
|
returns?: number[]
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2024-01-28 01:31:20 +08:00
|
|
|
export interface RootIRNode extends Omit<BlockFunctionIRNode, 'type'> {
|
|
|
|
type: IRNodeTypes.ROOT
|
|
|
|
node: RootNode
|
|
|
|
source: string
|
2024-02-07 17:29:04 +08:00
|
|
|
template: Array<TemplateFactoryIRNode>
|
2024-01-28 01:31:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface IfIRNode extends BaseIRNode {
|
|
|
|
type: IRNodeTypes.IF
|
|
|
|
id: number
|
|
|
|
condition: IRExpression
|
|
|
|
positive: BlockFunctionIRNode
|
2024-01-29 03:42:56 +08:00
|
|
|
negative?: BlockFunctionIRNode | IfIRNode
|
2024-01-28 01:31:20 +08:00
|
|
|
}
|
|
|
|
|
2024-01-31 17:00:19 +08:00
|
|
|
export interface ForIRNode extends BaseIRNode {
|
|
|
|
type: IRNodeTypes.FOR
|
|
|
|
id: number
|
|
|
|
source: IRExpression
|
|
|
|
value?: SimpleExpressionNode
|
|
|
|
key?: SimpleExpressionNode
|
|
|
|
index?: SimpleExpressionNode
|
|
|
|
render: BlockFunctionIRNode
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-06 02:35:52 +08:00
|
|
|
export interface IRProp extends Omit<DirectiveTransformResult, 'value'> {
|
|
|
|
values: SimpleExpressionNode[]
|
|
|
|
}
|
|
|
|
export type IRProps = IRProp[] | SimpleExpressionNode
|
|
|
|
|
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
|
2024-02-06 02:35:52 +08:00
|
|
|
prop: IRProp
|
2024-02-05 02:44:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SetDynamicPropsIRNode extends BaseIRNode {
|
|
|
|
type: IRNodeTypes.SET_DYNAMIC_PROPS
|
|
|
|
element: number
|
2024-02-06 02:35:52 +08:00
|
|
|
props: IRProps[]
|
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
|
2024-02-07 04:29:53 +08:00
|
|
|
values: SimpleExpressionNode[]
|
2023-11-24 11:07:31 +08:00
|
|
|
}
|
|
|
|
|
2023-12-10 00:06:20 +08:00
|
|
|
export type KeyOverride = [find: string, replacement: string]
|
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-10 00:06:20 +08:00
|
|
|
value?: SimpleExpressionNode
|
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-12-10 00:06:20 +08:00
|
|
|
keyOverride?: KeyOverride
|
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
|
|
|
}
|
|
|
|
|
2024-01-20 23:48:10 +08:00
|
|
|
export interface SetRefIRNode extends BaseIRNode {
|
|
|
|
type: IRNodeTypes.SET_REF
|
|
|
|
element: number
|
|
|
|
value: IRExpression
|
|
|
|
}
|
|
|
|
|
2024-01-21 02:16:30 +08:00
|
|
|
export interface SetModelValueIRNode extends BaseIRNode {
|
|
|
|
type: IRNodeTypes.SET_MODEL_VALUE
|
|
|
|
element: number
|
|
|
|
key: IRExpression
|
|
|
|
value: IRExpression
|
2024-01-21 13:43:23 +08:00
|
|
|
bindingType?: BindingTypes
|
2024-01-21 02:16:30 +08:00
|
|
|
isComponent: boolean
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
2024-01-30 22:21:59 +08:00
|
|
|
builtin?: VaporHelper
|
2023-12-03 18:36:01 +08:00
|
|
|
}
|
|
|
|
|
2024-02-07 17:29:04 +08:00
|
|
|
export type IRNode = OperationNode | RootIRNode | TemplateFactoryIRNode
|
2023-11-24 15:25:34 +08:00
|
|
|
export type OperationNode =
|
|
|
|
| SetPropIRNode
|
2024-02-05 02:44:30 +08:00
|
|
|
| SetDynamicPropsIRNode
|
2023-11-24 15:25:34 +08:00
|
|
|
| SetTextIRNode
|
|
|
|
| SetEventIRNode
|
|
|
|
| SetHtmlIRNode
|
2024-01-20 23:48:10 +08:00
|
|
|
| SetRefIRNode
|
2024-01-21 02:16:30 +08:00
|
|
|
| SetModelValueIRNode
|
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
|
2024-01-28 01:31:20 +08:00
|
|
|
| IfIRNode
|
2024-01-31 17:00:19 +08:00
|
|
|
| ForIRNode
|
2024-01-28 01:31:20 +08:00
|
|
|
|
|
|
|
export type BlockIRNode = RootIRNode | BlockFunctionIRNode
|
2023-11-24 11:07:31 +08:00
|
|
|
|
2024-01-29 22:08:57 +08:00
|
|
|
export enum DynamicFlag {
|
|
|
|
NONE = 0,
|
|
|
|
/**
|
|
|
|
* This node is referenced and needs to be saved as a variable.
|
|
|
|
*/
|
|
|
|
REFERENCED = 1,
|
|
|
|
/**
|
|
|
|
* This node is not generated from template, but is generated dynamically.
|
|
|
|
*/
|
|
|
|
NON_TEMPLATE = 1 << 1,
|
|
|
|
/**
|
|
|
|
* This node needs to be inserted back into the template.
|
|
|
|
*/
|
|
|
|
INSERT = 1 << 2,
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface IRDynamicInfo {
|
2023-11-24 11:07:31 +08:00
|
|
|
id: number | null
|
2024-01-30 22:52:42 +08:00
|
|
|
flags: DynamicFlag
|
2024-01-29 23:06:21 +08:00
|
|
|
anchor: number | null
|
2024-01-29 22:28:40 +08:00
|
|
|
children: IRDynamicInfo[]
|
2023-12-01 23:30:21 +08:00
|
|
|
}
|
|
|
|
|
2023-12-05 17:13:25 +08:00
|
|
|
export type IRExpression = SimpleExpressionNode | string
|
2023-12-01 23:30:21 +08:00
|
|
|
export interface IREffect {
|
|
|
|
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
|
|
|
}
|
|
|
|
>
|