Skip to content

Compile time asserts #2367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ export class QueryDocumentSnapshot extends DocumentSnapshot
typeof data === 'object',
'Document in a QueryDocumentSnapshot should exist'
);
return data as firestore.DocumentData;
return data;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/api/user_data_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export class UserDataConverter {
context.fieldTransforms.length === 0,
'Field transforms should have been disallowed.'
);
return parsed!;
return parsed;
}

/** Sends data through this.preConverter, handling any thrown errors. */
Expand Down
17 changes: 7 additions & 10 deletions packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,12 @@ export class FieldFilter extends Filter {
'Comparing on key with IN, but filter value not an ArrayValue'
);
assert(
(value as ArrayValue).internalValue.every(elem => {
value.internalValue.every(elem => {
return elem instanceof RefValue;
}),
'Comparing on key with IN, but an array value was not a RefValue'
);
return new KeyFieldInFilter(field, value as ArrayValue);
return new KeyFieldInFilter(field, value);
} else {
assert(
value instanceof RefValue,
Expand All @@ -539,7 +539,7 @@ export class FieldFilter extends Filter {
op !== Operator.ARRAY_CONTAINS && op !== Operator.ARRAY_CONTAINS_ANY,
`'${op.toString()}' queries don't make sense on document keys.`
);
return new KeyFieldFilter(field, op, value as RefValue);
return new KeyFieldFilter(field, op, value);
}
} else if (value.isEqual(NullValue.INSTANCE)) {
if (op !== Operator.EQUAL) {
Expand All @@ -564,13 +564,13 @@ export class FieldFilter extends Filter {
value instanceof ArrayValue,
'IN filter has invalid value: ' + value.toString()
);
return new InFilter(field, value as ArrayValue);
return new InFilter(field, value);
} else if (op === Operator.ARRAY_CONTAINS_ANY) {
assert(
value instanceof ArrayValue,
'ARRAY_CONTAINS_ANY filter has invalid value: ' + value.toString()
);
return new ArrayContainsAnyFilter(field, value as ArrayValue);
return new ArrayContainsAnyFilter(field, value);
} else {
return new FieldFilter(field, op, value);
}
Expand Down Expand Up @@ -764,17 +764,14 @@ export class Bound {
component instanceof RefValue,
'Bound has a non-key value where the key path is being used.'
);
comparison = DocumentKey.comparator(
(component as RefValue).key,
doc.key
);
comparison = DocumentKey.comparator(component.key, doc.key);
} else {
const docValue = doc.field(orderByComponent.field);
assert(
docValue !== null,
'Field should exist since document matched the orderBy already.'
);
comparison = component.compareTo(docValue!);
comparison = component.compareTo(docValue);
}
if (orderByComponent.dir === Direction.DESCENDING) {
comparison = comparison * -1;
Expand Down
10 changes: 5 additions & 5 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
assert(!!queryView, `No query view found for ${query}`);

const viewChange = await this.synchronizeViewAndComputeSnapshot(
queryView!
queryView
);
if (viewChange.snapshot) {
newViewSnapshots.push(viewChange.snapshot);
Expand All @@ -995,7 +995,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
// allocate the target in LocalStore and initialize a new View.
const target = await this.localStore.getTarget(targetId);
assert(!!target, `Target for id ${targetId} not found`);
targetData = await this.localStore.allocateTarget(target!);
targetData = await this.localStore.allocateTarget(target);
await this.initializeViewAndComputeSnapshot(
this.synthesizeTargetToQuery(target!),
targetId,
Expand Down Expand Up @@ -1097,9 +1097,9 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
);
const target = await this.localStore.getTarget(targetId);
assert(!!target, `Query data for active target ${targetId} not found`);
const targetData = await this.localStore.allocateTarget(target!);
const targetData = await this.localStore.allocateTarget(target);
await this.initializeViewAndComputeSnapshot(
this.synthesizeTargetToQuery(target!),
this.synthesizeTargetToQuery(target),
targetData.targetId,
/*current=*/ false
);
Expand Down Expand Up @@ -1151,7 +1151,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
for (const query of queries) {
const queryView = this.queryViewsByQuery.get(query);
assert(!!queryView, `No query view found for ${query}`);
keySet = keySet.unionWith(queryView!.view.syncedDocuments);
keySet = keySet.unionWith(queryView.view.syncedDocuments);
}
return keySet;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/local/indexeddb_mutation_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
mutation.userId === this.userId,
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
);
results.push(this.serializer.fromDbMutationBatch(mutation!));
results.push(this.serializer.fromDbMutationBatch(mutation));
});
})
.next(() => results);
Expand Down Expand Up @@ -483,7 +483,7 @@ export class IndexedDbMutationQueue implements MutationQueue {
mutation.userId === this.userId,
`Unexpected user '${mutation.userId}' for mutation batch ${batchId}`
);
results.push(this.serializer.fromDbMutationBatch(mutation!));
results.push(this.serializer.fromDbMutationBatch(mutation));
})
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ export class IndexedDbPersistence implements Persistence {
typeof this.document.addEventListener === 'function',
"Expected 'document.addEventListener' to be a function"
);
this.document!.removeEventListener(
this.document.removeEventListener(
'visibilitychange',
this.documentVisibilityHandler
);
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/indexeddb_target_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ function retrieveMetadata(
);
return globalStore.get(DbTargetGlobal.key).next(metadata => {
assert(metadata !== null, 'Missing metadata row.');
return metadata!;
return metadata;
});
}

Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/local/local_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,8 @@ export class LocalStore {
.lookupMutationBatch(txn, batchId)
.next((batch: MutationBatch | null) => {
assert(batch !== null, 'Attempt to reject nonexistent batch!');
affectedKeys = batch!.keys();
return this.mutationQueue.removeMutationBatch(txn, batch!);
affectedKeys = batch.keys();
return this.mutationQueue.removeMutationBatch(txn, batch);
})
.next(() => {
return this.mutationQueue.performConsistencyCheck(txn);
Expand Down Expand Up @@ -746,8 +746,8 @@ export class LocalStore {
);

// Advance the last limbo free snapshot version
const lastLimboFreeSnapshotVersion = targetData!.snapshotVersion;
const updatedTargetData = targetData!.withLastLimboFreeSnapshotVersion(
const lastLimboFreeSnapshotVersion = targetData.snapshotVersion;
const updatedTargetData = targetData.withLastLimboFreeSnapshotVersion(
lastLimboFreeSnapshotVersion
);
this.targetDataByTarget = this.targetDataByTarget.insert(
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/memory_mutation_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class MemoryMutationQueue implements MutationQueue {
const mutationBatch = this.findMutationBatch(batchId);
assert(mutationBatch != null, 'Failed to find local mutation batch.');
return PersistencePromise.resolve<DocumentKeySet | null>(
mutationBatch!.keys()
mutationBatch.keys()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export abstract class RemoteDocumentChangeBuffer {
this._readTime !== undefined,
'Read time is not set. All removeEntry() calls must include a readTime if `trackRemovals` is used.'
);
return this._readTime!;
return this._readTime;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/local/shared_client_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ export class WebStorageSharedClientState implements SharedClientState {
): RemoteClientState | null {
const clientId = this.fromWebStorageClientStateKey(key);
assert(clientId !== null, `Cannot parse client state key '${key}'`);
return RemoteClientState.fromWebStorageEntry(clientId!, value);
return RemoteClientState.fromWebStorageEntry(clientId, value);
}

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

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

const targetId = Number(match![1]);
const targetId = Number(match[1]);
return QueryTargetMetadata.fromWebStorageEntry(targetId, value);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/simple_query_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class SimpleQueryEngine implements QueryEngine {

// TODO: Once LocalDocumentsView provides a getCollectionDocuments()
// method, we should call that here and then filter the results.
return this.localDocumentsView!.getDocumentsMatchingQuery(
return this.localDocumentsView.getDocumentsMatchingQuery(
transaction,
query,
SnapshotVersion.MIN
Expand Down
5 changes: 2 additions & 3 deletions packages/firestore/src/model/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,11 @@ export class TransformMutation extends Mutation {
maybeDoc instanceof Document,
'Unknown MaybeDocument type ' + maybeDoc
);
const doc = maybeDoc! as Document;
assert(
doc.key.isEqual(this.key),
maybeDoc.key.isEqual(this.key),
'Can only transform a document with the same key'
);
return doc;
return maybeDoc;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/model/transform_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export class NumericIncrementTransformOperation implements TransformOperation {
transformResult !== null,
"Didn't receive transformResult for NUMERIC_ADD transform"
);
return transformResult!;
return transformResult;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class Datastore {
keys.forEach(key => {
const doc = docs.get(key);
assert(!!doc, 'Missing entity in write response for ' + key);
result.push(doc!);
result.push(doc);
});
return result;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/persistent_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ export class PersistentWriteStream extends PersistentStream<
!!responseProto.streamToken,
'Got a write response without a stream token'
);
this.lastStreamToken = responseProto.streamToken!;
this.lastStreamToken = responseProto.streamToken;

if (!this.handshakeComplete_) {
// The first response is always the handshake response
Expand Down
Loading