Skip to content

Commit b51f275

Browse files
committed
Improve styled-components skipping and add tests
1 parent b2e4271 commit b51f275

File tree

5 files changed

+120
-6
lines changed

5 files changed

+120
-6
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
"prettier": "^1.16.4",
7676
"react": "^16.8.4",
7777
"react-dom": "^16.8.4",
78-
"react-is": "^16.8.4"
78+
"react-is": "^16.8.4",
79+
"styled-components": "^4.2.0"
7980
},
8081
"dependencies": {
8182
"object-is": "^1.0.1"

src/__tests__/visitor.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import React, {
1212
} from 'react'
1313

1414
import { createPortal } from 'react-dom'
15+
import styled from 'styled-components'
1516

1617
import {
1718
Dispatcher,
@@ -105,6 +106,25 @@ describe('visitElement', () => {
105106
expect(children[1].type).toBe(Noop)
106107
})
107108

109+
it('walks StyledComponent DOM elements', () => {
110+
const Comp = styled.div``
111+
const children = visitElement(
112+
<Comp>
113+
<Noop />
114+
</Comp>,
115+
[],
116+
() => {}
117+
)
118+
expect(children.length).toBe(1)
119+
expect(children[0].type).toBe(Noop)
120+
})
121+
122+
it('walks StyledComponent wrapper elements', () => {
123+
const Comp = styled(Noop)``
124+
const children = visitElement(<Comp />, [], () => {})
125+
expect(children.length).toBe(1)
126+
})
127+
108128
it('walks Providers and Consumers', () => {
109129
const Context = createContext('default')
110130
const leaf = jest.fn().mockReturnValue(null)

src/types/element.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ export type ForwardRefElement = {
104104
type: {
105105
render: ComponentType<DefaultProps> & ComponentStatics,
106106
$$typeof: typeof REACT_FORWARD_REF_TYPE,
107-
styledComponentId?: string // styled-components specific property
107+
// styled-components specific properties
108+
styledComponentId?: string,
109+
target?: ComponentType<mixed> | string
108110
},
109111
props: DefaultProps,
110112
$$typeof: typeof REACT_ELEMENT_TYPE

src/visitor.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,12 @@ export const visitElement = (
132132

133133
case REACT_FORWARD_REF_TYPE: {
134134
const refElement = ((element: any): ForwardRefElement)
135-
if (typeof refElement.type.styledComponentId === 'string') {
135+
if (
136+
typeof refElement.type.styledComponentId === 'string' &&
137+
typeof refElement.type.target !== 'function'
138+
) {
136139
// This is an optimization that's specific to styled-components
137-
// We can safely skip them and return their children
140+
// We can safely skip them if they're not wrapping a component
138141
return getChildrenArray(refElement.props.children)
139142
} else {
140143
const {

yarn.lock

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,23 @@
716716
exec-sh "^0.3.2"
717717
minimist "^1.2.0"
718718

719+
"@emotion/is-prop-valid@^0.7.3":
720+
version "0.7.3"
721+
resolved "https://registry.yarnpkg.com/@emotion/is-prop-valid/-/is-prop-valid-0.7.3.tgz#a6bf4fa5387cbba59d44e698a4680f481a8da6cc"
722+
integrity sha512-uxJqm/sqwXw3YPA5GXX365OBcJGFtxUVkB6WyezqFHlNe9jqUWH5ur2O2M8dGBz61kn1g3ZBlzUunFQXQIClhA==
723+
dependencies:
724+
"@emotion/memoize" "0.7.1"
725+
726+
"@emotion/[email protected]":
727+
version "0.7.1"
728+
resolved "https://registry.yarnpkg.com/@emotion/memoize/-/memoize-0.7.1.tgz#e93c13942592cf5ef01aa8297444dc192beee52f"
729+
integrity sha512-Qv4LTqO11jepd5Qmlp3M1YEjBumoTHcHFdgPTQ+sFlIL5myi/7xu/POwP7IRu6odBdmLXdtIs1D6TuW6kbwbbg==
730+
731+
"@emotion/unitless@^0.7.0":
732+
version "0.7.3"
733+
resolved "https://registry.yarnpkg.com/@emotion/unitless/-/unitless-0.7.3.tgz#6310a047f12d21a1036fb031317219892440416f"
734+
integrity sha512-4zAPlpDEh2VwXswwr/t8xGNDGg8RQiPxtxZ3qQEXyQsBV39ptTdESCjuBvGze1nLMVrxmTIKmnO/nAV8Tqjjzg==
735+
719736
"@jest/console@^24.3.0":
720737
version "24.3.0"
721738
resolved "https://registry.yarnpkg.com/@jest/console/-/console-24.3.0.tgz#7bd920d250988ba0bf1352c4493a48e1cb97671e"
@@ -1241,6 +1258,21 @@ babel-plugin-jest-hoist@^24.3.0:
12411258
dependencies:
12421259
"@types/babel__traverse" "^7.0.6"
12431260

1261+
"babel-plugin-styled-components@>= 1":
1262+
version "1.10.0"
1263+
resolved "https://registry.yarnpkg.com/babel-plugin-styled-components/-/babel-plugin-styled-components-1.10.0.tgz#ff1f42ad2cc78c21f26b62266b8f564dbc862939"
1264+
integrity sha512-sQVKG8irFXx14ZfaK1bBePirfkacl3j8nZwSZK+ZjsbnadRHKQTbhXbe/RB1vT6Vgkz45E+V95LBq4KqdhZUNw==
1265+
dependencies:
1266+
"@babel/helper-annotate-as-pure" "^7.0.0"
1267+
"@babel/helper-module-imports" "^7.0.0"
1268+
babel-plugin-syntax-jsx "^6.18.0"
1269+
lodash "^4.17.10"
1270+
1271+
babel-plugin-syntax-jsx@^6.18.0:
1272+
version "6.18.0"
1273+
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946"
1274+
integrity sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY=
1275+
12441276
babel-plugin-transform-async-to-promises@^0.8.3:
12451277
version "0.8.5"
12461278
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-to-promises/-/babel-plugin-transform-async-to-promises-0.8.5.tgz#8d9817a4a99d36df57616857650522b7c2be511c"
@@ -1470,6 +1502,11 @@ camelcase@^5.0.0:
14701502
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.2.0.tgz#e7522abda5ed94cc0489e1b8466610e88404cf45"
14711503
integrity sha512-IXFsBS2pC+X0j0N/GE7Dm7j3bsEBp+oTpb7F50dwEVX7rf3IgwO9XatnegTsDtniKCUtEJH4fSU6Asw7uoVLfQ==
14721504

1505+
camelize@^1.0.0:
1506+
version "1.0.0"
1507+
resolved "https://registry.yarnpkg.com/camelize/-/camelize-1.0.0.tgz#164a5483e630fa4321e5af07020e531831b2609b"
1508+
integrity sha1-FkpUg+Yw+kMh5a8HAg5TGDGyYJs=
1509+
14731510
caniuse-api@^1.5.2:
14741511
version "1.6.1"
14751512
resolved "https://registry.yarnpkg.com/caniuse-api/-/caniuse-api-1.6.1.tgz#b534e7c734c4f81ec5fbe8aca2ad24354b962c6c"
@@ -1797,6 +1834,11 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
17971834
shebang-command "^1.2.0"
17981835
which "^1.2.9"
17991836

1837+
css-color-keywords@^1.0.0:
1838+
version "1.0.0"
1839+
resolved "https://registry.yarnpkg.com/css-color-keywords/-/css-color-keywords-1.0.0.tgz#fea2616dc676b2962686b3af8dbdbe180b244e05"
1840+
integrity sha1-/qJhbcZ2spYmhrOvjb2+GAskTgU=
1841+
18001842
[email protected], css-color-names@^0.0.4:
18011843
version "0.0.4"
18021844
resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-0.0.4.tgz#808adc2e79cf84738069b646cb20ec27beb629e0"
@@ -1846,6 +1888,15 @@ css-selector-tokenizer@^0.7.0:
18461888
fastparse "^1.1.1"
18471889
regexpu-core "^1.0.0"
18481890

1891+
css-to-react-native@^2.2.2:
1892+
version "2.3.0"
1893+
resolved "https://registry.yarnpkg.com/css-to-react-native/-/css-to-react-native-2.3.0.tgz#bf80d24ec4a08e430306ef429c0586e6ed5485f7"
1894+
integrity sha512-IhR7bNIrCFwbJbKZOAjNDZdwpsbjTN6f1agXeELHDqg1wHPA8c2QLruttKOW7hgMGetkfraRJCIEMrptifBfVw==
1895+
dependencies:
1896+
camelize "^1.0.0"
1897+
css-color-keywords "^1.0.0"
1898+
postcss-value-parser "^3.3.0"
1899+
18491900
18501901
version "1.0.0-alpha.28"
18511902
resolved "https://registry.yarnpkg.com/css-tree/-/css-tree-1.0.0-alpha.28.tgz#8e8968190d886c9477bc8d61e96f61af3f7ffa7f"
@@ -4262,6 +4313,11 @@ mem@^4.0.0:
42624313
mimic-fn "^2.0.0"
42634314
p-is-promise "^2.0.0"
42644315

4316+
memoize-one@^5.0.0:
4317+
version "5.0.4"
4318+
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.0.4.tgz#005928aced5c43d890a4dfab18ca908b0ec92cbc"
4319+
integrity sha512-P0z5IeAH6qHHGkJIXWw0xC2HNEgkx/9uWWBQw64FJj3/ol14VYdfVGWWr0fXfjhhv3TKVIqUq65os6O4GUNksA==
4320+
42654321
memorystream@^0.3.1:
42664322
version "0.3.1"
42674323
resolved "https://registry.yarnpkg.com/memorystream/-/memorystream-0.3.1.tgz#86d7090b30ce455d63fbae12dda51a47ddcaf9b2"
@@ -5619,7 +5675,7 @@ prompts@^2.0.1:
56195675
kleur "^3.0.2"
56205676
sisteransi "^1.0.0"
56215677

5622-
prop-types@^15.6.2:
5678+
prop-types@^15.5.4, prop-types@^15.6.2:
56235679
version "15.7.2"
56245680
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
56255681
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
@@ -5719,6 +5775,11 @@ react-dom@^16.8.4:
57195775
prop-types "^15.6.2"
57205776
scheduler "^0.13.4"
57215777

5778+
react-is@^16.6.0:
5779+
version "16.8.6"
5780+
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16"
5781+
integrity sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==
5782+
57225783
react-is@^16.8.1, react-is@^16.8.4:
57235784
version "16.8.4"
57245785
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.4.tgz#90f336a68c3a29a096a3d648ab80e87ec61482a2"
@@ -6637,6 +6698,23 @@ style-inject@^0.3.0:
66376698
resolved "https://registry.yarnpkg.com/style-inject/-/style-inject-0.3.0.tgz#d21c477affec91811cc82355832a700d22bf8dd3"
66386699
integrity sha512-IezA2qp+vcdlhJaVm5SOdPPTUu0FCEqfNSli2vRuSIBbu5Nq5UvygTk/VzeCqfLz2Atj3dVII5QBKGZRZ0edzw==
66396700

6701+
styled-components@^4.2.0:
6702+
version "4.2.0"
6703+
resolved "https://registry.yarnpkg.com/styled-components/-/styled-components-4.2.0.tgz#811fbbec4d64c7189f6c7482b9eb6fefa7fefef7"
6704+
integrity sha512-L/LzkL3ZbBhqIVHdR7DbYujy4tqvTNRfc+4JWDCYyhTatI+8CRRQUmdaR0+ARl03DWsfKLhjewll5uNLrqrl4A==
6705+
dependencies:
6706+
"@babel/helper-module-imports" "^7.0.0"
6707+
"@emotion/is-prop-valid" "^0.7.3"
6708+
"@emotion/unitless" "^0.7.0"
6709+
babel-plugin-styled-components ">= 1"
6710+
css-to-react-native "^2.2.2"
6711+
memoize-one "^5.0.0"
6712+
prop-types "^15.5.4"
6713+
react-is "^16.6.0"
6714+
stylis "^3.5.0"
6715+
stylis-rule-sheet "^0.0.10"
6716+
supports-color "^5.5.0"
6717+
66406718
stylehacks@^4.0.0:
66416719
version "4.0.3"
66426720
resolved "https://registry.yarnpkg.com/stylehacks/-/stylehacks-4.0.3.tgz#6718fcaf4d1e07d8a1318690881e8d96726a71d5"
@@ -6646,6 +6724,16 @@ stylehacks@^4.0.0:
66466724
postcss "^7.0.0"
66476725
postcss-selector-parser "^3.0.0"
66486726

6727+
stylis-rule-sheet@^0.0.10:
6728+
version "0.0.10"
6729+
resolved "https://registry.yarnpkg.com/stylis-rule-sheet/-/stylis-rule-sheet-0.0.10.tgz#44e64a2b076643f4b52e5ff71efc04d8c3c4a430"
6730+
integrity sha512-nTbZoaqoBnmK+ptANthb10ZRZOGC+EmTLLUxeYIuHNkEKcmKgXX1XWKkUBT2Ac4es3NybooPe0SmvKdhKJZAuw==
6731+
6732+
stylis@^3.5.0:
6733+
version "3.5.4"
6734+
resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.5.4.tgz#f665f25f5e299cf3d64654ab949a57c768b73fbe"
6735+
integrity sha512-8/3pSmthWM7lsPBKv7NXkzn2Uc9W7NotcwGNpJaa3k7WMM1XDCA4MgT5k/8BIexd5ydZdboXtU90XH9Ec4Bv/Q==
6736+
66496737
supports-color@^2.0.0:
66506738
version "2.0.0"
66516739
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"
@@ -6658,7 +6746,7 @@ supports-color@^3.2.3:
66586746
dependencies:
66596747
has-flag "^1.0.0"
66606748

6661-
supports-color@^5.3.0, supports-color@^5.4.0:
6749+
supports-color@^5.3.0, supports-color@^5.4.0, supports-color@^5.5.0:
66626750
version "5.5.0"
66636751
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
66646752
integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==

0 commit comments

Comments
 (0)