@@ -24,6 +24,13 @@ const mustUseDomProps = (tag, type, attributeName) => {
24
24
)
25
25
}
26
26
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
+
27
34
/**
28
35
* Get tag (first attribute for h) from JSXOpeningElement
29
36
* @param t
@@ -128,16 +135,21 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
128
135
const namePath = path . get ( 'name' )
129
136
let prefix
130
137
let name
131
- /* istanbul ignore else */
132
- if ( t . isJSXIdentifier ( namePath ) ) {
133
- name = path . get ( 'name.name' ) . node
134
- prefix = prefixes . find ( el => name . startsWith ( el ) ) || 'attrs'
135
- name = name . replace ( new RegExp ( `^${ prefix } \-?` ) , '' )
136
- name = name [ 0 ] . toLowerCase ( ) + name . substr ( 1 )
138
+ let modifiers
139
+ let argument
140
+ if ( t . isJSXNamespacedName ( namePath ) ) {
141
+ name = `${ namePath . get ( 'namespace.name' ) . node } :${ namePath . get ( 'name.name' ) . node } `
137
142
} else {
138
- throw new Error ( `getAttributes (attribute name): ${ namePath . type } is not supported` )
143
+ name = namePath . get ( ' name' ) . node
139
144
}
140
145
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
+
141
153
const valuePath = path . get ( 'value' )
142
154
let value
143
155
if ( ! valuePath . node ) {
@@ -156,13 +168,13 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
156
168
}
157
169
}
158
170
171
+ value . _argument = argument
172
+ value . _modifiers = modifiers
173
+
159
174
if ( rootAttributes . includes ( name ) ) {
160
175
attributes [ name ] = value
161
176
} 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' ) {
177
+ if ( isDirective ( name ) ) {
166
178
name = kebabcase ( name . substr ( 1 ) )
167
179
prefix = 'directives'
168
180
}
@@ -255,6 +267,21 @@ const transformDirectives = (t, directives) =>
255
267
t . objectExpression ( [
256
268
t . objectProperty ( t . identifier ( 'name' ) , directive . key ) ,
257
269
t . objectProperty ( t . identifier ( 'value' ) , directive . value ) ,
270
+ ...( directive . value . _argument
271
+ ? [ t . objectProperty ( t . identifier ( 'arg' ) , t . stringLiteral ( directive . value . _argument ) ) ]
272
+ : [ ] ) ,
273
+ ...( directive . value . _modifiers && directive . value . _modifiers . length > 0
274
+ ? [
275
+ t . objectProperty (
276
+ t . identifier ( 'modifiers' ) ,
277
+ t . objectExpression (
278
+ directive . value . _modifiers . map ( modifier =>
279
+ t . objectProperty ( t . stringLiteral ( modifier ) , t . booleanLiteral ( true ) ) ,
280
+ ) ,
281
+ ) ,
282
+ ) ,
283
+ ]
284
+ : [ ] ) ,
258
285
] ) ,
259
286
) ,
260
287
)
0 commit comments