Skip to content

Commit 5cec1ea

Browse files
rubennortefacebook-github-bot
authored andcommitted
Make most modules in Image flow strict-local (facebook#40728)
Summary: Pull Request resolved: facebook#40728 Just improving type safety of a bunch of modules in the `Image` directory. Changelog: [internal] Reviewed By: NickGerleman Differential Revision: D50080136 fbshipit-source-id: cbfb89aa01cad3882aa08a8ba637e561017d5db6
1 parent 62714b0 commit 5cec1ea

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

packages/react-native/Libraries/Image/AssetSourceResolver.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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

@@ -68,7 +68,7 @@ class AssetSourceResolver {
6868
}
6969

7070
isLoadedFromFileSystem(): boolean {
71-
return !!(this.jsbundleUrl && this.jsbundleUrl.startsWith('file://'));
71+
return this.jsbundleUrl != null && this.jsbundleUrl?.startsWith('file://');
7272
}
7373

7474
defaultAsset(): ResolvedAssetSource {
@@ -90,7 +90,7 @@ class AssetSourceResolver {
9090
* from the devserver
9191
*/
9292
assetServerURL(): ResolvedAssetSource {
93-
invariant(!!this.serverUrl, 'need server to load from');
93+
invariant(this.serverUrl != null, 'need server to load from');
9494
return this.fromSource(
9595
this.serverUrl +
9696
getScaledAssetPath(this.asset) +
@@ -114,7 +114,7 @@ class AssetSourceResolver {
114114
* E.g. 'file:///sdcard/bundle/assets/AwesomeModule/icon@2x.png'
115115
*/
116116
scaledAssetURLNearBundle(): ResolvedAssetSource {
117-
const path = this.jsbundleUrl || 'file://';
117+
const path = this.jsbundleUrl ?? 'file://';
118118
return this.fromSource(
119119
// Assets can have relative paths outside of the project root.
120120
// When bundling them we replace `../` with `_` to make sure they
@@ -143,7 +143,7 @@ class AssetSourceResolver {
143143
* E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png'
144144
*/
145145
drawableFolderInBundle(): ResolvedAssetSource {
146-
const path = this.jsbundleUrl || 'file://';
146+
const path = this.jsbundleUrl ?? 'file://';
147147
return this.fromSource(path + getAssetPathInDrawableFolder(this.asset));
148148
}
149149

packages/react-native/Libraries/Image/AssetUtils.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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

@@ -14,12 +14,11 @@ let cacheBreaker;
1414
let warnIfCacheBreakerUnset = true;
1515

1616
export function pickScale(scales: Array<number>, deviceScale?: number): number {
17-
if (deviceScale == null) {
18-
deviceScale = PixelRatio.get();
19-
}
17+
const requiredDeviceScale = deviceScale ?? PixelRatio.get();
18+
2019
// Packager guarantees that `scales` array is sorted
2120
for (let i = 0; i < scales.length; i++) {
22-
if (scales[i] >= deviceScale) {
21+
if (scales[i] >= requiredDeviceScale) {
2322
return scales[i];
2423
}
2524
}

packages/react-native/Libraries/Image/ImageBackground.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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

@@ -45,7 +45,7 @@ import * as React from 'react';
4545
* ```
4646
*/
4747
class ImageBackground extends React.Component<ImageBackgroundProps> {
48-
setNativeProps(props: Object) {
48+
setNativeProps(props: {...}) {
4949
// Work-around flow
5050
const viewRef = this._viewRef;
5151
if (viewRef) {

packages/react-native/Libraries/Image/ImageUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
88
* @format
99
*/
1010

packages/react-native/Libraries/Image/resolveAssetSource.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @format
8-
* @flow
8+
* @flow strict-local
99
*/
1010

1111
// Resolves an asset into a `source` for `Image`.
1212

1313
'use strict';
1414

1515
import type {ResolvedAssetSource} from './AssetSourceResolver';
16+
import type {ImageSource} from './ImageSource';
1617

1718
const AssetSourceResolver = require('./AssetSourceResolver');
1819
const {pickScale} = require('./AssetUtils');
@@ -22,7 +23,7 @@ let _customSourceTransformer, _serverURL, _scriptURL;
2223

2324
let _sourceCodeScriptURL: ?string;
2425
function getSourceCodeScriptURL(): ?string {
25-
if (_sourceCodeScriptURL) {
26+
if (_sourceCodeScriptURL != null) {
2627
return _sourceCodeScriptURL;
2728
}
2829

@@ -38,8 +39,7 @@ function getSourceCodeScriptURL(): ?string {
3839
function getDevServerURL(): ?string {
3940
if (_serverURL === undefined) {
4041
const sourceCodeScriptURL = getSourceCodeScriptURL();
41-
const match =
42-
sourceCodeScriptURL && sourceCodeScriptURL.match(/^https?:\/\/.*?\//);
42+
const match = sourceCodeScriptURL?.match(/^https?:\/\/.*?\//);
4343
if (match) {
4444
// jsBundle was loaded from network
4545
_serverURL = match[0];
@@ -52,19 +52,25 @@ function getDevServerURL(): ?string {
5252
}
5353

5454
function _coerceLocalScriptURL(scriptURL: ?string): ?string {
55-
if (scriptURL) {
56-
if (scriptURL.startsWith('assets://')) {
55+
let normalizedScriptURL = scriptURL;
56+
57+
if (normalizedScriptURL != null) {
58+
if (normalizedScriptURL.startsWith('assets://')) {
5759
// android: running from within assets, no offline path to use
5860
return null;
5961
}
60-
scriptURL = scriptURL.substring(0, scriptURL.lastIndexOf('/') + 1);
61-
if (!scriptURL.includes('://')) {
62+
normalizedScriptURL = normalizedScriptURL.substring(
63+
0,
64+
normalizedScriptURL.lastIndexOf('/') + 1,
65+
);
66+
if (!normalizedScriptURL.includes('://')) {
6267
// Add file protocol in case we have an absolute file path and not a URL.
6368
// This shouldn't really be necessary. scriptURL should be a URL.
64-
scriptURL = 'file://' + scriptURL;
69+
normalizedScriptURL = 'file://' + normalizedScriptURL;
6570
}
6671
}
67-
return scriptURL;
72+
73+
return normalizedScriptURL;
6874
}
6975

7076
function getScriptURL(): ?string {
@@ -84,8 +90,10 @@ function setCustomSourceTransformer(
8490
* `source` is either a number (opaque type returned by require('./foo.png'))
8591
* or an `ImageSource` like { uri: '<http location || file path>' }
8692
*/
87-
function resolveAssetSource(source: any): ?ResolvedAssetSource {
88-
if (typeof source === 'object') {
93+
function resolveAssetSource(source: ?ImageSource): ?ResolvedAssetSource {
94+
if (source == null || typeof source === 'object') {
95+
// $FlowFixMe[incompatible-exact] `source` doesn't exactly match `ResolvedAssetSource`
96+
// $FlowFixMe[incompatible-return] `source` doesn't exactly match `ResolvedAssetSource`
8997
return source;
9098
}
9199

0 commit comments

Comments
 (0)