vue3-core/packages/compiler-vapor/src/transform.ts

474 lines
11 KiB
TypeScript
Raw Normal View History

2023-11-29 02:38:01 +08:00
import type {
NodeTypes,
2023-11-29 02:38:01 +08:00
RootNode,
Node,
TemplateChildNode,
ElementNode,
AttributeNode,
InterpolationNode,
TransformOptions,
DirectiveNode,
ExpressionNode,
2023-11-17 03:01:19 +08:00
} from '@vue/compiler-dom'
import {
2023-11-24 15:02:47 +08:00
type OperationNode,
type RootIRNode,
IRNodeTypes,
2023-11-27 05:16:21 +08:00
DynamicInfo,
} from './ir'
2023-11-24 14:59:10 +08:00
import { isVoidTag } from '@vue/shared'
import {
ErrorCodes,
createCompilerError,
defaultOnError,
defaultOnWarn,
} from './errors'
2023-11-23 23:42:08 +08:00
export interface TransformContext<T extends Node = Node> {
node: T
parent: TransformContext | null
root: TransformContext<RootNode>
index: number
options: TransformOptions
2023-11-27 05:16:21 +08:00
2023-11-23 23:42:08 +08:00
template: string
2023-11-27 05:16:21 +08:00
dynamic: DynamicInfo
2023-11-24 15:25:34 +08:00
once: boolean
2023-11-23 23:42:08 +08:00
2023-11-27 05:16:21 +08:00
reference(): number
increaseId(): number
2023-11-23 23:42:08 +08:00
registerTemplate(): number
2023-11-24 15:25:34 +08:00
registerEffect(expr: string, operation: OperationNode): void
registerOperation(...operations: OperationNode[]): void
helper(name: string): string
2023-11-23 23:42:08 +08:00
}
function createRootContext(
ir: RootIRNode,
node: RootNode,
options: TransformOptions,
): TransformContext<RootNode> {
2023-11-26 03:08:35 +08:00
let globalId = 0
2023-11-24 15:02:47 +08:00
const { effect, operation: operation, helpers, vaporHelpers } = ir
2023-11-23 23:42:08 +08:00
const ctx: TransformContext<RootNode> = {
node,
parent: null,
index: 0,
root: null!, // set later
2023-11-23 23:42:08 +08:00
options,
2023-11-27 05:16:21 +08:00
dynamic: ir.dynamic,
2023-11-24 15:25:34 +08:00
once: false,
2023-11-23 23:42:08 +08:00
increaseId: () => globalId++,
2023-11-27 05:16:21 +08:00
reference() {
if (this.dynamic.id !== null) return this.dynamic.id
this.dynamic.referenced = true
return (this.dynamic.id = this.increaseId())
2023-11-26 03:08:35 +08:00
},
2023-11-24 15:25:34 +08:00
registerEffect(expr, operation) {
2023-11-27 05:16:21 +08:00
if (this.once) {
return this.registerOperation(operation)
2023-11-27 05:16:21 +08:00
}
if (!effect[expr]) effect[expr] = []
2023-11-24 15:25:34 +08:00
effect[expr].push(operation)
2023-11-23 23:42:08 +08:00
},
template: '',
registerTemplate() {
if (!ctx.template) return -1
2023-11-26 03:53:47 +08:00
const idx = ir.template.findIndex(
(t) =>
t.type === IRNodeTypes.TEMPLATE_FACTORY &&
t.template === ctx.template,
)
2023-11-23 23:42:08 +08:00
if (idx !== -1) return idx
ir.template.push({
2023-11-26 03:53:47 +08:00
type: IRNodeTypes.TEMPLATE_FACTORY,
2023-11-23 23:42:08 +08:00
template: ctx.template,
loc: node.loc,
})
return ir.template.length - 1
},
registerOperation(...node) {
2023-11-24 15:02:47 +08:00
operation.push(...node)
},
// TODO not used yet
helper(name, vapor = true) {
;(vapor ? vaporHelpers : helpers).add(name)
return name
},
2023-11-23 23:42:08 +08:00
}
ctx.root = ctx
2023-11-27 05:16:21 +08:00
ctx.reference()
2023-11-23 23:42:08 +08:00
return ctx
}
function createContext<T extends TemplateChildNode>(
node: T,
parent: TransformContext,
index: number,
): TransformContext<T> {
const ctx: TransformContext<T> = {
...parent,
node,
parent,
index,
2023-11-27 05:16:21 +08:00
template: '',
dynamic: {
id: null,
referenced: false,
ghost: false,
placeholder: null,
children: {},
2023-11-24 15:25:34 +08:00
},
2023-11-23 23:42:08 +08:00
}
return ctx
}
2023-11-17 03:01:19 +08:00
// AST -> IR
export function transform(
root: RootNode,
2023-11-23 23:42:08 +08:00
options: TransformOptions = {},
2023-11-17 03:01:19 +08:00
): RootIRNode {
options.onError ||= defaultOnError
options.onWarn ||= defaultOnWarn
2023-11-23 23:42:08 +08:00
const ir: RootIRNode = {
2023-11-17 03:01:19 +08:00
type: IRNodeTypes.ROOT,
loc: root.loc,
2023-11-23 23:42:08 +08:00
template: [],
2023-11-27 05:16:21 +08:00
dynamic: {
id: null,
referenced: true,
ghost: true,
placeholder: null,
children: {},
},
2023-11-23 23:42:08 +08:00
effect: Object.create(null),
2023-11-24 15:02:47 +08:00
operation: [],
helpers: new Set([]),
vaporHelpers: new Set([]),
2023-11-17 03:01:19 +08:00
}
2023-11-23 23:42:08 +08:00
const ctx = createRootContext(ir, root, options)
2023-11-24 14:44:57 +08:00
// TODO: transform presets, see packages/compiler-core/src/transforms
transformChildren(ctx, true)
2023-11-26 03:53:47 +08:00
if (ir.template.length === 0) {
ir.template.push({
type: IRNodeTypes.FRAGMENT_FACTORY,
loc: root.loc,
})
}
2023-11-23 23:42:08 +08:00
return ir
2023-11-17 03:01:19 +08:00
}
2023-11-24 14:44:57 +08:00
function transformChildren(
ctx: TransformContext<RootNode | ElementNode>,
root?: boolean,
) {
2023-11-23 23:42:08 +08:00
const {
node: { children },
} = ctx
2023-11-27 05:16:21 +08:00
const childrenTemplate: string[] = []
2023-11-23 23:42:08 +08:00
children.forEach((child, i) => walkNode(child, i))
2023-11-27 14:16:05 +08:00
processDynamicChildren()
ctx.template += childrenTemplate.join('')
2023-11-27 14:16:05 +08:00
if (root) ctx.registerTemplate()
2023-11-27 05:16:21 +08:00
2023-11-27 14:16:05 +08:00
function processDynamicChildren() {
let prevChildren: DynamicInfo[] = []
let hasStatic = false
for (let index = 0; index < children.length; index++) {
const child = ctx.dynamic.children[index]
if (!child || !child.ghost) {
if (prevChildren.length)
if (hasStatic) {
childrenTemplate[index - prevChildren.length] = `<!>`
const anchor = (prevChildren[0].placeholder = ctx.increaseId())
2023-11-27 14:16:05 +08:00
ctx.registerOperation({
2023-11-27 14:16:05 +08:00
type: IRNodeTypes.INSERT_NODE,
loc: ctx.node.loc,
element: prevChildren.map((child) => child.id!),
parent: ctx.reference(),
anchor,
})
} else {
ctx.registerOperation({
2023-11-27 14:16:05 +08:00
type: IRNodeTypes.PREPEND_NODE,
loc: ctx.node.loc,
elements: prevChildren.map((child) => child.id!),
parent: ctx.reference(),
})
}
hasStatic = true
prevChildren = []
continue
}
2023-11-27 05:16:21 +08:00
2023-11-27 14:16:05 +08:00
prevChildren.push(child)
2023-11-27 05:16:21 +08:00
2023-11-27 14:16:05 +08:00
if (index === children.length - 1) {
ctx.registerOperation({
2023-11-27 14:16:05 +08:00
type: IRNodeTypes.APPEND_NODE,
loc: ctx.node.loc,
elements: prevChildren.map((child) => child.id!),
parent: ctx.reference(),
})
}
}
}
2023-11-24 14:44:57 +08:00
2023-11-27 05:16:21 +08:00
function walkNode(node: TemplateChildNode, index: number) {
2023-11-23 23:42:08 +08:00
const child = createContext(node, ctx, index)
2023-11-27 05:16:21 +08:00
const isFirst = index === 0
const isLast = index === children.length - 1
2023-11-17 03:01:19 +08:00
switch (node.type) {
case 1 satisfies NodeTypes.ELEMENT: {
2023-11-23 23:42:08 +08:00
transformElement(child as TransformContext<ElementNode>)
2023-11-17 03:01:19 +08:00
break
}
2023-11-23 23:42:08 +08:00
case 2 satisfies NodeTypes.TEXT: {
2023-11-27 05:16:21 +08:00
child.template += node.content
2023-11-17 03:01:19 +08:00
break
2023-11-23 23:42:08 +08:00
}
case 3 satisfies NodeTypes.COMMENT: {
2023-11-27 05:16:21 +08:00
child.template += `<!--${node.content}-->`
2023-11-17 17:35:49 +08:00
break
2023-11-23 23:42:08 +08:00
}
case 5 satisfies NodeTypes.INTERPOLATION: {
transformInterpolation(
child as TransformContext<InterpolationNode>,
isFirst,
isLast,
)
2023-11-17 03:01:19 +08:00
break
2023-11-23 23:42:08 +08:00
}
2023-11-24 11:39:49 +08:00
case 12 satisfies NodeTypes.TEXT_CALL:
// never?
break
2023-11-23 23:42:08 +08:00
default: {
2023-11-24 11:39:49 +08:00
// TODO handle other types
// CompoundExpressionNode
// IfNode
// IfBranchNode
// ForNode
2023-11-27 05:16:21 +08:00
child.template += `[type: ${node.type}]`
2023-11-23 23:42:08 +08:00
}
2023-11-17 03:01:19 +08:00
}
2023-11-23 23:42:08 +08:00
2023-11-27 05:16:21 +08:00
childrenTemplate.push(child.template)
2023-11-23 23:42:08 +08:00
2023-11-27 05:16:21 +08:00
if (
child.dynamic.ghost ||
child.dynamic.referenced ||
child.dynamic.placeholder ||
Object.keys(child.dynamic.children).length
) {
ctx.dynamic.children[index] = child.dynamic
}
2023-11-17 03:01:19 +08:00
}
}
2023-11-23 23:42:08 +08:00
function transformElement(ctx: TransformContext<ElementNode>) {
const { node } = ctx
const { tag, props, children } = node
ctx.template += `<${tag}`
props.forEach((prop) => transformProp(prop, ctx))
2023-11-24 14:59:10 +08:00
ctx.template += `>`
2023-11-23 23:42:08 +08:00
2023-11-24 11:39:49 +08:00
if (children.length) transformChildren(ctx)
2023-11-24 14:59:10 +08:00
// TODO remove unnecessary close tag, e.g. if it's the last element of the template
2023-11-27 14:13:09 +08:00
if (!isVoidTag(tag)) {
2023-11-24 14:59:10 +08:00
ctx.template += `</${tag}>`
}
2023-11-23 23:42:08 +08:00
}
function transformInterpolation(
ctx: TransformContext<InterpolationNode>,
isFirst: boolean,
isLast: boolean,
) {
const { node } = ctx
2023-11-27 05:16:21 +08:00
if (node.content.type === (8 satisfies NodeTypes.COMPOUND_EXPRESSION)) {
// TODO: CompoundExpressionNode: {{ count + 1 }}
2023-11-24 11:39:49 +08:00
return
2023-11-17 03:01:19 +08:00
}
2023-11-24 11:39:49 +08:00
2023-11-27 05:16:21 +08:00
const expr = processExpression(ctx, node.content)!
if (isFirst && isLast) {
const parent = ctx.parent!
const parentId = parent.reference()
ctx.registerEffect(expr, {
type: IRNodeTypes.SET_TEXT,
loc: node.loc,
element: parentId,
value: expr,
})
} else {
const id = ctx.reference()
ctx.dynamic.ghost = true
ctx.registerOperation({
2023-11-27 05:16:21 +08:00
type: IRNodeTypes.CREATE_TEXT_NODE,
loc: node.loc,
id,
value: expr,
})
ctx.registerEffect(expr, {
type: IRNodeTypes.SET_TEXT,
loc: node.loc,
element: id,
value: expr,
})
}
2023-11-17 03:01:19 +08:00
}
2023-11-23 23:42:08 +08:00
function transformProp(
node: DirectiveNode | AttributeNode,
ctx: TransformContext<ElementNode>,
): void {
const { name } = node
2023-11-17 03:01:19 +08:00
2023-11-23 23:42:08 +08:00
if (node.type === (6 satisfies NodeTypes.ATTRIBUTE)) {
if (node.value) {
ctx.template += ` ${name}="${node.value.content}"`
} else {
ctx.template += ` ${name}`
}
return
2023-11-17 03:01:19 +08:00
}
const { exp, loc, modifiers } = node
const expr = processExpression(ctx, exp)
2023-11-24 14:44:57 +08:00
switch (name) {
case 'bind': {
if (
!exp ||
2023-11-29 02:38:01 +08:00
(exp.type === (4 satisfies NodeTypes.SIMPLE_EXPRESSION) &&
!exp.content.trim())
) {
ctx.options.onError!(
createCompilerError(ErrorCodes.VAPOR_BIND_NO_EXPRESSION, loc),
)
return
}
2023-11-24 15:25:34 +08:00
if (expr === null) {
// TODO: Vue 3.4 supported shorthand syntax
// https://github.com/vuejs/core/pull/9451
return
} else if (!node.arg) {
2023-11-24 14:44:57 +08:00
// TODO support v-bind="{}"
return
} else if (
node.arg.type === (8 satisfies NodeTypes.COMPOUND_EXPRESSION)
) {
// TODO support :[foo]="bar"
return
}
ctx.registerEffect(expr, {
type: IRNodeTypes.SET_PROP,
loc: node.loc,
2023-11-27 05:16:21 +08:00
element: ctx.reference(),
2023-11-24 14:44:57 +08:00
name: node.arg.content,
2023-11-24 15:25:34 +08:00
value: expr,
2023-11-24 14:44:57 +08:00
})
break
}
case 'on': {
if (!exp && !modifiers.length) {
ctx.options.onError!(
createCompilerError(ErrorCodes.VAPOR_ON_NO_EXPRESSION, loc),
)
return
}
2023-11-24 14:44:57 +08:00
if (!node.arg) {
// TODO support v-on="{}"
return
} else if (
node.arg.type === (8 satisfies NodeTypes.COMPOUND_EXPRESSION)
) {
// TODO support @[foo]="bar"
return
2023-11-24 15:25:34 +08:00
} else if (expr === null) {
// TODO: support @foo
// https://github.com/vuejs/core/pull/9451
return
2023-11-24 14:44:57 +08:00
}
ctx.registerEffect(expr, {
type: IRNodeTypes.SET_EVENT,
loc: node.loc,
2023-11-27 05:16:21 +08:00
element: ctx.reference(),
2023-11-24 14:44:57 +08:00
name: node.arg.content,
2023-11-24 15:25:34 +08:00
value: expr,
2023-11-24 14:44:57 +08:00
})
break
}
2023-11-24 15:25:34 +08:00
case 'html': {
const value = expr || '""'
ctx.registerEffect(value, {
2023-11-24 14:44:57 +08:00
type: IRNodeTypes.SET_HTML,
loc: node.loc,
2023-11-27 05:16:21 +08:00
element: ctx.reference(),
2023-11-24 15:25:34 +08:00
value,
2023-11-24 14:44:57 +08:00
})
break
2023-11-24 15:25:34 +08:00
}
case 'text': {
const value = expr || '""'
ctx.registerEffect(value, {
2023-11-24 14:48:51 +08:00
type: IRNodeTypes.SET_TEXT,
loc: node.loc,
2023-11-27 05:16:21 +08:00
element: ctx.reference(),
2023-11-24 15:25:34 +08:00
value,
2023-11-24 14:48:51 +08:00
})
break
2023-11-24 15:25:34 +08:00
}
case 'once': {
ctx.once = true
break
}
2023-11-24 15:40:38 +08:00
case 'cloak': {
// do nothing
break
}
2023-11-23 23:42:08 +08:00
}
2023-11-17 03:01:19 +08:00
}
2023-11-24 11:39:49 +08:00
// TODO: reuse packages/compiler-core/src/transforms/transformExpression.ts
2023-11-24 15:25:34 +08:00
function processExpression(
ctx: TransformContext,
expr: ExpressionNode | undefined,
): string | null {
if (!expr) return null
if (expr.type === (8 satisfies NodeTypes.COMPOUND_EXPRESSION)) {
// TODO
return ''
}
const { content } = expr
if (ctx.options.bindingMetadata?.[content] === 'setup-ref') {
return content + '.value'
2023-11-23 23:42:08 +08:00
}
2023-11-24 15:25:34 +08:00
return content
2023-11-17 03:01:19 +08:00
}