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

refactor: cleaner lint results; make anys explicit #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions packages/core/src/composition/compose/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
/*
* Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-disable @typescript-eslint/no-explicit-any */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@belsrc tagging you for final polish pass since most of this was written by yourself :D are we good remove the eslint-disable lines across the board?

import type { UnaryFunction } from '@/types';

// https://stackoverflow.com/questions/49310886/typing-compose-function-in-typescript-flow-compose#answer-73082627

// If its a list of functions, last being Unary
type ComposeParams<Fns> = Fns extends readonly [
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@belsrc similarly, are you able to give some justification / explanation for these ignores?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't most of these just be converted to unknowns?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC @belsrc mentioned some issues with using unknown

...any[],
infer Last extends UnaryFunction,
]
Expand All @@ -21,10 +34,12 @@ type Composable<Fn> =
Fn extends readonly [UnaryFunction]
? Fn
: // if its a list of Unary funcs (ignoring the first)
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
Fn extends readonly [any, ...infer Rest extends readonly UnaryFunction[]]
? // Start building the list of func type by using the return type of the first in Rest
// as the arg of the next in line and recursively spread the rest (doing the same thing)
// The first is ignored but handled by the top level ComposeReturn
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
readonly [(arg: ComposeReturn<Rest>) => any, ...Composable<Rest>]
: never;

Expand Down
20 changes: 17 additions & 3 deletions packages/core/src/composition/curry/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
// https://github.com/type-challenges/type-challenges/issues/15988
/*
* Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

import type { Callable } from '@/types';

// https://github.com/type-challenges/type-challenges/issues/15988
export type Curried<T extends unknown[], R> = <P extends Partial<T>>(
...args: P
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
) => ((...args: T) => any) extends (...args: [...P, ...infer Args]) => any
? Args extends []
? R
Expand All @@ -19,8 +32,9 @@ export type Curried<T extends unknown[], R> = <P extends Partial<T>>(
* curried(2)(3, 4);
* curried(2, 3, 4);
*/
export function autoCurry<T extends (...args: any[]) => any>(
export function autoCurry<T extends Callable>(
fn: T,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
_args = [] as any[],
): Curried<Parameters<T>, ReturnType<T>> {
return (...__args) =>
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/composition/pipe/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
/*
* Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-disable @typescript-eslint/no-explicit-any */
import type { UnaryFunction } from '@/types';

// If its a list of functions, last being Unary
type PipeParams<Fns> = Fns extends readonly [
infer First extends UnaryFunction,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
...any[],
]
? // Get Params of the first, which returns [...argTypes], so get the first one [0]
Expand All @@ -15,6 +28,7 @@ type PipeParams<Fns> = Fns extends readonly [
// have to spread and infer last so that it gets the right type for the last one
// [-1] no bueno
type PipeReturn<Fns> = ReturnType<
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
Fns extends readonly [...any[], infer Last extends UnaryFunction]
? Last
: never
Expand All @@ -25,10 +39,12 @@ type Pipeable<Fn> =
Fn extends readonly [UnaryFunction]
? Fn
: // if its a list of Unary funcs (ignoring the last)
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
Fn extends readonly [...infer Head extends readonly UnaryFunction[], any]
? // Start building the list of func type by using the return type of the last in Head
// as the arg of the previous in line and recursively spread the rest (doing the same thing)
// The last is ignored but handled by the top level FlowReturn
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
readonly [...Pipeable<Head>, (arg: PipeReturn<Head>) => any]
: never;

Expand Down
24 changes: 20 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
export type UnaryFunction = (x: any) => any;
/*
* Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

export type Accumulator<T, R> = (acc: R, x: T) => R;

export type ArrayElementType<T> = T extends (infer E)[] ? E : T;

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type Callable = (...all: any) => any;

export type Comparator<T> = (x: T) => boolean;

export type Predicate<T> = (x: T, idx?: number) => boolean;
export type MapFn<T, R> = (x: T, idx?: number) => R;

export type Accumulator<T, R> = (acc: R, x: T) => R;
export type Predicate<T> = (x: T, idx?: number) => boolean;

export type MapFn<T, R> = (x: T, idx?: number) => R;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type UnaryFunction = (x: any) => any;
18 changes: 6 additions & 12 deletions packages/core/src/utility/lookup/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Callable } from '@/types';
import { identity } from '../../combinators/i';

type Cache = Record<string | number | symbol, unknown>;

/**
* Takes an object and an optional fallback function and returns a function that
* takes a string and returns the lookup value or the result default fallback.
Expand All @@ -16,15 +19,6 @@ import { identity } from '../../combinators/i';
* colorLookup(data.value);
*/
export const lookup =
<
A extends Record<string | number | symbol, unknown>,
B extends (...args: any[]) => any,
>(
obj: A,
def?: B,
) =>
<C extends keyof A>(prop: string | number | symbol): A[C] => {
const fn = def ?? identity;

return fn(obj[prop]);
};
<A extends Cache, B extends Callable>(obj: A, def?: B) =>
<C extends keyof A>(prop: string | number | symbol): A[C] =>
(def ?? identity)(obj[prop]);
18 changes: 9 additions & 9 deletions packages/core/src/utility/once/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// TS' `Function` type only models the object side of it, not whether it is callable.
type SomeFunction = (...args: any[]) => any;
import type { Callable } from '@/types';

/**
* Ensures that the given function is only called once.
*/
export const once = <T extends SomeFunction>(fn: T) => {
export const once = <T extends Callable>(fn: T) => {
let done = false;

// TODO: Better types, since it can return void?
// biome-ignore lint/suspicious/noConfusingVoidType: <explanation>
return (...args: Parameters<T>): ReturnType<T> | void =>
// biome-ignore lint/suspicious/noAssignInExpressions: Shhhh
// biome-ignore lint/style/noCommaOperator: Shhh
done ? void 0 : ((done = true), fn(args));
return (...args: Parameters<T>): ReturnType<T> | undefined => {
if (!done) {
done = true;

return fn(...args);
}
};
};
Loading