Skip to content

Commit a547892

Browse files
Compile time asserts (#2367)
1 parent 9b8ef59 commit a547892

21 files changed

+78
-85
lines changed

packages/firestore/src/api/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ export class QueryDocumentSnapshot extends DocumentSnapshot
14341434
typeof data === 'object',
14351435
'Document in a QueryDocumentSnapshot should exist'
14361436
);
1437-
return data as firestore.DocumentData;
1437+
return data;
14381438
}
14391439
}
14401440

packages/firestore/src/api/user_data_converter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ export class UserDataConverter {
505505
context.fieldTransforms.length === 0,
506506
'Field transforms should have been disallowed.'
507507
);
508-
return parsed!;
508+
return parsed;
509509
}
510510

511511
/** Sends data through this.preConverter, handling any thrown errors. */

packages/firestore/src/core/query.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,12 @@ export class FieldFilter extends Filter {
524524
'Comparing on key with IN, but filter value not an ArrayValue'
525525
);
526526
assert(
527-
(value as ArrayValue).internalValue.every(elem => {
527+
value.internalValue.every(elem => {
528528
return elem instanceof RefValue;
529529
}),
530530
'Comparing on key with IN, but an array value was not a RefValue'
531531
);
532-
return new KeyFieldInFilter(field, value as ArrayValue);
532+
return new KeyFieldInFilter(field, value);
533533
} else {
534534
assert(
535535
value instanceof RefValue,
@@ -539,7 +539,7 @@ export class FieldFilter extends Filter {
539539
op !== Operator.ARRAY_CONTAINS && op !== Operator.ARRAY_CONTAINS_ANY,
540540
`'${op.toString()}' queries don't make sense on document keys.`
541541
);
542-
return new KeyFieldFilter(field, op, value as RefValue);
542+
return new KeyFieldFilter(field, op, value);
543543
}
544544
} else if (value.isEqual(NullValue.INSTANCE)) {
545545
if (op !== Operator.EQUAL) {
@@ -564,13 +564,13 @@ export class FieldFilter extends Filter {
564564
value instanceof ArrayValue,
565565
'IN filter has invalid value: ' + value.toString()
566566
);
567-
return new InFilter(field, value as ArrayValue);
567+
return new InFilter(field, value);
568568
} else if (op === Operator.ARRAY_CONTAINS_ANY) {
569569
assert(
570570
value instanceof ArrayValue,
571571
'ARRAY_CONTAINS_ANY filter has invalid value: ' + value.toString()
572572
);
573-
return new ArrayContainsAnyFilter(field, value as ArrayValue);
573+
return new ArrayContainsAnyFilter(field, value);
574574
} else {
575575
return new FieldFilter(field, op, value);
576576
}
@@ -764,17 +764,14 @@ export class Bound {
764764
component instanceof RefValue,
765765
'Bound has a non-key value where the key path is being used.'
766766
);
767-
comparison = DocumentKey.comparator(
768-
(component as RefValue).key,
769-
doc.key
770-
);
767+
comparison = DocumentKey.comparator(component.key, doc.key);
771768
} else {
772769
const docValue = doc.field(orderByComponent.field);
773770
assert(
774771
docValue !== null,
775772
'Field should exist since document matched the orderBy already.'
776773
);
777-
comparison = component.compareTo(docValue!);
774+
comparison = component.compareTo(docValue);
778775
}
779776
if (orderByComponent.dir === Direction.DESCENDING) {
780777
comparison = comparison * -1;

packages/firestore/src/core/sync_engine.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
980980
assert(!!queryView, `No query view found for ${query}`);
981981

982982
const viewChange = await this.synchronizeViewAndComputeSnapshot(
983-
queryView!
983+
queryView
984984
);
985985
if (viewChange.snapshot) {
986986
newViewSnapshots.push(viewChange.snapshot);
@@ -995,7 +995,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
995995
// allocate the target in LocalStore and initialize a new View.
996996
const target = await this.localStore.getTarget(targetId);
997997
assert(!!target, `Target for id ${targetId} not found`);
998-
targetData = await this.localStore.allocateTarget(target!);
998+
targetData = await this.localStore.allocateTarget(target);
999999
await this.initializeViewAndComputeSnapshot(
10001000
this.synthesizeTargetToQuery(target!),
10011001
targetId,
@@ -1097,9 +1097,9 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
10971097
);
10981098
const target = await this.localStore.getTarget(targetId);
10991099
assert(!!target, `Query data for active target ${targetId} not found`);
1100-
const targetData = await this.localStore.allocateTarget(target!);
1100+
const targetData = await this.localStore.allocateTarget(target);
11011101
await this.initializeViewAndComputeSnapshot(
1102-
this.synthesizeTargetToQuery(target!),
1102+
this.synthesizeTargetToQuery(target),
11031103
targetData.targetId,
11041104
/*current=*/ false
11051105
);
@@ -1151,7 +1151,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
11511151
for (const query of queries) {
11521152
const queryView = this.queryViewsByQuery.get(query);
11531153
assert(!!queryView, `No query view found for ${query}`);
1154-
keySet = keySet.unionWith(queryView!.view.syncedDocuments);
1154+
keySet = keySet.unionWith(queryView.view.syncedDocuments);
11551155
}
11561156
return keySet;
11571157
}

packages/firestore/src/local/indexeddb_mutation_queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
352352
mutation.userId === this.userId,
353353
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
354354
);
355-
results.push(this.serializer.fromDbMutationBatch(mutation!));
355+
results.push(this.serializer.fromDbMutationBatch(mutation));
356356
});
357357
})
358358
.next(() => results);
@@ -483,7 +483,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
483483
mutation.userId === this.userId,
484484
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
485485
);
486-
results.push(this.serializer.fromDbMutationBatch(mutation!));
486+
results.push(this.serializer.fromDbMutationBatch(mutation));
487487
})
488488
);
489489
});

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ export class IndexedDbPersistence implements Persistence {
949949
typeof this.document.addEventListener === 'function',
950950
"Expected 'document.addEventListener' to be a function"
951951
);
952-
this.document!.removeEventListener(
952+
this.document.removeEventListener(
953953
'visibilitychange',
954954
this.documentVisibilityHandler
955955
);

packages/firestore/src/local/indexeddb_target_cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ function retrieveMetadata(
421421
);
422422
return globalStore.get(DbTargetGlobal.key).next(metadata => {
423423
assert(metadata !== null, 'Missing metadata row.');
424-
return metadata!;
424+
return metadata;
425425
});
426426
}
427427

