Skip to content

Commit

Permalink
Fix some eslint checks present in mozilla-central (#2051)
Browse files Browse the repository at this point in the history
* Enable no-else-return

* Enable object-shorthand

* Fix mozilla/prefer-boolean-length-check

* Enable dot-notation

* Enable consistent-return

* Fix prettier

* Disable dot-notation on test files
  • Loading branch information
leplatrem committed Jun 20, 2023
1 parent 3cc62df commit 49c5885
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 41 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
],
"no-var": 2,
"prefer-const": 1,
"semi": [2, "always"]
"semi": [2, "always"],
"no-else-return": "error",
"object-shorthand": ["error", "always"],
"dot-notation": "error",
"consistent-return": "error"
},
"env": {
"es6": true,
Expand Down
2 changes: 1 addition & 1 deletion src/KintoBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class KintoBase<
*/
static get adapters() {
return {
BaseAdapter: BaseAdapter,
BaseAdapter,
};
}

Expand Down
12 changes: 8 additions & 4 deletions src/adapters/IDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ export default class IDB<

// No option to preload records, go straight to `callback`.
if (!options.preload) {
return runCallback();
runCallback();
return;
}

// Preload specified records using a list request.
Expand All @@ -538,7 +539,7 @@ export default class IDB<
// Store obtained records by id.
const preloaded: { [key: string]: KintoObject } = {};
for (const record of records) {
delete record["_cid"];
delete record._cid;
preloaded[record.id] = record;
}
runCallback(preloaded);
Expand All @@ -556,7 +557,7 @@ export default class IDB<
* @param {String} id The record id.
* @return {Promise}
*/
async get(id: string): Promise<B | undefined> {
async get(id: string): Promise<B | null> {
try {
let record: B;
await this.prepare("records", (store) => {
Expand All @@ -567,6 +568,7 @@ export default class IDB<
} catch (e: any) {
this._handleError("get", e);
}
return null;
}

/**
Expand All @@ -589,7 +591,7 @@ export default class IDB<
// we have received all requested records that match the filters,
// we now park them within current scope and hide the `_cid` attribute.
for (const result of _results) {
delete result["_cid"];
delete result._cid;
}
results = _results;
});
Expand Down Expand Up @@ -722,6 +724,7 @@ export default class IDB<
return metadata;
} catch (e: any) {
this._handleError("saveMetadata", e);
return null;
}
}

Expand All @@ -735,6 +738,7 @@ export default class IDB<
return entry ? (entry as { metadata: any }).metadata : null;
} catch (e: any) {
this._handleError("getMetadata", e);
return null;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ export class SyncResultObject {
}

get errors() {
return this._lists["errors"];
return this._lists.errors;
}

get conflicts() {
return this._lists["conflicts"];
return this._lists.conflicts;
}

get skipped() {
Expand Down Expand Up @@ -328,7 +328,7 @@ function importChange<
}
return {
type: "conflicts",
data: { type: "incoming", local: local, remote: remote },
data: { type: "incoming", local, remote },
};
}
// Local record was synced.
Expand Down
5 changes: 2 additions & 3 deletions src/http/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ export default class KintoClientBase {
const serverSettings = await this.fetchServerSettings({
retry: this._getRetry(options),
});
const maxRequests = serverSettings["batch_max_requests"];
const maxRequests = serverSettings.batch_max_requests;
if (maxRequests && requests.length > maxRequests) {
const chunks = partition(requests, maxRequests);
const results = [];
Expand Down Expand Up @@ -515,9 +515,8 @@ export default class KintoClientBase {
const responses = await this._batchRequests(rootBatch._requests, options);
if (options.aggregate) {
return aggregate(responses, rootBatch._requests);
} else {
return responses;
}
return responses;
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/http/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,11 @@ export default class Collection {
const path = this._endpoints.record(this.bucket.name, this.name);
if (options.at) {
return this.getSnapshot<T>(options.at);
} else {
return this.client.paginatedList<T>(path, options, {
headers: this._getHeaders(options),
retry: this._getRetry(options),
});
}
return this.client.paginatedList<T>(path, options, {
headers: this._getHeaders(options),
retry: this._getRetry(options),
});
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,8 @@ export default class HTTP {
// If number of allowed of retries is not exhausted, retry the same request.
if (retryAfter && options.retry > 0) {
return this.retry<T>(url, retryAfter, request, options);
} else {
return this.processResponse<T>(response);
}
return this.processResponse<T>(response);
}

_checkForDeprecationHeader(headers: FetchHeaders): void {
Expand Down Expand Up @@ -246,10 +245,10 @@ export default class HTTP {
}
}

_checkForRetryAfterHeader(headers: FetchHeaders): number | undefined {
_checkForRetryAfterHeader(headers: FetchHeaders): number | null {
const retryAfter = headers.get("Retry-After");
if (!retryAfter) {
return;
return null;
}
const delay = parseInt(retryAfter, 10) * 1000;
const tryAgainAfter = new Date().getTime() + delay;
Expand Down
8 changes: 3 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ function makeNestedObjectFromArr(
return (acc[cv] = val);
} else if (Object.prototype.hasOwnProperty.call(acc, cv)) {
return acc[cv];
} else {
return (acc[cv] = {});
}
return (acc[cv] = {});
}, nestedFiltersObj);
}

Expand Down Expand Up @@ -266,9 +265,8 @@ export function qsify(obj: { [key: string]: any }): string {
const ks = encode(k) + "=";
if (Array.isArray(stripped[k])) {
return ks + stripped[k].map((v: any) => encode(v)).join(",");
} else {
return ks + encode(stripped[k]);
}
return ks + encode(stripped[k]);
})
.join("&");
}
Expand Down Expand Up @@ -376,7 +374,7 @@ export function capable(capabilities: string[]): DecoratorReturn {
.fetchServerCapabilities()
.then((available: string[]) => {
const missing = capabilities.filter((c) => !(c in available));
if (missing.length > 0) {
if (missing.length) {
const missingStr = missing.join(", ");
throw new Error(
`Required capabilities ${missingStr} not present on server`
Expand Down
1 change: 1 addition & 0 deletions test/adapters/IDB_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint dot-notation: off */
import sinon from "sinon";

import IDB, { open, execute } from "../../src/adapters/IDB";
Expand Down
27 changes: 14 additions & 13 deletions test/collection_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint dot-notation: off */
import sinon from "sinon";
import { EventEmitter } from "events";
import { v4 as uuid4 } from "uuid";
Expand Down Expand Up @@ -197,7 +198,7 @@ describe("Collection", () => {
TEST_COLLECTION_NAME,
{ api } as unknown as KintoBase<any>,
{
adapter: function () {},
adapter() {},
} as any
);
}).to.Throw(Error, /Unsupported adapter/);
Expand Down Expand Up @@ -344,7 +345,7 @@ describe("Collection", () => {
TEST_COLLECTION_NAME,
{ api } as unknown as KintoBase<any>,
{
idSchema: idSchema,
idSchema,
}
);
}
Expand Down Expand Up @@ -1056,8 +1057,8 @@ describe("Collection", () => {
};
conflict = {
type: "incoming",
local: local,
remote: remote,
local,
remote,
};
});

Expand Down Expand Up @@ -1144,7 +1145,7 @@ describe("Collection", () => {
const res_1 = await articles.get(id, { includeDeleted: true });
res_1.data.should.deep.equal({
_status: "deleted",
id: id,
id,
title: "foo",
url: "http://foo",
});
Expand Down Expand Up @@ -1176,7 +1177,7 @@ describe("Collection", () => {
const res_1 = await articles.getAny(id);
res_1.data.should.deep.equal({
_status: "deleted",
id: id,
id,
title: "foo",
url: "http://foo",
});
Expand Down Expand Up @@ -1740,7 +1741,7 @@ describe("Collection", () => {
});
const id = uuid4();
await articles.create(
{ id: id, title: "some title" },
{ id, title: "some title" },
{ synced: true }
);
await articles.delete(id);
Expand Down Expand Up @@ -2173,7 +2174,7 @@ describe("Collection", () => {
const id = uuid4();
listRecords.returns(
Promise.resolve({
data: [{ id: id, deleted: true }],
data: [{ id, deleted: true }],
next: () => {},
last_modified: "42",
})
Expand Down Expand Up @@ -2264,8 +2265,8 @@ describe("Collection", () => {
const remote = { ...local, title: "blah", last_modified: 42 };
const conflict = {
type: "incoming" as const,
local: local,
remote: remote,
local,
remote,
};
const resolution = { ...local, title: "resolved" };
sandbox
Expand Down Expand Up @@ -2588,9 +2589,9 @@ describe("Collection", () => {
updateRecord: sinon.SinonExpectation;
beforeEach(() => {
batch = {
deleteRecord: function () {},
createRecord: function () {},
updateRecord: function () {},
deleteRecord() {},
createRecord() {},
updateRecord() {},
};
batchSpy = sandbox.mock(batch);
deleteRecord = batchSpy.expects("deleteRecord");
Expand Down
2 changes: 2 additions & 0 deletions test/http/api_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint dot-notation: off */
import sinon from "sinon";
import { EventEmitter } from "events";
import { fakeServerResponse, Stub, expectAsyncError } from "../test_utils";
Expand Down Expand Up @@ -743,6 +744,7 @@ describe("KintoClient", () => {
if (name === "ETag") {
return ETag;
}
return "";
},
},
})
Expand Down
1 change: 1 addition & 0 deletions test/http/bucket_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint dot-notation: off */
import sinon from "sinon";
import KintoClient from "../../src/http";
import Bucket, { BucketOptions } from "../../src/http/bucket";
Expand Down
1 change: 1 addition & 0 deletions test/http/collection_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint dot-notation: off */
import sinon from "sinon";
import KintoClient from "../../src/http";
import Bucket from "../../src/http/bucket";
Expand Down
2 changes: 2 additions & 0 deletions test/http/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe("HTTP class", () => {
if (name !== "Alert") {
return "fake";
}
return "";
},
},
text() {
Expand Down Expand Up @@ -267,6 +268,7 @@ describe("HTTP class", () => {
if (name === "Content-Length") {
return 0;
}
return "";
},
},
text() {
Expand Down
2 changes: 1 addition & 1 deletion test/http/integration_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe("HTTP Integration tests", function (__test) {

after(() => {
if (skipLocalServer) {
return;
return Promise.resolve();
}
const logLines = server.logs.toString().split("\n");
const serverDidCrash = logLines.some((l) => l.startsWith("Traceback"));
Expand Down
1 change: 1 addition & 0 deletions test/index_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint dot-notation: off */
import sinon from "sinon";
import { EventEmitter } from "events";
import { SUPPORTED_PROTOCOL_VERSION as SPV } from "../src/http";
Expand Down
1 change: 1 addition & 0 deletions test/integration_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint dot-notation: off */
import { v4 as uuid4 } from "uuid";
import sinon from "sinon";
import KintoServer from "kinto-node-test-server";
Expand Down
2 changes: 1 addition & 1 deletion test/test_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function fakeServerResponse(
respHeaders.set("Content-Length", JSON.stringify(json).length.toString());
}
return Promise.resolve({
status: status,
status,
statusText: status === 200 ? "OK" : "Error",
headers: respHeaders,
text() {
Expand Down

0 comments on commit 49c5885

Please sign in to comment.