1
1
function extractVariants ( t , path , state ) {
2
- const maybeVariants = path . node . body . find ( node => (
2
+ const maybeVariants = path . node . body . filter ( node => (
3
3
t . isExpressionStatement ( node ) &&
4
4
t . isCallExpression ( node . expression ) &&
5
5
t . isMemberExpression ( node . expression . callee )
6
6
) )
7
7
8
- if ( ! maybeVariants ) {
8
+ if ( maybeVariants . length === 0 ) {
9
9
return
10
10
}
11
11
12
- const calleeName = maybeVariants . expression . callee . object . name
13
- const isVariant = (
14
- t . isIdentifier ( maybeVariants . expression . callee . object , { name : calleeName } ) &&
15
- t . isIdentifier ( maybeVariants . expression . callee . property , { name : 'useVariants' } ) &&
16
- t . isObjectExpression ( maybeVariants . expression . arguments [ 0 ] )
17
- )
12
+ const targetVariant = maybeVariants . find ( variant => {
13
+ const calleeName = variant . expression . callee . object . name
14
+
15
+ return (
16
+ t . isIdentifier ( variant . expression . callee . object , { name : calleeName } ) &&
17
+ t . isIdentifier ( variant . expression . callee . property , { name : 'useVariants' } ) &&
18
+ t . isObjectExpression ( variant . expression . arguments [ 0 ] )
19
+ )
20
+ } )
18
21
19
- if ( ! isVariant ) {
22
+ if ( ! targetVariant ) {
20
23
return
21
24
}
22
25
23
- const node = maybeVariants . expression
26
+ const calleeName = targetVariant . expression . callee . object . name
27
+ const node = targetVariant . expression
24
28
const newUniqueName = path . scope . generateUidIdentifier ( calleeName )
25
29
26
30
// Create shadow declaration eg. const _styles = styles
@@ -39,7 +43,7 @@ function extractVariants(t, path, state) {
39
43
40
44
// Find the current node's index, we will move everything after to new block
41
45
const pathIndex = path . node . body
42
- . findIndex ( bodyPath => bodyPath === maybeVariants )
46
+ . findIndex ( bodyPath => bodyPath === targetVariant )
43
47
const rest = path . node . body . slice ( pathIndex + 1 )
44
48
45
49
// move rest to new block (scope)
0 commit comments