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

Improve the types of task arguments #5527

Merged
merged 5 commits into from
Jul 28, 2024
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
127 changes: 89 additions & 38 deletions v-next/core/src/internal/tasks/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {
TaskOverrideDefinition,
EmptyTaskDefinitionBuilder,
EmptyTaskDefinition,
ExtendTaskArguments,
TaskArguments,
} from "../../types/tasks.js";

import { HardhatError } from "@ignored/hardhat-vnext-errors";
Expand Down Expand Up @@ -55,8 +57,9 @@ export class EmptyTaskDefinitionBuilderImplementation
}
}

export class NewTaskDefinitionBuilderImplementation
implements NewTaskDefinitionBuilder
export class NewTaskDefinitionBuilderImplementation<
TaskArgumentsT extends TaskArguments = TaskArguments,
> implements NewTaskDefinitionBuilder<TaskArgumentsT>
{
readonly #id: string[];
readonly #usedNames: Set<string> = new Set();
Expand All @@ -66,7 +69,7 @@ export class NewTaskDefinitionBuilderImplementation

#description: string;

#action?: NewTaskActionFunction | string;
#action?: NewTaskActionFunction<TaskArgumentsT> | string;

constructor(id: string | string[], description: string = "") {
validateId(id);
Expand All @@ -80,25 +83,32 @@ export class NewTaskDefinitionBuilderImplementation
return this;
}

public setAction(action: NewTaskActionFunction | string): this {
public setAction(
action: NewTaskActionFunction<TaskArgumentsT> | string,
): this {
validateAction(action);

this.#action = action;

return this;
}

public addOption<T extends ArgumentType>({
public addOption<
NameT extends string,
TypeT extends ArgumentType = ArgumentType.STRING,
>({
name,
description = "",
type,
defaultValue,
}: {
name: string;
name: NameT;
description?: string;
type?: T;
defaultValue: ArgumentTypeToValueType<T>;
}): this {
type?: TypeT;
defaultValue: ArgumentTypeToValueType<TypeT>;
}): NewTaskDefinitionBuilder<
ExtendTaskArguments<NameT, TypeT, TaskArgumentsT>
> {
const argumentType = type ?? ArgumentType.STRING;

const optionDefinition = {
Expand All @@ -115,32 +125,47 @@ export class NewTaskDefinitionBuilderImplementation
return this;
}

public addFlag(flagConfig: { name: string; description?: string }): this {
public addFlag<NameT extends string>(flagConfig: {
name: NameT;
description?: string;
}): NewTaskDefinitionBuilder<
ExtendTaskArguments<NameT, ArgumentType.BOOLEAN, TaskArgumentsT>
> {
return this.addOption({
...flagConfig,
type: ArgumentType.BOOLEAN,
defaultValue: false,
});
}

public addPositionalArgument<T extends ArgumentType>(argConfig: {
name: string;
public addPositionalArgument<
NameT extends string,
TypeT extends ArgumentType = ArgumentType.STRING,
>(argConfig: {
name: NameT;
description?: string;
type?: T;
defaultValue?: ArgumentTypeToValueType<T>;
}): this {
type?: TypeT;
defaultValue?: ArgumentTypeToValueType<TypeT>;
}): NewTaskDefinitionBuilder<
ExtendTaskArguments<NameT, TypeT, TaskArgumentsT>
> {
return this.#addPositionalArgument({
...argConfig,
isVariadic: false,
});
}

public addVariadicArgument<T extends ArgumentType>(argConfig: {
name: string;
public addVariadicArgument<
NameT extends string,
TypeT extends ArgumentType = ArgumentType.STRING,
>(argConfig: {
name: NameT;
description?: string;
type?: T;
defaultValue?: Array<ArgumentTypeToValueType<T>>;
}): this {
type?: TypeT;
defaultValue?: Array<ArgumentTypeToValueType<TypeT>>;
}): NewTaskDefinitionBuilder<
ExtendTaskArguments<NameT, TypeT, TaskArgumentsT>
> {
return this.#addPositionalArgument({
...argConfig,
isVariadic: true,
Expand All @@ -158,27 +183,36 @@ export class NewTaskDefinitionBuilderImplementation
type: TaskDefinitionType.NEW_TASK,
id: this.#id,
description: this.#description,
action: this.#action,
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
-- The type of the action is narrowed in the setAction function to
improve the argument types. Once the task is built, we use the more
general type to avoid having to parameterize the NewTaskDefinition */
action: this.#action as NewTaskActionFunction,
options: this.#options,
positionalArguments: this.#positionalArgs,
};
}

#addPositionalArgument<T extends ArgumentType>({
#addPositionalArgument<
NameT extends string,
TypeT extends ArgumentType = ArgumentType.STRING,
>({
name,
description = "",
type,
defaultValue,
isVariadic,
}: {
name: string;
name: NameT;
description?: string;
type?: T;
type?: TypeT;
defaultValue?:
| ArgumentTypeToValueType<T>
| Array<ArgumentTypeToValueType<T>>;
| ArgumentTypeToValueType<TypeT>
| Array<ArgumentTypeToValueType<TypeT>>;
isVariadic: boolean;
}): this {
}): NewTaskDefinitionBuilder<
ExtendTaskArguments<NameT, TypeT, TaskArgumentsT>
> {
const argumentType = type ?? ArgumentType.STRING;

const positionalArgDef = {
Expand All @@ -203,16 +237,17 @@ export class NewTaskDefinitionBuilderImplementation
}
}

export class TaskOverrideDefinitionBuilderImplementation
implements TaskOverrideDefinitionBuilder
export class TaskOverrideDefinitionBuilderImplementation<
TaskArgumentsT extends TaskArguments = TaskArguments,
> implements TaskOverrideDefinitionBuilder<TaskArgumentsT>
{
readonly #id: string[];

readonly #options: Record<string, OptionDefinition> = {};

#description?: string;

#action?: TaskOverrideActionFunction | string;
#action?: TaskOverrideActionFunction<TaskArgumentsT> | string;

constructor(id: string | string[]) {
validateId(id);
Expand All @@ -225,25 +260,32 @@ export class TaskOverrideDefinitionBuilderImplementation
return this;
}

public setAction(action: TaskOverrideActionFunction | string): this {
public setAction(
action: TaskOverrideActionFunction<TaskArgumentsT> | string,
): this {
validateAction(action);

this.#action = action;

return this;
}

public addOption<T extends ArgumentType>({
public addOption<
NameT extends string,
TypeT extends ArgumentType = ArgumentType.STRING,
>({
name,
description = "",
type,
defaultValue,
}: {
name: string;
name: NameT;
description?: string;
type?: T;
defaultValue: ArgumentTypeToValueType<T>;
}): this {
type?: TypeT;
defaultValue: ArgumentTypeToValueType<TypeT>;
}): TaskOverrideDefinitionBuilder<
ExtendTaskArguments<NameT, TypeT, TaskArgumentsT>
> {
const argumentType = type ?? ArgumentType.STRING;

const optionDefinition = {
Expand All @@ -264,7 +306,12 @@ export class TaskOverrideDefinitionBuilderImplementation
return this;
}

public addFlag(flagConfig: { name: string; description?: string }): this {
public addFlag<NameT extends string>(flagConfig: {
name: string;
description?: string;
}): TaskOverrideDefinitionBuilder<
ExtendTaskArguments<NameT, ArgumentType.BOOLEAN, TaskArgumentsT>
> {
return this.addOption({
...flagConfig,
type: ArgumentType.BOOLEAN,
Expand All @@ -283,7 +330,11 @@ export class TaskOverrideDefinitionBuilderImplementation
type: TaskDefinitionType.TASK_OVERRIDE,
id: this.#id,
description: this.#description,
action: this.#action,
/* eslint-disable-next-line @typescript-eslint/consistent-type-assertions
-- The type of the action is narrowed in the setAction function to
improve the argument types. Once the task is built, we use the more
general type to avoid having to parameterize the TaskOverrideDefinition */
action: this.#action as TaskOverrideActionFunction,
options: this.#options,
};
}
Expand Down
Loading