Skip to content

Commit

Permalink
Make most modules in Image flow strict-local
Browse files Browse the repository at this point in the history
Summary:
Just improving type safety of a bunch of modules in the `Image` directory.

Changelog: [internal]

Differential Revision: D50080136

fbshipit-source-id: 542ead30b1c530468cb058ce833c028392fe07aa
  • Loading branch information
rubennorte authored and facebook-github-bot committed Oct 9, 2023
1 parent b92dc02 commit 460ff8d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
10 changes: 5 additions & 5 deletions packages/react-native/Libraries/Image/AssetSourceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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 {
Expand All @@ -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) +
Expand All @@ -114,7 +114,7 @@ class AssetSourceResolver {
* E.g. 'file:///sdcard/bundle/assets/AwesomeModule/[email protected]'
*/
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
Expand Down Expand Up @@ -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));
}

Expand Down
9 changes: 4 additions & 5 deletions packages/react-native/Libraries/Image/AssetUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -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];
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Image/ImageBackground.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/Image/ImageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down
32 changes: 20 additions & 12 deletions packages/react-native/Libraries/Image/resolveAssetSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @flow strict-local
*/

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

'use strict';

import type {ResolvedAssetSource} from './AssetSourceResolver';
import type {ImageSource} from './ImageSource';

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

let _sourceCodeScriptURL: ?string;
function getSourceCodeScriptURL(): ?string {
if (_sourceCodeScriptURL) {
if (_sourceCodeScriptURL != null) {
return _sourceCodeScriptURL;
}

Expand All @@ -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];
Expand All @@ -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 {
Expand All @@ -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;
}

Expand Down

0 comments on commit 460ff8d

Please sign in to comment.