From fcf0c5cfb3e532d67b007fa3c746e21aa5b59e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Fri, 27 Oct 2023 16:27:51 +0800 Subject: [PATCH 1/2] fix(compiler-core): fix variable naming conflicts using priority --- .../__snapshots__/transformExpressions.spec.ts.snap | 4 ++-- .../__tests__/transforms/transformExpressions.spec.ts | 9 ++++++--- .../compiler-core/src/transforms/transformExpression.ts | 5 ++++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap b/packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap index c72e0229832..504c866e128 100644 --- a/packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap +++ b/packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap @@ -2,7 +2,7 @@ exports[`compiler: expression transform > bindingMetadata > inline mode 1`] = ` "(_ctx, _cache) => { - return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString(__props.props) + \\" \\" + _toDisplayString(_unref(setup)) + \\" \\" + _toDisplayString(setupConst) + \\" \\" + _toDisplayString(_ctx.data) + \\" \\" + _toDisplayString(_ctx.options), 1 /* TEXT */)) + return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString(__props.props) + \\" \\" + _toDisplayString(_unref(setup)) + \\" \\" + _toDisplayString(setupConst) + \\" \\" + _toDisplayString(_ctx.data) + \\" \\" + _toDisplayString(_ctx.options) + \\" \\" + _toDisplayString(isNaN.value), 1 /* TEXT */)) }" `; @@ -10,6 +10,6 @@ exports[`compiler: expression transform > bindingMetadata > non-inline mode 1`] "const { toDisplayString: _toDisplayString, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue return function render(_ctx, _cache, $props, $setup, $data, $options) { - return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString($props.props) + \\" \\" + _toDisplayString($setup.setup) + \\" \\" + _toDisplayString($data.data) + \\" \\" + _toDisplayString($options.options), 1 /* TEXT */)) + return (_openBlock(), _createElementBlock(\\"div\\", null, _toDisplayString($props.props) + \\" \\" + _toDisplayString($setup.setup) + \\" \\" + _toDisplayString($data.data) + \\" \\" + _toDisplayString($options.options) + \\" \\" + _toDisplayString($setup.isNaN), 1 /* TEXT */)) }" `; diff --git a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts index 686794c23ab..f8b82396b08 100644 --- a/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts @@ -506,7 +506,8 @@ describe('compiler: expression transform', () => { data: BindingTypes.DATA, options: BindingTypes.OPTIONS, reactive: BindingTypes.SETUP_REACTIVE_CONST, - literal: BindingTypes.LITERAL_CONST + literal: BindingTypes.LITERAL_CONST, + isNaN: BindingTypes.SETUP_REF } function compileWithBindingMetadata( @@ -522,10 +523,11 @@ describe('compiler: expression transform', () => { test('non-inline mode', () => { const { code } = compileWithBindingMetadata( - `
{{ props }} {{ setup }} {{ data }} {{ options }}
` + `
{{ props }} {{ setup }} {{ data }} {{ options }} {{ isNaN }}
` ) expect(code).toMatch(`$props.props`) expect(code).toMatch(`$setup.setup`) + expect(code).toMatch(`$setup.isNaN`) expect(code).toMatch(`$data.data`) expect(code).toMatch(`$options.options`) expect(code).toMatch(`_ctx, _cache, $props, $setup, $data, $options`) @@ -534,7 +536,7 @@ describe('compiler: expression transform', () => { test('inline mode', () => { const { code } = compileWithBindingMetadata( - `
{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }}
`, + `
{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }} {{ isNaN }}
`, { inline: true } ) expect(code).toMatch(`__props.props`) @@ -542,6 +544,7 @@ describe('compiler: expression transform', () => { expect(code).toMatch(`_toDisplayString(setupConst)`) expect(code).toMatch(`_ctx.data`) expect(code).toMatch(`_ctx.options`) + expect(code).toMatch(`isNaN.value`) expect(code).toMatchSnapshot() }) diff --git a/packages/compiler-core/src/transforms/transformExpression.ts b/packages/compiler-core/src/transforms/transformExpression.ts index eab6b237f13..5a8483dedf0 100644 --- a/packages/compiler-core/src/transforms/transformExpression.ts +++ b/packages/compiler-core/src/transforms/transformExpression.ts @@ -235,7 +235,10 @@ export function processExpression( } node.content = rewriteIdentifier(rawExp) } else if (!isScopeVarReference) { - if (isLiteral) { + if (isAllowedGlobal && bindingMetadata[rawExp]) { + // #9482 + node.content = rewriteIdentifier(rawExp) + } else if (isLiteral) { node.constType = ConstantTypes.CAN_STRINGIFY } else { node.constType = ConstantTypes.CAN_HOIST From 8f2e32cb9b43b0b8e949e455ac78b634d119a3c9 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sat, 28 Oct 2023 16:17:50 +0900 Subject: [PATCH 2/2] refactor: move condition up --- .../src/transforms/transformExpression.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/compiler-core/src/transforms/transformExpression.ts b/packages/compiler-core/src/transforms/transformExpression.ts index 5a8483dedf0..52d1fb42a13 100644 --- a/packages/compiler-core/src/transforms/transformExpression.ts +++ b/packages/compiler-core/src/transforms/transformExpression.ts @@ -227,18 +227,20 @@ export function processExpression( const isScopeVarReference = context.identifiers[rawExp] const isAllowedGlobal = isGloballyAllowed(rawExp) const isLiteral = isLiteralWhitelisted(rawExp) - if (!asParams && !isScopeVarReference && !isAllowedGlobal && !isLiteral) { + if ( + !asParams && + !isScopeVarReference && + !isLiteral && + (!isAllowedGlobal || bindingMetadata[rawExp]) + ) { // const bindings exposed from setup can be skipped for patching but // cannot be hoisted to module scope - if (isConst(bindingMetadata[node.content])) { + if (isConst(bindingMetadata[rawExp])) { node.constType = ConstantTypes.CAN_SKIP_PATCH } node.content = rewriteIdentifier(rawExp) } else if (!isScopeVarReference) { - if (isAllowedGlobal && bindingMetadata[rawExp]) { - // #9482 - node.content = rewriteIdentifier(rawExp) - } else if (isLiteral) { + if (isLiteral) { node.constType = ConstantTypes.CAN_STRINGIFY } else { node.constType = ConstantTypes.CAN_HOIST