packages/firestore/src/local/local_store.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ export class LocalStore {
432432
.lookupMutationBatch(txn, batchId)
433433
.next((batch: MutationBatch | null) => {
434434
assert(batch !== null, 'Attempt to reject nonexistent batch!');
435-
affectedKeys = batch!.keys();
436-
return this.mutationQueue.removeMutationBatch(txn, batch!);
435+
affectedKeys = batch.keys();
436+
return this.mutationQueue.removeMutationBatch(txn, batch);
437437
})
438438
.next(() => {
439439
return this.mutationQueue.performConsistencyCheck(txn);
@@ -746,8 +746,8 @@ export class LocalStore {
746746
);
747747

748748
// Advance the last limbo free snapshot version
749-
const lastLimboFreeSnapshotVersion = targetData!.snapshotVersion;
750-
const updatedTargetData = targetData!.withLastLimboFreeSnapshotVersion(
749+
const lastLimboFreeSnapshotVersion = targetData.snapshotVersion;
750+
const updatedTargetData = targetData.withLastLimboFreeSnapshotVersion(
751751
lastLimboFreeSnapshotVersion
752752
);
753753
this.targetDataByTarget = this.targetDataByTarget.insert(

packages/firestore/src/local/memory_mutation_queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class MemoryMutationQueue implements MutationQueue {
158158
const mutationBatch = this.findMutationBatch(batchId);
159159
assert(mutationBatch != null, 'Failed to find local mutation batch.');
160160
return PersistencePromise.resolve<DocumentKeySet | null>(
161-
mutationBatch!.keys()
161+
mutationBatch.keys()
162162
);
163163
}
164164

packages/firestore/src/local/remote_document_change_buffer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export abstract class RemoteDocumentChangeBuffer {
8282
this._readTime !== undefined,
8383
'Read time is not set. All removeEntry() calls must include a readTime if `trackRemovals` is used.'
8484
);
85-
return this._readTime!;
85+
return this._readTime;
8686
}
8787

8888
/**

packages/firestore/src/local/shared_client_state.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ export class WebStorageSharedClientState implements SharedClientState {
963963
): RemoteClientState | null {
964964
const clientId = this.fromWebStorageClientStateKey(key);
965965
assert(clientId !== null, `Cannot parse client state key '${key}'`);
966-
return RemoteClientState.fromWebStorageEntry(clientId!, value);
966+
return RemoteClientState.fromWebStorageEntry(clientId, value);
967967
}
968968

969969
/**
@@ -977,8 +977,8 @@ export class WebStorageSharedClientState implements SharedClientState {
977977
const match = this.mutationBatchKeyRe.exec(key);
978978
assert(match !== null, `Cannot parse mutation batch key '${key}'`);
979979

980-
const batchId = Number(match![1]);
981-
const userId = match![2] !== undefined ? match![2] : null;
980+
const batchId = Number(match[1]);
981+
const userId = match[2] !== undefined ? match[2] : null;
982982
return MutationMetadata.fromWebStorageEntry(
983983
new User(userId),
984984
batchId,
@@ -997,7 +997,7 @@ export class WebStorageSharedClientState implements SharedClientState {
997997
const match = this.queryTargetKeyRe.exec(key);
998998
assert(match !== null, `Cannot parse query target key '${key}'`);
999999

1000-
const targetId = Number(match![1]);
1000+
const targetId = Number(match[1]);
10011001
return QueryTargetMetadata.fromWebStorageEntry(targetId, value);
10021002
}
10031003

packages/firestore/src/local/simple_query_engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class SimpleQueryEngine implements QueryEngine {
4949

5050
// TODO: Once LocalDocumentsView provides a getCollectionDocuments()
5151
// method, we should call that here and then filter the results.
52-
return this.localDocumentsView!.getDocumentsMatchingQuery(
52+
return this.localDocumentsView.getDocumentsMatchingQuery(
5353
transaction,
5454
query,
5555
SnapshotVersion.MIN

packages/firestore/src/model/mutation.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,11 @@ export class TransformMutation extends Mutation {
654654
maybeDoc instanceof Document,
655655
'Unknown MaybeDocument type ' + maybeDoc
656656
);
657-
const doc = maybeDoc! as Document;
658657
assert(
659-
doc.key.isEqual(this.key),
658+
maybeDoc.key.isEqual(this.key),
660659
'Can only transform a document with the same key'
661660
);
662-
return doc;
661+
return maybeDoc;
663662
}
664663

665664
/**

packages/firestore/src/model/transform_operation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class NumericIncrementTransformOperation implements TransformOperation {
219219
transformResult !== null,
220220
"Didn't receive transformResult for NUMERIC_ADD transform"
221221
);
222-
return transformResult!;
222+
return transformResult;
223223
}
224224

225225
/**

packages/firestore/src/remote/datastore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class Datastore {
115115
keys.forEach(key => {
116116
const doc = docs.get(key);
117117
assert(!!doc, 'Missing entity in write response for ' + key);
118-
result.push(doc!);
118+
result.push(doc);
119119
});
120120
return result;
121121
});

packages/firestore/src/remote/persistent_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ export class PersistentWriteStream extends PersistentStream<
698698
!!responseProto.streamToken,
699699
'Got a write response without a stream token'
700700
);
701-
this.lastStreamToken = responseProto.streamToken!;
701+
this.lastStreamToken = responseProto.streamToken;
702702

703703
if (!this.handshakeComplete_) {
704704
// The first response is always the handshake response

0 commit comments

Comments
 (0)