Files
react-native/RNTester/js/examples/Transform/TransformExample.js
T
Eli White 0a68763743 Add explicit useNativeDriver: false to callsites
Summary:
In order to cleanup the callsites that are not using Animated's native driver, we are going to make useNativeDriver a required option so people have to think about whether they want the native driver or not.

I made this change by changing [Animated.js](https://fburl.com/ritcebri) to have this animation config type:

```
export type AnimationConfig = {
  isInteraction?: boolean,
  useNativeDriver: true,
  onComplete?: ?EndCallback,
  iterations?: number,
};
```

This causes Flow to error anywhere where useNativeDriver isn't set or where it is set to false.

I then used these Flow errors to codemod the callsites.

I got the location of the Flow errors by running:
```
flow status --strip-root --json --message-width=0 | jq '.errors | [.[].extra | .[].message | .[].loc | objects | {source: .source, start: .start, end: .end}]'
```

And then ran this codemod:
```
const json = JSON.parse('JSON RESULT FROM FLOW');

const fileLookup = new Map();

json.forEach(item => {
  if (!fileLookup.has(item.source)) {
    fileLookup.set(item.source, []);
  }

  fileLookup.get(item.source).push(item);
});

export default function transformer(file, api) {
  const j = api.jscodeshift;

  const filePath = file.path;
  if (!fileLookup.has(filePath)) {
    return;
  }

  const locationInfo = fileLookup.get(filePath);

  return j(file.source)
    .find(j.ObjectExpression)
    .forEach(path => {
      if (
        path.node.properties.some(
          property =>
            property != null &&
            property.key != null &&
            property.key.name === 'useNativeDriver',
        )
      ) {
        return;
      }

      const hasErrorOnLine = locationInfo.some(
        singleLocationInfo =>
          singleLocationInfo.start.line === path.node.loc.start.line &&
          Math.abs(
            singleLocationInfo.start.column - path.node.loc.start.column,
          ) <= 2,
      );
      if (!hasErrorOnLine) {
        return;
      }

      path.node.properties.push(
        j.property(
          'init',
          j.identifier('useNativeDriver'),
          j.booleanLiteral(false),
        ),
      );
    })
    .toSource();
}

export const parser = 'flow';
```

```
yarn jscodeshift --parser=flow --transform addUseNativeDriver.js RKJSModules react-native-github
```

Followed up with

```
hg status -n --change . | xargs js1 prettier
```

Reviewed By: mdvacca

Differential Revision: D16611291

fbshipit-source-id: 1157587416ec7603d1a59e1fad6a821f1f57b952
2019-08-01 16:46:30 -07:00

283 lines
6.0 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const React = require('react');
const {Animated, StyleSheet, Text, View} = require('react-native');
class Flip extends React.Component<{}, $FlowFixMeState> {
state = {
theta: new Animated.Value(45),
};
componentDidMount() {
this._animate();
}
_animate = () => {
this.state.theta.setValue(0);
Animated.timing(this.state.theta, {
toValue: 360,
duration: 5000,
useNativeDriver: false,
}).start(this._animate);
};
render() {
return (
<View style={styles.flipCardContainer}>
<Animated.View
style={[
styles.flipCard,
{
transform: [
{perspective: 850},
{
rotateX: this.state.theta.interpolate({
inputRange: [0, 180],
outputRange: ['0deg', '180deg'],
}),
},
],
},
]}>
<Text style={styles.flipText}>This text is flipping great.</Text>
</Animated.View>
<Animated.View
style={[
styles.flipCard,
styles.flipCard1,
{
transform: [
{perspective: 850},
{
rotateX: this.state.theta.interpolate({
inputRange: [0, 180],
outputRange: ['180deg', '360deg'],
}),
},
],
},
]}>
<Text style={styles.flipText}>On the flip side...</Text>
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
height: 500,
},
box1: {
left: 0,
backgroundColor: 'green',
height: 50,
position: 'absolute',
top: 0,
transform: [
{translateX: 100},
{translateY: 50},
{rotate: '30deg'},
{scaleX: 2},
{scaleY: 2},
],
width: 50,
},
box2: {
left: 0,
backgroundColor: 'purple',
height: 50,
position: 'absolute',
top: 0,
transform: [
{scaleX: 2},
{scaleY: 2},
{translateX: 100},
{translateY: 50},
{rotate: '30deg'},
],
width: 50,
},
box3step1: {
left: 0,
backgroundColor: 'lightpink',
height: 50,
position: 'absolute',
top: 0,
transform: [{rotate: '30deg'}],
width: 50,
},
box3step2: {
left: 0,
backgroundColor: 'hotpink',
height: 50,
opacity: 0.5,
position: 'absolute',
top: 0,
transform: [{rotate: '30deg'}, {scaleX: 2}, {scaleY: 2}],
width: 50,
},
box3step3: {
left: 0,
backgroundColor: 'deeppink',
height: 50,
opacity: 0.5,
position: 'absolute',
top: 0,
transform: [
{rotate: '30deg'},
{scaleX: 2},
{scaleY: 2},
{translateX: 100},
{translateY: 50},
],
width: 50,
},
box4: {
left: 0,
backgroundColor: 'darkorange',
height: 50,
position: 'absolute',
top: 0,
transform: [{translate: [200, 350]}, {scale: 2.5}, {rotate: '-0.2rad'}],
width: 100,
},
box5: {
backgroundColor: 'maroon',
height: 50,
position: 'absolute',
right: 0,
top: 0,
width: 50,
},
box5Transform: {
transform: [{translate: [-50, 35]}, {rotate: '50deg'}, {scale: 2}],
},
flipCardContainer: {
marginVertical: 40,
flex: 1,
alignSelf: 'center',
},
flipCard: {
width: 200,
height: 200,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'blue',
backfaceVisibility: 'hidden',
},
flipCard1: {
position: 'absolute',
top: 0,
backgroundColor: 'red',
},
flipText: {
width: 90,
fontSize: 20,
color: 'white',
fontWeight: 'bold',
},
});
exports.title = 'Transforms';
exports.description = 'View transforms';
exports.examples = [
{
title: 'Perspective, Rotate, Animation',
description: 'perspective: 850, rotateX: Animated.timing(0 -> 360)',
render(): React.Element<any> {
return <Flip />;
},
},
{
title: 'Translate, Rotate, Scale',
description:
"translateX: 100, translateY: 50, rotate: '30deg', scaleX: 2, scaleY: 2",
render() {
return (
<View style={styles.container}>
<View style={styles.box1} />
</View>
);
},
},
{
title: 'Scale, Translate, Rotate, ',
description:
"scaleX: 2, scaleY: 2, translateX: 100, translateY: 50, rotate: '30deg'",
render() {
return (
<View style={styles.container}>
<View style={styles.box2} />
</View>
);
},
},
{
title: 'Rotate',
description: "rotate: '30deg'",
render() {
return (
<View style={styles.container}>
<View style={styles.box3step1} />
</View>
);
},
},
{
title: 'Rotate, Scale',
description: "rotate: '30deg', scaleX: 2, scaleY: 2",
render() {
return (
<View style={styles.container}>
<View style={styles.box3step2} />
</View>
);
},
},
{
title: 'Rotate, Scale, Translate ',
description:
"rotate: '30deg', scaleX: 2, scaleY: 2, translateX: 100, translateY: 50",
render() {
return (
<View style={styles.container}>
<View style={styles.box3step3} />
</View>
);
},
},
{
title: 'Translate, Scale, Rotate',
description: "translate: [200, 350], scale: 2.5, rotate: '-0.2rad'",
render() {
return (
<View style={styles.container}>
<View style={styles.box4} />
</View>
);
},
},
{
title: 'Translate, Rotate, Scale',
description: "translate: [-50, 35], rotate: '50deg', scale: 2",
render() {
return (
<View style={styles.container}>
<View style={[styles.box5, styles.box5Transform]} />
</View>
);
},
},
];