Make most modules in Image flow strict-local (#40728)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Rubén Norte
2023-10-10 05:18:33 -07:00
committed by Facebook GitHub Bot
parent 62714b002d
commit 5cec1eaabf
5 changed files with 32 additions and 25 deletions
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @flow strict-local
* @format
*/
@@ -68,7 +68,7 @@ class AssetSourceResolver {
}
isLoadedFromFileSystem(): boolean {
return !!(this.jsbundleUrl && this.jsbundleUrl.startsWith('file://'));
return this.jsbundleUrl != null && this.jsbundleUrl?.startsWith('file://');
}
defaultAsset(): ResolvedAssetSource {
@@ -90,7 +90,7 @@ class AssetSourceResolver {
* from the devserver
*/
assetServerURL(): ResolvedAssetSource {
invariant(!!this.serverUrl, 'need server to load from');
invariant(this.serverUrl != null, 'need server to load from');
return this.fromSource(
this.serverUrl +
getScaledAssetPath(this.asset) +
@@ -114,7 +114,7 @@ class AssetSourceResolver {
* E.g. 'file:///sdcard/bundle/assets/AwesomeModule/icon@2x.png'
*/
scaledAssetURLNearBundle(): ResolvedAssetSource {
const path = this.jsbundleUrl || 'file://';
const path = this.jsbundleUrl ?? 'file://';
return this.fromSource(
// Assets can have relative paths outside of the project root.
// When bundling them we replace `../` with `_` to make sure they
@@ -143,7 +143,7 @@ class AssetSourceResolver {
* E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png'
*/
drawableFolderInBundle(): ResolvedAssetSource {
const path = this.jsbundleUrl || 'file://';
const path = this.jsbundleUrl ?? 'file://';
return this.fromSource(path + getAssetPathInDrawableFolder(this.asset));
}
+4 -5
View File
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @flow strict-local
* @format
*/
@@ -14,12 +14,11 @@ let cacheBreaker;
let warnIfCacheBreakerUnset = true;
export function pickScale(scales: Array<number>, deviceScale?: number): number {
if (deviceScale == null) {
deviceScale = PixelRatio.get();
}
const requiredDeviceScale = deviceScale ?? PixelRatio.get();
// Packager guarantees that `scales` array is sorted
for (let i = 0; i < scales.length; i++) {
if (scales[i] >= deviceScale) {
if (scales[i] >= requiredDeviceScale) {
return scales[i];
}
}
+2 -2
View File
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @flow strict-local
* @format
*/
@@ -45,7 +45,7 @@ import * as React from 'react';
* ```
*/
class ImageBackground extends React.Component<ImageBackgroundProps> {
setNativeProps(props: Object) {
setNativeProps(props: {...}) {
// Work-around flow
const viewRef = this._viewRef;
if (viewRef) {
+1 -1
View File
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @flow strict
* @format
*/
+20 -12
View File
@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @flow strict-local
*/
// Resolves an asset into a `source` for `Image`.
@@ -13,6 +13,7 @@
'use strict';
import type {ResolvedAssetSource} from './AssetSourceResolver';
import type {ImageSource} from './ImageSource';
const AssetSourceResolver = require('./AssetSourceResolver');
const {pickScale} = require('./AssetUtils');
@@ -22,7 +23,7 @@ let _customSourceTransformer, _serverURL, _scriptURL;
let _sourceCodeScriptURL: ?string;
function getSourceCodeScriptURL(): ?string {
if (_sourceCodeScriptURL) {
if (_sourceCodeScriptURL != null) {
return _sourceCodeScriptURL;
}
@@ -38,8 +39,7 @@ function getSourceCodeScriptURL(): ?string {
function getDevServerURL(): ?string {
if (_serverURL === undefined) {
const sourceCodeScriptURL = getSourceCodeScriptURL();
const match =
sourceCodeScriptURL && sourceCodeScriptURL.match(/^https?:\/\/.*?\//);
const match = sourceCodeScriptURL?.match(/^https?:\/\/.*?\//);
if (match) {
// jsBundle was loaded from network
_serverURL = match[0];
@@ -52,19 +52,25 @@ function getDevServerURL(): ?string {
}
function _coerceLocalScriptURL(scriptURL: ?string): ?string {
if (scriptURL) {
if (scriptURL.startsWith('assets://')) {
let normalizedScriptURL = scriptURL;
if (normalizedScriptURL != null) {
if (normalizedScriptURL.startsWith('assets://')) {
// android: running from within assets, no offline path to use
return null;
}
scriptURL = scriptURL.substring(0, scriptURL.lastIndexOf('/') + 1);
if (!scriptURL.includes('://')) {
normalizedScriptURL = normalizedScriptURL.substring(
0,
normalizedScriptURL.lastIndexOf('/') + 1,
);
if (!normalizedScriptURL.includes('://')) {
// Add file protocol in case we have an absolute file path and not a URL.
// This shouldn't really be necessary. scriptURL should be a URL.
scriptURL = 'file://' + scriptURL;
normalizedScriptURL = 'file://' + normalizedScriptURL;
}
}
return scriptURL;
return normalizedScriptURL;
}
function getScriptURL(): ?string {
@@ -84,8 +90,10 @@ function setCustomSourceTransformer(
* `source` is either a number (opaque type returned by require('./foo.png'))
* or an `ImageSource` like { uri: '<http location || file path>' }
*/
function resolveAssetSource(source: any): ?ResolvedAssetSource {
if (typeof source === 'object') {
function resolveAssetSource(source: ?ImageSource): ?ResolvedAssetSource {
if (source == null || typeof source === 'object') {
// $FlowFixMe[incompatible-exact] `source` doesn't exactly match `ResolvedAssetSource`
// $FlowFixMe[incompatible-return] `source` doesn't exactly match `ResolvedAssetSource`
return source;
}