Skip to content

Commit 04eed56

Browse files
committed
merge withConfig arguments to allow for shouldForwardProp
1 parent 8fb3906 commit 04eed56

File tree

4 files changed

+98
-2
lines changed

4 files changed

+98
-2
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"semi": false,
33
"singleQuote": true,
4-
"trailingComma": "es5"
4+
"trailingComma": "es5",
5+
"arrowParens": "avoid"
56
}

src/visitors/displayNameAndId.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const addConfig = t => (path, displayName, componentId) => {
1717
}
1818

1919
const withConfigProps = []
20+
2021
if (displayName) {
2122
withConfigProps.push(
2223
t.objectProperty(
@@ -34,6 +35,34 @@ const addConfig = t => (path, displayName, componentId) => {
3435
)
3536
}
3637

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+
3766
if (path.node.tag) {
3867
// Replace x`...` with x.withConfig({ })`...`
3968
path.node.tag = t.callExpression(
@@ -53,6 +82,29 @@ const addConfig = t => (path, displayName, componentId) => {
5382
}
5483
}
5584

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+
56108
const getBlockName = file => {
57109
const name = path.basename(
58110
file.opts.filename,
@@ -155,7 +207,16 @@ export default t => (path, state) => {
155207
t.isMemberExpression(path.node.callee.callee) &&
156208
path.node.callee.callee.property &&
157209
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+
))
159220
) {
160221
const displayName =
161222
useDisplayName(state) &&

test/fixtures/add-identifier-and-display-name/code.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,18 @@ const WrappedComponent3 = styled(Inner)({})
1313
const WrappedComponent4 = styled(Inner).attrs(() => ({ something: 'else' }))({})
1414
const WrappedComponent5 = styled.div.attrs(() => ({ something: 'else' }))({})
1515
const WrappedComponent6 = styled.div.attrs(() => ({ something: 'else' }))``
16+
const WrappedComponent7 = styled.div.withConfig({
17+
shouldForwardProp: () => {},
18+
})({})
19+
20+
const WrappedComponent8 = styled.div
21+
.withConfig({
22+
shouldForwardProp: () => {},
23+
})
24+
.attrs(() => ({ something: 'else' }))({})
25+
26+
const WrappedComponent9 = styled.div
27+
.attrs(() => ({ something: 'else' }))
28+
.withConfig({
29+
shouldForwardProp: () => {},
30+
})({})

test/fixtures/add-identifier-and-display-name/output.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,22 @@ const WrappedComponent6 = styled.div.attrs(() => ({
5151
displayName: "WrappedComponent6",
5252
componentId: "sc-1cza72q-10"
5353
})``;
54+
const WrappedComponent7 = styled.div.withConfig({
55+
shouldForwardProp: () => {},
56+
displayName: "WrappedComponent7",
57+
componentId: "sc-1cza72q-11"
58+
})({});
59+
const WrappedComponent8 = styled.div.withConfig({
60+
shouldForwardProp: () => {},
61+
displayName: "WrappedComponent8",
62+
componentId: "sc-1cza72q-12"
63+
}).attrs(() => ({
64+
something: 'else'
65+
}))({});
66+
const WrappedComponent9 = styled.div.attrs(() => ({
67+
something: 'else'
68+
})).withConfig({
69+
shouldForwardProp: () => {},
70+
displayName: "WrappedComponent9",
71+
componentId: "sc-1cza72q-13"
72+
})({});

0 commit comments

Comments
 (0)