fix(compiler-core): check if expression is constant (#7974)

close #7973
This commit is contained in:
三咲智子 Kevin Deng 2023-03-29 09:02:16 +08:00 committed by GitHub
parent 63ad77f6f6
commit 77686cf476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -13,6 +13,7 @@ import {
} from '../../src' } from '../../src'
import { transformIf } from '../../src/transforms/vIf' import { transformIf } from '../../src/transforms/vIf'
import { transformExpression } from '../../src/transforms/transformExpression' import { transformExpression } from '../../src/transforms/transformExpression'
import { PatchFlagNames, PatchFlags } from '../../../shared/src'
function parseWithExpressionTransform( function parseWithExpressionTransform(
template: string, template: string,
@ -494,7 +495,9 @@ describe('compiler: expression transform', () => {
setup: BindingTypes.SETUP_MAYBE_REF, setup: BindingTypes.SETUP_MAYBE_REF,
setupConst: BindingTypes.SETUP_CONST, setupConst: BindingTypes.SETUP_CONST,
data: BindingTypes.DATA, data: BindingTypes.DATA,
options: BindingTypes.OPTIONS options: BindingTypes.OPTIONS,
reactive: BindingTypes.SETUP_REACTIVE_CONST,
literal: BindingTypes.LITERAL_CONST
} }
function compileWithBindingMetadata( function compileWithBindingMetadata(
@ -532,5 +535,25 @@ describe('compiler: expression transform', () => {
expect(code).toMatch(`_ctx.options`) expect(code).toMatch(`_ctx.options`)
expect(code).toMatchSnapshot() expect(code).toMatchSnapshot()
}) })
test('literal const handling', () => {
const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`, {
inline: true
})
// #7973 should skip patch for literal const
expect(code).not.toMatch(
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
)
})
test('reactive const handling', () => {
const { code } = compileWithBindingMetadata(`<div>{{ reactive }}</div>`, {
inline: true
})
// #7973 should not skip patch for reactive const
expect(code).toMatch(
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
)
})
}) })
}) })

View File

@ -128,7 +128,11 @@ export function processExpression(
const isDestructureAssignment = const isDestructureAssignment =
parent && isInDestructureAssignment(parent, parentStack) parent && isInDestructureAssignment(parent, parentStack)
if (isConst(type) || localVars[raw]) { if (
isConst(type) ||
type === BindingTypes.SETUP_REACTIVE_CONST ||
localVars[raw]
) {
return raw return raw
} else if (type === BindingTypes.SETUP_REF) { } else if (type === BindingTypes.SETUP_REF) {
return `${raw}.value` return `${raw}.value`
@ -371,8 +375,6 @@ export function stringifyExpression(exp: ExpressionNode | string): string {
function isConst(type: unknown) { function isConst(type: unknown) {
return ( return (
type === BindingTypes.SETUP_CONST || type === BindingTypes.SETUP_CONST || type === BindingTypes.LITERAL_CONST
type === BindingTypes.LITERAL_CONST ||
type === BindingTypes.SETUP_REACTIVE_CONST
) )
} }