Skip to content

Commit

Permalink
Merge branch 'master' of github.com:flow-typed/flow-typed into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Brianzchen committed Mar 26, 2023
2 parents 1542529 + 1ec13c5 commit 65d9a42
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 24 deletions.
2 changes: 2 additions & 0 deletions cli/src/commands/runTests.js
Expand Up @@ -381,6 +381,7 @@ export async function writeFlowConfig(
'[options]',
'include_warnings=true',
'server.max_workers=0',
semver.gte(version, '0.200.0') ? 'exact_by_default=true' : '', // from version 0.202.0 default is true
// Fixes out of shared memory error for Mac Rosetta 2, see https://github.com/facebook/flow/issues/8538
'sharedmemory.heap_size=3221225472',
semver.lt(version, '0.125.0')
Expand Down Expand Up @@ -542,6 +543,7 @@ async function removeTrashFromBinDir() {
const CONFIGURATION_CHANGE_VERSIONS = [
'v0.104.0', // Adding lint
'v0.125.0', // Remove suppress_comments
'v0.200.0', // exact_by_default become required
];

/**
Expand Down
Expand Up @@ -24,13 +24,10 @@ declare module "react-router-dom" {

declare export var NavLink: React$ComponentType<{
+to: string | LocationShape,
+activeClassName?: string,
+className?: string,
+activeStyle?: { +[string]: mixed, ... },
+style?: { +[string]: mixed, ... },
+isActive?: (match: Match, location: Location) => boolean,
+children?: React$Node,
+exact?: boolean,
+className?: string | (props: {| isActive: boolean, isPending: boolean |}) => string | void,
+style?: { +[string]: mixed, ... } | (props: {| isActive: boolean, isPending: boolean|}) => { +[string]: mixed, ... } | void,
+children?: React$Node | ({| isActive: boolean, isPending: boolean |}) => React$Node,
+end?: boolean,
+strict?: boolean,
...
}>
Expand Down Expand Up @@ -441,7 +438,7 @@ declare module "react-router-dom" {

declare export function useHistory(): $PropertyType<ContextRouter, 'history'>;
declare export function useLocation(): $PropertyType<ContextRouter, 'location'>;
declare export function useOutletContext<T>(): T;
declare export function useOutletContext<T = any>(): T;
declare export function useParams<Params = $PropertyType<$PropertyType<ContextRouter, 'match'>, 'params'>>(): Params;
declare export function useRouteMatch(path?: MatchPathOptions | string | string[]): $PropertyType<ContextRouter, 'match'>;

Expand All @@ -464,28 +461,23 @@ declare module "react-router-dom" {

declare export type RouteObject = IndexRouteObject | NonIndexRouteObject;

declare export function createRoutesFromChildren(
children: React$Node,
): Array<RouteObject>;
declare export function createRoutesFromElements(elements: React$Node): RouteObject[]
declare export var createRoutesFromChildren: typeof createRoutesFromElements;

declare export type Params<Key: string> = {
+[key: Key]: string | void;
};

declare type RouteMatch<ParamKey: string> = {|
params: Params<ParamKey>,
pathname: string,
route: RouteObject,
|};
declare export type RouteMatch<ParamKey: string = string, RouteObjectType: RouteObject = RouteObject> = AgnosticRouteMatch<ParamKey, RouteObjectType>

declare export function matchRoutes(
declare export function matchRoutes<RouteObjectType: RouteObject = RouteObject>(
routes: Array<RouteObject>,
location: LocationShape | string,
basename?: string,
): Array<RouteMatch<string>> | null;
): Array<AgnosticRouteMatch<string, RouteObjectType>> | null;

declare export function renderMatches(
matches: Array<RouteMatch<string>> | null,
declare export function renderMatches<RouteObjectType = RouteObject>(
matches: Array<RouteMatch<string, RouteObjectType>> | null,
): React$Element<any> | null;

declare type PathPattern = {|
Expand Down
Expand Up @@ -6,6 +6,8 @@ import {
Link,
NavLink,
matchPath,
matchRoutes,
renderMatches,
withRouter,
Navigate,
Outlet,
Expand All @@ -14,12 +16,15 @@ import {
useHistory,
useLocation,
useNavigate,
useOutlet,
useOutletContext,
useParams,
useRouteMatch,
useMatches,
} from "react-router-dom";
import type {
AgnosticRouteMatch,
RouteObject,
Location,
ContextRouter,
Match,
Expand Down Expand Up @@ -121,13 +126,10 @@ describe("react-router-dom", () => {

<NavLink
to="/about"
activeClassName="active"
className="link"
activeStyle={{ color: "red" }}
style={{ color: "blue" }}
isActive={(match, location) => true}
strict
exact
end
>
About
</NavLink>;
Expand Down Expand Up @@ -163,6 +165,44 @@ describe("react-router-dom", () => {

// $FlowExpectedError[incompatible-type] - to prop must be a string or LocationShape
<NavLink to={[]} />;

// activeClassName, activeStyle, end, isActive have been dropped unfortunately props cannot be strict so no errors can be expected
<NavLink
to="/about"
activeClassName="active"
activeStyle={{ color: "red" }}
isActive={(match: any, location: any) => true}
end
>
About
</NavLink>;
});

it('supports enhanced className & style props', () => {
<NavLink
to="/about"
className={({ isActive, isPending}) =>
isPending ? "pending" : isActive ? "active" : undefined
}
style={({ isActive, isPending}) =>
isPending ? { color: "red" } : isActive ? { color: "blue" } : undefined
}
>
About
</NavLink>;

// $FlowExpectedError[incompatible-type]
<NavLink to="/about" className={{'invalid': ''}} />;
// $FlowExpectedError[incompatible-type]
<NavLink to="/about" style={3} />;
});

it('supports render prop as children', () => {
<NavLink to="/about">
{({ isActive, isPending }) => (
<span className={isActive ? "active" : ""}>Tasks</span>
)}
</NavLink>
});
});

Expand Down Expand Up @@ -197,6 +237,58 @@ describe("react-router-dom", () => {
});
});

describe('renderMatches', () => {
it('works', () => {
renderMatches([]);

renderMatches<RouteObject>([]);

const contentWithEmptyMatches: null|React$Element<any> = renderMatches([]);

const contentWithMatches: null|React$Element<any> = renderMatches([{
params: {},
pathname: '/',
pathnameBase: '',
route: {
index: false,
children: [{
index: true,
}],
},
}]);
});

it('raises', () => {
// $FlowExpectedError[incompatible-call]
renderMatches(5);

// $FlowExpectedError[incompatible-type]
const contentWithEmptyMatches: number = renderMatches([]);
});
});

describe('matchRoutes', () => {
it('works', () => {
matchRoutes([], '/');

matchRoutes<RouteObject>([], '/');

const contentWithEmptyMatches: Array<AgnosticRouteMatch<string, RouteObject>> | null = matchRoutes([], '/');

const contentWithMatches: Array<AgnosticRouteMatch<string, RouteObject>> | null = matchRoutes([{
id: 'bar',
path: 'bar',
index: false,
children: [],
}], '/');
});

it('raises an error with invalid arguments', () => {
// $FlowExpectedError[incompatible-call]
matchRoutes(5, '/');
});
});

describe("withRouter", () => {
type Props = {
history: RouterHistory,
Expand Down Expand Up @@ -496,12 +588,34 @@ describe("react-router-dom", () => {
});

it('useOutlet', () => {
useOutlet()

const Component = () => <div>Hi!</div>;

useOutlet<typeof Component>()

// $FlowExpectedError[extra-arg]
useOutlet('')
})

it('useOutletContext', () => {
const [count, setCount] = useOutletContext();
const increment = () => setCount((c) => c + 1);
<button onClick={increment}>{count}</button>;

// $FlowExpectedError[extra-arg]
useOutletContext('');

const t1: number = useOutletContext<number>();

type Tuple = [string, (foo: string) => void];

const [foo, setFoo] = useOutletContext<Tuple>();
(foo: string);
(setFoo: (foo: string) => void);

// $FlowExpectedError[incompatible-type]
const t2: string = useOutletContext<Tuple>();
});

it('useParams', () => {
Expand Down

0 comments on commit 65d9a42

Please sign in to comment.