From 250e344c355c03596aa19b069d253703f5c51f47 Mon Sep 17 00:00:00 2001 From: Rob Hogan Date: Mon, 16 Dec 2024 05:19:02 -0800 Subject: [PATCH] Flow types: strengthen change event guarantee, fix missing type coverage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `metadata` on file change events out of metro-file-map is typed as optional and nullable, but in fact is always set within metro-file-map, and DeltaCalculator would throw if it wasn’t - Flow didn’t pick that up only because a $FlowFixMe hid the problem. This is a Flow-only change to reflect that and improve coverage. Changelog: Internal Reviewed By: huntie Differential Revision: D67259536 --- packages/metro-file-map/src/flow-types.js | 2 +- .../metro/src/DeltaBundler/DeltaCalculator.js | 17 +++-------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/packages/metro-file-map/src/flow-types.js b/packages/metro-file-map/src/flow-types.js index 34d66b2133..c52a44c90b 100644 --- a/packages/metro-file-map/src/flow-types.js +++ b/packages/metro-file-map/src/flow-types.js @@ -125,7 +125,7 @@ export type DuplicatesIndex = Map>; export type EventsQueue = Array<{ filePath: Path, - metadata?: ?ChangeEventMetadata, + metadata: ChangeEventMetadata, type: string, }>; diff --git a/packages/metro/src/DeltaBundler/DeltaCalculator.js b/packages/metro/src/DeltaBundler/DeltaCalculator.js index 402bdc6a5a..36d67bab73 100644 --- a/packages/metro/src/DeltaBundler/DeltaCalculator.js +++ b/packages/metro/src/DeltaBundler/DeltaCalculator.js @@ -13,7 +13,7 @@ import type {DeltaResult, Options} from './types.flow'; import type {RootPerfLogger} from 'metro-config'; -import type {ChangeEventMetadata} from 'metro-file-map'; +import type {ChangeEvent} from 'metro-file-map'; import {Graph} from './Graph'; import path from 'path'; @@ -173,9 +173,7 @@ class DeltaCalculator extends EventEmitter { return this._graph; } - /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's - * LTI update could not be added via codemod */ - _handleMultipleFileChanges = changeEvent => { + _handleMultipleFileChanges = (changeEvent: ChangeEvent) => { changeEvent.eventsQueue.forEach(eventInfo => { this._handleFileChange(eventInfo, changeEvent.logger); }); @@ -187,16 +185,7 @@ class DeltaCalculator extends EventEmitter { * when the delta needs to be calculated. */ _handleFileChange = ( - { - type, - filePath, - metadata, - }: { - type: string, - filePath: string, - metadata: ChangeEventMetadata, - ... - }, + {type, filePath, metadata}: ChangeEvent['eventsQueue'][number], logger: ?RootPerfLogger, ): mixed => { debug('Handling %s: %s (type: %s)', type, filePath, metadata.type);