chore: Turn on @typescript-eslint/strict-boolean-expressions

- Fix some bugs about bad conditions

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-06-01 09:27:44 -04:00
1 parent d3f45bfe49
commit 1675c56e59
165 files changed
+853 -1141

No files matched your search

@@ -13,7 +13,7 @@ import type { Logger } from "@k8slens/logger";
import type Fetch from "@k8slens/node-fetch";
import type { RequestInit, Response } from "@k8slens/node-fetch";
import type { Defaulted } from "@k8slens/utilities";
import { isArray, isObject, isString, json } from "@k8slens/utilities";
import { isDefined, isArray, isObject, isString, json } from "@k8slens/utilities";
export interface JsonApiError {
code?: number;
@@ -150,7 +150,7 @@ export class JsonApi<Data = unknown, Params extends { data?: unknown } = { data?
);
const { query } = params ?? {};
if (query && Object.keys(query).length > 0) {
if (isObject(query) && Object.keys(query).length > 0) {
const queryString = stringify(query as unknown as QueryParams);
reqUrl += (reqUrl.includes("?") ? "&" : "?") + queryString;
@@ -206,13 +206,13 @@ export class JsonApi<Data = unknown, Params extends { data?: unknown } = { data?
) {
let reqUrl = `${this.config.serverAddress}${this.config.apiBase}${path}`;
const reqInit = merge({}, this.reqInit, await this.getRequestOptions(), init);
const { data, query } = params || {};
const { data, query } = params ?? {};
if (data && !reqInit.body) {
if (isDefined(data) && reqInit.body == null) {
reqInit.body = JSON.stringify(data);
}
if (query && Object.keys(query).length > 0) {
if (isObject(query) && Object.keys(query).length > 0) {
const queryString = stringify(query as unknown as QueryParams);
reqUrl += (reqUrl.includes("?") ? "&" : "?") + queryString;
@@ -65,6 +65,10 @@ export function parseKubeApi(path: string | undefined): IKubeApiParsed | undefin
const { apiOrApis, apiGroup, namespace, apiVersion, resource, name } = parsedPath;
if (!apiOrApis || !apiGroup || !apiVersion || !resource) {
return undefined;
}
return {
apiBase: getApiBase(apiOrApis, apiGroup, apiVersion, resource),
apiPrefix: getApiPrefix(apiOrApis),
@@ -487,11 +487,11 @@ export class KubeApi<
}
protected normalizeQuery(query: Partial<KubeApiQueryParams> = {}) {
if (query.labelSelector) {
if (query.labelSelector !== undefined) {
query.labelSelector = [query.labelSelector].flat().join(",");
}
if (query.fieldSelector) {
if (query.fieldSelector !== undefined) {
query.fieldSelector = [query.fieldSelector].flat().join(",");
}
@@ -499,7 +499,7 @@ export class KubeApi<
}
protected parseResponse(data: unknown, namespace?: string): Kube | Kube[] | null {
if (!data) {
if (!Boolean(data)) {
return null;
}
@@ -4,6 +4,7 @@
*/
import { runInAction } from "mobx";
import type { Simplify, ValueOf } from "type-fest";
import { inspect } from "util";
import { isDefined } from "./type-narrowing";
@@ -141,3 +142,16 @@ export function toggle<K>(set: Set<K>, key: K): void {
export function includes<T>(src: T[], value: T | null | undefined): boolean {
return isDefined(value) && src.includes(value);
}
/**
* Attempt to get a value from an object where the key may not exist on the object
* @param obj An object to key into
* @param key Some key
*/
export function maybeGet<T extends object>(obj: T, key: string): Simplify<ValueOf<T>> | undefined {
if (key in obj) {
return (obj as Record<string, ValueOf<T>>)[key];
}
return undefined;
}
@@ -4,6 +4,7 @@
*/
import { getOrInsert } from "./collection-functions";
import type { Tuple } from "./tuple";
export type Falsy = false | 0 | "" | null | undefined;
@@ -288,11 +289,30 @@ function nFircate<T>(from: Iterable<T>, field: keyof T, parts: T[typeof field][]
for (const item of from) {
const index = parts.indexOf(item[field]);
if (index < 0) {
continue;
res[index]?.push(item);
}
return res;
}
function chunks<T, ChunkSize extends number>(src: Iterable<T>, size: ChunkSize): Tuple<T, ChunkSize>[] {
const res: Tuple<T, ChunkSize>[] = [];
const iterating = src[Symbol.iterator]();
top: for (;;) {
const item: T[] = [];
for (let i = 0; i < size; i += 1) {
const result = iterating.next();
if (result.done === true) {
break top;
}
item.push(result.value);
}
res[index].push(item);
res.push(item as Tuple<T, ChunkSize>);
}
return res;
@@ -300,6 +320,7 @@ function nFircate<T>(from: Iterable<T>, field: keyof T, parts: T[typeof field][]
export const iter = {
chain,
chunks,
concat,
every,
filter,
@@ -311,8 +332,8 @@ export const iter = {
flatMap,
join,
map,
nFircate,
newEmpty,
nFircate,
nth,
reduce,
take,
@@ -22,7 +22,7 @@ type TupleOfImpl<T, N extends number, R extends unknown[]> = R["length"] extends
* is empty. Then it returns a tuple with the rest of each of tuples
* @param sources The source arrays
* @yields A tuple of the next element from each of the sources
* @returns The tuple of all the sources as soon as at least one of the sources is exausted
* @returns The tuple of all the sources as soon as at least one of the sources is exhausted
*/
function zip<T>(...sources: Tuple<T[], 0>): Iterator<Tuple<T, 0>, Tuple<T[], 0>>;
function zip<T>(...sources: Tuple<T[], 1>): Iterator<Tuple<T, 1>, Tuple<T[], 1>>;
@@ -5,6 +5,8 @@
import type { ExecException, ExecFileException } from "child_process";
import type { IncomingMessage } from "http";
import type { Falsy } from "./iter";
import type { Tuple } from "./tuple";
/**
* Narrows `val` to include the property `key` (if true is returned)
@@ -77,6 +79,18 @@ export function isTypedArray<T>(val: unknown, isEntry: (entry: unknown) => entry
return Array.isArray(val) && val.every(isEntry);
}
export function hasLengthGreaterThan<T, Len extends number>(x: T[], len: Len): x is Tuple<T, Len> & T[] {
return x.length > len;
}
export function isTruthy<T>(x: T): x is Exclude<T, Falsy> {
return !!(x as unknown);
}
export function isFalsy<T>(x: T): x is Extract<T, Falsy> {
return !(x as unknown);
}
/**
* checks if val is of type string
* @param val the value to be checked