Skip to content

Commit b1c8036

Browse files
committed
feat: change the syntax for argument and modifiers
1 parent 0085b8f commit b1c8036

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

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

+14-13
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,19 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
137137
let name
138138
let modifiers
139139
let argument
140-
/* istanbul ignore else */
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('--')
146-
prefix = prefixes.find(el => name.startsWith(el)) || 'attrs'
147-
name = name.replace(new RegExp(`^${prefix}\-?`), '')
148-
name = name[0].toLowerCase() + name.substr(1)
140+
if (t.isJSXNamespacedName(namePath)) {
141+
name = `${namePath.get('namespace.name').node}:${namePath.get('name.name').node}`
149142
} else {
150-
throw new Error(`getAttributes (attribute name): ${namePath.type} is not supported`)
143+
name = namePath.get('name').node
151144
}
152145

146+
;[name, ...modifiers] = name.split('_')
147+
;[name, argument] = name.split(':')
148+
149+
prefix = prefixes.find(el => name.startsWith(el)) || 'attrs'
150+
name = name.replace(new RegExp(`^${prefix}\-?`), '')
151+
name = name[0].toLowerCase() + name.substr(1)
152+
153153
const valuePath = path.get('value')
154154
let value
155155
if (!valuePath.node) {
@@ -168,14 +168,15 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
168168
}
169169
}
170170

171+
value._argument = argument
172+
value._modifiers = modifiers
173+
171174
if (rootAttributes.includes(name)) {
172175
attributes[name] = value
173176
} else {
174177
if (isDirective(name)) {
175178
name = kebabcase(name.substr(1))
176179
prefix = 'directives'
177-
value._argument = argument
178-
value._modifiers = modifiers
179180
}
180181
if (name.match(xlinkRE)) {
181182
name = name.replace(xlinkRE, (_, firstCharacter) => {
@@ -269,7 +270,7 @@ const transformDirectives = (t, directives) =>
269270
...(directive.value._argument
270271
? [t.objectProperty(t.identifier('arg'), t.stringLiteral(directive.value._argument))]
271272
: []),
272-
...(directive.value._modifiers
273+
...(directive.value._modifiers && directive.value._modifiers.length > 0
273274
? [
274275
t.objectProperty(
275276
t.identifier('modifiers'),

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

+7-14
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,17 @@ render(h => h("div", _mergeJSXProps([{}, spread, {
215215
},
216216
{
217217
name: 'Directives',
218-
from: `render(h => <div v-test={ 123 } vOtherStuff--argument:modifier1-modifier2={ 234 } />)`,
218+
from: `render(h => <div v-test={ 123 } vSomething_modifier={ 1234 } vOtherStuff:argument_modifier1_modifier2={ 234 } />)`,
219219
to: `render(h => h("div", {
220220
"directives": [{
221221
name: "test",
222222
value: 123
223+
}, {
224+
name: "something",
225+
value: 1234,
226+
modifiers: {
227+
"modifier": true
228+
}
223229
}, {
224230
name: "other-stuff",
225231
value: 234,
@@ -283,19 +289,6 @@ render(h => h("div", _mergeJSXProps([{}, spread, {
283289

284290
tests.forEach(({ name, from, to }) => test(name, async t => t.is(await transpile(from), to)))
285291

286-
test('JSXNamespacedName attribute name throws error', t =>
287-
new Promise(resolve => {
288-
transpile(`render(h => <a a:b="test" />)`)
289-
.then(() => {
290-
t.fail()
291-
resolve()
292-
})
293-
.catch(e => {
294-
t.is(e.message, 'getAttributes (attribute name): JSXNamespacedName is not supported')
295-
resolve()
296-
})
297-
}))
298-
299292
test('JSXElement attribute value throws error', t =>
300293
new Promise(resolve => {
301294
transpile(`render(h => <a key=<b/> />)`)

0 commit comments

Comments
 (0)