mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Make element inspector dev support modules Flow strict-local (#57741)
Summary: Pull Request resolved: https://github.com/react/react-native/pull/57741 Upgrade the element inspector dev support modules from `flow` to `flow strict-local`, replacing loose `Object`/`Function` types with the accurate types already used by their callers and dependencies. Changelog: [Internal] Reviewed By: javache Differential Revision: D113763787 fbshipit-source-id: 114d12cb02e244431d59b18e3b3c6cfa363bd25f
This commit is contained in:
1 parent
5211157ab7
commit
a9dc32ce58
8 files changed
+88
-45
No files matched your search
+4
-4
@@ -61,10 +61,10 @@ type BoxContainerProps = Readonly<{
|
||||
title: string,
|
||||
titleStyle?: TextStyleProp,
|
||||
box: Readonly<{
|
||||
top: number,
|
||||
left: number,
|
||||
right: number,
|
||||
bottom: number,
|
||||
top: number | string,
|
||||
left: number | string,
|
||||
right: number | string,
|
||||
bottom: number | string,
|
||||
}>,
|
||||
children: React.Node,
|
||||
}>;
|
||||
|
||||
+19
-11
@@ -16,8 +16,6 @@ import type {InspectedElementFrame} from './Inspector';
|
||||
import * as React from 'react';
|
||||
|
||||
const View = require('../../../../../Libraries/Components/View/View').default;
|
||||
const flattenStyle =
|
||||
require('../../../../../Libraries/StyleSheet/flattenStyle').default;
|
||||
const StyleSheet =
|
||||
require('../../../../../Libraries/StyleSheet/StyleSheet').default;
|
||||
const Dimensions =
|
||||
@@ -31,9 +29,10 @@ type Props = Readonly<{
|
||||
}>;
|
||||
|
||||
function ElementBox({frame, style}: Props): React.Node {
|
||||
const flattenedStyle = flattenStyle(style) || {};
|
||||
let margin: ?Readonly<Style> = resolveBoxStyle('margin', flattenedStyle);
|
||||
let padding: ?Readonly<Style> = resolveBoxStyle('padding', flattenedStyle);
|
||||
const marginBox = resolveBoxStyle('margin', style);
|
||||
const paddingBox = resolveBoxStyle('padding', style);
|
||||
let margin: ?ResolvedStyle = null;
|
||||
let padding: ?ResolvedStyle = null;
|
||||
|
||||
const frameStyle = {...frame};
|
||||
const contentStyle: {width: number, height: number} = {
|
||||
@@ -41,8 +40,8 @@ function ElementBox({frame, style}: Props): React.Node {
|
||||
height: frame.height,
|
||||
};
|
||||
|
||||
if (margin != null) {
|
||||
margin = resolveRelativeSizes(margin);
|
||||
if (marginBox != null) {
|
||||
margin = resolveRelativeSizes(marginBox);
|
||||
|
||||
frameStyle.top -= margin.top;
|
||||
frameStyle.left -= margin.left;
|
||||
@@ -63,8 +62,8 @@ function ElementBox({frame, style}: Props): React.Node {
|
||||
}
|
||||
}
|
||||
|
||||
if (padding != null) {
|
||||
padding = resolveRelativeSizes(padding);
|
||||
if (paddingBox != null) {
|
||||
padding = resolveRelativeSizes(paddingBox);
|
||||
|
||||
contentStyle.width -= padding.left + padding.right;
|
||||
contentStyle.height -= padding.top + padding.bottom;
|
||||
@@ -97,6 +96,14 @@ const styles = StyleSheet.create({
|
||||
});
|
||||
|
||||
type Style = {
|
||||
top: number | string,
|
||||
right: number | string,
|
||||
bottom: number | string,
|
||||
left: number | string,
|
||||
...
|
||||
};
|
||||
|
||||
type ResolvedStyle = {
|
||||
top: number,
|
||||
right: number,
|
||||
bottom: number,
|
||||
@@ -110,12 +117,13 @@ type Style = {
|
||||
* @param style the style to resolve
|
||||
* @return a modified copy
|
||||
*/
|
||||
function resolveRelativeSizes(style: Readonly<Style>): Style {
|
||||
let resolvedStyle = {...style};
|
||||
function resolveRelativeSizes(style: Readonly<Style>): ResolvedStyle {
|
||||
const resolvedStyle = {...style};
|
||||
resolveSizeInPlace(resolvedStyle, 'top', 'height');
|
||||
resolveSizeInPlace(resolvedStyle, 'right', 'width');
|
||||
resolveSizeInPlace(resolvedStyle, 'bottom', 'height');
|
||||
resolveSizeInPlace(resolvedStyle, 'left', 'width');
|
||||
// $FlowFixMe[incompatible-type] resolveSizeInPlace has converted every percentage/`auto` value to a number
|
||||
return resolvedStyle;
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -4,7 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
import type {InspectorData} from '../../../../../Libraries/Renderer/shims/ReactNativeTypes';
|
||||
import type {ViewStyleProp} from '../../../../../Libraries/StyleSheet/StyleSheet';
|
||||
import type {InspectedElementFrame} from './Inspector';
|
||||
|
||||
import * as React from 'react';
|
||||
|
||||
@@ -33,7 +34,7 @@ const StyleInspector = require('./StyleInspector').default;
|
||||
type Props = Readonly<{
|
||||
hierarchy: ?InspectorData['hierarchy'],
|
||||
style?: ?ViewStyleProp,
|
||||
frame?: ?Object,
|
||||
frame?: ?InspectedElementFrame,
|
||||
selection?: ?number,
|
||||
setSelection?: number => unknown,
|
||||
}>;
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
|
||||
Vendored
+1
-1
@@ -4,7 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
|
||||
+18
-6
@@ -4,7 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
@@ -19,25 +19,37 @@ export type ReactRenderer = {
|
||||
inspectedView: ?HostInstance,
|
||||
locationX: number,
|
||||
locationY: number,
|
||||
callback: Function,
|
||||
callback: (viewData: TouchedViewDataAtPoint) => void,
|
||||
) => void,
|
||||
...
|
||||
},
|
||||
};
|
||||
type AttachedRendererEventPayload = {id: number, renderer: ReactRenderer};
|
||||
|
||||
const reactDevToolsHook = (window as any).__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
||||
type ReactDevToolsHook = {
|
||||
readonly on: (
|
||||
eventName: string,
|
||||
handler: (payload: AttachedRendererEventPayload) => void,
|
||||
) => void,
|
||||
readonly renderers: ReadonlyMap<unknown, ReactRenderer>,
|
||||
...
|
||||
};
|
||||
|
||||
const reactDevToolsHook: ?ReactDevToolsHook = (
|
||||
window as {readonly __REACT_DEVTOOLS_GLOBAL_HOOK__?: ReactDevToolsHook, ...}
|
||||
).__REACT_DEVTOOLS_GLOBAL_HOOK__;
|
||||
invariant(
|
||||
Boolean(reactDevToolsHook),
|
||||
reactDevToolsHook != null,
|
||||
'getInspectorDataForViewAtPoint should not be used if React DevTools hook is not injected',
|
||||
);
|
||||
|
||||
const renderers: Array<ReactRenderer> = Array.from(
|
||||
(window as any).__REACT_DEVTOOLS_GLOBAL_HOOK__.renderers.values(),
|
||||
reactDevToolsHook.renderers.values(),
|
||||
);
|
||||
|
||||
const appendRenderer = ({renderer}: AttachedRendererEventPayload) =>
|
||||
const appendRenderer = ({renderer}: AttachedRendererEventPayload) => {
|
||||
renderers.push(renderer);
|
||||
};
|
||||
reactDevToolsHook.on('renderer', appendRenderer);
|
||||
|
||||
function validateRenderers(): void {
|
||||
|
||||
+41
-19
@@ -4,14 +4,18 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @flow
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import type {ViewStyleProp} from '../../../../../Libraries/StyleSheet/StyleSheet';
|
||||
|
||||
const I18nManager =
|
||||
require('../../../../../Libraries/ReactNative/I18nManager').default;
|
||||
const flattenStyle =
|
||||
require('../../../../../Libraries/StyleSheet/flattenStyle').default;
|
||||
|
||||
/**
|
||||
* Resolve a style property into its component parts.
|
||||
@@ -25,15 +29,32 @@ const I18nManager =
|
||||
*/
|
||||
function resolveBoxStyle(
|
||||
prefix: string,
|
||||
style: Object,
|
||||
style: ?ViewStyleProp,
|
||||
): ?Readonly<{
|
||||
bottom: number,
|
||||
left: number,
|
||||
right: number,
|
||||
top: number,
|
||||
bottom: number | string,
|
||||
left: number | string,
|
||||
right: number | string,
|
||||
top: number | string,
|
||||
}> {
|
||||
const flatStyle: ?Readonly<{[string]: unknown}> = flattenStyle(style);
|
||||
if (flatStyle == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const getStyleValue = (key: string): number | string | null => {
|
||||
const value = flatStyle[key];
|
||||
return typeof value === 'number' || typeof value === 'string'
|
||||
? value
|
||||
: null;
|
||||
};
|
||||
|
||||
let hasParts = false;
|
||||
const result = {
|
||||
const result: {
|
||||
bottom: number | string,
|
||||
left: number | string,
|
||||
right: number | string,
|
||||
top: number | string,
|
||||
} = {
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
@@ -42,33 +63,34 @@ function resolveBoxStyle(
|
||||
|
||||
// TODO: Fix issues with multiple properties affecting the same side.
|
||||
|
||||
const styleForAll = style[prefix];
|
||||
const styleForAll = getStyleValue(prefix);
|
||||
if (styleForAll != null) {
|
||||
for (const key of Object.keys(result)) {
|
||||
result[key] = styleForAll;
|
||||
}
|
||||
result.bottom = styleForAll;
|
||||
result.left = styleForAll;
|
||||
result.right = styleForAll;
|
||||
result.top = styleForAll;
|
||||
hasParts = true;
|
||||
}
|
||||
|
||||
const styleForHorizontal = style[prefix + 'Horizontal'];
|
||||
const styleForHorizontal = getStyleValue(prefix + 'Horizontal');
|
||||
if (styleForHorizontal != null) {
|
||||
result.left = styleForHorizontal;
|
||||
result.right = styleForHorizontal;
|
||||
hasParts = true;
|
||||
} else {
|
||||
const styleForLeft = style[prefix + 'Left'];
|
||||
const styleForLeft = getStyleValue(prefix + 'Left');
|
||||
if (styleForLeft != null) {
|
||||
result.left = styleForLeft;
|
||||
hasParts = true;
|
||||
}
|
||||
|
||||
const styleForRight = style[prefix + 'Right'];
|
||||
const styleForRight = getStyleValue(prefix + 'Right');
|
||||
if (styleForRight != null) {
|
||||
result.right = styleForRight;
|
||||
hasParts = true;
|
||||
}
|
||||
|
||||
const styleForEnd = style[prefix + 'End'];
|
||||
const styleForEnd = getStyleValue(prefix + 'End');
|
||||
if (styleForEnd != null) {
|
||||
const constants = I18nManager.getConstants();
|
||||
if (constants.isRTL && constants.doLeftAndRightSwapInRTL) {
|
||||
@@ -78,7 +100,7 @@ function resolveBoxStyle(
|
||||
}
|
||||
hasParts = true;
|
||||
}
|
||||
const styleForStart = style[prefix + 'Start'];
|
||||
const styleForStart = getStyleValue(prefix + 'Start');
|
||||
if (styleForStart != null) {
|
||||
const constants = I18nManager.getConstants();
|
||||
if (constants.isRTL && constants.doLeftAndRightSwapInRTL) {
|
||||
@@ -90,19 +112,19 @@ function resolveBoxStyle(
|
||||
}
|
||||
}
|
||||
|
||||
const styleForVertical = style[prefix + 'Vertical'];
|
||||
const styleForVertical = getStyleValue(prefix + 'Vertical');
|
||||
if (styleForVertical != null) {
|
||||
result.bottom = styleForVertical;
|
||||
result.top = styleForVertical;
|
||||
hasParts = true;
|
||||
} else {
|
||||
const styleForBottom = style[prefix + 'Bottom'];
|
||||
const styleForBottom = getStyleValue(prefix + 'Bottom');
|
||||
if (styleForBottom != null) {
|
||||
result.bottom = styleForBottom;
|
||||
hasParts = true;
|
||||
}
|
||||
|
||||
const styleForTop = style[prefix + 'Top'];
|
||||
const styleForTop = getStyleValue(prefix + 'Top');
|
||||
if (styleForTop != null) {
|
||||
result.top = styleForTop;
|
||||
hasParts = true;
|
||||
|
||||
Reference in new issue
Block a user