Skip to content

Commit 2ebbf8b

Browse files
SamChou19815facebook-github-bot
authored andcommitted
[flow] Ban more kinds of usages of React dollar types
Summary: It's starting locking down on these types, so that we can remove it in the next next release. Changelog: [errors] Use of `React$ComponentType`, `React$Context` and `React$RefSetter` will now trigger `internal-type` errors. Reviewed By: gkz Differential Revision: D70014990 fbshipit-source-id: 4e73ce37154b6080a181087ee319a93d0f874e36
1 parent 4b1a742 commit 2ebbf8b

File tree

15 files changed

+88
-88
lines changed

15 files changed

+88
-88
lines changed

src/typing/type_annotation.ml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,28 @@ module Make (Statement : Statement_sig.S) : Type_annotation_sig.S = struct
13711371
)
13721372
);
13731373
local_generic_type ()
1374+
| "React$ComponentType" ->
1375+
if not (Context.is_lib_file cx) then
1376+
Flow_js_utils.add_output
1377+
cx
1378+
(Error_message.EInternalType
1379+
( loc,
1380+
Flow_intermediate_error_types.ReactDollarUtilityTypesWithNonDollarAliases
1381+
"ComponentType"
1382+
)
1383+
);
1384+
local_generic_type ()
1385+
| "React$Context" ->
1386+
if not (Context.is_lib_file cx) then
1387+
Flow_js_utils.add_output
1388+
cx
1389+
(Error_message.EInternalType
1390+
( loc,
1391+
Flow_intermediate_error_types.ReactDollarUtilityTypesWithNonDollarAliases
1392+
"Context"
1393+
)
1394+
);
1395+
local_generic_type ()
13741396
| "React$ElementRef" ->
13751397
if not (Context.is_lib_file cx) then
13761398
Flow_js_utils.add_output
@@ -1382,6 +1404,17 @@ module Make (Statement : Statement_sig.S) : Type_annotation_sig.S = struct
13821404
)
13831405
);
13841406
local_generic_type ()
1407+
| "React$RefSetter" ->
1408+
if not (Context.is_lib_file cx) then
1409+
Flow_js_utils.add_output
1410+
cx
1411+
(Error_message.EInternalType
1412+
( loc,
1413+
Flow_intermediate_error_types.ReactDollarUtilityTypesWithNonDollarAliases
1414+
"RefSetter"
1415+
)
1416+
);
1417+
local_generic_type ()
13851418
(* other applications with id as head expr *)
13861419
| _ -> local_generic_type ()
13871420
end

tests/badly_positioned_unknown_use/badly_positioned_unknown_use.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ References:
1111
20| var x = f<React.ComponentType<{}>, _>()(foo(Comp));
1212
^^ [1]
1313
test1.js:4:34
14-
4| Component: React$ComponentType<{|...P|}>,
14+
4| Component: React.ComponentType<{|...P|}>,
1515
^^^^^^^^ [2]
1616

1717

tests/badly_positioned_unknown_use/test1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as React from 'react';
22

33
declare export function foo<P>(
4-
Component: React$ComponentType<{|...P|}>,
5-
): React$ComponentType<P>;
4+
Component: React.ComponentType<{|...P|}>,
5+
): React.ComponentType<P>;
66

77
class Comp extends React.Component<{}, {}> {}
88

tests/implicit_instantiation/ub_order_regression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as React from 'react';
44
declare var Comp: (props: {}) => $FlowFixMe;
55
class C<X> {}
66
declare var withStore: <Props: {...}>(
7-
C: React$ComponentType<Props>,
7+
C: React.ComponentType<Props>,
88
) => C<$Diff<Props, {...}>>;
99

1010
withStore(Comp) as C<{}>; // okay

tests/more_react/react-copy-write.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//@flow
22
var React = require("react");
33

4+
import type {ComponentType} from 'react';
5+
46
export type Recipe<T> = (draft: T, state: $ReadOnly<T>) => void;
57
export type Mutate<T> = (recipe: Recipe<T>) => void;
68

@@ -11,7 +13,7 @@ type ProviderProps<T> = {|
1113
initialState?: T,
1214
|};
1315

14-
export type Provider<T> = React$ComponentType<ProviderProps<T>>;
16+
export type Provider<T> = ComponentType<ProviderProps<T>>;
1517

1618
type GetReturnType = <T, S>((T) => S) => S;
1719

tests/opaque_types_and_component_syntax_destructors/opaque.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {createContext, useContext} from 'react'
22

33
export opaque type Tag = number;
44

5-
export const Context: React$Context<Tag> = createContext<Tag>(3);
5+
export const Context: React.Context<Tag> = createContext<Tag>(3);
66

77
component Foo() {
88
const context = useContext(Context);

tests/opaque_types_and_component_syntax_destructors/opaque_types_and_component_syntax_destructors.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Cannot cast `context` to number because `Tag` [1] is incompatible with number [2
88

99
References:
1010
opaque.js:5:37
11-
5| export const Context: React$Context<Tag> = createContext<Tag>(3);
11+
5| export const Context: React.Context<Tag> = createContext<Tag>(3);
1212
^^^ [1]
1313
non-local.js:7:14
1414
7| context as number; // ERROR

tests/react/contravariant_refsetter.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import * as React from 'react';
22

33
declare const x: React.RefSetter<number | string>;
44
(x: React.RefSetter<number>); // OK!
5-
(x: React$RefSetter<number>); // OK!
65

7-
declare const y: React$RefSetter<number | string>;
6+
declare const y: React.RefSetter<number | string>;
87
(y: React.RefSetter<number>); // OK!
9-
(y: React$RefSetter<number>); // OK!
108

119
(x: React.RefSetter<number | string | boolean>); // ERROR
1210
(y: React.RefSetter<number | string | boolean>); // ERROR

tests/react/creatRef.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
{
44
class MyComponent extends React.Component<void> {}
55

6-
const ref: {current: null | React$ComponentType<MyComponent>} = React.createRef(); // Ok
6+
const ref: {current: null | React.ComponentType<MyComponent>} = React.createRef(); // Ok
77
}
88

99
{

tests/react/hoc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as React from 'react';
22

33
function myHOC(
4-
Component: React$ComponentType<{foo: number, bar: number}>,
5-
): React$ComponentType<{foo: number}> {
4+
Component: React.ComponentType<{foo: number, bar: number}>,
5+
): React.ComponentType<{foo: number}> {
66
return class extends React.Component<{foo: number}, {bar: number}> {
77
state: {bar: number} = {bar: 2};
88
render(): React.Node {
@@ -33,7 +33,7 @@ function UnwrappedFun(props: {foo: number, bar: number}) {
3333
myHOC(class Empty extends React.Component<{foo: string}, void> {}); // Error
3434
myHOC(function Empty(props: {foo: string}) {}); // Error
3535

36-
const Wrapped: React$ComponentType<{foo: number}> = myHOC(Unwrapped);
36+
const Wrapped: React.ComponentType<{foo: number}> = myHOC(Unwrapped);
3737
const WrappedFun = myHOC(UnwrappedFun);
3838

3939
<Wrapped nonsense="what" />; // Error: `foo` is required.

0 commit comments

Comments
 (0)