Skip to content
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

fix: add rawName to directive parse #294

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions packages/babel-plugin-transform-vue-jsx/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
let name
let modifiers
let argument
let rawName
if (t.isJSXNamespacedName(namePath)) {
name = `${namePath.get('namespace.name').node}:${namePath.get('name.name').node}`
} else {
name = namePath.get('name').node
}
rawName = name
if (prefixes.includes(name) && t.isJSXExpressionContainer(path.get('value'))) {
return t.JSXSpreadAttribute(t.objectExpression([t.objectProperty(t.stringLiteral(name), path.get('value').node.expression)]))
}
Expand Down Expand Up @@ -188,6 +190,7 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =

value._argument = argument
value._modifiers = modifiers
value._rawName = rawName

if (rootAttributes.includes(name)) {
attributes[name] = value
Expand Down Expand Up @@ -294,6 +297,9 @@ const transformDirectives = (t, directives) =>
directives.properties.map(directive =>
t.objectExpression([
t.objectProperty(t.identifier('name'), directive.key),
...(directive.value._rawName
? [t.objectProperty(t.identifier('rawName'), t.stringLiteral(directive.value._rawName))]
: []),
t.objectProperty(t.identifier('value'), directive.value),
...(directive.value._argument
? [t.objectProperty(t.identifier('arg'), t.stringLiteral(directive.value._argument))]
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-plugin-transform-vue-jsx/test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ test('Custom directives', t => {
})

t.is(wrapper.vnode.data.directives.length, 2)
t.deepEqual(wrapper.vnode.data.directives[0], { def: directive, modifiers: {}, name: 'test', value: 123 })
t.deepEqual(wrapper.vnode.data.directives[1], { def: directive, modifiers: {}, name: 'other', value: 234 })
t.deepEqual(wrapper.vnode.data.directives[0], { def: directive, modifiers: {}, name: 'test', rawName: 'v-test', value: 123 })
t.deepEqual(wrapper.vnode.data.directives[1], { def: directive, modifiers: {}, name: 'other', rawName: 'vOther', value: 234 })
})

test('xlink:href', t => {
Expand Down
14 changes: 13 additions & 1 deletion packages/babel-plugin-transform-vue-jsx/test/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,25 +232,37 @@ render(h => h("div", _mergeJSXProps([{}, spread, {
},
{
name: 'Directives',
from: `render(h => <div v-test={ 123 } vSomething_modifier={ 1234 } vOtherStuff:argument_modifier1_modifier2={ 234 } />)`,
from: `render(h => <div v-test={ 123 } vSomething_modifier={ 1234 } vOtherStuff:argument_modifier1_modifier2={ 234 } vOtherStuff:argument2_modifier1_modifier2={ 2345 } />)`,
to: `render(h => h("div", {
"directives": [{
name: "test",
rawName: "v-test",
value: 123
}, {
name: "something",
rawName: "vSomething_modifier",
value: 1234,
modifiers: {
"modifier": true
}
}, {
name: "other-stuff",
rawName: "vOtherStuff:argument_modifier1_modifier2",
value: 234,
arg: "argument",
modifiers: {
"modifier1": true,
"modifier2": true
}
}, {
name: "other-stuff",
rawName: "vOtherStuff:argument2_modifier1_modifier2",
value: 2345,
arg: "argument2",
modifiers: {
"modifier1": true,
"modifier2": true
}
}]
}));`,
},
Expand Down