Skip to content
Open
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
28 changes: 26 additions & 2 deletions packages/uni-mp-compiler/__tests__/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('compiler: transform v-bind', () => {
},
},
})
parseWithVBind(`<view v-bind:arg />`, { onError })
parseWithVBind(`<view v-bind:arg="" />`, { onError })
expect(onError.mock.calls[1][0]).toMatchObject({
code: ErrorCodes.X_V_BIND_NO_EXPRESSION,
loc: {
Expand All @@ -116,12 +116,36 @@ describe('compiler: transform v-bind', () => {
},
end: {
line: 1,
column: 17,
column: 20,
},
},
})
})

test('same-name shorthand (Vue 3.4)', () => {
assert(
`<view :id/>`,
`<view id="{{a}}"/>`,
`(_ctx, _cache) => {
return { a: _ctx.id }
}`
)
assert(
`<view v-bind:arg />`,
`<view arg="{{a}}"/>`,
`(_ctx, _cache) => {
return { a: _ctx.arg }
}`
)
assert(
`<view :foo-bar/>`,
`<view foo-bar="{{a}}"/>`,
`(_ctx, _cache) => {
return { a: _ctx.fooBar }
}`
)
})

test('.camel modifier', () => {
assert(
`<view v-bind:foo-bar.camel="id"/>`,
Expand Down
41 changes: 30 additions & 11 deletions packages/uni-mp-compiler/src/transforms/vBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,39 @@ import {
} from '@vue/compiler-core'
import type { DirectiveTransform } from '../transform'
import { MPErrorCodes, createMPCompilerError } from '../errors'
import { processExpression } from './transformExpression'

export const transformBind: DirectiveTransform = (dir, _node, context) => {
const { exp, modifiers, loc } = dir
const { modifiers, loc } = dir
const arg = dir.arg!
let { exp } = dir

// 空字符串表达式仍是错误(Vue: `:id=""`)
if (exp && exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content.trim()) {
context.onError(createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc))
return {
props: [createObjectProperty(arg, createSimpleExpression('', true, loc))],
}
}

// Vue 3.4 同名简写:`:id` => `:id="id"`
if (!exp) {
if (arg.type !== NodeTypes.SIMPLE_EXPRESSION || !arg.isStatic) {
context.onError(
createCompilerError(ErrorCodes.X_V_BIND_INVALID_SAME_NAME_ARGUMENT, loc)
)
return {
props: [
createObjectProperty(arg, createSimpleExpression('', true, loc)),
],
}
}
const propName = camelize(arg.content)
exp = dir.exp = createSimpleExpression(propName, false, arg.loc)
if (context.prefixIdentifiers) {
exp = dir.exp = processExpression(exp, context)
}
}

if (arg.type !== NodeTypes.SIMPLE_EXPRESSION) {
arg.children.unshift(`(`)
Expand Down Expand Up @@ -45,16 +74,6 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
)
}

if (
!exp ||
(exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content.trim())
) {
context.onError(createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc))
return {
props: [createObjectProperty(arg, createSimpleExpression('', true, loc))],
}
}

return {
props: [createObjectProperty(arg, exp)],
}
Expand Down
Loading