Skip to content

Commit

Permalink
Convert assets-registry to Flow comment syntax (#48458)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #48458

NOTE: This change is made once and is not guaranteed.

**Motivation**: Requiring Flow parsing for `react-native` and its dependencies in user space can involve friction. For the case of `react-native/assets-registry` → `react-native-web`, I believe we should do the pragmatic thing to relax this requirement.

- This is a convenience stopgap until #39542 can be stabilised.
- This package is tiny and infrequently modified — I believe it's pragmatic/safe to do a one-time conversion, with the above notice and no changelog (i.e. "experimental" for now).

Resolves #48349.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D67764460

fbshipit-source-id: 7687fd79c6dea73c234a46e475c1cc745225830b
  • Loading branch information
huntie authored and facebook-github-bot committed Jan 2, 2025
1 parent 5ef9e1f commit 697e946
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
18 changes: 10 additions & 8 deletions packages/assets/path-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict
* @format
*/

'use strict';

import type {PackagerAsset} from './registry.js';
/*:: import type {PackagerAsset} from './registry.js'; */

const androidScaleSuffix = {
'0.75': 'ldpi',
Expand All @@ -27,7 +27,7 @@ const ANDROID_BASE_DENSITY = 160;
* FIXME: using number to represent discrete scale numbers is fragile in essence because of
* floating point numbers imprecision.
*/
function getAndroidAssetSuffix(scale: number): string {
function getAndroidAssetSuffix(scale /*: number */) /*: string */ {
if (scale.toString() in androidScaleSuffix) {
// $FlowFixMe[invalid-computed-prop]
return androidScaleSuffix[scale.toString()];
Expand All @@ -53,9 +53,9 @@ const drawableFileTypes = new Set([
]);

function getAndroidResourceFolderName(
asset: PackagerAsset,
scale: number,
): string {
asset /*: PackagerAsset */,
scale /*: number */,
) /*: string */ {
if (!drawableFileTypes.has(asset.type)) {
return 'raw';
}
Expand All @@ -73,15 +73,17 @@ function getAndroidResourceFolderName(
return 'drawable-' + suffix;
}

function getAndroidResourceIdentifier(asset: PackagerAsset): string {
function getAndroidResourceIdentifier(
asset /*: PackagerAsset */,
) /*: string */ {
return (getBasePath(asset) + '/' + asset.name)
.toLowerCase()
.replace(/\//g, '_') // Encode folder structure in file name
.replace(/([^a-z0-9_])/g, '') // Remove illegal chars
.replace(/^(?:assets|assetsunstable_path)_/, ''); // Remove "assets_" or "assetsunstable_path_" prefix
}

function getBasePath(asset: PackagerAsset): string {
function getBasePath(asset /*: PackagerAsset */) /*: string */ {
const basePath = asset.httpServerLocation;
return basePath.startsWith('/') ? basePath.slice(1) : basePath;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/assets/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

'use strict';

/*::
export type AssetDestPathResolver = 'android' | 'generic';
export type PackagerAsset = {
Expand All @@ -25,16 +26,17 @@ export type PackagerAsset = {
+resolver?: AssetDestPathResolver,
...
};
*/

const assets: Array<PackagerAsset> = [];
const assets /*: Array<PackagerAsset> */ = [];

function registerAsset(asset: PackagerAsset): number {
function registerAsset(asset /*: PackagerAsset */) /*: number */ {
// `push` returns new array length, so the first asset will
// get id 1 (not 0) to make the value truthy
return assets.push(asset);
}

function getAssetByID(assetId: number): PackagerAsset {
function getAssetByID(assetId /*: number */) /*: PackagerAsset */ {
return assets[assetId - 1];
}

Expand Down

0 comments on commit 697e946

Please sign in to comment.