Skip to content

Commit 0085b8f

Browse files
committed
feat: add support for argument and modifiers for directives
1 parent ca985c5 commit 0085b8f

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

Diff for: packages/babel-plugin-transform-vue-jsx/src/index.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const mustUseDomProps = (tag, type, attributeName) => {
2424
)
2525
}
2626

27+
/**
28+
* Checks if string is describing a directive
29+
* @param src string
30+
*/
31+
const isDirective = src =>
32+
src.startsWith(`v-`) || (src.startsWith('v') && src.length >= 2 && src[1] >= 'A' && src[1] <= 'Z')
33+
2734
/**
2835
* Get tag (first attribute for h) from JSXOpeningElement
2936
* @param t
@@ -128,9 +135,14 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
128135
const namePath = path.get('name')
129136
let prefix
130137
let name
138+
let modifiers
139+
let argument
131140
/* istanbul ignore else */
132-
if (t.isJSXIdentifier(namePath)) {
133-
name = path.get('name.name').node
141+
if (t.isJSXNamespacedName(namePath) && isDirective(namePath.get('namespace.name').node)) {
142+
;[name, argument] = namePath.get('namespace.name').node.split('--')
143+
modifiers = namePath.get('name.name').node.split('-')
144+
} else if (t.isJSXIdentifier(namePath)) {
145+
;[name, argument] = path.get('name.name').node.split('--')
134146
prefix = prefixes.find(el => name.startsWith(el)) || 'attrs'
135147
name = name.replace(new RegExp(`^${prefix}\-?`), '')
136148
name = name[0].toLowerCase() + name.substr(1)
@@ -159,12 +171,11 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
159171
if (rootAttributes.includes(name)) {
160172
attributes[name] = value
161173
} else {
162-
if (name.startsWith(`v-`)) {
163-
name = name.replace(directiveRE, '')
164-
prefix = 'directives'
165-
} else if (name.startsWith('v') && name.length >= 2 && name[1] >= 'A' && name[1] <= 'Z') {
174+
if (isDirective(name)) {
166175
name = kebabcase(name.substr(1))
167176
prefix = 'directives'
177+
value._argument = argument
178+
value._modifiers = modifiers
168179
}
169180
if (name.match(xlinkRE)) {
170181
name = name.replace(xlinkRE, (_, firstCharacter) => {
@@ -255,6 +266,21 @@ const transformDirectives = (t, directives) =>
255266
t.objectExpression([
256267
t.objectProperty(t.identifier('name'), directive.key),
257268
t.objectProperty(t.identifier('value'), directive.value),
269+
...(directive.value._argument
270+
? [t.objectProperty(t.identifier('arg'), t.stringLiteral(directive.value._argument))]
271+
: []),
272+
...(directive.value._modifiers
273+
? [
274+
t.objectProperty(
275+
t.identifier('modifiers'),
276+
t.objectExpression(
277+
directive.value._modifiers.map(modifier =>
278+
t.objectProperty(t.stringLiteral(modifier), t.booleanLiteral(true)),
279+
),
280+
),
281+
),
282+
]
283+
: []),
258284
]),
259285
),
260286
)

Diff for: packages/babel-plugin-transform-vue-jsx/test/snapshot.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,19 @@ render(h => h("div", _mergeJSXProps([{}, spread, {
215215
},
216216
{
217217
name: 'Directives',
218-
from: `render(h => <div v-test={ 123 } vOtherStuff={ 234 } />)`,
218+
from: `render(h => <div v-test={ 123 } vOtherStuff--argument:modifier1-modifier2={ 234 } />)`,
219219
to: `render(h => h("div", {
220220
"directives": [{
221221
name: "test",
222222
value: 123
223223
}, {
224224
name: "other-stuff",
225-
value: 234
225+
value: 234,
226+
arg: "argument",
227+
modifiers: {
228+
"modifier1": true,
229+
"modifier2": true
230+
}
226231
}]
227232
}));`,
228233
},

0 commit comments

Comments
 (0)