提交 590dcb8d 编写于 作者: fxy060608's avatar fxy060608 提交者: qiang

fix(mp): BindingTypes.LITERAL_CONST (#4952)

上级 f2b93d70
import { BindingTypes } from '@vue/compiler-core'
import { assert } from './testUtils'
describe('compiler: codegen', () => {
......@@ -91,4 +92,22 @@ return function render(_ctx, _cache) {
}`
)
})
test('setup with literal-const', () => {
assert(
`{{add(count)}}`,
`{{a}}`,
`import { toDisplayString as _toDisplayString, t as _t } from "vue"
export function render(_ctx, _cache, $props, $setup, $data, $options) {
return { a: _t($setup.add($setup.count)) }
}`,
{
inline: false,
bindingMetadata: {
add: BindingTypes.SETUP_CONST,
count: BindingTypes.LITERAL_CONST,
},
}
)
})
})
......@@ -148,10 +148,10 @@ function resolveSetupReference(name: string, context: TransformContext) {
return PascalName
}
}
const fromConst =
checkType(BindingTypes.SETUP_CONST) ||
checkType(BindingTypes.SETUP_REACTIVE_CONST)
checkType(BindingTypes.SETUP_REACTIVE_CONST) ||
checkType(BindingTypes.LITERAL_CONST)
if (fromConst) {
return context.inline
? // in inline mode, const setup bindings (e.g. imports) can be used as-is
......
......@@ -98,6 +98,7 @@ export function processExpression(
}
const { inline, bindingMetadata } = context
const rewriteIdentifier = (raw: string, parent?: Node, id?: Identifier) => {
const type = hasOwn(bindingMetadata, raw) && bindingMetadata[raw]
if (inline) {
......@@ -111,11 +112,7 @@ export function processExpression(
const isDestructureAssignment =
parent && isInDestructureAssignment(parent, parentStack)
if (
type === BindingTypes.SETUP_CONST ||
type === BindingTypes.SETUP_REACTIVE_CONST ||
localVars[raw]
) {
if (isConst(type) || localVars[raw]) {
return raw
} else if (type === BindingTypes.SETUP_REF) {
return `${raw}.value`
......@@ -180,7 +177,10 @@ export function processExpression(
return genPropsAccessExp(bindingMetadata.__propsAliases![raw])
}
} else {
if (type && type.startsWith('setup')) {
if (
(type && type.startsWith('setup')) ||
type === BindingTypes.LITERAL_CONST
) {
// setup bindings in non-inline mode
return `$setup.${raw}`
} else if (type === BindingTypes.PROPS_ALIASED) {
......@@ -215,7 +215,7 @@ export function processExpression(
) {
// const bindings exposed from setup can be skipped for patching but
// cannot be hoisted to module scope
if (bindingMetadata[node.content] === BindingTypes.SETUP_CONST) {
if (isConst(bindingMetadata[node.content])) {
node.constType = ConstantTypes.CAN_SKIP_PATCH
}
node.content = rewriteIdentifier(rawExp)
......@@ -374,3 +374,11 @@ export function isBuiltInIdentifier(id: string | ExpressionNode) {
}
return builtInIdentifiers.includes(id)
}
function isConst(type: unknown) {
return (
type === BindingTypes.SETUP_CONST ||
type === BindingTypes.LITERAL_CONST ||
type === BindingTypes.SETUP_REACTIVE_CONST
)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册