Skip to content

Commit ed877f9

Browse files
committed
annotate object-form styled component usage with displayName
1 parent 3ce09e5 commit ed877f9

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import assignStyledRequired from './visitors/assignStyledRequired'
77
export default function({ types: t }) {
88
return {
99
visitor: {
10+
CallExpression(path, state) {
11+
displayNameAndId(t)(path, state)
12+
},
1013
TaggedTemplateExpression(path, state) {
1114
minify(t)(path, state)
1215
displayNameAndId(t)(path, state)

src/visitors/displayNameAndId.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,23 @@ const addConfig = t => (path, displayName, componentId) => {
2929
)
3030
}
3131

32-
// Replace x`...` with x.withConfig({ })`...`
33-
path.node.tag = t.callExpression(
34-
t.memberExpression(path.node.tag, t.identifier('withConfig')),
35-
[t.objectExpression(withConfigProps)]
36-
)
32+
if (path.node.tag) {
33+
// Replace x`...` with x.withConfig({ })`...`
34+
path.node.tag = t.callExpression(
35+
t.memberExpression(path.node.tag, t.identifier('withConfig')),
36+
[t.objectExpression(withConfigProps)]
37+
)
38+
} else {
39+
path.replaceWith(
40+
t.callExpression(
41+
t.callExpression(
42+
t.memberExpression(path.node.callee, t.identifier('withConfig')),
43+
[t.objectExpression(withConfigProps)]
44+
),
45+
path.node.arguments
46+
)
47+
)
48+
}
3749
}
3850

3951
const getBlockName = file => {
@@ -122,7 +134,14 @@ const getComponentId = state => {
122134
}
123135

124136
export default t => (path, state) => {
125-
if (isStyled(t)(path.node.tag, state)) {
137+
if (
138+
path.node.tag
139+
? isStyled(t)(path.node.tag, state)
140+
: isStyled(t)(path.node.callee, state) &&
141+
path.node.callee.property &&
142+
path.node.callee.property.name !== 'withConfig' &&
143+
t.isObjectExpression(path.node.arguments[0])
144+
) {
126145
const displayName =
127146
useDisplayName(state) &&
128147
getDisplayName(t)(path, useFileName(state) && state)

test/__snapshots__/index.test.js.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,13 @@ var NamedWithInterpolation = _styledComponents.default.div.withConfig({
389389
390390
var Wrapped = (0, _styledComponents.default)(Inner).withConfig({
391391
displayName: \\"transpile-template-literals-with-config__Wrapped\\"
392-
})([\\"\\\\n color: red;\\\\n\\"]);"
392+
})([\\"\\\\n color: red;\\\\n\\"]);
393+
394+
var Foo = _styledComponents.default.div.withConfig({
395+
displayName: \\"transpile-template-literals-with-config__Foo\\"
396+
})({
397+
color: 'green'
398+
});"
393399
`;
394400
395401
exports[`fixtures should transpile template literals without config 1`] = `

0 commit comments

Comments
 (0)