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

buildGlobalParametersMap implementation & testing #5399

Merged
merged 3 commits into from
Jun 19, 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
2 changes: 1 addition & 1 deletion v-next/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export async function createHardhatRuntimeEnvironment(
}

export { resolvePluginList } from "./internal/plugins/resolve-plugin-list.js";
export { buildGlobalParameterMap } from "./internal/global-parameters.js";
export { buildGlobalParametersMap } from "./internal/global-parameters.js";
Copy link
Member

@alcuadrado alcuadrado Jun 19, 2024

Choose a reason for hiding this comment

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

@schaable We already discussed changing this name in a future PR, but just FYI, this was correct. It can be tricky for us, native Spanish speakers, but it's ParameterMap, not ParametsMap. You have to keep the first word in singular, even though the name represents a collection of those things — pretty much the opposite of what you'd do in Spanish.

38 changes: 20 additions & 18 deletions v-next/core/src/internal/global-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ParameterTypeToValueType } from "../types/common.js";
import type {
GlobalArguments,
GlobalParameter,
GlobalParameterMap,
GlobalParametersMap,
} from "../types/global-parameters.js";
import type { HardhatPlugin } from "../types/plugins.js";

Expand All @@ -23,39 +23,41 @@ import {
* shouldn't be consider validated. Hence, we should validate the global
* parameters.
*/
export function buildGlobalParameterMap(
export function buildGlobalParametersMap(
resolvedPlugins: HardhatPlugin[],
): GlobalParameterMap {
const globalParametersIndex: GlobalParameterMap = new Map();
): GlobalParametersMap {
const globalParametersMap: GlobalParametersMap = new Map();

for (const plugin of resolvedPlugins) {
if (plugin.globalParameters === undefined) {
continue;
}

for (const [name, param] of Object.entries(plugin.globalParameters)) {
// TODO: Validate name casing
// TODO: Validate default value matches with type
// TODO: Validate that the name is not one of the reserved ones in parameters.ts

const existingByName = globalParametersIndex.get(name);

for (const param of plugin.globalParameters) {
const existingByName = globalParametersMap.get(param.name);
if (existingByName !== undefined) {
throw new Error(
`Plugin ${plugin.id} is trying to define the global parameter ${name} but it is already defined by plugin ${existingByName.pluginId}`,
throw new HardhatError(
HardhatError.ERRORS.GENERAL.GLOBAL_PARAMETER_ALREADY_DEFINED,
{
plugin: plugin.id,
globalParameter: param.name,
definedByPlugin: existingByName.pluginId,
},
);
}

const indexEntry = {
const validatedGlobalParam = buildGlobalParameterDefinition(param);

const mapEntry = {
pluginId: plugin.id,
param,
param: validatedGlobalParam,
};

globalParametersIndex.set(param.name, indexEntry);
globalParametersMap.set(validatedGlobalParam.name, mapEntry);
}
}

return globalParametersIndex;
return globalParametersMap;
}

export function buildGlobalParameterDefinition<T extends ParameterType>({
Expand Down Expand Up @@ -104,7 +106,7 @@ export function buildGlobalParameterDefinition<T extends ParameterType>({

export function resolveGlobalArguments(
userProvidedGlobalArguments: Partial<GlobalArguments>,
_globalParametersMap: GlobalParameterMap,
_globalParametersMap: GlobalParametersMap,
): GlobalArguments {
// TODO: Validate the userProvidedGlobalArguments and get the remaining ones
// from env variables
Expand Down
18 changes: 9 additions & 9 deletions v-next/core/src/internal/hre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { UnsafeHardhatRuntimeEnvironmentOptions } from "../types/cli.js";
import type { HardhatUserConfig, HardhatConfig } from "../types/config.js";
import type {
GlobalArguments,
GlobalParameterMap,
GlobalParametersMap,
} from "../types/global-parameters.js";
import type {
HardhatUserConfigValidationError,
Expand All @@ -16,7 +16,7 @@ import type { UserInterruptionManager } from "../types/user-interruptions.js";

import { ResolvedConfigurationVariableImplementation } from "./configuration-variables.js";
import {
buildGlobalParameterMap,
buildGlobalParametersMap,
resolveGlobalArguments,
} from "./global-parameters.js";
import { HookManagerImplementation } from "./hook-manager.js";
Expand Down Expand Up @@ -85,13 +85,13 @@ export class HardhatRuntimeEnvironmentImplementation
plugins: resolvedPlugins,
};

const globalParametersIndex =
unsafeOptions?.globalParameterMap ??
buildGlobalParameterMap(resolvedPlugins);
const globalParametersMap =
unsafeOptions?.globalParametersMap ??
buildGlobalParametersMap(resolvedPlugins);

const globalArguments = resolveGlobalArguments(
userProvidedGlobalArguments,
globalParametersIndex,
globalParametersMap,
);

// Set the HookContext in the hook manager so that non-config hooks can
Expand All @@ -114,7 +114,7 @@ export class HardhatRuntimeEnvironmentImplementation
hooks,
interruptions,
globalArguments,
globalParametersIndex,
globalParametersMap,
);

await hooks.runSequentialHandlers("hre", "created", [hre]);
Expand All @@ -130,9 +130,9 @@ export class HardhatRuntimeEnvironmentImplementation
public readonly hooks: HookManager,
public readonly interruptions: UserInterruptionManager,
public readonly globalArguments: GlobalArguments,
globalParametersIndex: GlobalParameterMap,
globalParametersMap: GlobalParametersMap,
) {
this.tasks = new TaskManagerImplementation(this, globalParametersIndex);
this.tasks = new TaskManagerImplementation(this, globalParametersMap);
}
}

Expand Down
18 changes: 9 additions & 9 deletions v-next/core/src/internal/tasks/task-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GlobalParameterMap } from "../../types/global-parameters.js";
import type { GlobalParametersMap } from "../../types/global-parameters.js";
import type { HardhatRuntimeEnvironment } from "../../types/hre.js";
import type {
Task,
Expand All @@ -24,7 +24,7 @@ export class TaskManagerImplementation implements TaskManager {

constructor(
hre: HardhatRuntimeEnvironment,
globalParameterIndex: GlobalParameterMap,
globalParametersMap: GlobalParametersMap,
) {
this.#hre = hre;

Expand All @@ -36,7 +36,7 @@ export class TaskManagerImplementation implements TaskManager {

for (const taskDefinition of plugin.tasks) {
this.#reduceTaskDefinition(
globalParameterIndex,
globalParametersMap,
taskDefinition,
plugin.id,
);
Expand All @@ -45,7 +45,7 @@ export class TaskManagerImplementation implements TaskManager {

// reduce global user defined tasks
for (const taskDefinition of this.#hre.config.tasks) {
this.#reduceTaskDefinition(globalParameterIndex, taskDefinition);
this.#reduceTaskDefinition(globalParametersMap, taskDefinition);
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ export class TaskManagerImplementation implements TaskManager {
}

#reduceTaskDefinition(
globalParameterIndex: GlobalParameterMap,
globalParametersMap: GlobalParametersMap,
taskDefinition: TaskDefinition,
pluginId?: string,
) {
Expand All @@ -154,7 +154,7 @@ export class TaskManagerImplementation implements TaskManager {
}
case TaskDefinitionType.NEW_TASK: {
this.#validateClashesWithGlobalParams(
globalParameterIndex,
globalParametersMap,
taskDefinition,
pluginId,
);
Expand All @@ -174,7 +174,7 @@ export class TaskManagerImplementation implements TaskManager {
}
case TaskDefinitionType.TASK_OVERRIDE: {
this.#validateClashesWithGlobalParams(
globalParameterIndex,
globalParametersMap,
taskDefinition,
pluginId,
);
Expand All @@ -186,7 +186,7 @@ export class TaskManagerImplementation implements TaskManager {
}

#validateClashesWithGlobalParams(
globalParameterIndex: GlobalParameterMap,
globalParametersMap: GlobalParametersMap,
taskDefinition: NewTaskDefinition | TaskOverrideDefinition,
pluginId?: string,
) {
Expand All @@ -197,7 +197,7 @@ export class TaskManagerImplementation implements TaskManager {
: [];

[...namedParamNames, ...positionalParamNames].forEach((paramName) => {
const globalParamEntry = globalParameterIndex.get(paramName);
const globalParamEntry = globalParametersMap.get(paramName);
if (globalParamEntry !== undefined) {
throw new HardhatError(
HardhatError.ERRORS.TASK_DEFINITIONS.TASK_PARAMETER_ALREADY_DEFINED,
Expand Down
4 changes: 2 additions & 2 deletions v-next/core/src/types/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GlobalParameterMap } from "./global-parameters.js";
import type { GlobalParametersMap } from "./global-parameters.js";
import type { HardhatPlugin } from "./plugins.js";

/**
Expand All @@ -7,5 +7,5 @@ import type { HardhatPlugin } from "./plugins.js";
*/
export interface UnsafeHardhatRuntimeEnvironmentOptions {
resolvedPlugins?: HardhatPlugin[];
globalParameterMap?: GlobalParameterMap;
globalParametersMap?: GlobalParametersMap;
}
8 changes: 4 additions & 4 deletions v-next/core/src/types/global-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export interface GlobalParameter<T extends ParameterType = ParameterType> {
export interface GlobalArguments {}

/**
* An entry in the global parameter map.
* @see GlobalParameterMap
* An entry in the global parameters map.
* @see GlobalParametersMap
*/
export interface GlobalParameterMapEntry {
export interface GlobalParametersMapEntry {
pluginId: string;
param: GlobalParameter;
}

/**
* A map with all the `GlobalParameter`s and which plugin defined them.
*/
export type GlobalParameterMap = Map<string, GlobalParameterMapEntry>;
export type GlobalParametersMap = Map<string, GlobalParametersMapEntry>;
Loading
Loading