抽象语法树代码源文件。

createVNodeCall

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

  export function createVNodeCall(
    context: TransformContext | null,
    tag: VNodeCall['tag'],
    props?: VNodeCall['props'],
    children?: VNodeCall['children'],
    patchFlag?: VNodeCall['patchFlag'],
    dynamicProps?: VNodeCall['dynamicProps'],
    directives?: VNodeCall['directives'],
    isBlock: VNodeCall['isBlock'] = false,
    isForBlock: VNodeCall['isForBlock'] = false,
    loc = locStub
  ): VNodeCall {
    if (context) {
      if (isBlock) {
        context.helper(OPEN_BLOCK)
        context.helper(CREATE_BLOCK)
      } else {
        context.helper(CREATE_VNODE)
      }
      if (directives) {
        context.helper(WITH_DIRECTIVES)
      }
    }

    return {
      type: NodeTypes.VNODE_CALL,
      tag,
      props,
      children,
      patchFlag,
      dynamicProps,
      directives,
      isBlock,
      isForBlock,
      loc
    }
  }

createRoot(children, loc = locStub)

创建根节点对象,返回一个 RootNode 类型对象。

参数:

  1. children 节点子孙节点,类型:TemplateChildNode[]​

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
      export type TemplateChildNode =
          | ElementNode // 节元素点类型
          | InterpolationNode // 插值节点
          | CompoundExpressionNode // 混合表达式节点
          | TextNode // 文本节点
          | CommentNode // 注释节点
          | IfNode // v-if 节点
          | IfBranchNode // v-else, v-else-if 分支节点
          | ForNode // v-ofr 节点
          | TextCallNode // ???
    
  2. loc 一个 SourceLoation 类型的结构,默认值为 locStub

    1
    2
    3
    4
    5
    
      export const locStub: SourceLocation = {
          source: '',
          start: { line: 1, column: 1, offset: 0 },
          end: { line: 1, column: 1, offset: 0 }
      }
    

代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  export function createRoot(
      children: TemplateChildNode[],
      loc = locStub
  ): RootNode {
      return {
          type: NodeTypes.ROOT,
          children,
          helpers: [],
          components: [],
          directives: [],
          hoists: [],
          imports: [],
          cached: 0,
          temps: 0,
          codegenNode: undefined,
          loc
      }
  }