44 * This source code is licensed under the MIT license found in the
55 * LICENSE file in the root directory of this source tree.
66 *
7- * @flow
7+ * @flow strict-local
88 * @format
99 */
1010
1313const stringifySafe = require ( '../Utilities/stringifySafe' ) . default ;
1414const invariant = require ( 'invariant' ) ;
1515
16+ type TransformObject = { [ string ] : unknown , ...} ;
17+ type TransformArray = Array < TransformObject > ;
18+
1619/**
1720 * Generate a transform matrix based on the provided transforms, and use that
1821 * within the style object instead.
@@ -21,12 +24,11 @@ const invariant = require('invariant');
2124 * be applied in an arbitrary order, and yet have a universal, singular
2225 * interface to native code.
2326 */
24- function processTransform (
25- transform : Array < Object > | string ,
26- ) : Array < Object > | Array < number > {
27+ function processTransform ( transform : TransformArray | string ) : TransformArray {
28+ let normalizedTransform ;
2729 if ( typeof transform === 'string' ) {
2830 const regex = new RegExp ( / ( \w + ) \( ( [ ^ ) ] + ) \) / g) ;
29- const transformArray : Array < Object > = [];
31+ const transformArray : TransformArray = [ ] ;
3032 let matches ;
3133
3234 while ( ( matches = regex . exec ( transform ) ) ) {
@@ -39,14 +41,16 @@ function processTransform(
3941 transformArray . push ( { [ key ] : value } ) ;
4042 }
4143 }
42- transform = transformArray ;
44+ normalizedTransform = transformArray ;
45+ } else {
46+ normalizedTransform = transform ;
4347 }
4448
4549 if ( __DEV__ ) {
46- _validateTransforms ( transform ) ;
50+ _validateTransforms ( normalizedTransform ) ;
4751 }
4852
49- return transform ;
53+ return normalizedTransform ;
5054}
5155
5256const _getKeyAndValueFromCSSTransform : (
@@ -138,7 +142,7 @@ const _getKeyAndValueFromCSSTransform: (
138142 }
139143} ;
140144
141- function _validateTransforms ( transform : Array < Object > ) : void {
145+ function _validateTransforms ( transform : TransformArray ) : void {
142146 transform. forEach ( transformation => {
143147 const keys = Object . keys ( transformation ) ;
144148 invariant (
@@ -160,11 +164,16 @@ function _validateTransforms(transform: Array<Object>): void {
160164
161165function _validateTransform (
162166 key : string ,
163- value: any | number | string ,
164- transformation: any ,
167+ value : unknown ,
168+ transformation : TransformObject ,
165169) {
170+ const isAnimatedValue =
171+ value != null &&
172+ typeof value === 'object' &&
173+ 'getValue' in value &&
174+ typeof value . getValue === 'function' ;
166175 invariant (
167- ! value . getValue ,
176+ ! isAnimatedValue ,
168177 'You passed an Animated.Value to a normal component. ' +
169178 'You need to wrap that component in an Animated. For example, ' +
170179 'replace <View /> by <Animated.View />.' ,
@@ -181,24 +190,30 @@ function _validateTransform(
181190 }
182191 switch ( key ) {
183192 case 'matrix ':
193+ invariant (
194+ Array . isArray ( value ) ,
195+ 'Transform with key of %s must have an array as the value: %s' ,
196+ key ,
197+ stringifySafe ( transformation ) ,
198+ ) ;
184199 invariant (
185200 value . length === 9 || value . length === 16 ,
186201 'Matrix transform must have a length of 9 (2d) or 16 (3d). ' +
187202 'Provided matrix has a length of %s: %s' ,
188- /* $FlowFixMe[prop-missing] (>=0.84.0 site=react_native_fb) This
189- * comment suppresses an error found when Flow v0.84 was deployed. To
190- * see the error, delete this comment and run Flow. */
191203 value . length ,
192204 stringifySafe ( transformation ) ,
193205 ) ;
194206 break ;
195207 case 'translate ':
208+ invariant (
209+ Array . isArray ( value ) ,
210+ 'Transform with key of %s must have an array as the value: %s' ,
211+ key ,
212+ stringifySafe ( transformation ) ,
213+ ) ;
196214 invariant (
197215 value . length === 2 || value . length === 3 ,
198216 'Transform with key translate must be an array of length 2 or 3, found %s: %s' ,
199- /* $FlowFixMe[prop-missing] (>=0.84.0 site=react_native_fb) This
200- * comment suppresses an error found when Flow v0.84 was deployed. To
201- * see the error, delete this comment and run Flow. */
202217 value . length ,
203218 stringifySafe ( transformation ) ,
204219 ) ;
0 commit comments