From 734c1505f4e3c845320f96e23ce717ae44c2112d Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Mon, 9 Sep 2024 20:34:57 -0700 Subject: [PATCH] Animated: Improve Types in `Animated{Props,Style}` (#46383) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46383 Minor refactors to the internal types of `AnimatedProps` and `AnimatedStyle` to avoid using `Object` and `any`. This also migrates private properties prefixed with `_` to instead use `#` (ECMAScript private properties). This has the desired benefit of minimizing thrash in the `public-api-test.js` snapshot, too. There are no behavior changes associated with this commit. Changelog: [Internal] Reviewed By: javache Differential Revision: D62351005 fbshipit-source-id: bb69d4db50aef5e0b3193971dac581686e359a95 --- .../Libraries/Animated/nodes/AnimatedProps.js | 47 +++++++++---------- .../Libraries/Animated/nodes/AnimatedStyle.js | 27 +++++------ .../__snapshots__/public-api-test.js.snap | 9 +--- 3 files changed, 36 insertions(+), 47 deletions(-) diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js b/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js index 90d240dd2ac..64e23273a6e 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedProps.js @@ -8,8 +8,6 @@ * @format */ -'use strict'; - import type {PlatformConfig} from '../AnimatedPlatformConfig'; import {findNodeHandle} from '../../ReactNative/RendererProxy'; @@ -20,12 +18,12 @@ import AnimatedObject from './AnimatedObject'; import AnimatedStyle from './AnimatedStyle'; import invariant from 'invariant'; -function createAnimatedProps( - inputProps: Object, -): [$ReadOnlyArray, $ReadOnlyArray, Object] { +function createAnimatedProps(inputProps: { + [string]: mixed, +}): [$ReadOnlyArray, $ReadOnlyArray, {[string]: mixed}] { const nodeKeys: Array = []; const nodes: Array = []; - const props: Object = {}; + const props: {[string]: mixed} = {}; const keys = Object.keys(inputProps); for (let ii = 0, length = keys.length; ii < length; ii++) { @@ -62,29 +60,28 @@ function createAnimatedProps( } export default class AnimatedProps extends AnimatedNode { + #animatedView: any = null; + #callback: () => void; #nodeKeys: $ReadOnlyArray; #nodes: $ReadOnlyArray; + #props: {[string]: mixed}; - _animatedView: any = null; - _props: Object; - _callback: () => void; - - constructor(inputProps: Object, callback: () => void) { + constructor(inputProps: {[string]: mixed}, callback: () => void) { super(); const [nodeKeys, nodes, props] = createAnimatedProps(inputProps); this.#nodeKeys = nodeKeys; this.#nodes = nodes; - this._props = props; - this._callback = callback; + this.#props = props; + this.#callback = callback; } __getValue(): Object { - const props: {[string]: any | ((...args: any) => void)} = {}; + const props: {[string]: mixed} = {}; - const keys = Object.keys(this._props); + const keys = Object.keys(this.#props); for (let ii = 0, length = keys.length; ii < length; ii++) { const key = keys[ii]; - const value = this._props[key]; + const value = this.#props[key]; if (value instanceof AnimatedNode) { props[key] = value.__getValue(); @@ -99,7 +96,7 @@ export default class AnimatedProps extends AnimatedNode { } __getAnimatedValue(): Object { - const props: {[string]: any} = {}; + const props: {[string]: mixed} = {}; const nodeKeys = this.#nodeKeys; const nodes = this.#nodes; @@ -121,10 +118,10 @@ export default class AnimatedProps extends AnimatedNode { } __detach(): void { - if (this.__isNative && this._animatedView) { + if (this.__isNative && this.#animatedView) { this.__disconnectAnimatedView(); } - this._animatedView = null; + this.#animatedView = null; const nodes = this.#nodes; for (let ii = 0, length = nodes.length; ii < length; ii++) { @@ -136,7 +133,7 @@ export default class AnimatedProps extends AnimatedNode { } update(): void { - this._callback(); + this.#callback(); } __makeNative(platformConfig: ?PlatformConfig): void { @@ -154,17 +151,17 @@ export default class AnimatedProps extends AnimatedNode { // where it will be needed to traverse the graph of attached values. super.__setPlatformConfig(platformConfig); - if (this._animatedView) { + if (this.#animatedView) { this.__connectAnimatedView(); } } } setNativeView(animatedView: any): void { - if (this._animatedView === animatedView) { + if (this.#animatedView === animatedView) { return; } - this._animatedView = animatedView; + this.#animatedView = animatedView; if (this.__isNative) { this.__connectAnimatedView(); } @@ -172,7 +169,7 @@ export default class AnimatedProps extends AnimatedNode { __connectAnimatedView(): void { invariant(this.__isNative, 'Expected node to be marked as "native"'); - const nativeViewTag: ?number = findNodeHandle(this._animatedView); + const nativeViewTag: ?number = findNodeHandle(this.#animatedView); invariant( nativeViewTag != null, 'Unable to locate attached view in the native tree', @@ -185,7 +182,7 @@ export default class AnimatedProps extends AnimatedNode { __disconnectAnimatedView(): void { invariant(this.__isNative, 'Expected node to be marked as "native"'); - const nativeViewTag: ?number = findNodeHandle(this._animatedView); + const nativeViewTag: ?number = findNodeHandle(this.#animatedView); invariant( nativeViewTag != null, 'Unable to locate attached view in the native tree', diff --git a/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js b/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js index 455884bfaaf..9faede47bed 100644 --- a/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js +++ b/packages/react-native/Libraries/Animated/nodes/AnimatedStyle.js @@ -8,8 +8,6 @@ * @format */ -'use strict'; - import type {PlatformConfig} from '../AnimatedPlatformConfig'; import {validateStyles} from '../../../src/private/animated/NativeAnimatedValidation'; @@ -24,10 +22,10 @@ import AnimatedWithChildren from './AnimatedWithChildren'; function createAnimatedStyle( inputStyle: {[string]: mixed}, keepUnanimatedValues: boolean, -): [$ReadOnlyArray, $ReadOnlyArray, Object] { +): [$ReadOnlyArray, $ReadOnlyArray, {[string]: mixed}] { const nodeKeys: Array = []; const nodes: Array = []; - const style: {[string]: any} = {}; + const style: {[string]: mixed} = {}; const keys = Object.keys(inputStyle); for (let ii = 0, length = keys.length; ii < length; ii++) { @@ -71,11 +69,10 @@ function createAnimatedStyle( } export default class AnimatedStyle extends AnimatedWithChildren { + #inputStyle: any; #nodeKeys: $ReadOnlyArray; #nodes: $ReadOnlyArray; - - _inputStyle: any; - _style: {[string]: any}; + #style: {[string]: mixed}; /** * Creates an `AnimatedStyle` if `value` contains `AnimatedNode` instances. @@ -99,23 +96,23 @@ export default class AnimatedStyle extends AnimatedWithChildren { constructor( nodeKeys: $ReadOnlyArray, nodes: $ReadOnlyArray, - style: {[string]: any}, + style: {[string]: mixed}, inputStyle: any, ) { super(); this.#nodeKeys = nodeKeys; this.#nodes = nodes; - this._style = style; - this._inputStyle = inputStyle; + this.#style = style; + this.#inputStyle = inputStyle; } __getValue(): Object | Array { - const style: {[string]: any} = {}; + const style: {[string]: mixed} = {}; - const keys = Object.keys(this._style); + const keys = Object.keys(this.#style); for (let ii = 0, length = keys.length; ii < length; ii++) { const key = keys[ii]; - const value = this._style[key]; + const value = this.#style[key]; if (value instanceof AnimatedNode) { style[key] = value.__getValue(); @@ -124,11 +121,11 @@ export default class AnimatedStyle extends AnimatedWithChildren { } } - return Platform.OS === 'web' ? [this._inputStyle, style] : style; + return Platform.OS === 'web' ? [this.#inputStyle, style] : style; } __getAnimatedValue(): Object { - const style: {[string]: any} = {}; + const style: {[string]: mixed} = {}; const nodeKeys = this.#nodeKeys; const nodes = this.#nodes; diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index d1560f53c05..d1dede2de91 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -946,10 +946,7 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedProps.js 1`] = ` "declare export default class AnimatedProps extends AnimatedNode { - _animatedView: any; - _props: Object; - _callback: () => void; - constructor(inputProps: Object, callback: () => void): void; + constructor(inputProps: { [string]: mixed }, callback: () => void): void; __getValue(): Object; __getAnimatedValue(): Object; __attach(): void; @@ -967,13 +964,11 @@ exports[`public API should not change unintentionally Libraries/Animated/nodes/A exports[`public API should not change unintentionally Libraries/Animated/nodes/AnimatedStyle.js 1`] = ` "declare export default class AnimatedStyle extends AnimatedWithChildren { - _inputStyle: any; - _style: { [string]: any }; static from(inputStyle: any): ?AnimatedStyle; constructor( nodeKeys: $ReadOnlyArray, nodes: $ReadOnlyArray, - style: { [string]: any }, + style: { [string]: mixed }, inputStyle: any ): void; __getValue(): Object | Array;