From 1d0368411ce45a6a9a364eab032ac10ef950ba00 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Fri, 16 Feb 2018 16:11:59 -0800 Subject: [PATCH] Add PropTypes to existing React components Test Plan: Run `yarn start` and note that the app runs without any console warnings, but that changing some of the PropTypes to introduce an error actually does cause a warning. Upcoming: an automatic test for this. wchargin-branch: add-proptypes --- explorer/src/FileExplorer.js | 20 ++++++++++++++++++++ explorer/src/UserExplorer.js | 26 +++++++++++++++++++++----- explorer/src/commitUtils.js | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/explorer/src/FileExplorer.js b/explorer/src/FileExplorer.js index 95d8e32..41f11cc 100644 --- a/explorer/src/FileExplorer.js +++ b/explorer/src/FileExplorer.js @@ -1,7 +1,15 @@ import React, { Component } from 'react'; +import PropTypes from 'prop-types'; import {buildTree} from './commitUtils'; +import {propTypes as commitUtilsPropTypes} from './commitUtils'; export class FileExplorer extends Component { + static propTypes = { + selectedPath: PropTypes.string, + onSelectPath: PropTypes.func.isRequired, + data: commitUtilsPropTypes.commitData.isRequired, + } + render() { // within the FileExplorer, paths start with "./", outside they don't // which is hacky and should be cleaned up @@ -31,6 +39,18 @@ export class FileExplorer extends Component { } class FileEntry extends Component { + static propTypes = { + name: PropTypes.string.isRequired, + path: PropTypes.string.isRequired, + alwaysExpand: PropTypes.bool.isRequired, + + // The type for the tree is recursive, and is annoying to specify as + // a proptype. The Flow type definition is in commitUtils.js. + tree: PropTypes.object.isRequired, + + selectedPath: PropTypes.string.isRequired, + onSelectPath: PropTypes.func.isRequired, + } constructor() { super(); diff --git a/explorer/src/UserExplorer.js b/explorer/src/UserExplorer.js index 068f3a1..7127178 100644 --- a/explorer/src/UserExplorer.js +++ b/explorer/src/UserExplorer.js @@ -1,7 +1,19 @@ import React, { Component } from 'react'; -import {userWeightForPath, commitWeight} from './commitUtils'; +import PropTypes from 'prop-types'; +import { + commitWeight, + propTypes as commitUtilsPropTypes, + userWeightForPath, +} from './commitUtils'; export class UserExplorer extends Component { + static propTypes = { + selectedPath: PropTypes.string.isRequired, + selectedUser: PropTypes.string, + onSelectUser: PropTypes.func.isRequired, + data: commitUtilsPropTypes.commitData.isRequired, + } + render() { const weights = userWeightForPath(this.props.selectedPath, this.props.data, commitWeight); const sortedUserWeightTuples = Object.entries(weights).sort((a,b) => b[1] - a[1]); @@ -16,11 +28,15 @@ export class UserExplorer extends Component { } } +/** + * Record the cred earned by the user in a given scope. + */ class UserEntry extends Component { - // Record the cred earned by the user in a given scope - // Props: - // userId, string - // weight, number + static propTypes = { + userId: PropTypes.string.isRequired, + weight: PropTypes.number.isRequired, + } + render() { return
{this.props.userId} diff --git a/explorer/src/commitUtils.js b/explorer/src/commitUtils.js index 33c3d6e..4cca4a0 100644 --- a/explorer/src/commitUtils.js +++ b/explorer/src/commitUtils.js @@ -1,5 +1,7 @@ // @flow +import PropTypes from 'prop-types'; + type CommitData = { // TODO improve variable names fileToCommits: {[filename: string]: string[]}; @@ -7,6 +9,23 @@ type CommitData = { authors: string[]; } +export const propTypes = { + commitData: PropTypes.shape({ + fileToCommits: PropTypes.objectOf( + PropTypes.arrayOf(PropTypes.string.isRequired).isRequired, + ).isRequired, + commits: PropTypes.objectOf(PropTypes.shape({ + author: PropTypes.string.isRequired, + stats: PropTypes.objectOf(PropTypes.shape({ + lines: PropTypes.number.isRequired, + insertions: PropTypes.number.isRequired, + deletions: PropTypes.number.isRequired, + }).isRequired).isRequired, + }).isRequired).isRequired, + }), +}; + + type Commit = { author: string; stats: {[filename: string]: FileStats};