Skip to content
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

chore: enforce return types #1065

Merged
merged 1 commit into from
May 1, 2020
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
19 changes: 18 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
{
"extends": "./node_modules/gts"
"extends": "./node_modules/gts",
"overrides": [
{
"files": [
"dev/src/*.ts"
],
"parser": "@typescript-eslint/parser",
"rules": {
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowExpressions": true,
"allowTypedFunctionExpressions": true
}
]
}
}
]
}
2 changes: 1 addition & 1 deletion dev/src/backoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class ExponentialBackoff {
* @returns {number} The jitter to apply based on the current delay.
* @private
*/
private jitterDelayMs() {
private jitterDelayMs(): number {
return (Math.random() - 0.5) * this.jitterFactor * this.currentBaseMs;
}
}
8 changes: 4 additions & 4 deletions dev/src/document-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class DocumentChange<T = DocumentData> {
* // Remove this listener.
* unsubscribe();
*/
get type() {
get type(): DocumentChangeType {
return this._type;
}

Expand All @@ -96,7 +96,7 @@ export class DocumentChange<T = DocumentData> {
* // Remove this listener.
* unsubscribe();
*/
get doc() {
get doc(): QueryDocumentSnapshot<T> {
return this._document;
}

Expand Down Expand Up @@ -127,7 +127,7 @@ export class DocumentChange<T = DocumentData> {
* // Remove this listener.
* unsubscribe();
*/
get oldIndex() {
get oldIndex(): number {
return this._oldIndex;
}

Expand Down Expand Up @@ -159,7 +159,7 @@ export class DocumentChange<T = DocumentData> {
* // Remove this listener.
* unsubscribe();
*/
get newIndex() {
get newIndex(): number {
return this._newIndex;
}

Expand Down
14 changes: 9 additions & 5 deletions dev/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ export class DocumentMask {
* @private
* @return {boolean} Whether this document mask is empty.
*/
get isEmpty() {
get isEmpty(): boolean {
return this._sortedPaths.length === 0;
}

Expand Down Expand Up @@ -771,13 +771,13 @@ export class DocumentMask {
* Applies this DocumentMask to 'data' and computes the list of field paths
* that were specified in the mask but are not present in 'data'.
*/
const applyDocumentMask = (data: DocumentData) => {
const applyDocumentMask: (data: DocumentData) => DocumentData = data => {
const remainingPaths = this._sortedPaths.slice(0);

const processObject = (
const processObject: (
currentData: DocumentData,
currentPath?: FieldPath
) => {
) => DocumentData | null = (currentData, currentPath) => {
let result: DocumentData | null = null;

Object.keys(currentData).forEach(key => {
Expand Down Expand Up @@ -902,7 +902,11 @@ export class DocumentTransform<T = DocumentData> {
): DocumentTransform<T> {
const transforms = new Map<FieldPath, FieldTransform>();

function encode_(val: unknown, path: FieldPath, allowTransforms: boolean) {
function encode_(
val: unknown,
path: FieldPath,
allowTransforms: boolean
): void {
if (val instanceof FieldTransform && val.includeInDocumentTransform) {
if (allowTransforms) {
transforms.set(path, val);
Expand Down
6 changes: 3 additions & 3 deletions dev/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,15 +1266,15 @@ export class Firestore {
let streamInitialized = false;

return new Promise<Duplex>((resolve, reject) => {
function streamReady() {
function streamReady(): void {
if (!streamInitialized) {
streamInitialized = true;
logger('Firestore._initializeStream', requestTag, 'Releasing stream');
resolve(resultStream);
}
}

function streamEnded() {
function streamEnded(): void {
logger(
'Firestore._initializeStream',
requestTag,
Expand All @@ -1285,7 +1285,7 @@ export class Firestore {
lifetime.resolve();
}

function streamFailed(err: Error) {
function streamFailed(err: Error): void {
if (!streamInitialized) {
// If we receive an error before we were able to receive any data,
// reject this stream.
Expand Down
8 changes: 4 additions & 4 deletions dev/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export class ResourcePath extends Path<ResourcePath> {
* database.
* @private
*/
get relativeName() {
get relativeName(): string {
return this.segments.join('/');
}

Expand Down Expand Up @@ -543,7 +543,7 @@ export class FieldPath extends Path<FieldPath> {
*
* @returns {FieldPath}
*/
static documentId() {
static documentId(): FieldPath {
return FieldPath._DOCUMENT_ID;
}

Expand All @@ -556,7 +556,7 @@ export class FieldPath extends Path<FieldPath> {
* @param {string|FieldPath} fieldPath The FieldPath to create.
* @returns {FieldPath} A field path representation.
*/
static fromArgument(fieldPath: string | FieldPath) {
static fromArgument(fieldPath: string | FieldPath): FieldPath {
// validateFieldPath() is used in all public API entry points to validate
// that fromArgument() is only called with a Field Path or a string.
return fieldPath instanceof FieldPath
Expand Down Expand Up @@ -613,7 +613,7 @@ export class FieldPath extends Path<FieldPath> {
* @param segments Sequence of field names.
* @returns The newly created FieldPath.
*/
construct(segments: string[]) {
construct(segments: string[]): FieldPath {
return new FieldPath(...segments);
}

Expand Down
6 changes: 3 additions & 3 deletions dev/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ export class QueryOptions<T> {
return this.fieldOrders.length > 0;
}

isEqual(other: QueryOptions<T>) {
isEqual(other: QueryOptions<T>): boolean {
if (this === other) {
return true;
}
Expand Down Expand Up @@ -1129,7 +1129,7 @@ export class Query<T = DocumentData> {
static _extractFieldValues(
documentSnapshot: DocumentSnapshot,
fieldOrders: FieldOrder[]
) {
): unknown[] {
const fieldValues: unknown[] = [];

for (const fieldOrder of fieldOrders) {
Expand Down Expand Up @@ -2215,7 +2215,7 @@ export class CollectionReference<T = DocumentData> extends Query<T> {
* Returns a resource path for this collection.
* @private
*/
private get resourcePath() {
private get resourcePath(): ResourcePath {
return this._queryOptions.parentPath.append(
this._queryOptions.collectionId
);
Expand Down
8 changes: 4 additions & 4 deletions dev/src/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Timestamp {
*
* @return {Timestamp} A new `Timestamp` representing the current date.
*/
static now() {
static now(): Timestamp {
return Timestamp.fromMillis(Date.now());
}

Expand All @@ -86,7 +86,7 @@ export class Timestamp {
* @return {Timestamp} A new `Timestamp` representing the same point in time
* as the given date.
*/
static fromDate(date: Date) {
static fromDate(date: Date): Timestamp {
return Timestamp.fromMillis(date.getTime());
}

Expand All @@ -103,7 +103,7 @@ export class Timestamp {
* @return {Timestamp} A new `Timestamp` representing the same point in time
* as the given number of milliseconds.
*/
static fromMillis(milliseconds: number) {
static fromMillis(milliseconds: number): Timestamp {
const seconds = Math.floor(milliseconds / 1000);
const nanos = (milliseconds - seconds * 1000) * MS_TO_NANOS;
return new Timestamp(seconds, nanos);
Expand All @@ -115,7 +115,7 @@ export class Timestamp {
* @private
* @param {Object} timestamp The `Timestamp` Protobuf object.
*/
static fromProto(timestamp: google.protobuf.ITimestamp) {
static fromProto(timestamp: google.protobuf.ITimestamp): Timestamp {
return new Timestamp(Number(timestamp.seconds || 0), timestamp.nanos || 0);
}

Expand Down
2 changes: 1 addition & 1 deletion dev/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export class Transaction {
* @private
* @return A Promise that resolves after the delay expired.
*/
private async maybeBackoff(error?: GoogleError) {
private async maybeBackoff(error?: GoogleError): Promise<void> {
if (error && error.code === Status.RESOURCE_EXHAUSTED) {
this._backoff.resetToMax();
}
Expand Down
2 changes: 1 addition & 1 deletion dev/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export function validateInteger(
export function invalidArgumentMessage(
arg: string | number,
expectedType: string
) {
): string {
return `${formatArgumentName(arg)} is not a valid ${expectedType}.`;
}

Expand Down
8 changes: 4 additions & 4 deletions dev/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ const ChangeType: {[k: string]: DocumentChangeType} = {
* The comparator used for document watches (which should always get called with
* the same document).
*/
const DOCUMENT_WATCH_COMPARATOR = <T>(
const DOCUMENT_WATCH_COMPARATOR: <T>(
doc1: QueryDocumentSnapshot<T>,
doc2: QueryDocumentSnapshot<T>
) => {
) => number = (doc1, doc2) => {
assert(doc1 === doc2, 'Document watches only support one document.');
return 0;
};

const EMPTY_FUNCTION = () => {};
const EMPTY_FUNCTION: () => void = () => {};

/**
* @private
Expand Down Expand Up @@ -250,7 +250,7 @@ abstract class Watch<T = DocumentData> {

this.initStream();

const unsubscribe = () => {
const unsubscribe: () => void = () => {
logger('Watch.onSnapshot', this.requestTag, 'Unsubscribe called');
// Prevent further callbacks.
if (this.isActive) {
Expand Down
14 changes: 8 additions & 6 deletions dev/src/write-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ interface WriteOp {
precondition?: api.IPrecondition | null;
}

export type PendingWriteOp = () => WriteOp;

/**
* A Firestore WriteBatch that can be used to atomically commit multiple write
* operations at once.
Expand All @@ -136,7 +138,7 @@ export class WriteBatch {
* resulting `api.IWrite` will be sent to the backend.
* @private
*/
private readonly _ops: Array<() => WriteOp> = [];
private readonly _ops: Array<PendingWriteOp> = [];

private _committed = false;

Expand Down Expand Up @@ -202,7 +204,7 @@ export class WriteBatch {

const precondition = new Precondition({exists: false});

const op = () => {
const op: PendingWriteOp = () => {
const document = DocumentSnapshot.fromObject(documentRef, firestoreData);
const write = document.toProto();
if (!transform.isEmpty) {
Expand Down Expand Up @@ -254,7 +256,7 @@ export class WriteBatch {

const conditions = new Precondition(precondition);

const op = () => {
const op: PendingWriteOp = () => {
return {
write: {
delete: documentRef.formattedName,
Expand Down Expand Up @@ -326,7 +328,7 @@ export class WriteBatch {
const transform = DocumentTransform.fromObject(documentRef, firestoreData);
transform.validate();

const op = () => {
const op: PendingWriteOp = () => {
const document = DocumentSnapshot.fromObject(documentRef, firestoreData);

if (mergePaths) {
Expand Down Expand Up @@ -490,7 +492,7 @@ export class WriteBatch {

const documentMask = DocumentMask.fromUpdateMap(updateMap);

const op = () => {
const op: PendingWriteOp = () => {
const document = DocumentSnapshot.fromUpdateMap(documentRef, updateMap);
const write = document.toProto();
write!.updateMask = documentMask.toProto();
Expand Down Expand Up @@ -666,7 +668,7 @@ export class WriteBatch {
* Resets the WriteBatch and dequeues all pending operations.
* @private
*/
_reset() {
_reset(): void {
this._ops.splice(0);
this._committed = false;
}
Expand Down