Skip to content

Commit 7e11092

Browse files
authored
switch back to mainline hoist-non-react-statics (styled-components#2934)
should fix styled-components#2930
1 parent 9e29e49 commit 7e11092

File tree

9 files changed

+106
-333
lines changed

9 files changed

+106
-333
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// flow-typed signature: 3010d1e44b78eedb9cf85c4f9af67238
2+
// flow-typed version: a9e75cb9a5/hoist-non-react-statics_v3.x.x/flow_>=v0.84.x <=v0.102.x
3+
4+
declare module 'hoist-non-react-statics' {
5+
/**
6+
* Inspired by DefinitelyTyped/types/hoist-non-react-statics/index.d.ts
7+
*
8+
* Unfortunately, unlike in TypeScript, current flow definitions for React does not allow us to tell whether
9+
* a React$ComponentType is “clean”, or had been the result of a call to React.memo or React.forwardRef.
10+
* Therefore we’ll only be able to precisely filter out statics that are common to all 3 cases, and will
11+
* have to live with maybes for everything else. This is not 100% precise, but is better than a blanket
12+
* $Shape call on the source component’s statics like what we were doing before in the v2.x.x defs.
13+
*/
14+
15+
// declare type REACT_STATICS = {
16+
// childContextTypes: any,
17+
// contextType: any,
18+
// contextTypes: any,
19+
// defaultProps: any,
20+
// displayName: any,
21+
// getDefaultProps: any,
22+
// getDerivedStateFromError: any,
23+
// getDerivedStateFromProps: any,
24+
// mixins: any,
25+
// propTypes: any,
26+
// type: any,
27+
// ...
28+
// };
29+
30+
// declare type MEMO_STATICS = {
31+
// $$typeof: any,
32+
// compare: any,
33+
// defaultProps: any,
34+
// displayName: any,
35+
// propTypes: any,
36+
// type: any,
37+
// ...
38+
// };
39+
40+
// declare type FORWARD_REF_STATICS = {
41+
// $$typeof: any,
42+
// render: any,
43+
// defaultProps: any,
44+
// displayName: any,
45+
// propTypes: any,
46+
// ...
47+
// };
48+
49+
declare type REACT_STATICS = {
50+
// “Maybe” React statics
51+
$$typeof?: any,
52+
childContextTypes?: any,
53+
compare?: any,
54+
contextType?: any,
55+
contextTypes?: any,
56+
getDefaultProps?: any,
57+
getDerivedStateFromError?: any,
58+
getDerivedStateFromProps?: any,
59+
mixins?: any,
60+
render?: any,
61+
type?: any,
62+
// Common React statics
63+
defaultProps: any,
64+
displayName: any,
65+
propTypes: any,
66+
...
67+
};
68+
69+
declare type $HoistedStatics<S, C> = $Call<
70+
& (empty => $Diff<S, REACT_STATICS>)
71+
& (any => $Diff<S, $ObjMap<C, any> & REACT_STATICS>),
72+
C
73+
>;
74+
75+
/*
76+
TP - target component props
77+
T - target component statics
78+
S - source component statics
79+
*/
80+
declare function hoistNonReactStatics<TP, T, S, C: { [key: string]: true, ... }>(
81+
TargetComponent: React$ComponentType<TP> & T,
82+
SourceComponent: React$ComponentType<any> & S,
83+
customStatics?: C
84+
): React$ComponentType<TP> & $HoistedStatics<S, C> & T;
85+
86+
declare export default typeof hoistNonReactStatics;
87+
}

packages/styled-components/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@emotion/unitless": "^0.7.4",
7070
"babel-plugin-styled-components": ">= 1",
7171
"css-to-react-native": "^3.0.0",
72+
"hoist-non-react-statics": "^3.0.0",
7273
"shallowequal": "^1.1.0",
7374
"stylis-rule-sheet": "^0.0.10",
7475
"supports-color": "^5.5.0"

packages/styled-components/src/hoc/withTheme.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @flow
22
import React, { useContext, type AbstractComponent } from 'react';
3+
import hoistStatics from 'hoist-non-react-statics';
34
import { ThemeContext } from '../models/ThemeProvider';
45
import determineTheme from '../utils/determineTheme';
56
import getComponentName from '../utils/getComponentName';
6-
import hoistStatics from '../utils/hoist';
77

88
// NOTE: this would be the correct signature:
99
// export default <Config: { theme?: any }, Instance>(

packages/styled-components/src/models/StyledComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import React, {
77
type AbstractComponent,
88
type Ref,
99
} from 'react';
10+
import hoist from 'hoist-non-react-statics';
1011
import merge from '../utils/mixinDeep';
1112
import ComponentStyle from './ComponentStyle';
1213
import createWarnTooManyClasses from '../utils/createWarnTooManyClasses';
@@ -15,7 +16,6 @@ import escape from '../utils/escape';
1516
import generateDisplayName from '../utils/generateDisplayName';
1617
import getComponentName from '../utils/getComponentName';
1718
import hasher from '../utils/hasher';
18-
import hoist from '../utils/hoist';
1919
import isFunction from '../utils/isFunction';
2020
import isStyledComponent from '../utils/isStyledComponent';
2121
import isTag from '../utils/isTag';
@@ -283,7 +283,7 @@ export default function createStyledComponent(
283283
WrappedStyledComponent.toString = () => `.${WrappedStyledComponent.styledComponentId}`;
284284

285285
if (isCompositeComponent) {
286-
hoist(WrappedStyledComponent, target, {
286+
hoist(WrappedStyledComponent, (target: any), {
287287
// all SC-specific things should not be hoisted
288288
attrs: true,
289289
componentStyle: true,

packages/styled-components/src/models/StyledNativeComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @flow
22
import React, { createElement, Component } from 'react';
3+
import hoist from 'hoist-non-react-statics';
34
import merge from '../utils/mixinDeep';
45
import determineTheme from '../utils/determineTheme';
56
import { EMPTY_ARRAY, EMPTY_OBJECT } from '../utils/empties';
67
import generateDisplayName from '../utils/generateDisplayName';
7-
import hoist from '../utils/hoist';
88
import isFunction from '../utils/isFunction';
99
import isTag from '../utils/isTag';
1010
import isStyledComponent from '../utils/isStyledComponent';
@@ -183,7 +183,7 @@ export default (InlineStyle: Function) => {
183183
});
184184

185185
if (isClass) {
186-
hoist(WrappedStyledNativeComponent, target, {
186+
hoist(WrappedStyledNativeComponent, (target: any), {
187187
// all SC-specific things should not be hoisted
188188
attrs: true,
189189
displayName: true,

packages/styled-components/src/test/basic.test.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// @flow
22
/* eslint-disable no-console */
3+
import hoist from 'hoist-non-react-statics';
34
import React, { Component, StrictMode } from 'react';
45
import { findDOMNode } from 'react-dom';
56
import { findRenderedComponentWithType, renderIntoDocument } from 'react-dom/test-utils';
67
import TestRenderer from 'react-test-renderer';
7-
88
import { resetStyled, expectCSSMatches } from './utils';
9-
import hoistStatics from '../utils/hoist';
109
import { find } from '../../test-utils';
1110

1211
let styled;

packages/styled-components/src/utils/hoist.js

-103
This file was deleted.

0 commit comments

Comments
 (0)