feat: camel modifier for `v-bind` (#39)
This commit is contained in:
parent
5f769745fa
commit
26308c51eb
|
@ -8,7 +8,7 @@ export function render(_ctx) {
|
||||||
const n0 = t0()
|
const n0 = t0()
|
||||||
const { 0: [n1],} = _children(n0)
|
const { 0: [n1],} = _children(n0)
|
||||||
_effect(() => {
|
_effect(() => {
|
||||||
_setAttr(n1, "foo-bar", undefined, _ctx.id)
|
_setAttr(n1, "fooBar", undefined, _ctx.id)
|
||||||
})
|
})
|
||||||
return n0
|
return n0
|
||||||
}"
|
}"
|
||||||
|
|
|
@ -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"/>`)
|
const node = parseWithVBind(`<div v-bind:foo-bar.camel="id"/>`)
|
||||||
expect(node.effect[0].operations[0]).toMatchObject({
|
expect(node.effect[0].operations[0]).toMatchObject({
|
||||||
key: {
|
key: {
|
||||||
|
@ -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 />`)
|
const node = parseWithVBind(`<div v-bind:foo-bar.camel />`)
|
||||||
expect(node.effect[0].operations[0]).toMatchObject({
|
expect(node.effect[0].operations[0]).toMatchObject({
|
||||||
key: {
|
key: {
|
||||||
|
@ -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"/>`)
|
const node = parseWithVBind(`<div v-bind:[foo].camel="id"/>`)
|
||||||
expect(node.effect[0].operations[0]).toMatchObject({
|
expect(node.effect[0].operations[0]).toMatchObject({
|
||||||
|
runtimeCamelize: true,
|
||||||
key: {
|
key: {
|
||||||
content: `foo`,
|
content: `foo`,
|
||||||
isStatic: false,
|
isStatic: false,
|
||||||
somethingShouldBeTrue: true,
|
|
||||||
},
|
},
|
||||||
value: {
|
value: {
|
||||||
content: `id`,
|
content: `id`,
|
||||||
|
@ -289,8 +289,7 @@ describe('compiler: codegen v-bind', () => {
|
||||||
expect(code).contains('_setAttr(n1, _ctx.id, undefined, _ctx.id)')
|
expect(code).contains('_setAttr(n1, _ctx.id, undefined, _ctx.id)')
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: camel modifier for v-bind
|
test('.camel modifier', () => {
|
||||||
test.fails('.camel modifier', () => {
|
|
||||||
const code = compile(`<div v-bind:foo-bar.camel="id"/>`)
|
const code = compile(`<div v-bind:foo-bar.camel="id"/>`)
|
||||||
|
|
||||||
expect(code).matchSnapshot()
|
expect(code).matchSnapshot()
|
||||||
|
|
|
@ -370,9 +370,11 @@ function genOperation(oper: OperationNode, context: CodegenContext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function genSetProp(oper: SetPropIRNode, 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}, `)
|
pushWithNewline(`${vaporHelper('setAttr')}(n${oper.element}, `)
|
||||||
|
if (oper.runtimeCamelize) push(`${helper('camelize')}(`)
|
||||||
genExpression(oper.key, context)
|
genExpression(oper.key, context)
|
||||||
|
if (oper.runtimeCamelize) push(`)`)
|
||||||
push(`, undefined, `)
|
push(`, undefined, `)
|
||||||
genExpression(oper.value, context)
|
genExpression(oper.value, context)
|
||||||
push(')')
|
push(')')
|
||||||
|
|
|
@ -60,6 +60,7 @@ export interface SetPropIRNode extends BaseIRNode {
|
||||||
element: number
|
element: number
|
||||||
key: IRExpression
|
key: IRExpression
|
||||||
value: IRExpression
|
value: IRExpression
|
||||||
|
runtimeCamelize: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SetTextIRNode extends BaseIRNode {
|
export interface SetTextIRNode extends BaseIRNode {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import { IRNodeTypes } from '../ir'
|
||||||
import type { DirectiveTransform } from '../transform'
|
import type { DirectiveTransform } from '../transform'
|
||||||
|
|
||||||
export const transformVBind: DirectiveTransform = (dir, node, context) => {
|
export const transformVBind: DirectiveTransform = (dir, node, context) => {
|
||||||
let { arg, exp, loc } = dir
|
let { arg, exp, loc, modifiers } = dir
|
||||||
|
|
||||||
if (!arg) {
|
if (!arg) {
|
||||||
// TODO support v-bind="{}"
|
// TODO support v-bind="{}"
|
||||||
|
@ -21,6 +21,15 @@ export const transformVBind: DirectiveTransform = (dir, node, context) => {
|
||||||
exp.ast = null
|
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()) {
|
if (!exp.content.trim()) {
|
||||||
context.options.onError(
|
context.options.onError(
|
||||||
createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
|
createCompilerError(ErrorCodes.X_V_BIND_NO_EXPRESSION, loc),
|
||||||
|
@ -38,6 +47,7 @@ export const transformVBind: DirectiveTransform = (dir, node, context) => {
|
||||||
element: context.reference(),
|
element: context.reference(),
|
||||||
key: arg,
|
key: arg,
|
||||||
value: exp,
|
value: exp,
|
||||||
|
runtimeCamelize: camel,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue