提交 fed8c872 编写于 作者: fxy060608's avatar fxy060608

fix(mp): camelize props (#3071)

上级 ab2f2c98
...@@ -115,10 +115,10 @@ describe('compiler: transform component', () => { ...@@ -115,10 +115,10 @@ describe('compiler: transform component', () => {
}, },
}) })
assert( assert(
`<van-button><template #default><view/></template><template #head><view/></template></van-button>`, `<van-button custom-style="background-color: unset;" :close-on-click-overlay="true"><template #default><view/></template><template #head><view/></template></van-button>`,
`<van-button u-t="m" u-i="dc555fe4-0" bind:__l="__l"><view/><view slot="head"/></van-button>`, `<van-button u-t="m" u-i="dc555fe4-0" bind:__l="__l" u-p="{{a}}"><view/><view slot="head"/></van-button>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return {} return { a: _p({ customStyle: 'background-color: unset;', closeOnClickOverlay: true }) }
}`, }`,
{ {
filename, filename,
......
import { camelize } from '@vue/shared'
import { import {
ComponentNode, ComponentNode,
createSimpleExpression, createSimpleExpression,
...@@ -23,6 +24,7 @@ import { ...@@ -23,6 +24,7 @@ import {
} from './utils' } from './utils'
import { genExpr, genBabelExpr } from '../codegen' import { genExpr, genBabelExpr } from '../codegen'
import { import {
Expression,
identifier, identifier,
logicalExpression, logicalExpression,
objectExpression, objectExpression,
...@@ -140,9 +142,23 @@ function isComponentProp(name: string) { ...@@ -140,9 +142,23 @@ function isComponentProp(name: string) {
* @param context * @param context
*/ */
export function rewriteBinding( export function rewriteBinding(
{ props }: ComponentNode, { tag, props }: ComponentNode,
context: TransformContext context: TransformContext
) { ) {
const isMiniProgramComponent = context.isMiniProgramComponent(tag)
const createObjectProperty = isMiniProgramComponent
? (name: string, value: Expression) =>
objectProperty(identifier(camelize(name)), value)
: (name: string, value: Expression) => {
const computed = !isSimpleIdentifier(name)
return objectProperty(
computed ? stringLiteral(name) : identifier(name),
value,
computed
)
}
const properties: ObjectProperty[] = [] const properties: ObjectProperty[] = []
for (let i = 0; i < props.length; i++) { for (let i = 0; i < props.length; i++) {
const prop = props[i] const prop = props[i]
...@@ -151,13 +167,8 @@ export function rewriteBinding( ...@@ -151,13 +167,8 @@ export function rewriteBinding(
if (!isComponentProp(name)) { if (!isComponentProp(name)) {
continue continue
} }
const computed = !isSimpleIdentifier(name)
properties.push( properties.push(
objectProperty( createObjectProperty(name, stringLiteral(prop.value?.content || ''))
computed ? stringLiteral(name) : identifier(name),
stringLiteral(prop.value?.content || ''),
computed
)
) )
} else if (prop.type === NodeTypes.DIRECTIVE) { } else if (prop.type === NodeTypes.DIRECTIVE) {
if (prop.name !== 'bind') { if (prop.name !== 'bind') {
...@@ -176,15 +187,7 @@ export function rewriteBinding( ...@@ -176,15 +187,7 @@ export function rewriteBinding(
if (!valueExpr) { if (!valueExpr) {
continue continue
} }
const name = arg.content properties.push(createObjectProperty(arg.content, valueExpr))
const computed = !isSimpleIdentifier(name)
properties.push(
objectProperty(
computed ? stringLiteral(name) : identifier(name),
valueExpr,
computed
)
)
} else { } else {
// :[dynamic]="dynamic" // :[dynamic]="dynamic"
const leftExpr = parseExpr(genExpr(arg), context, exp) const leftExpr = parseExpr(genExpr(arg), context, exp)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册