Skip to content

feat: camel modifier for v-bind #39

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

Merged
merged 5 commits into from
Dec 9, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function render(_ctx) {
const n0 = t0()
const { 0: [n1],} = _children(n0)
_effect(() => {
_setAttr(n1, "foo-bar", undefined, _ctx.id)
_setAttr(n1, "fooBar", undefined, _ctx.id)
})
return n0
}"
Expand Down
11 changes: 5 additions & 6 deletions packages/compiler-vapor/__tests__/transforms/vBind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe('compiler: transform v-bind', () => {
})
})

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

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

test.fails('.camel modifier w/ dynamic arg', () => {
test('.camel modifier w/ dynamic arg', () => {
const node = parseWithVBind(`<div v-bind:[foo].camel="id"/>`)
expect(node.effect[0].operations[0]).toMatchObject({
runtimeCamelize: true,
key: {
content: `foo`,
isStatic: false,
somethingShouldBeTrue: true,
},
value: {
content: `id`,
Expand Down Expand Up @@ -289,8 +289,7 @@ describe('compiler: codegen v-bind', () => {
expect(code).contains('_setAttr(n1, _ctx.id, undefined, _ctx.id)')
})

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

expect(code).matchSnapshot()
Expand Down
4 changes: 3 additions & 1 deletion packages/compiler-vapor/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,11 @@ function genOperation(oper: OperationNode, context: CodegenContext) {
}

function genSetProp(oper: SetPropIRNode, context: CodegenContext) {
const { push, pushWithNewline, vaporHelper } = context
const { push, pushWithNewline, vaporHelper, helper } = context
pushWithNewline(`${vaporHelper('setAttr')}(n${oper.element}, `)
if (oper.runtimeCamelize) push(`${helper('camelize')}(`)
genExpression(oper.key, context)
if (oper.runtimeCamelize) push(`)`)
push(`, undefined, `)
genExpression(oper.value, context)
push(')')
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-vapor/src/ir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface SetPropIRNode extends BaseIRNode {
element: number
key: IRExpression
value: IRExpression
runtimeCamelize: boolean
}

export interface SetTextIRNode extends BaseIRNode {
Expand Down
12 changes: 11 additions & 1 deletion packages/compiler-vapor/src/transforms/vBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { IRNodeTypes } from '../ir'
import type { DirectiveTransform } from '../transform'

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

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

let camel = false
if (modifiers.includes('camel')) {
if (arg.isStatic) {
arg.content = camelize(arg.content)
} else {
camel = true
}
}

if (!exp.content.trim()) {
context.options.onError(
createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
Expand All @@ -38,6 +47,7 @@ export const transformVBind: DirectiveTransform = (dir, node, context) => {
element: context.reference(),
key: arg,
value: exp,
runtimeCamelize: camel,
},
],
)
Expand Down