diff --git a/packages/react-native/Libraries/Image/AssetSourceResolver.js b/packages/react-native/Libraries/Image/AssetSourceResolver.js index d7ae700437b..5e0d967f349 100644 --- a/packages/react-native/Libraries/Image/AssetSourceResolver.js +++ b/packages/react-native/Libraries/Image/AssetSourceResolver.js @@ -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)); } diff --git a/packages/react-native/Libraries/Image/AssetUtils.js b/packages/react-native/Libraries/Image/AssetUtils.js index fd370621f0c..18bb15c07d8 100644 --- a/packages/react-native/Libraries/Image/AssetUtils.js +++ b/packages/react-native/Libraries/Image/AssetUtils.js @@ -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, 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]; } } diff --git a/packages/react-native/Libraries/Image/ImageBackground.js b/packages/react-native/Libraries/Image/ImageBackground.js index c839532501e..c08bf495576 100644 --- a/packages/react-native/Libraries/Image/ImageBackground.js +++ b/packages/react-native/Libraries/Image/ImageBackground.js @@ -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 { - setNativeProps(props: Object) { + setNativeProps(props: {...}) { // Work-around flow const viewRef = this._viewRef; if (viewRef) { diff --git a/packages/react-native/Libraries/Image/ImageUtils.js b/packages/react-native/Libraries/Image/ImageUtils.js index 734f36c7e90..87e4f2eac02 100644 --- a/packages/react-native/Libraries/Image/ImageUtils.js +++ b/packages/react-native/Libraries/Image/ImageUtils.js @@ -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 */ diff --git a/packages/react-native/Libraries/Image/resolveAssetSource.js b/packages/react-native/Libraries/Image/resolveAssetSource.js index 2ff2bcfd019..df18b1432c1 100644 --- a/packages/react-native/Libraries/Image/resolveAssetSource.js +++ b/packages/react-native/Libraries/Image/resolveAssetSource.js @@ -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: '' } */ -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; }