Skip to content

Commit

Permalink
Enable recommended rule style/noUselessElse
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Oct 14, 2024
1 parent a04912c commit 655a4d1
Show file tree
Hide file tree
Showing 135 changed files with 900 additions and 979 deletions.
2 changes: 0 additions & 2 deletions biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
"deniedGlobals": ["fetch"]
}
},
// There are a lot of places we use if/else pattern. Should be fixed in an independent PR.
"noUselessElse": "off",
// We prefer to use `Math.pow` over `**` operator
"useExponentiationOperator": "off",
// In some cases the enums are initialized with values of other enums
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/utils/client/eventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
export async function getEventSource(): Promise<typeof EventSource> {
if (globalThis.EventSource) {
return EventSource;
} else {
return (await import("eventsource")).default as unknown as typeof EventSource;
}
return (await import("eventsource")).default as unknown as typeof EventSource;
}
28 changes: 11 additions & 17 deletions packages/api/src/utils/client/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,10 @@ export class HttpClient implements IHttpClient {

if (init.retries > 0) {
return this.requestWithRetries(definition, args, init);
} else {
return this.getRequestMethod(init)(definition, args, init);
}
} else {
return this.requestWithFallbacks(definition, args, localInit);
return this.getRequestMethod(init)(definition, args, init);
}
return this.requestWithFallbacks(definition, args, localInit);
}

