2019-09-18 07:08:47 +08:00
|
|
|
import {
|
|
|
|
RootNode,
|
|
|
|
NodeTypes,
|
|
|
|
ParentNode,
|
|
|
|
ChildNode,
|
|
|
|
ElementNode,
|
2019-09-22 05:42:12 +08:00
|
|
|
DirectiveNode,
|
|
|
|
Property
|
2019-09-18 07:08:47 +08:00
|
|
|
} from './ast'
|
|
|
|
import { isString } from '@vue/shared'
|
2019-09-21 00:16:19 +08:00
|
|
|
import { CompilerError, defaultOnError } from './errors'
|
2019-09-18 07:08:47 +08:00
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
// There are two types of transforms:
|
|
|
|
//
|
|
|
|
// - NodeTransform:
|
|
|
|
// Transforms that operate directly on a ChildNode. NodeTransforms may mutate,
|
|
|
|
// replace or remove the node being processed.
|
|
|
|
export type NodeTransform = (node: ChildNode, context: TransformContext) => void
|
2019-09-18 07:08:47 +08:00
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
// - DirectiveTransform:
|
|
|
|
// Transforms that handles a single directive attribute on an element.
|
|
|
|
// It translates the raw directive into actual props for the VNode.
|
2019-09-18 07:08:47 +08:00
|
|
|
export type DirectiveTransform = (
|
2019-09-22 05:42:12 +08:00
|
|
|
dir: DirectiveNode,
|
|
|
|
context: TransformContext
|
|
|
|
) => {
|
|
|
|
props: Property | Property[]
|
|
|
|
needRuntime: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
// A structural directive transform is a techically a NodeTransform;
|
|
|
|
// Only v-if and v-for fall into this category.
|
|
|
|
export type StructuralDirectiveTransform = (
|
2019-09-18 07:08:47 +08:00
|
|
|
node: ElementNode,
|
|
|
|
dir: DirectiveNode,
|
|
|
|
context: TransformContext
|
2019-09-22 05:42:12 +08:00
|
|
|
) => void
|
2019-09-18 07:08:47 +08:00
|
|
|
|
|
|
|
export interface TransformOptions {
|
2019-09-22 05:42:12 +08:00
|
|
|
nodeTransforms?: NodeTransform[]
|
|
|
|
directiveTransforms?: { [name: string]: DirectiveTransform }
|
2019-09-18 07:08:47 +08:00
|
|
|
onError?: (error: CompilerError) => void
|
|
|
|
}
|
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
export interface TransformContext extends Required<TransformOptions> {
|
2019-09-23 11:07:36 +08:00
|
|
|
imports: Set<string>
|
|
|
|
statements: string[]
|
|
|
|
identifiers: { [name: string]: true }
|
2019-09-18 07:08:47 +08:00
|
|
|
parent: ParentNode
|
|
|
|
ancestors: ParentNode[]
|
|
|
|
childIndex: number
|
2019-09-20 03:41:17 +08:00
|
|
|
currentNode: ChildNode | null
|
2019-09-18 07:08:47 +08:00
|
|
|
replaceNode(node: ChildNode): void
|
2019-09-20 03:41:17 +08:00
|
|
|
removeNode(node?: ChildNode): void
|
|
|
|
onNodeRemoved: () => void
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createTransformContext(
|
|
|
|
root: RootNode,
|
|
|
|
options: TransformOptions
|
|
|
|
): TransformContext {
|
|
|
|
const context: TransformContext = {
|
2019-09-23 11:07:36 +08:00
|
|
|
imports: new Set(),
|
|
|
|
statements: [],
|
|
|
|
identifiers: {},
|
2019-09-22 05:42:12 +08:00
|
|
|
nodeTransforms: options.nodeTransforms || [],
|
|
|
|
directiveTransforms: options.directiveTransforms || {},
|
|
|
|
onError: options.onError || defaultOnError,
|
2019-09-18 07:08:47 +08:00
|
|
|
parent: root,
|
2019-09-20 00:20:59 +08:00
|
|
|
ancestors: [],
|
2019-09-18 07:08:47 +08:00
|
|
|
childIndex: 0,
|
2019-09-20 03:41:17 +08:00
|
|
|
currentNode: null,
|
2019-09-18 07:08:47 +08:00
|
|
|
replaceNode(node) {
|
2019-09-20 03:41:17 +08:00
|
|
|
if (__DEV__ && !context.currentNode) {
|
|
|
|
throw new Error(`node being replaced is already removed.`)
|
2019-09-20 00:20:59 +08:00
|
|
|
}
|
2019-09-20 03:41:17 +08:00
|
|
|
context.parent.children[context.childIndex] = context.currentNode = node
|
2019-09-18 07:08:47 +08:00
|
|
|
},
|
2019-09-20 03:41:17 +08:00
|
|
|
removeNode(node) {
|
|
|
|
const list = context.parent.children
|
|
|
|
const removalIndex = node
|
2019-09-23 14:52:54 +08:00
|
|
|
? list.indexOf(node as any)
|
2019-09-20 03:41:17 +08:00
|
|
|
: context.currentNode
|
|
|
|
? context.childIndex
|
|
|
|
: -1
|
|
|
|
if (__DEV__ && removalIndex < 0) {
|
|
|
|
throw new Error(`node being removed is not a child of current parent`)
|
|
|
|
}
|
|
|
|
if (!node || node === context.currentNode) {
|
|
|
|
// current node removed
|
|
|
|
context.currentNode = null
|
|
|
|
context.onNodeRemoved()
|
|
|
|
} else {
|
|
|
|
// sibling node removed
|
|
|
|
if (context.childIndex > removalIndex) {
|
|
|
|
context.childIndex--
|
|
|
|
context.onNodeRemoved()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
context.parent.children.splice(removalIndex, 1)
|
2019-09-18 07:08:47 +08:00
|
|
|
},
|
2019-09-20 03:41:17 +08:00
|
|
|
onNodeRemoved: () => {}
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:07:36 +08:00
|
|
|
export function transform(root: RootNode, options: TransformOptions) {
|
|
|
|
const context = createTransformContext(root, options)
|
|
|
|
traverseChildren(root, context)
|
|
|
|
root.imports = [...context.imports]
|
|
|
|
root.statements = context.statements
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:55:18 +08:00
|
|
|
export function traverseChildren(
|
2019-09-18 07:08:47 +08:00
|
|
|
parent: ParentNode,
|
2019-09-23 08:55:18 +08:00
|
|
|
context: TransformContext
|
2019-09-18 07:08:47 +08:00
|
|
|
) {
|
2019-09-23 11:07:36 +08:00
|
|
|
// ancestors and identifiers need to be cached here since they may get
|
|
|
|
// replaced during a child's traversal
|
2019-09-23 08:55:18 +08:00
|
|
|
const ancestors = context.ancestors.concat(parent)
|
2019-09-23 11:07:36 +08:00
|
|
|
const identifiers = context.identifiers
|
2019-09-20 03:41:17 +08:00
|
|
|
let i = 0
|
|
|
|
const nodeRemoved = () => {
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
for (; i < parent.children.length; i++) {
|
2019-09-23 14:52:54 +08:00
|
|
|
const child = parent.children[i]
|
|
|
|
if (isString(child)) continue
|
|
|
|
context.currentNode = child
|
2019-09-18 07:08:47 +08:00
|
|
|
context.parent = parent
|
|
|
|
context.ancestors = ancestors
|
|
|
|
context.childIndex = i
|
2019-09-20 03:41:17 +08:00
|
|
|
context.onNodeRemoved = nodeRemoved
|
2019-09-23 11:07:36 +08:00
|
|
|
context.identifiers = identifiers
|
2019-09-23 14:52:54 +08:00
|
|
|
traverseNode(child, context)
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 08:55:18 +08:00
|
|
|
export function traverseNode(node: ChildNode, context: TransformContext) {
|
2019-09-18 07:08:47 +08:00
|
|
|
// apply transform plugins
|
2019-09-22 05:42:12 +08:00
|
|
|
const { nodeTransforms } = context
|
|
|
|
for (let i = 0; i < nodeTransforms.length; i++) {
|
|
|
|
const plugin = nodeTransforms[i]
|
2019-09-20 00:20:59 +08:00
|
|
|
plugin(node, context)
|
2019-09-23 08:55:18 +08:00
|
|
|
if (!context.currentNode) {
|
|
|
|
// node was removed
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// node may have been replaced
|
|
|
|
node = context.currentNode
|
|
|
|
}
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// further traverse downwards
|
|
|
|
switch (node.type) {
|
|
|
|
case NodeTypes.IF:
|
|
|
|
for (let i = 0; i < node.branches.length; i++) {
|
2019-09-23 08:55:18 +08:00
|
|
|
traverseChildren(node.branches[i], context)
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case NodeTypes.FOR:
|
|
|
|
case NodeTypes.ELEMENT:
|
2019-09-23 08:55:18 +08:00
|
|
|
traverseChildren(node, context)
|
2019-09-18 07:08:47 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 05:42:12 +08:00
|
|
|
export function createStructuralDirectiveTransform(
|
2019-09-18 07:08:47 +08:00
|
|
|
name: string | RegExp,
|
2019-09-22 05:42:12 +08:00
|
|
|
fn: StructuralDirectiveTransform
|
|
|
|
): NodeTransform {
|
2019-09-18 07:08:47 +08:00
|
|
|
const matches = isString(name)
|
|
|
|
? (n: string) => n === name
|
|
|
|
: (n: string) => name.test(n)
|
|
|
|
|
|
|
|
return (node, context) => {
|
|
|
|
if (node.type === NodeTypes.ELEMENT) {
|
2019-09-22 05:42:12 +08:00
|
|
|
const { props } = node
|
|
|
|
for (let i = 0; i < props.length; i++) {
|
|
|
|
const prop = props[i]
|
|
|
|
if (prop.type === NodeTypes.DIRECTIVE && matches(prop.name)) {
|
2019-09-23 08:55:18 +08:00
|
|
|
// structural directives are removed to avoid infinite recursion
|
|
|
|
// also we remove them *before* applying so that it can further
|
|
|
|
// traverse itself in case it moves the node around
|
2019-09-22 05:42:12 +08:00
|
|
|
props.splice(i, 1)
|
|
|
|
i--
|
2019-09-23 08:55:18 +08:00
|
|
|
fn(node, prop, context)
|
2019-09-18 07:08:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|