Skip to content

Commit 228fcd1

Browse files
authored
Merge pull request #5399 from NomicFoundation/build-global-param
buildGlobalParametersMap implementation & testing
2 parents 4b68018 + 0e1ba7a commit 228fcd1

File tree

10 files changed

+222
-61
lines changed

10 files changed

+222
-61
lines changed

v-next/core/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ export async function createHardhatRuntimeEnvironment(
2828
}
2929

3030
export { resolvePluginList } from "./internal/plugins/resolve-plugin-list.js";
31-
export { buildGlobalParameterMap } from "./internal/global-parameters.js";
31+
export { buildGlobalParametersMap } from "./internal/global-parameters.js";

v-next/core/src/internal/global-parameters.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ParameterTypeToValueType } from "../types/common.js";
22
import type {
33
GlobalArguments,
44
GlobalParameter,
5-
GlobalParameterMap,
5+
GlobalParametersMap,
66
} from "../types/global-parameters.js";
77
import type { HardhatPlugin } from "../types/plugins.js";
88

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

3131
for (const plugin of resolvedPlugins) {
3232
if (plugin.globalParameters === undefined) {
3333
continue;
3434
}
3535

36-
for (const [name, param] of Object.entries(plugin.globalParameters)) {
37-
// TODO: Validate name casing
38-
// TODO: Validate default value matches with type
39-
// TODO: Validate that the name is not one of the reserved ones in parameters.ts
40-
41-
const existingByName = globalParametersIndex.get(name);
42-
36+
for (const param of plugin.globalParameters) {
37+
const existingByName = globalParametersMap.get(param.name);
4338
if (existingByName !== undefined) {
44-
throw new Error(
45-
`Plugin ${plugin.id} is trying to define the global parameter ${name} but it is already defined by plugin ${existingByName.pluginId}`,
39+
throw new HardhatError(
40+
HardhatError.ERRORS.GENERAL.GLOBAL_PARAMETER_ALREADY_DEFINED,
41+
{
42+
plugin: plugin.id,
43+
globalParameter: param.name,
44+
definedByPlugin: existingByName.pluginId,
45+
},
4646
);
4747
}
4848

49-
const indexEntry = {
49+
const validatedGlobalParam = buildGlobalParameterDefinition(param);
50+
51+
const mapEntry = {
5052
pluginId: plugin.id,
51-
param,
53+
param: validatedGlobalParam,
5254
};
5355

54-
globalParametersIndex.set(param.name, indexEntry);
56+
globalParametersMap.set(validatedGlobalParam.name, mapEntry);
5557
}
5658
}
5759

58-
return globalParametersIndex;
60+
return globalParametersMap;
5961
}
6062

6163
export function buildGlobalParameterDefinition<T extends ParameterType>({
@@ -104,7 +106,7 @@ export function buildGlobalParameterDefinition<T extends ParameterType>({
104106

105107
export function resolveGlobalArguments(
106108
userProvidedGlobalArguments: Partial<GlobalArguments>,
107-
_globalParametersMap: GlobalParameterMap,
109+
_globalParametersMap: GlobalParametersMap,
108110
): GlobalArguments {
109111
// TODO: Validate the userProvidedGlobalArguments and get the remaining ones
110112
// from env variables

v-next/core/src/internal/hre.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { UnsafeHardhatRuntimeEnvironmentOptions } from "../types/cli.js";
22
import type { HardhatUserConfig, HardhatConfig } from "../types/config.js";
33
import type {
44
GlobalArguments,
5-
GlobalParameterMap,
5+
GlobalParametersMap,
66
} from "../types/global-parameters.js";
77
import type {
88
HardhatUserConfigValidationError,
@@ -16,7 +16,7 @@ import type { UserInterruptionManager } from "../types/user-interruptions.js";
1616

1717
import { ResolvedConfigurationVariableImplementation } from "./configuration-variables.js";
1818
import {
19-
buildGlobalParameterMap,
19+
buildGlobalParametersMap,
2020
resolveGlobalArguments,
2121
} from "./global-parameters.js";
2222
import { HookManagerImplementation } from "./hook-manager.js";
@@ -85,13 +85,13 @@ export class HardhatRuntimeEnvironmentImplementation
8585
plugins: resolvedPlugins,
8686
};
8787

88-
const globalParametersIndex =
89-
unsafeOptions?.globalParameterMap ??
90-
buildGlobalParameterMap(resolvedPlugins);
88+
const globalParametersMap =
89+
unsafeOptions?.globalParametersMap ??
90+
buildGlobalParametersMap(resolvedPlugins);
9191

9292
const globalArguments = resolveGlobalArguments(
9393
userProvidedGlobalArguments,
94-
globalParametersIndex,
94+
globalParametersMap,
9595
);
9696

9797
// Set the HookContext in the hook manager so that non-config hooks can
@@ -114,7 +114,7 @@ export class HardhatRuntimeEnvironmentImplementation
114114
hooks,
115115
interruptions,
116116
globalArguments,
117-
globalParametersIndex,
117+
globalParametersMap,
118118
);
119119

120120
await hooks.runSequentialHandlers("hre", "created", [hre]);
@@ -130,9 +130,9 @@ export class HardhatRuntimeEnvironmentImplementation
130130
public readonly hooks: HookManager,
131131
public readonly interruptions: UserInterruptionManager,
132132
public readonly globalArguments: GlobalArguments,
133-
globalParametersIndex: GlobalParameterMap,
133+
globalParametersMap: GlobalParametersMap,
134134
) {
135-
this.tasks = new TaskManagerImplementation(this, globalParametersIndex);
135+
this.tasks = new TaskManagerImplementation(this, globalParametersMap);
136136
}
137137
}
138138

v-next/core/src/internal/tasks/task-manager.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GlobalParameterMap } from "../../types/global-parameters.js";
1+
import type { GlobalParametersMap } from "../../types/global-parameters.js";
22
import type { HardhatRuntimeEnvironment } from "../../types/hre.js";
33
import type {
44
Task,
@@ -24,7 +24,7 @@ export class TaskManagerImplementation implements TaskManager {
2424

2525
constructor(
2626
hre: HardhatRuntimeEnvironment,
27-
globalParameterIndex: GlobalParameterMap,
27+
globalParametersMap: GlobalParametersMap,
2828
) {
2929
this.#hre = hre;
3030

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

3737
for (const taskDefinition of plugin.tasks) {
3838
this.#reduceTaskDefinition(
39-
globalParameterIndex,
39+
globalParametersMap,
4040
taskDefinition,
4141
plugin.id,
4242
);
@@ -45,7 +45,7 @@ export class TaskManagerImplementation implements TaskManager {
4545

4646
// reduce global user defined tasks
4747
for (const taskDefinition of this.#hre.config.tasks) {
48-
this.#reduceTaskDefinition(globalParameterIndex, taskDefinition);
48+
this.#reduceTaskDefinition(globalParametersMap, taskDefinition);
4949
}
5050
}
5151

@@ -136,7 +136,7 @@ export class TaskManagerImplementation implements TaskManager {
136136
}
137137

138138
#reduceTaskDefinition(
139-
globalParameterIndex: GlobalParameterMap,
139+
globalParametersMap: GlobalParametersMap,
140140
taskDefinition: TaskDefinition,
141141
pluginId?: string,
142142
) {
@@ -154,7 +154,7 @@ export class TaskManagerImplementation implements TaskManager {
154154
}
155155
case TaskDefinitionType.NEW_TASK: {
156156
this.#validateClashesWithGlobalParams(
157-
globalParameterIndex,
157+
globalParametersMap,
158158
taskDefinition,
159159
pluginId,
160160
);
@@ -174,7 +174,7 @@ export class TaskManagerImplementation implements TaskManager {
174174
}
175175
case TaskDefinitionType.TASK_OVERRIDE: {
176176
this.#validateClashesWithGlobalParams(
177-
globalParameterIndex,
177+
globalParametersMap,
178178
taskDefinition,
179179
pluginId,
180180
);
@@ -186,7 +186,7 @@ export class TaskManagerImplementation implements TaskManager {
186186
}
187187

188188
#validateClashesWithGlobalParams(
189-
globalParameterIndex: GlobalParameterMap,
189+
globalParametersMap: GlobalParametersMap,
190190
taskDefinition: NewTaskDefinition | TaskOverrideDefinition,
191191
pluginId?: string,
192192
) {
@@ -197,7 +197,7 @@ export class TaskManagerImplementation implements TaskManager {
197197
: [];
198198

199199
[...namedParamNames, ...positionalParamNames].forEach((paramName) => {
200-
const globalParamEntry = globalParameterIndex.get(paramName);
200+
const globalParamEntry = globalParametersMap.get(paramName);
201201
if (globalParamEntry !== undefined) {
202202
throw new HardhatError(
203203
HardhatError.ERRORS.TASK_DEFINITIONS.TASK_PARAMETER_ALREADY_DEFINED,

v-next/core/src/types/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GlobalParameterMap } from "./global-parameters.js";
1+
import type { GlobalParametersMap } from "./global-parameters.js";
22
import type { HardhatPlugin } from "./plugins.js";
33

44
/**
@@ -7,5 +7,5 @@ import type { HardhatPlugin } from "./plugins.js";
77
*/
88
export interface UnsafeHardhatRuntimeEnvironmentOptions {
99
resolvedPlugins?: HardhatPlugin[];
10-
globalParameterMap?: GlobalParameterMap;
10+
globalParametersMap?: GlobalParametersMap;
1111
}

v-next/core/src/types/global-parameters.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ export interface GlobalParameter<T extends ParameterType = ParameterType> {
2828
export interface GlobalArguments {}
2929

3030
/**
31-
* An entry in the global parameter map.
32-
* @see GlobalParameterMap
31+
* An entry in the global parameters map.
32+
* @see GlobalParametersMap
3333
*/
34-
export interface GlobalParameterMapEntry {
34+
export interface GlobalParametersMapEntry {
3535
pluginId: string;
3636
param: GlobalParameter;
3737
}
3838

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

0 commit comments

Comments
 (0)