Skip to content

Commit a092bc8

Browse files
authored
Merge pull request #167 from styled-components/displayname-function-variant
add handling for styled.div(styleObject) syntax
2 parents 328d3c7 + d643460 commit a092bc8

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ 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+
pureAnnotation(t)(path, state)
13+
},
1014
TaggedTemplateExpression(path, state) {
1115
minify(t)(path, state)
1216
displayNameAndId(t)(path, state)

src/visitors/displayNameAndId.js

Lines changed: 24 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,13 @@ 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+
) {
126144
const displayName =
127145
useDisplayName(state) &&
128146
getDisplayName(t)(path, useFileName(state) && state)

src/visitors/pure.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { isStyled, isPureHelper } from '../utils/detectors'
66
export default t => (path, state) => {
77
if (usePureAnnotation(state)) {
88
if (
9-
isStyled(path.node, state) ||
10-
isStyled(path.node.callee, state) ||
11-
isPureHelper(path.node.callee, state)
9+
isStyled(t)(path.node, state) ||
10+
isStyled(t)(path.node.callee, state) ||
11+
isPureHelper(t)(path.node.tag || path.node.callee, state)
1212
) {
1313
if (
14-
path.parent.type == 'VariableDeclarator' ||
15-
path.parent.type == 'TaggedTemplateExpression'
14+
path.parent.type === 'VariableDeclarator' ||
15+
path.parent.type === 'TaggedTemplateExpression'
1616
) {
1717
annotateAsPure(path)
1818
}

test/__snapshots__/index.test.js.snap

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,22 @@ styled(Inner).withConfig({
162162
displayName: \\"annotate-styled-calls-with-pure-comments__WrappedComponent\\",
163163
componentId: \\"sc-6tnjs3-6\\"
164164
})([\\"\\"]);
165-
const notStyled =
165+
const StyledObjectForm =
166166
/*#__PURE__*/
167-
styled.div('')\`\`; // not transpiled by styled components but should add pure comment
168-
167+
styled.div.withConfig({
168+
displayName: \\"annotate-styled-calls-with-pure-comments__StyledObjectForm\\",
169+
componentId: \\"sc-6tnjs3-7\\"
170+
})({
171+
color: red
172+
});
173+
const StyledFunctionForm =
174+
/*#__PURE__*/
175+
styled.div.withConfig({
176+
displayName: \\"annotate-styled-calls-with-pure-comments__StyledFunctionForm\\",
177+
componentId: \\"sc-6tnjs3-8\\"
178+
})(p => ({
179+
color: p.color || 'red'
180+
}));
169181
const normalFunc = add(5, 3);"
170182
`;
171183
@@ -389,7 +401,13 @@ var NamedWithInterpolation = _styledComponents.default.div.withConfig({
389401
390402
var Wrapped = (0, _styledComponents.default)(Inner).withConfig({
391403
displayName: \\"transpile-template-literals-with-config__Wrapped\\"
392-
})([\\"\\\\n color: red;\\\\n\\"]);"
404+
})([\\"\\\\n color: red;\\\\n\\"]);
405+
406+
var Foo = _styledComponents.default.div.withConfig({
407+
displayName: \\"transpile-template-literals-with-config__Foo\\"
408+
})({
409+
color: 'green'
410+
});"
393411
`;
394412
395413
exports[`fixtures should transpile template literals without config 1`] = `

test/fixtures/annotate-styled-calls-with-pure-comments/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ const styles = { One: styled.div`` }
77
let Component
88
Component = styled.div``
99
const WrappedComponent = styled(Inner)``
10-
const notStyled = styled.div('')`` // not transpiled by styled components but should add pure comment
10+
const StyledObjectForm = styled.div({ color: red })
11+
const StyledFunctionForm = styled.div(p => ({ color: p.color || 'red' }))
1112
const normalFunc = add(5, 3)

test/fixtures/transpile-template-literals-with-config/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ const NamedWithInterpolation = styled.div`
1111
const Wrapped = styled(Inner)`
1212
color: red;
1313
`
14+
15+
const Foo = styled.div({
16+
color: 'green',
17+
})

0 commit comments

Comments
 (0)