Skip to content

Commit

Permalink
Merge pull request #323 from ithinkdancan/main
Browse files Browse the repository at this point in the history
merge existing withConfig arguments to allow SSR when using shouldForwardProp
  • Loading branch information
quantizor authored Jun 29, 2021
2 parents f4e4c94 + 04eed56 commit 6a355b5
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"semi": false,
"singleQuote": true,
"trailingComma": "es5"
"trailingComma": "es5",
"arrowParens": "avoid"
}
63 changes: 62 additions & 1 deletion src/visitors/displayNameAndId.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const addConfig = t => (path, displayName, componentId) => {
}

const withConfigProps = []

if (displayName) {
withConfigProps.push(
t.objectProperty(
Expand All @@ -34,6 +35,34 @@ const addConfig = t => (path, displayName, componentId) => {
)
}

const existingConfig = getExistingConfig(t)(path)

if (
existingConfig &&
existingConfig.arguments.length &&
!existingConfig.arguments[0].properties.some(prop =>
['displayName', 'componentId'].includes(prop.key.name)
)
) {
existingConfig.arguments[0].properties.push(...withConfigProps)
return
}

if (
path.node.callee &&
t.isMemberExpression(path.node.callee.callee) &&
path.node.callee.callee.property &&
path.node.callee.callee.property.name &&
path.node.callee.callee.property.name == 'withConfig' &&
path.node.callee.arguments.length &&
!path.node.callee.arguments[0].properties.some(prop =>
['displayName', 'componentId'].includes(prop.key.name)
)
) {
path.node.callee.arguments[0].properties.push(...withConfigProps)
return
}

if (path.node.tag) {
// Replace x`...` with x.withConfig({ })`...`
path.node.tag = t.callExpression(
Expand All @@ -53,6 +82,29 @@ const addConfig = t => (path, displayName, componentId) => {
}
}

const getExistingConfig = t => path => {
if (
path.node.callee &&
t.isMemberExpression(path.node.callee.callee) &&
path.node.callee.callee.property &&
path.node.callee.callee.property.name &&
path.node.callee.callee.property.name == 'withConfig'
) {
return path.node.callee
}

if (
path.node.callee &&
t.isMemberExpression(path.node.callee.callee) &&
path.node.callee.callee.object &&
path.node.callee.callee.object.callee &&
path.node.callee.callee.object.callee.property &&
path.node.callee.callee.object.callee.property.name === 'withConfig'
) {
return path.node.callee.callee.object
}
}

const getBlockName = file => {
const name = path.basename(
file.opts.filename,
Expand Down Expand Up @@ -155,7 +207,16 @@ export default t => (path, state) => {
t.isMemberExpression(path.node.callee.callee) &&
path.node.callee.callee.property &&
path.node.callee.callee.property.name &&
path.node.callee.callee.property.name !== 'withConfig')
path.node.callee.callee.property.name !== 'withConfig') ||
// styled(x).withConfig({})
(isStyled(t)(path.node.callee, state) &&
t.isMemberExpression(path.node.callee.callee) &&
path.node.callee.callee.property &&
path.node.callee.callee.property.name &&
path.node.callee.callee.property.name == 'withConfig' &&
!path.node.callee.arguments[0].properties.some((prop) =>
['displayName', 'componentId'].includes(prop.key.name)
))
) {
const displayName =
useDisplayName(state) &&
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/add-identifier-and-display-name/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ const WrappedComponent3 = styled(Inner)({})
const WrappedComponent4 = styled(Inner).attrs(() => ({ something: 'else' }))({})
const WrappedComponent5 = styled.div.attrs(() => ({ something: 'else' }))({})
const WrappedComponent6 = styled.div.attrs(() => ({ something: 'else' }))``
const WrappedComponent7 = styled.div.withConfig({
shouldForwardProp: () => {},
})({})

const WrappedComponent8 = styled.div
.withConfig({
shouldForwardProp: () => {},
})
.attrs(() => ({ something: 'else' }))({})

const WrappedComponent9 = styled.div
.attrs(() => ({ something: 'else' }))
.withConfig({
shouldForwardProp: () => {},
})({})
19 changes: 19 additions & 0 deletions test/fixtures/add-identifier-and-display-name/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,22 @@ const WrappedComponent6 = styled.div.attrs(() => ({
displayName: "WrappedComponent6",
componentId: "sc-1cza72q-10"
})``;
const WrappedComponent7 = styled.div.withConfig({
shouldForwardProp: () => {},
displayName: "WrappedComponent7",
componentId: "sc-1cza72q-11"
})({});
const WrappedComponent8 = styled.div.withConfig({
shouldForwardProp: () => {},
displayName: "WrappedComponent8",
componentId: "sc-1cza72q-12"
}).attrs(() => ({
something: 'else'
}))({});
const WrappedComponent9 = styled.div.attrs(() => ({
something: 'else'
})).withConfig({
shouldForwardProp: () => {},
displayName: "WrappedComponent9",
componentId: "sc-1cza72q-13"
})({});

0 comments on commit 6a355b5

Please sign in to comment.