/**
Expand Down Expand Up @@ -252,19 +250,16 @@ export class HttpClient implements IHttpClient {
});
if (res.ok) {
return res;
} else {
if (i >= this.urlsInits.length - 1) {
return res;
} else {
this.logger?.debug("Request error, retrying", {}, res.error() as Error);
}
}
if (i >= this.urlsInits.length - 1) {
return res;
}
this.logger?.debug("Request error, retrying", {}, res.error() as Error);
} catch (e) {
if (i >= this.urlsInits.length - 1) {
throw e;
} else {
this.logger?.debug("Request error, retrying", {}, e as Error);
}
this.logger?.debug("Request error, retrying", {}, e as Error);
}
}

Expand Down Expand Up @@ -391,14 +386,13 @@ export class HttpClient implements IHttpClient {
if (isAbortedError(e)) {
if (abortSignals.some((s) => s?.aborted)) {
throw new ErrorAborted(`${routeId} request`);
} else if (controller.signal.aborted) {
}
if (controller.signal.aborted) {
throw new TimeoutError(`${routeId} request`);
} else {
throw Error("Unknown aborted error");
}
} else {
throw e;
throw Error("Unknown aborted error");
}
throw e;
} finally {
timer?.();

Expand Down
6 changes: 2 additions & 4 deletions packages/api/src/utils/client/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ export class ApiResponse<E extends Endpoint> extends Response {
if (this.status === HttpStatusCode.NO_CONTENT) {
this._wireFormat = null;
return this._wireFormat;
} else {
throw Error("Content-Type header is required in response");
}
throw Error("Content-Type header is required in response");
}

const mediaType = parseContentTypeHeader(contentType);
Expand Down Expand Up @@ -197,9 +196,8 @@ export class ApiResponse<E extends Endpoint> extends Response {
return `${errJson.message}\n` + errJson.failures.map((e) => e.message).join("\n");
}
return errJson.message;
} else {
return errBody;
}
return errBody;
} catch (_e) {
return errBody || this.statusText;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/utils/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export const EmptyResponseCodec: ResponseCodec<EmptyResponseEndpoint> = {
export function ArrayOf<T>(elementType: Type<T>, limit = Infinity): ArrayType<Type<T>, unknown, unknown> {
if (isCompositeType(elementType)) {
return new ListCompositeType(elementType, limit) as unknown as ArrayType<Type<T>, unknown, unknown>;
} else if (isBasicType(elementType)) {
}
if (isBasicType(elementType)) {
return new ListBasicType(elementType, limit) as unknown as ArrayType<Type<T>, unknown, unknown>;
} else {
throw Error(`Unknown type ${elementType.typeName}`);
}
throw Error(`Unknown type ${elementType.typeName}`);
}

export function WithMeta<T, M extends {version: ForkName}>(getType: (m: M) => Type<T>): ResponseDataCodec<T, M> {
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/utils/serdes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export function querySerializeProofPathsArr(paths: JsonPath[]): string[] {
export function queryParseProofPathsArr(pathStrs: string | string[]): JsonPath[] {
if (Array.isArray(pathStrs)) {
return pathStrs.map((pathStr) => queryParseProofPaths(pathStr));
} else {
return [queryParseProofPaths(pathStrs)];
}
return [queryParseProofPaths(pathStrs)];
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/api/test/unit/client/httpClientFallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ describe("httpClient fallback", () => {
// which is handled separately from network errors
// but the fallback logic should be the same
return new Response(null, {status: 500});
} else {
throw Error(`test_error_server_${i}`);
}
} else {
return new Response(null, {status: 200});

throw Error(`test_error_server_${i}`);
}

return new Response(null, {status: 200});
});
});

Expand Down
4 changes: 3 additions & 1 deletion packages/api/test/utils/checkAgainstSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ type StringifiedProperty = string | StringifiedProperty[];
function stringifyProperty(value: unknown): StringifiedProperty {
if (typeof value === "number") {
return value.toString(10);
} else if (Array.isArray(value)) {
}

if (Array.isArray(value)) {
return value.map(stringifyProperty);
}
return String(value);
Expand Down
28 changes: 13 additions & 15 deletions packages/beacon-node/src/api/impl/beacon/blocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ export function getBeaconBlockApi({
const message = `Equivocation checks not yet implemented for broadcastValidation=${broadcastValidation}`;
if (chain.opts.broadcastValidationStrictness === "error") {
throw Error(message);
} else {
chain.logger.warn(message, valLogMeta);
}
chain.logger.warn(message, valLogMeta);
}
break;
}
Expand All @@ -193,9 +192,8 @@ export function getBeaconBlockApi({
const message = `Broadcast validation of ${broadcastValidation} type not implemented yet`;
if (chain.opts.broadcastValidationStrictness === "error") {
throw Error(message);
} else {
chain.logger.warn(message, valLogMeta);
}
chain.logger.warn(message, valLogMeta);
}
}

Expand Down Expand Up @@ -266,19 +264,19 @@ export function getBeaconBlockApi({

chain.logger.info("Publishing assembled block", {slot, blockRoot, source});
return publishBlock({signedBlockOrContents}, {...context, sszBytes: null}, opts);
} else {
const source = ProducedBlockSource.builder;
chain.logger.debug("Reconstructing signedBlockOrContents", {slot, blockRoot, source});
}

const signedBlockOrContents = await reconstructBuilderBlockOrContents(chain, signedBlindedBlock);
const source = ProducedBlockSource.builder;
chain.logger.debug("Reconstructing signedBlockOrContents", {slot, blockRoot, source});

// the full block is published by relay and it's possible that the block is already known to us
// by gossip
//
// see: https://github.com/ChainSafe/lodestar/issues/5404
chain.logger.info("Publishing assembled block", {slot, blockRoot, source});
return publishBlock({signedBlockOrContents}, {...context, sszBytes: null}, {...opts, ignoreIfKnown: true});
}
const signedBlockOrContents = await reconstructBuilderBlockOrContents(chain, signedBlindedBlock);

// the full block is published by relay and it's possible that the block is already known to us
// by gossip
//
// see: https://github.com/ChainSafe/lodestar/issues/5404
chain.logger.info("Publishing assembled block", {slot, blockRoot, source});
return publishBlock({signedBlockOrContents}, {...context, sszBytes: null}, {...opts, ignoreIfKnown: true});
};

return {
Expand Down
4 changes: 3 additions & 1 deletion packages/beacon-node/src/api/impl/beacon/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ export function getBeaconStateApi({
data: validatorResponses,
meta: {executionOptimistic, finalized},
};
} else if (statuses.length) {
}

if (statuses.length) {
const validatorsByStatus = filterStateValidatorsByStatus(statuses, state, pubkey2index, currentEpoch);
return {
data: validatorsByStatus,
Expand Down
3 changes: 1 addition & 2 deletions packages/beacon-node/src/api/impl/lodestar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ function stringifyKeys(keys: (Uint8Array | number | string)[]): string[] {
return keys.map((key) => {
if (key instanceof Uint8Array) {
return toHex(key);
} else {
return `${key}`;
}
return `${key}`;
});
}
5 changes: 2 additions & 3 deletions packages/beacon-node/src/api/impl/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ export function getNodeApi(
if (isSyncing || isOptimistic || elOffline) {
// 206: Node is syncing but can serve incomplete data
return {status: syncingStatus ?? routes.node.NodeHealth.SYNCING};
} else {
// 200: Node is ready
return {status: routes.node.NodeHealth.READY};
}
// 200: Node is ready
return {status: routes.node.NodeHealth.READY};
// else {
// 503: Node not initialized or having issues
// NOTE: Lodestar does not start its API until fully initialized, so this status can never be served
Expand Down
Loading

0 comments on commit 655a4d1

Please sign in to comment.