1+ const t = require ( 'babel-types' ) ;
2+ const u = require ( '../utils' ) ;
3+ const Directive = require ( './enum' ) ;
4+
5+ function transformClassDirective ( path , state ) {
6+ let classDirAttrs = path . node . openingElement . attributes ;
7+ let classAttrIdx = u . getDirectiveIndex ( classDirAttrs , Directive . CLASS ) ;
8+ let classExpression = classDirAttrs [ classAttrIdx ] . value . expression ;
9+ let classNameAttr = classDirAttrs . find ( prop => {
10+ return prop . name . name == 'className' ;
11+ } ) ;
12+ let classNameValue ;
13+
14+ if ( classNameAttr && t . isJSXExpressionContainer ( classNameAttr . value ) ) {
15+ classNameValue = classNameAttr . value . expression ;
16+ } else if ( classNameAttr && t . isStringLiteral ( classNameAttr . value ) ) {
17+ classNameValue = classNameAttr . value ;
18+ }
19+
20+ let classNameArr = classNameValue ? t . arrayExpression ( [
21+ classNameValue
22+ ] ) : t . arrayExpression ( [ ] ) ;
23+
24+ let classObjEntries = t . callExpression (
25+ t . memberExpression (
26+ t . identifier ( 'Object' ) ,
27+ t . identifier ( 'entries' )
28+ ) , [
29+ classExpression
30+ ]
31+ ) ;
32+
33+ let classObjFilteredEntries = t . callExpression (
34+ t . memberExpression (
35+ classObjEntries ,
36+ t . identifier ( 'filter' )
37+ ) , [
38+ t . arrowFunctionExpression (
39+ [ t . identifier ( 'c' ) ] ,
40+ t . binaryExpression (
41+ '===' ,
42+ t . memberExpression (
43+ t . identifier ( 'c' ) ,
44+ t . identifier ( '1' )
45+ ) ,
46+ t . booleanLiteral ( true )
47+ )
48+ )
49+ ]
50+ ) ;
51+
52+ let classObjMappedEntries = t . callExpression (
53+ t . memberExpression (
54+ classObjFilteredEntries ,
55+ t . identifier ( 'map' )
56+ ) , [
57+ t . arrowFunctionExpression (
58+ [ t . identifier ( 'c' ) ] ,
59+ t . memberExpression (
60+ t . identifier ( 'c' ) ,
61+ t . identifier ( '0' )
62+ )
63+ )
64+ ]
65+ ) ;
66+
67+ let classNameConcatenatedArr = t . callExpression (
68+ t . memberExpression (
69+ classNameArr ,
70+ t . identifier ( 'concat' )
71+ ) , [
72+ classObjMappedEntries
73+ ]
74+ ) ;
75+
76+ let classNameString = t . callExpression (
77+ t . memberExpression (
78+ classNameConcatenatedArr ,
79+ t . identifier ( 'join' )
80+ ) , [
81+ t . stringLiteral ( ' ' )
82+ ]
83+ ) ;
84+
85+ let newClassNameProp = t . jSXAttribute (
86+ t . jSXIdentifier ( 'className' ) ,
87+ t . jSXExpressionContainer (
88+ classNameString
89+ )
90+ ) ;
91+
92+ let classNameIdx = classDirAttrs . findIndex ( attr => {
93+ return attr . name . name == 'className' ;
94+ } ) ;
95+ if ( classNameIdx !== - 1 ) classDirAttrs . splice ( classNameIdx , 1 ) ;
96+ classDirAttrs . push ( newClassNameProp ) ;
97+ // return console.log('>>>>>>>>>>>>>>>>>', classDirAttrs);
98+
99+ path . replaceWith (
100+ u . createJSXElement (
101+ path . node . openingElement . name . name ,
102+ classDirAttrs ,
103+ path . node . children ,
104+ [ Directive . CLASS ]
105+ )
106+ ) ;
107+ }
108+
109+ module . exports = transformClassDirective ;
0 commit comments