Skip to content

Commit c08851c

Browse files
Enable strictNullChecks (#1159)
1 parent 546440e commit c08851c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+291
-277
lines changed

packages/firestore/src/api/database.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,12 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
319319

320320
enableNetwork(): Promise<void> {
321321
this.ensureClientConfigured();
322-
return this._firestoreClient.enableNetwork();
322+
return this._firestoreClient!.enableNetwork();
323323
}
324324

325325
disableNetwork(): Promise<void> {
326326
this.ensureClientConfigured();
327-
return this._firestoreClient.disableNetwork();
327+
return this._firestoreClient!.disableNetwork();
328328
}
329329

330330
enablePersistence(settings?: _PersistenceSettings): Promise<void> {
@@ -597,7 +597,7 @@ export class Transaction implements firestore.Transaction {
597597
} else if (doc instanceof Document) {
598598
return new DocumentSnapshot(this._firestore, ref._key, doc, false);
599599
} else {
600-
fail('MaybeDocument is neither Document nor NoDocument');
600+
throw fail('MaybeDocument is neither Document nor NoDocument');
601601
}
602602
});
603603
}
@@ -1060,8 +1060,8 @@ export class DocumentReference implements firestore.DocumentReference {
10601060
}
10611061

10621062
get(options?: firestore.GetOptions): Promise<firestore.DocumentSnapshot> {
1063-
validateOptionNames('DocumentReference.get', options, ['source']);
10641063
if (options) {
1064+
validateOptionNames('DocumentReference.get', options, ['source']);
10651065
validateNamedOptionalPropertyEquals(
10661066
'DocumentReference.get',
10671067
'options',
@@ -1888,11 +1888,10 @@ export class QuerySnapshot implements firestore.QuerySnapshot {
18881888
docChanges(
18891889
options?: firestore.SnapshotListenOptions
18901890
): firestore.DocumentChange[] {
1891-
validateOptionNames('QuerySnapshot.docChanges', options, [
1892-
'includeMetadataChanges'
1893-
]);
1894-
18951891
if (options) {
1892+
validateOptionNames('QuerySnapshot.docChanges', options, [
1893+
'includeMetadataChanges'
1894+
]);
18961895
validateNamedOptionalType(
18971896
'QuerySnapshot.docChanges',
18981897
'boolean',
@@ -1901,7 +1900,9 @@ export class QuerySnapshot implements firestore.QuerySnapshot {
19011900
);
19021901
}
19031902

1904-
const includeMetadataChanges = options && options.includeMetadataChanges;
1903+
const includeMetadataChanges = !!(
1904+
options && options.includeMetadataChanges
1905+
);
19051906

19061907
if (includeMetadataChanges && this._snapshot.excludesMetadataChanges) {
19071908
throw new FirestoreError(
@@ -2030,7 +2031,7 @@ export class CollectionReference extends Query
20302031
'Document path must be a non-empty string'
20312032
);
20322033
}
2033-
const path = ResourcePath.fromString(pathString);
2034+
const path = ResourcePath.fromString(pathString!);
20342035
return DocumentReference.forPath(
20352036
this._query.path.child(path),
20362037
this.firestore

packages/firestore/src/api/user_data_converter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,22 @@ export class UserDataConverter {
339339
fieldMask = new FieldMask(context.fieldMask);
340340
fieldTransforms = context.fieldTransforms;
341341
} else {
342-
const validatedFieldPaths = [];
342+
const validatedFieldPaths: FieldPath[] = [];
343343

344344
for (const stringOrFieldPath of fieldPaths) {
345-
let fieldPath;
345+
let fieldPath: FieldPath;
346346

347347
if (stringOrFieldPath instanceof ExternalFieldPath) {
348-
fieldPath = stringOrFieldPath as ExternalFieldPath;
348+
fieldPath = stringOrFieldPath._internalPath;
349349
} else if (typeof stringOrFieldPath === 'string') {
350350
fieldPath = fieldPathFromDotSeparatedString(
351351
methodName,
352352
stringOrFieldPath
353353
);
354354
} else {
355-
fail('Expected stringOrFieldPath to be a string or a FieldPath');
355+
throw fail(
356+
'Expected stringOrFieldPath to be a string or a FieldPath'
357+
);
356358
}
357359

358360
if (!context.contains(fieldPath)) {
@@ -693,7 +695,7 @@ export class UserDataConverter {
693695
methodName,
694696
FieldPath.EMPTY_PATH
695697
);
696-
return this.parseData(element, context.childContextForArray(i));
698+
return this.parseData(element, context.childContextForArray(i))!;
697699
});
698700
}
699701
}

packages/firestore/src/core/firestore_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ export class FirestoreClient {
508508
return view.applyChanges(
509509
viewDocChanges,
510510
/* updateLimboDocuments= */ false
511-
).snapshot;
511+
).snapshot!;
512512
});
513513
}
514514

packages/firestore/src/core/query.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ export class NanFilter extends Filter {
591591
}
592592

593593
matches(doc: Document): boolean {
594-
const val = doc.field(this.field).value();
594+
const field = doc.field(this.field);
595+
const val = field && field.value();
595596
return typeof val === 'number' && isNaN(val);
596597
}
597598

@@ -680,7 +681,7 @@ export class Bound {
680681
docValue !== undefined,
681682
'Field should exist since document matched the orderBy already.'
682683
);
683-
comparison = component.compareTo(docValue);
684+
comparison = component.compareTo(docValue!);
684685
}
685686
if (orderByComponent.dir === Direction.DESCENDING) {
686687
comparison = comparison * -1;

packages/firestore/src/core/sync_engine.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
252252
);
253253
const viewChange = view.applyChanges(
254254
viewDocChanges,
255-
/* updateLimboDocuments= */ this.isPrimary,
255+
/* updateLimboDocuments= */ this.isPrimary === true,
256256
synthesizedTargetChange
257257
);
258258
assert(
@@ -781,7 +781,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
781781
remoteEvent && remoteEvent.targetChanges[queryView.targetId];
782782
const viewChange = queryView.view.applyChanges(
783783
viewDocChanges,
784-
/* updateLimboDocuments= */ this.isPrimary,
784+
/* updateLimboDocuments= */ this.isPrimary === true,
785785
targetChange
786786
);
787787
return this.updateTrackedLimbos(
@@ -809,7 +809,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
809809
});
810810

811811
await Promise.all(queriesProcessed);
812-
this.syncEngineListener.onWatchChange(newSnaps);
812+
this.syncEngineListener!.onWatchChange(newSnaps);
813813
this.localStore.notifyLocalViewChanges(docChangesInAllViews);
814814
// TODO(multitab): Multitab garbage collection
815815
if (this.isPrimary) {
@@ -940,13 +940,14 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
940940
}
941941
} else {
942942
assert(
943-
this.isPrimary,
943+
this.isPrimary === true,
944944
'A secondary tab should never have an active query without an active view.'
945945
);
946946
// For queries that never executed on this client, we need to
947947
// allocate the query in LocalStore and initialize a new View.
948948
const query = await this.localStore.getQueryForTarget(targetId);
949-
queryData = await this.localStore.allocateQuery(query);
949+
assert(!!query, `Query data for target ${targetId} not found`);
950+
queryData = await this.localStore.allocateQuery(query!);
950951
await this.initializeViewAndComputeSnapshot(
951952
queryData,
952953
/*current=*/ false
@@ -1000,7 +1001,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
10001001
queryView.query,
10011002
/*keepPersistedQueryData=*/ true
10021003
);
1003-
this.syncEngineListener!.onWatchError(queryView.query, error);
1004+
this.syncEngineListener!.onWatchError(queryView.query, error!);
10041005
break;
10051006
}
10061007
default:
@@ -1025,7 +1026,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
10251026
);
10261027
const query = await this.localStore.getQueryForTarget(targetId);
10271028
assert(!!query, `Query data for active target ${targetId} not found`);
1028-
const queryData = await this.localStore.allocateQuery(query);
1029+
const queryData = await this.localStore.allocateQuery(query!);
10291030
await this.initializeViewAndComputeSnapshot(
10301031
queryData,
10311032
/*current=*/ false

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ export class IndexedDbMutationQueue implements MutationQueue {
221221
batchId: BatchId
222222
): PersistencePromise<DocumentKeySet | null> {
223223
if (this.documentKeysByBatchId[batchId]) {
224-
return PersistencePromise.resolve(this.documentKeysByBatchId[batchId]);
224+
return PersistencePromise.resolve<DocumentKeySet | null>(
225+
this.documentKeysByBatchId[batchId]
226+
);
225227
} else {
226228
return this.lookupMutationBatch(transaction, batchId).next(batch => {
227229
if (batch) {
@@ -324,7 +326,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
324326
.get(batchId)
325327
.next(mutation => {
326328
if (!mutation) {
327-
fail(
329+
throw fail(
328330
'Dangling document-mutation reference found: ' +
329331
indexKey +
330332
' which points to ' +
@@ -454,7 +456,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
454456
.get(batchId)
455457
.next(mutation => {
456458
if (mutation === null) {
457-
fail(
459+
throw fail(
458460
'Dangling document-mutation reference found, ' +
459461
'which points to ' +
460462
batchId

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ export class IndexedDbPersistence implements Persistence {
156156
if (txn instanceof IndexedDbTransaction) {
157157
return SimpleDb.getStore<Key, Value>(txn.simpleDbTransaction, store);
158158
} else {
159-
fail('IndexedDbPersistence must use instances of IndexedDbTransaction');
159+
throw fail(
160+
'IndexedDbPersistence must use instances of IndexedDbTransaction'
161+
);
160162
}
161163
}
162164

@@ -215,7 +217,7 @@ export class IndexedDbPersistence implements Persistence {
215217
this.dbName = persistenceKey + IndexedDbPersistence.MAIN_DATABASE;
216218
this.serializer = new LocalSerializer(serializer);
217219
this.document = platform.document;
218-
this.window = platform.window;
220+
this.window = platform.window!;
219221
this.allowTabSynchronization = synchronizeTabs;
220222
this.queryCache = new IndexedDbQueryCache(this.serializer);
221223
this.remoteDocumentCache = new IndexedDbRemoteDocumentCache(
@@ -354,7 +356,7 @@ export class IndexedDbPersistence implements Persistence {
354356
this.lastGarbageCollectionTime = Date.now();
355357

356358
let activeClients: DbClientMetadata[];
357-
let inactiveClients: DbClientMetadata[];
359+
let inactiveClients: DbClientMetadata[] = [];
358360

359361
await this.runTransaction('readwrite', true, txn => {
360362
const metadataStore = IndexedDbPersistence.getStore<
@@ -472,7 +474,7 @@ export class IndexedDbPersistence implements Persistence {
472474
}
473475

474476
if (!this.isLocalClient(currentPrimary)) {
475-
if (!currentPrimary.allowTabSynchronization) {
477+
if (!currentPrimary!.allowTabSynchronization) {
476478
// Fail the `canActAsPrimary` check if the current leaseholder has
477479
// not opted into multi-tab synchronization. If this happens at
478480
// client startup, we reject the Promise returned by
@@ -710,7 +712,7 @@ export class IndexedDbPersistence implements Persistence {
710712
!this.isClientZombied(currentPrimary.ownerId);
711713

712714
if (currentLeaseIsValid && !this.isLocalClient(currentPrimary)) {
713-
if (!currentPrimary.allowTabSynchronization) {
715+
if (!currentPrimary!.allowTabSynchronization) {
714716
throw new FirestoreError(
715717
Code.FAILED_PRECONDITION,
716718
PRIMARY_LEASE_EXCLUSIVE_ERROR_MSG
@@ -798,7 +800,7 @@ export class IndexedDbPersistence implements Persistence {
798800
) {
799801
this.documentVisibilityHandler = () => {
800802
this.queue.enqueueAndForget(() => {
801-
this.inForeground = this.document.visibilityState === 'visible';
803+
this.inForeground = this.document!.visibilityState === 'visible';
802804
return this.updateClientMetadataAndTryBecomePrimary();
803805
});
804806
};
@@ -819,7 +821,7 @@ export class IndexedDbPersistence implements Persistence {
819821
typeof this.document.addEventListener === 'function',
820822
"Expected 'document.addEventListener' to be a function"
821823
);
822-
this.document.removeEventListener(
824+
this.document!.removeEventListener(
823825
'visibilitychange',
824826
this.documentVisibilityHandler
825827
);
@@ -877,7 +879,7 @@ export class IndexedDbPersistence implements Persistence {
877879
process.env.USE_MOCK_PERSISTENCE === 'YES',
878880
'Operating without LocalStorage is only supported with IndexedDbShim.'
879881
);
880-
return null;
882+
return false;
881883
}
882884

883885
try {
@@ -895,7 +897,7 @@ export class IndexedDbPersistence implements Persistence {
895897
} catch (e) {
896898
// Gracefully handle if LocalStorage isn't available / working.
897899
log.error(LOG_TAG, 'Failed to get zombied client id.', e);
898-
return null;
900+
return false;
899901
}
900902
}
901903

packages/firestore/src/local/indexeddb_query_cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class IndexedDbQueryCache implements QueryCache {
138138
.get(DbTargetGlobal.key)
139139
.next(metadata => {
140140
assert(metadata !== null, 'Missing metadata row.');
141-
return metadata;
141+
return metadata!;
142142
});
143143
}
144144

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
188188
changedKeys = changedKeys.unionWith(
189189
this.serializer.fromDbResourcePaths(documentChange.changes)
190190
);
191-
this._lastProcessedDocumentChangeId = documentChange.id;
191+
this._lastProcessedDocumentChangeId = documentChange.id!;
192192
})
193193
.next(() => {
194194
const documentPromises: Array<PersistencePromise<void>> = [];

packages/firestore/src/local/local_store.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export class LocalStore {
298298
highestAck
299299
);
300300
} else {
301-
return PersistencePromise.resolve([]);
301+
return PersistencePromise.resolve([] as MutationBatch[]);
302302
}
303303
})
304304
.next(ackedBatches =>
@@ -344,9 +344,12 @@ export class LocalStore {
344344
.lookupMutationKeys(txn, batchId)
345345
.next(keys => {
346346
if (keys) {
347-
return this.localDocuments.getDocuments(txn, keys);
347+
return this.localDocuments.getDocuments(
348+
txn,
349+
keys
350+
) as PersistencePromise<MaybeDocumentMap | null>;
348351
} else {
349-
return PersistencePromise.resolve(null);
352+
return PersistencePromise.resolve<MaybeDocumentMap | null>(null);
350353
}
351354
});
352355
}
@@ -883,7 +886,7 @@ export class LocalStore {
883886
writesToRelease = this.queryCache
884887
.getLastRemoteSnapshotVersion(txn)
885888
.next(lastRemoteVersion => {
886-
const toRelease = [];
889+
const toRelease: MutationBatchResult[] = [];
887890
for (const batchResult of this.heldBatchResults) {
888891
if (batchResult.commitVersion.compareTo(lastRemoteVersion) > 0) {
889892
break;
@@ -1036,7 +1039,7 @@ export class LocalStore {
10361039
return this.persistence.runTransaction('Get query data', false, txn => {
10371040
return this.queryCache
10381041
.getQueryDataForTarget(txn, targetId)
1039-
.next(queryData => queryData.query);
1042+
.next(queryData => (queryData ? queryData.query : null));
10401043
});
10411044
}
10421045
}

packages/firestore/src/local/memory_mutation_queue.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class MemoryMutationQueue implements MutationQueue {
166166
const mutationBatch = this.findMutationBatch(batchId);
167167
assert(mutationBatch != null, 'Failed to find local mutation batch.');
168168
return PersistencePromise.resolve(
169-
!mutationBatch.isTombstone() ? mutationBatch.keys() : null
169+
!mutationBatch!.isTombstone() ? mutationBatch!.keys() : null
170170
);
171171
}
172172

@@ -190,10 +190,10 @@ export class MemoryMutationQueue implements MutationQueue {
190190
for (; index < size; index++) {
191191
const batch = this.mutationQueue[index];
192192
if (!batch.isTombstone()) {
193-
return PersistencePromise.resolve(batch);
193+
return PersistencePromise.resolve<MutationBatch | null>(batch);
194194
}
195195
}
196-
return PersistencePromise.resolve(null);
196+
return PersistencePromise.resolve<MutationBatch | null>(null);
197197
}
198198

199199
getAllMutationBatches(

packages/firestore/src/local/remote_document_change_buffer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class RemoteDocumentChangeBuffer {
6767

6868
const bufferedEntry = changes.get(documentKey);
6969
if (bufferedEntry) {
70-
return PersistencePromise.resolve(bufferedEntry);
70+
return PersistencePromise.resolve<MaybeDocument | null>(bufferedEntry);
7171
} else {
7272
return this.remoteDocumentCache.getEntry(transaction, documentKey);
7373
}

0 commit comments

Comments
 (0)