Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 26308c5

Browse files
authored
feat: camel modifier for v-bind (#39)
1 parent 5f76974 commit 26308c5

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vBind.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function render(_ctx) {
88
const n0 = t0()
99
const { 0: [n1],} = _children(n0)
1010
_effect(() => {
11-
_setAttr(n1, "foo-bar", undefined, _ctx.id)
11+
_setAttr(n1, "fooBar", undefined, _ctx.id)
1212
})
1313
return n0
1414
}"

packages/compiler-vapor/__tests__/transforms/vBind.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('compiler: transform v-bind', () => {
165165
})
166166
})
167167

168-
test.fails('.camel modifier', () => {
168+
test('.camel modifier', () => {
169169
const node = parseWithVBind(`<div v-bind:foo-bar.camel="id"/>`)
170170
expect(node.effect[0].operations[0]).toMatchObject({
171171
key: {
@@ -179,7 +179,7 @@ describe('compiler: transform v-bind', () => {
179179
})
180180
})
181181

182-
test.fails('.camel modifier w/ no expression', () => {
182+
test('.camel modifier w/ no expression', () => {
183183
const node = parseWithVBind(`<div v-bind:foo-bar.camel />`)
184184
expect(node.effect[0].operations[0]).toMatchObject({
185185
key: {
@@ -193,13 +193,13 @@ describe('compiler: transform v-bind', () => {
193193
})
194194
})
195195

196-
test.fails('.camel modifier w/ dynamic arg', () => {
196+
test('.camel modifier w/ dynamic arg', () => {
197197
const node = parseWithVBind(`<div v-bind:[foo].camel="id"/>`)
198198
expect(node.effect[0].operations[0]).toMatchObject({
199+
runtimeCamelize: true,
199200
key: {
200201
content: `foo`,
201202
isStatic: false,
202-
somethingShouldBeTrue: true,
203203
},
204204
value: {
205205
content: `id`,
@@ -289,8 +289,7 @@ describe('compiler: codegen v-bind', () => {
289289
expect(code).contains('_setAttr(n1, _ctx.id, undefined, _ctx.id)')
290290
})
291291

292-
// TODO: camel modifier for v-bind
293-
test.fails('.camel modifier', () => {
292+
test('.camel modifier', () => {
294293
const code = compile(`<div v-bind:foo-bar.camel="id"/>`)
295294

296295
expect(code).matchSnapshot()

packages/compiler-vapor/src/generate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,11 @@ function genOperation(oper: OperationNode, context: CodegenContext) {
370370
}
371371

372372
function genSetProp(oper: SetPropIRNode, context: CodegenContext) {
373-
const { push, pushWithNewline, vaporHelper } = context
373+
const { push, pushWithNewline, vaporHelper, helper } = context
374374
pushWithNewline(`${vaporHelper('setAttr')}(n${oper.element}, `)
375+
if (oper.runtimeCamelize) push(`${helper('camelize')}(`)
375376
genExpression(oper.key, context)
377+
if (oper.runtimeCamelize) push(`)`)
376378
push(`, undefined, `)
377379
genExpression(oper.value, context)
378380
push(')')

packages/compiler-vapor/src/ir.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface SetPropIRNode extends BaseIRNode {
6060
element: number
6161
key: IRExpression
6262
value: IRExpression
63+
runtimeCamelize: boolean
6364
}
6465

6566
export interface SetTextIRNode extends BaseIRNode {

packages/compiler-vapor/src/transforms/vBind.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { IRNodeTypes } from '../ir'
88
import type { DirectiveTransform } from '../transform'
99

1010
export const transformVBind: DirectiveTransform = (dir, node, context) => {
11-
let { arg, exp, loc } = dir
11+
let { arg, exp, loc, modifiers } = dir
1212

1313
if (!arg) {
1414
// TODO support v-bind="{}"
@@ -21,6 +21,15 @@ export const transformVBind: DirectiveTransform = (dir, node, context) => {
2121
exp.ast = null
2222
}
2323

24+
let camel = false
25+
if (modifiers.includes('camel')) {
26+
if (arg.isStatic) {
27+
arg.content = camelize(arg.content)
28+
} else {
29+
camel = true
30+
}
31+
}
32+
2433
if (!exp.content.trim()) {
2534
context.options.onError(
2635
createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
@@ -38,6 +47,7 @@ export const transformVBind: DirectiveTransform = (dir, node, context) => {
3847
element: context.reference(),
3948
key: arg,
4049
value: exp,
50+
runtimeCamelize: camel,
4151
},
4252
],
4353
)

0 commit comments

Comments
 (0)