Skip to content

Commit a43b7fa

Browse files
committed
add pure annotation for function(object) form styled-components
1 parent ed877f9 commit a43b7fa

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default function({ types: t }) {
99
visitor: {
1010
CallExpression(path, state) {
1111
displayNameAndId(t)(path, state)
12+
pureAnnotation(t)(path, state)
1213
},
1314
TaggedTemplateExpression(path, state) {
1415
minify(t)(path, 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ styled(Inner).withConfig({
162162
displayName: \\"annotate-styled-calls-with-pure-comments__WrappedComponent\\",
163163
componentId: \\"sc-6tnjs3-6\\"
164164
})([\\"\\"]);
165+
const StyledObjectForm =
166+
/*#__PURE__*/
167+
styled.div.withConfig({
168+
displayName: \\"annotate-styled-calls-with-pure-comments__StyledObjectForm\\",
169+
componentId: \\"sc-6tnjs3-7\\"
170+
})({
171+
color: red
172+
});
165173
const notStyled =
166174
/*#__PURE__*/
167175
styled.div('')\`\`; // not transpiled by styled components but should add pure comment

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

Lines changed: 1 addition & 0 deletions
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 StyledObjectForm = styled.div({ color: red })
1011
const notStyled = styled.div('')`` // not transpiled by styled components but should add pure comment
1112
const normalFunc = add(5, 3)

0 commit comments

Comments
 (0)