Skip to content

Commit 77686cf

Browse files
authored
fix(compiler-core): check if expression is constant (#7974)
close #7973
1 parent 63ad77f commit 77686cf

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '../../src'
1414
import { transformIf } from '../../src/transforms/vIf'
1515
import { transformExpression } from '../../src/transforms/transformExpression'
16+
import { PatchFlagNames, PatchFlags } from '../../../shared/src'
1617

1718
function parseWithExpressionTransform(
1819
template: string,
@@ -494,7 +495,9 @@ describe('compiler: expression transform', () => {
494495
setup: BindingTypes.SETUP_MAYBE_REF,
495496
setupConst: BindingTypes.SETUP_CONST,
496497
data: BindingTypes.DATA,
497-
options: BindingTypes.OPTIONS
498+
options: BindingTypes.OPTIONS,
499+
reactive: BindingTypes.SETUP_REACTIVE_CONST,
500+
literal: BindingTypes.LITERAL_CONST
498501
}
499502

500503
function compileWithBindingMetadata(
@@ -532,5 +535,25 @@ describe('compiler: expression transform', () => {
532535
expect(code).toMatch(`_ctx.options`)
533536
expect(code).toMatchSnapshot()
534537
})
538+
539+
test('literal const handling', () => {
540+
const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`, {
541+
inline: true
542+
})
543+
// #7973 should skip patch for literal const
544+
expect(code).not.toMatch(
545+
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
546+
)
547+
})
548+
549+
test('reactive const handling', () => {
550+
const { code } = compileWithBindingMetadata(`<div>{{ reactive }}</div>`, {
551+
inline: true
552+
})
553+
// #7973 should not skip patch for reactive const
554+
expect(code).toMatch(
555+
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
556+
)
557+
})
535558
})
536559
})

packages/compiler-core/src/transforms/transformExpression.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ export function processExpression(
128128
const isDestructureAssignment =
129129
parent && isInDestructureAssignment(parent, parentStack)
130130

131-
if (isConst(type) || localVars[raw]) {
131+
if (
132+
isConst(type) ||
133+
type === BindingTypes.SETUP_REACTIVE_CONST ||
134+
localVars[raw]
135+
) {
132136
return raw
133137
} else if (type === BindingTypes.SETUP_REF) {
134138
return `${raw}.value`
@@ -371,8 +375,6 @@ export function stringifyExpression(exp: ExpressionNode | string): string {
371375

372376
function isConst(type: unknown) {
373377
return (
374-
type === BindingTypes.SETUP_CONST ||
375-
type === BindingTypes.LITERAL_CONST ||
376-
type === BindingTypes.SETUP_REACTIVE_CONST
378+
type === BindingTypes.SETUP_CONST || type === BindingTypes.LITERAL_CONST
377379
)
378380
}

0 commit comments

Comments
 (0)