@samitouri / QOS-React / commits / d1f6e762e4

Improve Flow coverage by reducing typecasts through `any`

There are many unnecessary typecasts through `any` which break the Flow of types across the program. It's more bulletproof to avoid lying to ourselves about types. Also fixed sketchy null check where zero ID would be skipped: ```diff - } else if (selectedElementID) { + } else if (selectedElementID !== null) { ```

Ivan Babak committed Apr 28, 2019 at 03:44 UTC d1f6e762e483b134449d458b28dedab432bb264d
7 files changed +21 -22
src/devtools/store.js
+1 -1
@@ -503,7 +503,7 @@ export default class Store extends EventEmitter {
503 while (current != null) {
504 if (current.parentID === 0) {
505 const rendererID = this._rootIDToRendererID.get(current.id);
506 - return rendererID == null ? null : ((rendererID: any): number);
506 + return rendererID == null ? null : rendererID;
507 } else {
508 current = this._idToElement.get(current.parentID);
509 }
src/devtools/views/Profiler/ChartNode.js
+2 -2
@@ -10,8 +10,8 @@ type Props = {|
10 height: number,
11 isDimmed?: boolean,
12 label: string,
13 - onClick: Function,
14 - onDoubleClick?: Function,
13 + onClick: (event: SyntheticMouseEvent<*>) => mixed,
14 + onDoubleClick?: (event: SyntheticMouseEvent<*>) => mixed,
15 placeLabelAboveNode?: boolean,
16 width: number,
17 x: number,
src/devtools/views/Profiler/CommitFlamegraphListItem.js
+1 -1
@@ -26,7 +26,7 @@ function CommitFlamegraphListItem({ data, index, style }: Props) {
26 const { maxSelfDuration, rows } = chartData;
27
28 const handleClick = useCallback(
29 - (event: MouseEvent, id: number, name: string) => {
29 + (event: SyntheticMouseEvent<*>, id: number, name: string) => {
30 event.stopPropagation();
31 selectFiber(id, name);
32 },
src/devtools/views/Profiler/FilterModal.js
+7 -6
@@ -7,7 +7,7 @@ import { useModalDismissSignal } from '../hooks';
7 import styles from './FilterModal.css';
8
9 type Props = {|
10 - dismissModal: Function,
10 + dismissModal: () => void,
11 |};
12
13 export default function FilterModal({ dismissModal }: Props) {
@@ -19,8 +19,8 @@ export default function FilterModal({ dismissModal }: Props) {
19 } = useContext(ProfilerContext);
20
21 const handleNumberChange = useCallback(
22 - ({ currentTarget }) => {
23 - const newValue = parseFloat(currentTarget.value);
22 + (event: SyntheticEvent<HTMLInputElement>) => {
23 + const newValue = parseFloat(event.currentTarget.value);
24 setMinCommitDuration(
25 Number.isNaN(newValue) || newValue <= 0 ? 0 : newValue
26 );
@@ -29,9 +29,10 @@ export default function FilterModal({ dismissModal }: Props) {
29 );
30
31 const handleEnabledChange = useCallback(
32 - ({ currentTarget }) => {
33 - setIsCommitFilterEnabled(currentTarget.checked);
34 - if (currentTarget.checked) {
32 + (event: SyntheticEvent<HTMLInputElement>) => {
33 + const checked = event.currentTarget.checked;
34 + setIsCommitFilterEnabled(checked);
35 + if (checked) {
36 if (inputRef.current !== null) {
37 inputRef.current.focus();
38 }
src/devtools/views/Profiler/ProfilerContext.js
+6 -9
@@ -114,19 +114,16 @@ function ProfilerContextController({ children }: Props) {
114 let rootHasProfilingData = false;
115 if (importedProfilingData !== null) {
116 rootHasProfilingData = true;
117 - } else if (selectedElementID) {
118 - rendererID = store.getRendererIDForElement(
119 - ((selectedElementID: any): number)
120 - );
121 - rootID = store.getRootIDForElement(((selectedElementID: any): number));
122 - rootHasProfilingData = store.profilingOperations.has(
123 - ((rootID: any): number)
124 - );
117 + } else if (selectedElementID !== null) {
118 + rendererID = store.getRendererIDForElement(selectedElementID);
119 + rootID = store.getRootIDForElement(selectedElementID);
120 + rootHasProfilingData =
121 + rootID === null ? false : store.profilingOperations.has(rootID);
122 } else if (store.roots.length > 0) {
123 // If no root is selected, assume the first root; many React apps are single root anyway.
124 rootID = store.roots[0];
125 rootHasProfilingData = store.profilingOperations.has(rootID);
129 - rendererID = store.getRendererIDForElement(((rootID: any): number));
126 + rendererID = store.getRendererIDForElement(rootID);
127 }
128
129 const startProfiling = useCallback(() => store.startProfiling(), [store]);
src/devtools/views/Profiler/utils.js
+3 -2
@@ -27,15 +27,16 @@ export const calculateSelfDuration = (
27 return 0;
28 }
29
30 - let selfDuration = ((actualDurations.get(id): any): number);
30 const node = nodes.get(id);
31 if (node == null) {
32 throw Error(`Could not find node with id "${id}" in commit tree`);
33 }
34
35 + let selfDuration = actualDurations.get(id) || 0;
36 +
37 node.children.forEach(childID => {
38 if (actualDurations.has(childID)) {
38 - selfDuration -= ((actualDurations.get(childID): any): number);
39 + selfDuration -= actualDurations.get(childID) || 0;
40 }
41 });
42
src/devtools/views/hooks.js
+1 -1
@@ -88,7 +88,7 @@ export function useLocalStorage<T>(
88
89 export function useModalDismissSignal(
90 modalRef: { current: HTMLDivElement | null },
91 - dismissCallback: Function
91 + dismissCallback: () => void
92 ): void {
93 useEffect(() => {
94 if (modalRef.current === null) {