@@ -17,6 +17,7 @@ const addConfig = t => (path, displayName, componentId) => {
17
17
}
18
18
19
19
const withConfigProps = [ ]
20
+
20
21
if ( displayName ) {
21
22
withConfigProps . push (
22
23
t . objectProperty (
@@ -34,6 +35,34 @@ const addConfig = t => (path, displayName, componentId) => {
34
35
)
35
36
}
36
37
38
+ const existingConfig = getExistingConfig ( t ) ( path )
39
+
40
+ if (
41
+ existingConfig &&
42
+ existingConfig . arguments . length &&
43
+ ! existingConfig . arguments [ 0 ] . properties . some ( prop =>
44
+ [ 'displayName' , 'componentId' ] . includes ( prop . key . name )
45
+ )
46
+ ) {
47
+ existingConfig . arguments [ 0 ] . properties . push ( ...withConfigProps )
48
+ return
49
+ }
50
+
51
+ if (
52
+ path . node . callee &&
53
+ t . isMemberExpression ( path . node . callee . callee ) &&
54
+ path . node . callee . callee . property &&
55
+ path . node . callee . callee . property . name &&
56
+ path . node . callee . callee . property . name == 'withConfig' &&
57
+ path . node . callee . arguments . length &&
58
+ ! path . node . callee . arguments [ 0 ] . properties . some ( prop =>
59
+ [ 'displayName' , 'componentId' ] . includes ( prop . key . name )
60
+ )
61
+ ) {
62
+ path . node . callee . arguments [ 0 ] . properties . push ( ...withConfigProps )
63
+ return
64
+ }
65
+
37
66
if ( path . node . tag ) {
38
67
// Replace x`...` with x.withConfig({ })`...`
39
68
path . node . tag = t . callExpression (
@@ -53,6 +82,29 @@ const addConfig = t => (path, displayName, componentId) => {
53
82
}
54
83
}
55
84
85
+ const getExistingConfig = t => path => {
86
+ if (
87
+ path . node . callee &&
88
+ t . isMemberExpression ( path . node . callee . callee ) &&
89
+ path . node . callee . callee . property &&
90
+ path . node . callee . callee . property . name &&
91
+ path . node . callee . callee . property . name == 'withConfig'
92
+ ) {
93
+ return path . node . callee
94
+ }
95
+
96
+ if (
97
+ path . node . callee &&
98
+ t . isMemberExpression ( path . node . callee . callee ) &&
99
+ path . node . callee . callee . object &&
100
+ path . node . callee . callee . object . callee &&
101
+ path . node . callee . callee . object . callee . property &&
102
+ path . node . callee . callee . object . callee . property . name === 'withConfig'
103
+ ) {
104
+ return path . node . callee . callee . object
105
+ }
106
+ }
107
+
56
108
const getBlockName = file => {
57
109
const name = path . basename (
58
110
file . opts . filename ,
@@ -155,7 +207,16 @@ export default t => (path, state) => {
155
207
t . isMemberExpression ( path . node . callee . callee ) &&
156
208
path . node . callee . callee . property &&
157
209
path . node . callee . callee . property . name &&
158
- path . node . callee . callee . property . name !== 'withConfig' )
210
+ path . node . callee . callee . property . name !== 'withConfig' ) ||
211
+ // styled(x).withConfig({})
212
+ ( isStyled ( t ) ( path . node . callee , state ) &&
213
+ t . isMemberExpression ( path . node . callee . callee ) &&
214
+ path . node . callee . callee . property &&
215
+ path . node . callee . callee . property . name &&
216
+ path . node . callee . callee . property . name == 'withConfig' &&
217
+ ! path . node . callee . arguments [ 0 ] . properties . some ( ( prop ) =>
218
+ [ 'displayName' , 'componentId' ] . includes ( prop . key . name )
219
+ ) )
159
220
) {
160
221
const displayName =
161
222
useDisplayName ( state ) &&
0 commit comments