All files / compiler-sfc/src/template resolveTemplateAST.ts

100% Statements 7/7
100% Branches 7/7
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41                                              89x 85x       4x 4x           89x 5x   89x    
import {
  type CompilerError,
  type ElementNode,
  NodeTypes,
  type ParserOptions,
  type RootNode,
  createRoot,
} from '@vue/compiler-core'
import * as CompilerDOM from '@vue/compiler-dom'
 
interface TemplateParser {
  parse(template: string, options: ParserOptions): RootNode
}
 
export function resolveTemplateAST(
  inAST: RootNode | undefined,
  options: {
    compiler?: TemplateParser
    compilerOptions?: ParserOptions
    ssr?: boolean
    onError: (error: CompilerError) => void
  },
): RootNode | undefined {
  if (!inAST?.transformed) {
    return inAST
  }
 
  // Parse the full SFC source to preserve template locations relative to it.
  const { compiler = CompilerDOM, compilerOptions, ssr, onError } = options
  const newAST = (ssr ? CompilerDOM : compiler).parse(inAST.source, {
    prefixIdentifiers: true,
    ...compilerOptions,
    parseMode: 'sfc',
    onError,
  })
  const template = newAST.children.find(
    node => node.type === NodeTypes.ELEMENT && node.tag === 'template',
  ) as ElementNode
  return createRoot(template.children, inAST.source)
}