Skip to content

Commit a75d1c5

Browse files
authoredOct 28, 2023
fix(compiler-core): known global should be shadowed by local variables in expression rewrite (#9492)
fix #9482
1 parent edf2572 commit a75d1c5

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed
 

‎packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
exports[`compiler: expression transform > bindingMetadata > inline mode 1`] = `
44
"(_ctx, _cache) => {
5-
return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString(__props.props) + \\" \\" + _toDisplayString(_unref(setup)) + \\" \\" + _toDisplayString(setupConst) + \\" \\" + _toDisplayString(_ctx.data) + \\" \\" + _toDisplayString(_ctx.options), 1 /* TEXT */))
5+
return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString(__props.props) + \\" \\" + _toDisplayString(_unref(setup)) + \\" \\" + _toDisplayString(setupConst) + \\" \\" + _toDisplayString(_ctx.data) + \\" \\" + _toDisplayString(_ctx.options) + \\" \\" + _toDisplayString(isNaN.value), 1 /* TEXT */))
66
}"
77
`;
88

99
exports[`compiler: expression transform > bindingMetadata > non-inline mode 1`] = `
1010
"const { toDisplayString: _toDisplayString, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
1111
1212
return function render(_ctx, _cache, $props, $setup, $data, $options) {
13-
return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString($props.props) + \\" \\" + _toDisplayString($setup.setup) + \\" \\" + _toDisplayString($data.data) + \\" \\" + _toDisplayString($options.options), 1 /* TEXT */))
13+
return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString($props.props) + \\" \\" + _toDisplayString($setup.setup) + \\" \\" + _toDisplayString($data.data) + \\" \\" + _toDisplayString($options.options) + \\" \\" + _toDisplayString($setup.isNaN), 1 /* TEXT */))
1414
}"
1515
`;

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ describe('compiler: expression transform', () => {
506506
data: BindingTypes.DATA,
507507
options: BindingTypes.OPTIONS,
508508
reactive: BindingTypes.SETUP_REACTIVE_CONST,
509-
literal: BindingTypes.LITERAL_CONST
509+
literal: BindingTypes.LITERAL_CONST,
510+
isNaN: BindingTypes.SETUP_REF
510511
}
511512

512513
function compileWithBindingMetadata(
@@ -522,10 +523,11 @@ describe('compiler: expression transform', () => {
522523

523524
test('non-inline mode', () => {
524525
const { code } = compileWithBindingMetadata(
525-
`<div>{{ props }} {{ setup }} {{ data }} {{ options }}</div>`
526+
`<div>{{ props }} {{ setup }} {{ data }} {{ options }} {{ isNaN }}</div>`
526527
)
527528
expect(code).toMatch(`$props.props`)
528529
expect(code).toMatch(`$setup.setup`)
530+
expect(code).toMatch(`$setup.isNaN`)
529531
expect(code).toMatch(`$data.data`)
530532
expect(code).toMatch(`$options.options`)
531533
expect(code).toMatch(`_ctx, _cache, $props, $setup, $data, $options`)
@@ -534,14 +536,15 @@ describe('compiler: expression transform', () => {
534536

535537
test('inline mode', () => {
536538
const { code } = compileWithBindingMetadata(
537-
`<div>{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }}</div>`,
539+
`<div>{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }} {{ isNaN }}</div>`,
538540
{ inline: true }
539541
)
540542
expect(code).toMatch(`__props.props`)
541543
expect(code).toMatch(`_unref(setup)`)
542544
expect(code).toMatch(`_toDisplayString(setupConst)`)
543545
expect(code).toMatch(`_ctx.data`)
544546
expect(code).toMatch(`_ctx.options`)
547+
expect(code).toMatch(`isNaN.value`)
545548
expect(code).toMatchSnapshot()
546549
})
547550

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,15 @@ export function processExpression(
227227
const isScopeVarReference = context.identifiers[rawExp]
228228
const isAllowedGlobal = isGloballyAllowed(rawExp)
229229
const isLiteral = isLiteralWhitelisted(rawExp)
230-
if (!asParams && !isScopeVarReference && !isAllowedGlobal && !isLiteral) {
230+
if (
231+
!asParams &&
232+
!isScopeVarReference &&
233+
!isLiteral &&
234+
(!isAllowedGlobal || bindingMetadata[rawExp])
235+
) {
231236
// const bindings exposed from setup can be skipped for patching but
232237
// cannot be hoisted to module scope
233-
if (isConst(bindingMetadata[node.content])) {
238+
if (isConst(bindingMetadata[rawExp])) {
234239
node.constType = ConstantTypes.CAN_SKIP_PATCH
235240
}
236241
node.content = rewriteIdentifier(rawExp)

0 commit comments

Comments
 (0)
Please sign in to comment.