@@ -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,9 +135,14 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
128
135
const namePath = path . get ( 'name' )
129
136
let prefix
130
137
let name
138
+ let modifiers
139
+ let argument
131
140
/* 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 ( '--' )
134
146
prefix = prefixes . find ( el => name . startsWith ( el ) ) || 'attrs'
135
147
name = name . replace ( new RegExp ( `^${ prefix } \-?` ) , '' )
136
148
name = name [ 0 ] . toLowerCase ( ) + name . substr ( 1 )
@@ -159,12 +171,11 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
159
171
if ( rootAttributes . includes ( name ) ) {
160
172
attributes [ name ] = value
161
173
} 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 ) ) {
166
175
name = kebabcase ( name . substr ( 1 ) )
167
176
prefix = 'directives'
177
+ value . _argument = argument
178
+ value . _modifiers = modifiers
168
179
}
169
180
if ( name . match ( xlinkRE ) ) {
170
181
name = name . replace ( xlinkRE , ( _ , firstCharacter ) => {
@@ -255,6 +266,21 @@ const transformDirectives = (t, directives) =>
255
266
t . objectExpression ( [
256
267
t . objectProperty ( t . identifier ( 'name' ) , directive . key ) ,
257
268
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
+ : [ ] ) ,
258
284
] ) ,
259
285
) ,
260
286
)
0 commit comments