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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 48x 16x 47x 48x 95x 95x 95x 53x 42x 95x 95x 95x 112x 65x 65x 65x 17x 12x 12x 65x 46x 46x 42x 16x 1x 42x 7x 7x 7x 7x 7x 42x 3x 42x 16x 2x 14x 9x 5x 2x 46x 2x 65x 65x 33x 33x 95x 95x 95x 28x 18x 10x 10x | import type { SFCDescriptor } from '../parse'
import type { SFCTemplateCompileOptions } from '../compileTemplate'
import {
type ExpressionNode,
NodeTypes,
type SimpleExpressionNode,
type TemplateChildNode,
isSimpleIdentifier,
parserOptions,
walkIdentifiers,
} from '@vue/compiler-dom'
import type { LRUCache } from 'lru-cache'
import { createCache } from '../cache'
import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
import { resolveTemplateAST } from '../template/resolveTemplateAST'
type TemplateOptions = Pick<
SFCTemplateCompileOptions,
'compiler' | 'compilerOptions' | 'ssr'
>
/**
* 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,
options?: TemplateOptions,
): boolean {
return resolveTemplateUsedIdentifiers(sfc, options).has(local)
}
type TemplateAnalysisResult = {
usedIds?: Set<string>
vModelIds: Set<string>
}
export const templateAnalysisCache:
| Map<string, TemplateAnalysisResult>
| LRUCache<string, TemplateAnalysisResult> =
createCache<TemplateAnalysisResult>()
export function resolveTemplateVModelIdentifiers(
sfc: SFCDescriptor,
options?: TemplateOptions,
): Set<string> {
return resolveTemplateAnalysisResult(sfc, false, options).vModelIds
}
function resolveTemplateUsedIdentifiers(
sfc: SFCDescriptor,
options?: TemplateOptions,
): Set<string> {
return resolveTemplateAnalysisResult(sfc, true, options).usedIds!
}
function resolveTemplateAnalysisResult(
sfc: SFCDescriptor,
collectUsedIds = true,
options?: TemplateOptions,
): {
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>()
const root = resolveTemplateAST(ast, {
compiler: options?.compiler,
compilerOptions: options?.compilerOptions,
ssr: options?.ssr,
// ignore errors since they were already reported by the SFC parser
onError: () => {},
})
root!.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
Eif (exp && exp.type === NodeTypes.SIMPLE_EXPRESSION) {
const expString = exp.content.trim()
Eif (
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 Eif (node.ast === null) {
ids.add((node as SimpleExpressionNode).content)
}
}
|