Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-core): Fix the 'constType' error when using 'v-bind' to bind array variables to 'style', resulting in runtime errors(fix #5106) #5898

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/compiler-core/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { transformElement } from '../../src/transforms/transformElement'
import {
CAMELIZE,
helperNameMap,
NORMALIZE_PROPS
NORMALIZE_PROPS,
NORMALIZE_STYLE
} from '../../src/runtimeHelpers'
import { transformExpression } from '../../src/transforms/transformExpression'

Expand Down Expand Up @@ -310,4 +311,26 @@ describe('compiler: transform v-bind', () => {
}
})
})
test('Bind array constant to style', () => {
const node = parseWithVBind(`<div v-bind:style="[color:red]"/>`)
const props = (node.codegenNode as VNodeCall).props as ObjectExpression
expect(props.properties[0]).toMatchObject({
key: {
type:4,
content: `style`,
isStatic: true,
constType:3
},
value: {
type:14,
callee:NORMALIZE_STYLE,
arguments:[
{type : 4,
content : "[color:red]",
isStatic : false,
constType : 0,}
]
}
})
})
})
13 changes: 10 additions & 3 deletions packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ export const transformExpression: NodeTransform = (node, context) => {
exp.type === NodeTypes.SIMPLE_EXPRESSION &&
!(dir.name === 'on' && arg)
) {
const isBindStyle = dir.name === 'bind' && arg && (arg as SimpleExpressionNode).content && (arg as SimpleExpressionNode).content === 'style'
dir.exp = processExpression(
exp,
context,
// slot args must be processed as function params
dir.name === 'slot'
dir.name === 'slot',
false,
Object.create(context.identifiers),
(isBindStyle ? true : false)
)
}
if (arg && arg.type === NodeTypes.SIMPLE_EXPRESSION && !arg.isStatic) {
Expand Down Expand Up @@ -94,7 +98,8 @@ export function processExpression(
asParams = false,
// v-on handler values may contain multiple statements
asRawStatements = false,
localVars: Record<string, number> = Object.create(context.identifiers)
localVars: Record<string, number> = Object.create(context.identifiers),
isBindStyle:boolean | undefined = false,
): ExpressionNode {
if (__BROWSER__) {
if (__DEV__) {
Expand Down Expand Up @@ -208,7 +213,9 @@ export function processExpression(
// fast path if expression is a simple identifier.
const rawExp = node.content
// bail constant on parens (function invocation) and dot (member access)
const bailConstant = rawExp.indexOf(`(`) > -1 || rawExp.indexOf('.') > 0
// fix: #5106
const rawExpTrim = node.content.trim();
const bailConstant = rawExp.indexOf(`(`) > -1 || rawExp.indexOf('.') > 0 || (rawExpTrim[0] === '[' && rawExpTrim[rawExpTrim.length - 1] === ']' && isBindStyle)

if (isSimpleIdentifier(rawExp)) {
const isScopeVarReference = context.identifiers[rawExp]
Expand Down