Skip to content

Commit 4bf2356

Browse files
committed
Add flow types to utils
1 parent 990a13e commit 4bf2356

8 files changed

+78
-13
lines changed

flow-typed/slugify.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'slugify' {
2+
declare module.exports: any;
3+
}

src/utils/createLink.js

+34-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
@@ -12,7 +13,19 @@ import ExternalLinkSvg from 'templates/components/ExternalLinkSvg';
1213
import slugify from 'utils/slugify';
1314
import {colors, media} from 'theme';
1415

15-
const createLinkBlog = ({isActive, item, section}) => {
16+
import type {Node} from 'react';
17+
18+
type CreateLinkBaseProps = {
19+
isActive: boolean,
20+
item: Object,
21+
section: Object,
22+
};
23+
24+
const createLinkBlog = ({
25+
isActive,
26+
item,
27+
section,
28+
}: CreateLinkBaseProps): Node => {
1629
return (
1730
<Link css={[linkCss, isActive && activeLinkCss]} to={item.id}>
1831
{isActive && <span css={activeLinkBefore} />}
@@ -21,7 +34,11 @@ const createLinkBlog = ({isActive, item, section}) => {
2134
);
2235
};
2336

24-
const createLinkCommunity = ({isActive, item, section}) => {
37+
const createLinkCommunity = ({
38+
isActive,
39+
item,
40+
section,
41+
}: CreateLinkBaseProps): Node => {
2542
if (item.href) {
2643
return (
2744
<a css={[linkCss]} href={item.href} target="_blank" rel="noopener">
@@ -44,7 +61,11 @@ const createLinkCommunity = ({isActive, item, section}) => {
4461
});
4562
};
4663

47-
const createLinkDocs = ({isActive, item, section}) => {
64+
const createLinkDocs = ({
65+
isActive,
66+
item,
67+
section,
68+
}: CreateLinkBaseProps): Node => {
4869
return (
4970
<Link
5071
css={[linkCss, isActive && activeLinkCss]}
@@ -55,7 +76,16 @@ const createLinkDocs = ({isActive, item, section}) => {
5576
);
5677
};
5778

58-
const createLinkTutorial = ({isActive, item, onLinkClick, section}) => {
79+
type CreateLinkTutorialProps = {
80+
onLinkClick: Function,
81+
} & CreateLinkBaseProps;
82+
83+
const createLinkTutorial = ({
84+
isActive,
85+
item,
86+
onLinkClick,
87+
section,
88+
}: CreateLinkTutorialProps): Node => {
5989
return (
6090
<Link
6191
css={[linkCss, isActive && activeLinkCss]}

src/utils/createOgUrl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
89

910
import {urlRoot} from 'site-constants';
1011

11-
export default slug =>
12+
export default (slug: string): string | null =>
1213
slug == null ? null : `${urlRoot}/${slug.replace(/^\//, '')}`;

src/utils/findSectionForPath.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
@@ -12,7 +13,20 @@ import slugify from './slugify';
1213
* Helper method to locate the section containing the current URL/path.
1314
* This method specifically works with the nav_*.yml format.
1415
*/
15-
const findSectionForPath = (pathname, sections) => {
16+
17+
type Item = {
18+
id: string,
19+
subitems: Array<Item>,
20+
};
21+
22+
type Section = {
23+
items: Array<Item>,
24+
};
25+
26+
const findSectionForPath = (
27+
pathname: string,
28+
sections: Array<Section>,
29+
): Section | void => {
1630
let activeSection;
1731
const slugId = pathname.split('/').slice(-1)[0];
1832

src/utils/isItemActive.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
89

910
import slugify from 'utils/slugify';
1011

11-
const toAnchor = (href = '') => {
12+
const toAnchor = (href: string = ''): string => {
1213
const index = href.indexOf('#');
1314
return index >= 0 ? href.substr(index) : '';
1415
};
1516

1617
// TODO Account for redirect_from URLs somehow; they currently won't match.
1718
// This comment should not be true anymore since we're using 300 redirects
1819

19-
const isItemActive = (location, item) => {
20+
type Item = {
21+
id: string,
22+
href: string,
23+
};
24+
25+
const isItemActive = (location: Location, item: Item): boolean => {
2026
if (location.hash) {
2127
if (item.href) {
2228
return location.hash === toAnchor(item.href);

src/utils/sectionList.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
89

10+
// $FlowExpectedError
911
import navCommunity from '../../content/community/nav.yml';
12+
// $FlowExpectedError
1013
import navDocs from '../../content/docs/nav.yml';
14+
// $FlowExpectedError
1115
import navTutorial from '../../content/tutorial/nav.yml';
1216

13-
const sectionListDocs = navDocs.map(item => ({
17+
const sectionListDocs = navDocs.map((item: Object): Object => ({
1418
...item,
1519
directory: 'docs',
1620
}));
1721

18-
const sectionListCommunity = navCommunity.map(item => ({
22+
const sectionListCommunity = navCommunity.map((item: Object): Object => ({
1923
...item,
2024
directory: 'community',
2125
}));

src/utils/slugify.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
89

910
import slugify from 'slugify';
1011

11-
export default (string, directory) => {
12+
export default (string: string, directory?: string): string => {
1213
const filename = slugify(string) + '.html';
1314

1415
return directory ? `/${directory}/${filename}` : filename;

src/utils/toCommaSeparatedList.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
* Copyright (c) 2013-present, Facebook, Inc.
33
*
44
* @emails react-core
5+
* @flow
56
*/
67

78
'use strict';
89

910
import React from 'react';
1011

11-
const addString = (list, string) =>
12+
import type {Node} from 'react';
13+
14+
const addString = (list: Array<Node>, string: string) =>
1215
list.push(<span key={`${list.length}-${string}`}>{string}</span>);
1316

14-
const toCommaSeparatedList = (array, renderCallback) => {
17+
const toCommaSeparatedList = (
18+
array: Array<any>,
19+
renderCallback: Function,
20+
): Array<any> => {
1521
if (array.length <= 1) {
1622
return array.map(renderCallback);
1723
}

0 commit comments

Comments
 (0)