Skip to content

Commit cfa274f

Browse files
iwoplazagabrieldonadel
authored andcommitted
(reattempt) Use implicit namespace to better align Animated module with OSS types (facebook#49685)
Summary: Pull Request resolved: facebook#49685 ## Motivation Modernising the RN codebase to allow for modern Flow tooling to process it. ## This diff Renames `Animated.js` to `AnimatedExports.js`, and introduces an intermediate file that reexports `* as Animated` as a default. This should have equivalent runtime behavior, but allows for a common interface file: `Animated.js.flow` to reinterpret the module as having single exports. TypeScript treats this as a namespace. Changelog: [Internal] Reviewed By: huntie Differential Revision: D70237239 fbshipit-source-id: f552490cb6bb721c6163272689ec9b6c68386574
1 parent 415e571 commit cfa274f

File tree

6 files changed

+173
-47
lines changed

6 files changed

+173
-47
lines changed

packages/react-native/Libraries/Animated/Animated.js

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,16 @@
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
9+
* @oncall react_native
910
*/
1011

11-
export type {CompositeAnimation, Numeric} from './AnimatedImplementation';
12+
import typeof * as AnimatedExports from './AnimatedExports';
1213

13-
import typeof AnimatedFlatList from './components/AnimatedFlatList';
14-
import typeof AnimatedImage from './components/AnimatedImage';
15-
import typeof AnimatedScrollView from './components/AnimatedScrollView';
16-
import typeof AnimatedSectionList from './components/AnimatedSectionList';
17-
import typeof AnimatedText from './components/AnimatedText';
18-
import typeof AnimatedView from './components/AnimatedView';
14+
// The AnimatedExports module is typed as multiple exports to allow
15+
// for an implicit namespace, but underneath is's a single default export.
16+
const Animated: AnimatedExports = (require('./AnimatedExports') as $FlowFixMe)
17+
.default;
1918

20-
import Platform from '../Utilities/Platform';
21-
import AnimatedImplementation from './AnimatedImplementation';
22-
import AnimatedMock from './AnimatedMock';
23-
24-
const Animated: typeof AnimatedImplementation = Platform.isDisableAnimations
25-
? AnimatedMock
26-
: AnimatedImplementation;
27-
28-
export default {
29-
get FlatList(): AnimatedFlatList {
30-
return require('./components/AnimatedFlatList').default;
31-
},
32-
get Image(): AnimatedImage {
33-
return require('./components/AnimatedImage').default;
34-
},
35-
get ScrollView(): AnimatedScrollView {
36-
return require('./components/AnimatedScrollView').default;
37-
},
38-
get SectionList(): AnimatedSectionList {
39-
return require('./components/AnimatedSectionList').default;
40-
},
41-
get Text(): AnimatedText {
42-
return require('./components/AnimatedText').default;
43-
},
44-
get View(): AnimatedView {
45-
return require('./components/AnimatedView').default;
46-
},
47-
...Animated,
48-
};
19+
export default Animated;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import * as Animated from './AnimatedExports';
13+
14+
export type {CompositeAnimation, Numeric} from './AnimatedImplementation';
15+
export default Animated;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import typeof AnimatedFlatList from './components/AnimatedFlatList';
13+
import typeof AnimatedImage from './components/AnimatedImage';
14+
import typeof AnimatedScrollView from './components/AnimatedScrollView';
15+
import typeof AnimatedSectionList from './components/AnimatedSectionList';
16+
import typeof AnimatedText from './components/AnimatedText';
17+
import typeof AnimatedView from './components/AnimatedView';
18+
19+
import Platform from '../Utilities/Platform';
20+
import AnimatedImplementation from './AnimatedImplementation';
21+
import AnimatedMock from './AnimatedMock';
22+
23+
const Animated: typeof AnimatedImplementation = Platform.isDisableAnimations
24+
? AnimatedMock
25+
: AnimatedImplementation;
26+
27+
export default {
28+
get FlatList(): AnimatedFlatList {
29+
return require('./components/AnimatedFlatList').default;
30+
},
31+
get Image(): AnimatedImage {
32+
return require('./components/AnimatedImage').default;
33+
},
34+
get ScrollView(): AnimatedScrollView {
35+
return require('./components/AnimatedScrollView').default;
36+
},
37+
get SectionList(): AnimatedSectionList {
38+
return require('./components/AnimatedSectionList').default;
39+
},
40+
get Text(): AnimatedText {
41+
return require('./components/AnimatedText').default;
42+
},
43+
get View(): AnimatedView {
44+
return require('./components/AnimatedView').default;
45+
},
46+
...Animated,
47+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import AnimatedImplementation from './AnimatedImplementation';
13+
14+
export {default as FlatList} from './components/AnimatedFlatList';
15+
export {default as Image} from './components/AnimatedImage';
16+
export {default as ScrollView} from './components/AnimatedScrollView';
17+
export {default as SectionList} from './components/AnimatedSectionList';
18+
export {default as Text} from './components/AnimatedText';
19+
export {default as View} from './components/AnimatedView';
20+
export {default as Color} from './nodes/AnimatedColor';
21+
export {AnimatedEvent as Event} from './AnimatedEvent';
22+
export {default as Interpolation} from './nodes/AnimatedInterpolation';
23+
export {default as Node} from './nodes/AnimatedNode';
24+
export {default as Value} from './nodes/AnimatedValue';
25+
export {default as ValueXY} from './nodes/AnimatedValueXY';
26+
27+
export const add = AnimatedImplementation.add;
28+
export const attachNativeEvent = AnimatedImplementation.attachNativeEvent;
29+
export const createAnimatedComponent =
30+
AnimatedImplementation.createAnimatedComponent;
31+
export const decay = AnimatedImplementation.decay;
32+
export const delay = AnimatedImplementation.delay;
33+
export const diffClamp = AnimatedImplementation.diffClamp;
34+
export const divide = AnimatedImplementation.divide;
35+
export const event = AnimatedImplementation.event;
36+
export const forkEvent = AnimatedImplementation.forkEvent;
37+
export const loop = AnimatedImplementation.loop;
38+
export const modulo = AnimatedImplementation.modulo;
39+
export const multiply = AnimatedImplementation.multiply;
40+
export const parallel = AnimatedImplementation.parallel;
41+
export const sequence = AnimatedImplementation.sequence;
42+
export const spring = AnimatedImplementation.spring;
43+
export const stagger = AnimatedImplementation.stagger;
44+
export const subtract = AnimatedImplementation.subtract;
45+
export const timing = AnimatedImplementation.timing;
46+
export const unforkEvent = AnimatedImplementation.unforkEvent;

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,14 @@ exports[`public API should not change unintentionally Libraries/Alert/RCTAlertMa
112112
`;
113113

114114
exports[`public API should not change unintentionally Libraries/Animated/Animated.js 1`] = `
115+
"declare const Animated: AnimatedExports;
116+
declare export default typeof Animated;
117+
"
118+
`;
119+
120+
exports[`public API should not change unintentionally Libraries/Animated/Animated.js.flow 1`] = `
115121
"export type { CompositeAnimation, Numeric } from \\"./AnimatedImplementation\\";
116-
declare const Animated: typeof AnimatedImplementation;
117-
declare export default {
118-
get FlatList(): AnimatedFlatList,
119-
get Image(): AnimatedImage,
120-
get ScrollView(): AnimatedScrollView,
121-
get SectionList(): AnimatedSectionList,
122-
get Text(): AnimatedText,
123-
get View(): AnimatedView,
124-
...Animated,
125-
};
122+
declare export default typeof Animated;
126123
"
127124
`;
128125

@@ -148,6 +145,55 @@ declare export class AnimatedEvent {
148145
"
149146
`;
150147

148+
exports[`public API should not change unintentionally Libraries/Animated/AnimatedExports.js 1`] = `
149+
"declare const Animated: typeof AnimatedImplementation;
150+
declare export default {
151+
get FlatList(): AnimatedFlatList,
152+
get Image(): AnimatedImage,
153+
get ScrollView(): AnimatedScrollView,
154+
get SectionList(): AnimatedSectionList,
155+
get Text(): AnimatedText,
156+
get View(): AnimatedView,
157+
...Animated,
158+
};
159+
"
160+
`;
161+
162+
exports[`public API should not change unintentionally Libraries/Animated/AnimatedExports.js.flow 1`] = `
163+
"export { default as FlatList } from \\"./components/AnimatedFlatList\\";
164+
export { default as Image } from \\"./components/AnimatedImage\\";
165+
export { default as ScrollView } from \\"./components/AnimatedScrollView\\";
166+
export { default as SectionList } from \\"./components/AnimatedSectionList\\";
167+
export { default as Text } from \\"./components/AnimatedText\\";
168+
export { default as View } from \\"./components/AnimatedView\\";
169+
export { default as Color } from \\"./nodes/AnimatedColor\\";
170+
export { AnimatedEvent as Event } from \\"./AnimatedEvent\\";
171+
export { default as Interpolation } from \\"./nodes/AnimatedInterpolation\\";
172+
export { default as Node } from \\"./nodes/AnimatedNode\\";
173+
export { default as Value } from \\"./nodes/AnimatedValue\\";
174+
export { default as ValueXY } from \\"./nodes/AnimatedValueXY\\";
175+
declare export const add: $FlowFixMe;
176+
declare export const attachNativeEvent: $FlowFixMe;
177+
declare export const createAnimatedComponent: $FlowFixMe;
178+
declare export const decay: $FlowFixMe;
179+
declare export const delay: $FlowFixMe;
180+
declare export const diffClamp: $FlowFixMe;
181+
declare export const divide: $FlowFixMe;
182+
declare export const event: $FlowFixMe;
183+
declare export const forkEvent: $FlowFixMe;
184+
declare export const loop: $FlowFixMe;
185+
declare export const modulo: $FlowFixMe;
186+
declare export const multiply: $FlowFixMe;
187+
declare export const parallel: $FlowFixMe;
188+
declare export const sequence: $FlowFixMe;
189+
declare export const spring: $FlowFixMe;
190+
declare export const stagger: $FlowFixMe;
191+
declare export const subtract: $FlowFixMe;
192+
declare export const timing: $FlowFixMe;
193+
declare export const unforkEvent: $FlowFixMe;
194+
"
195+
`;
196+
151197
exports[`public API should not change unintentionally Libraries/Animated/AnimatedImplementation.js 1`] = `
152198
"export type CompositeAnimation = {
153199
start: (callback?: ?EndCallback, isLooping?: boolean) => void,

scripts/build/build-types/buildTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const IGNORE_PATTERNS = [
2828
const ENTRY_POINTS = [
2929
'packages/react-native/Libraries/ActionSheetIOS/ActionSheetIOS.js',
3030
'packages/react-native/Libraries/Alert/Alert.js',
31+
'packages/react-native/Libraries/Animated/Animated.js',
3132
'packages/react-native/Libraries/AppState/AppState.js',
3233
'packages/react-native/Libraries/BatchedBridge/NativeModules.js',
3334
'packages/react-native/Libraries/Blob/Blob.js',

0 commit comments

Comments
 (0)