All files / compiler-sfc/src/script importUsageCheck.ts

100% Statements 104/104
100% Branches 43/43
100% Functions 6/6
100% Lines 104/104

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1441x                                   1x 46x 46x   1x     1x   1x 45x 45x 45x 45x   46x 46x 46x   91x 91x 91x       91x 91x 91x 91x 51x 51x       91x 91x   91x   91x 108x 108x 63x 63x 63x 63x 17x 63x 17x 12x 12x 12x 17x 63x 46x 46x 42x 16x 1x 1x 16x     42x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x     42x 42x 16x 11x 42x 3x 3x   42x 16x 2x 15x 9x 14x   2x 2x 16x 42x 46x 46x 20x 4x 3x 46x 2x 2x 46x 63x 63x 108x 31x 31x 108x 108x   91x 91x 91x 91x   26x 26x 18x 26x 8x 8x 26x  
import type { SFCDescriptor } from '../parse'
import {
  type ExpressionNode,
  NodeTypes,
  type SimpleExpressionNode,
  type TemplateChildNode,
  isSimpleIdentifier,
  parserOptions,
  walkIdentifiers,
} from '@vue/compiler-dom'
import { createCache } from '../cache'
import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
 
/**
 * Check if an import is used in the SFC's template. This is used to determine
 * the properties that should be included in the object returned from setup()
 * when not using inline mode.
 */
export function isImportUsed(local: string, sfc: SFCDescriptor): boolean {
  return resolveTemplateUsedIdentifiers(sfc).has(local)
}
 
const templateAnalysisCache = createCache<{
  usedIds?: Set<string>
  vModelIds: Set<string>
}>()
 
export function resolveTemplateVModelIdentifiers(
  sfc: SFCDescriptor,
): Set<string> {
  return resolveTemplateAnalysisResult(sfc, false).vModelIds
}
 
function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set<string> {
  return resolveTemplateAnalysisResult(sfc).usedIds!
}
 
function resolveTemplateAnalysisResult(
  sfc: SFCDescriptor,
  collectUsedIds = true,
): {
  usedIds?: Set<string>
  vModelIds: Set<string>
} {
  const { content, ast } = sfc.template!
  const cached = templateAnalysisCache.get(content)
  if (cached && (!collectUsedIds || cached.usedIds)) {
    return cached
  }
 
  // When `collectUsedIds` is false we skip the expensive identifier extraction
  // and only collect `vModelIds`.
  const ids = collectUsedIds ? new Set<string>() : undefined
  const vModelIds = new Set<string>()
 
  ast!.children.forEach(walk)
 
  function walk(node: TemplateChildNode) {
    switch (node.type) {
      case NodeTypes.ELEMENT:
        let tag = node.tag
        if (tag.includes('.')) tag = tag.split('.')[0].trim()
        if (
          !parserOptions.isNativeTag!(tag) &&
          !parserOptions.isBuiltInComponent!(tag)
        ) {
          if (ids) {
            ids.add(camelize(tag))
            ids.add(capitalize(camelize(tag)))
          }
        }
        for (let i = 0; i < node.props.length; i++) {
          const prop = node.props[i]
          if (prop.type === NodeTypes.DIRECTIVE) {
            if (ids) {
              if (!isBuiltInDirective(prop.name)) {
                ids.add(`v${capitalize(camelize(prop.name))}`)
              }
            }
 
            // collect v-model target identifiers (simple identifiers only)
            if (prop.name === 'model') {
              const exp = prop.exp
              if (exp && exp.type === NodeTypes.SIMPLE_EXPRESSION) {
                const expString = exp.content.trim()
                if (
                  isSimpleIdentifier(expString) &&
                  expString !== 'undefined'
                ) {
                  vModelIds.add(expString)
                }
              }
            }
 
            // process dynamic directive arguments
            if (
              ids &&
              prop.arg &&
              !(prop.arg as SimpleExpressionNode).isStatic
            ) {
              extractIdentifiers(ids, prop.arg)
            }
 
            if (ids) {
              if (prop.name === 'for') {
                extractIdentifiers(ids, prop.forParseResult!.source)
              } else if (prop.exp) {
                extractIdentifiers(ids, prop.exp)
              } else if (prop.name === 'bind' && !prop.exp) {
                // v-bind shorthand name as identifier
                ids.add(camelize((prop.arg as SimpleExpressionNode).content))
              }
            }
          }
          if (
            ids &&
            prop.type === NodeTypes.ATTRIBUTE &&
            prop.name === 'ref' &&
            prop.value?.content
          ) {
            ids.add(prop.value.content)
          }
        }
        node.children.forEach(walk)
        break
      case NodeTypes.INTERPOLATION:
        if (ids) extractIdentifiers(ids, node.content)
        break
    }
  }
 
  const result = { usedIds: ids, vModelIds }
  templateAnalysisCache.set(content, result)
  return result
}
 
function extractIdentifiers(ids: Set<string>, node: ExpressionNode) {
  if (node.ast) {
    walkIdentifiers(node.ast, n => ids.add(n.name))
  } else if (node.ast === null) {
    ids.add((node as SimpleExpressionNode).content)
  }
}