refactor: use more efficient walk for importUsageCheck
This commit is contained in:
parent
37f9d3da8f
commit
b59eabdc0d
|
@ -5,8 +5,8 @@ import {
|
||||||
SimpleExpressionNode,
|
SimpleExpressionNode,
|
||||||
forAliasRE,
|
forAliasRE,
|
||||||
parserOptions,
|
parserOptions,
|
||||||
transform,
|
walkIdentifiers,
|
||||||
walkIdentifiers
|
TemplateChildNode
|
||||||
} from '@vue/compiler-dom'
|
} from '@vue/compiler-dom'
|
||||||
import { createCache } from '../cache'
|
import { createCache } from '../cache'
|
||||||
import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
|
import { camelize, capitalize, isBuiltInDirective } from '@vue/shared'
|
||||||
|
@ -34,53 +34,54 @@ function resolveTemplateUsageCheckString(sfc: SFCDescriptor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let code = ''
|
let code = ''
|
||||||
transform(ast, {
|
|
||||||
nodeTransforms: [
|
|
||||||
node => {
|
|
||||||
if (node.type === NodeTypes.ELEMENT) {
|
|
||||||
if (
|
|
||||||
!parserOptions.isNativeTag!(node.tag) &&
|
|
||||||
!parserOptions.isBuiltInComponent!(node.tag)
|
|
||||||
) {
|
|
||||||
code += `,${camelize(node.tag)},${capitalize(camelize(node.tag))}`
|
|
||||||
}
|
|
||||||
for (let i = 0; i < node.props.length; i++) {
|
|
||||||
const prop = node.props[i]
|
|
||||||
if (prop.type === NodeTypes.DIRECTIVE) {
|
|
||||||
if (!isBuiltInDirective(prop.name)) {
|
|
||||||
code += `,v${capitalize(camelize(prop.name))}`
|
|
||||||
}
|
|
||||||
|
|
||||||
// process dynamic directive arguments
|
ast!.children.forEach(walk)
|
||||||
if (prop.arg && !(prop.arg as SimpleExpressionNode).isStatic) {
|
|
||||||
code += `,${stripStrings(
|
|
||||||
(prop.arg as SimpleExpressionNode).content
|
|
||||||
)}`
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prop.exp) {
|
function walk(node: TemplateChildNode) {
|
||||||
code += `,${processExp(
|
switch (node.type) {
|
||||||
(prop.exp as SimpleExpressionNode).content,
|
case NodeTypes.ELEMENT:
|
||||||
prop.name
|
if (
|
||||||
)}`
|
!parserOptions.isNativeTag!(node.tag) &&
|
||||||
}
|
!parserOptions.isBuiltInComponent!(node.tag)
|
||||||
}
|
) {
|
||||||
if (
|
code += `,${camelize(node.tag)},${capitalize(camelize(node.tag))}`
|
||||||
prop.type === NodeTypes.ATTRIBUTE &&
|
|
||||||
prop.name === 'ref' &&
|
|
||||||
prop.value?.content
|
|
||||||
) {
|
|
||||||
code += `,${prop.value.content}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (node.type === NodeTypes.INTERPOLATION) {
|
|
||||||
code += `,${processExp(
|
|
||||||
(node.content as SimpleExpressionNode).content
|
|
||||||
)}`
|
|
||||||
}
|
}
|
||||||
}
|
for (let i = 0; i < node.props.length; i++) {
|
||||||
]
|
const prop = node.props[i]
|
||||||
})
|
if (prop.type === NodeTypes.DIRECTIVE) {
|
||||||
|
if (!isBuiltInDirective(prop.name)) {
|
||||||
|
code += `,v${capitalize(camelize(prop.name))}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// process dynamic directive arguments
|
||||||
|
if (prop.arg && !(prop.arg as SimpleExpressionNode).isStatic) {
|
||||||
|
code += `,${stripStrings(
|
||||||
|
(prop.arg as SimpleExpressionNode).content
|
||||||
|
)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prop.exp) {
|
||||||
|
code += `,${processExp(
|
||||||
|
(prop.exp as SimpleExpressionNode).content,
|
||||||
|
prop.name
|
||||||
|
)}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
prop.type === NodeTypes.ATTRIBUTE &&
|
||||||
|
prop.name === 'ref' &&
|
||||||
|
prop.value?.content
|
||||||
|
) {
|
||||||
|
code += `,${prop.value.content}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node.children.forEach(walk)
|
||||||
|
break
|
||||||
|
case NodeTypes.INTERPOLATION:
|
||||||
|
code += `,${processExp((node.content as SimpleExpressionNode).content)}`
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
code += ';'
|
code += ';'
|
||||||
templateUsageCheckCache.set(content, code)
|
templateUsageCheckCache.set(content, code)
|
||||||
|
|
Loading…
Reference in New Issue