Skip to content

Commit cb1ef17

Browse files
committed
Add validation for positional parameter clashes with global parameters
1 parent 5bf6df9 commit cb1ef17

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,26 @@ export class TaskManagerImplementation implements TaskManager {
185185
taskDefinition: NewTaskDefinition | TaskOverrideDefinition,
186186
pluginId?: string,
187187
) {
188-
for (const namedParamName of Object.keys(taskDefinition.namedParameters)) {
189-
const globalParamEntry = globalParameterIndex.get(namedParamName);
188+
const namedParamNames = Object.keys(taskDefinition.namedParameters);
189+
const positionalParamNames =
190+
"positionalParameters" in taskDefinition
191+
? taskDefinition.positionalParameters.map(({ name }) => name)
192+
: [];
193+
194+
[...namedParamNames, ...positionalParamNames].forEach((paramName) => {
195+
const globalParamEntry = globalParameterIndex.get(paramName);
190196
if (globalParamEntry !== undefined) {
191197
throw new HardhatError(
192198
HardhatError.ERRORS.TASK_DEFINITIONS.TASK_PARAMETER_ALREADY_DEFINED,
193199
{
194200
actorFragment: getActorFragment(pluginId),
195201
task: formatTaskId(taskDefinition.id),
196-
namedParamName,
202+
parameter: paramName,
197203
globalParamPluginId: globalParamEntry.pluginId,
198204
},
199205
);
200206
}
201-
}
207+
});
202208
}
203209

204210
#processTaskOverride(

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

+39-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,45 @@ describe("TaskManagerImplementation", () => {
387387
{
388388
actorFragment: "Plugin plugin1 is",
389389
task: "task1",
390-
namedParamName: "param1",
390+
parameter: "param1",
391+
globalParamPluginId: "plugin2",
392+
},
393+
),
394+
);
395+
});
396+
397+
it("should throw if there's a global parameter with the same name as a task positional parameter", async () => {
398+
await assert.rejects(
399+
createHardhatRuntimeEnvironment({
400+
plugins: [
401+
{
402+
id: "plugin1",
403+
tasks: [
404+
new NewTaskDefinitionBuilderImplementation("task1")
405+
.addPositionalParameter({ name: "param1" })
406+
.setAction(() => {})
407+
.build(),
408+
],
409+
},
410+
{
411+
id: "plugin2",
412+
globalParameters: [
413+
buildGlobalParameterDefinition({
414+
name: "param1",
415+
description: "",
416+
parameterType: ParameterType.STRING,
417+
defaultValue: "",
418+
}),
419+
],
420+
},
421+
],
422+
}),
423+
new HardhatError(
424+
HardhatError.ERRORS.TASK_DEFINITIONS.TASK_PARAMETER_ALREADY_DEFINED,
425+
{
426+
actorFragment: "Plugin plugin1 is",
427+
task: "task1",
428+
parameter: "param1",
391429
globalParamPluginId: "plugin2",
392430
},
393431
),

v-next/hardhat-errors/src/descriptors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ Please ensure that an action is defined for each task.`,
247247
TASK_PARAMETER_ALREADY_DEFINED: {
248248
number: 209,
249249
messageTemplate:
250-
"{actorFragment} trying to define task {task} with the named parameter {namedParamName} but it is already defined as a global parameter by plugin {globalParamPluginId}",
250+
"{actorFragment} trying to define task {task} with the parameter {parameter} but it is already defined as a global parameter by plugin {globalParamPluginId}",
251251
websiteTitle: "Task parameter already defined",
252252
websiteDescription:
253-
"The task named parameter is already defined as a global parameter by another plugin. Please ensure that task parameters are uniquely named to avoid conflicts.",
253+
"The task parameter is already defined as a global parameter by another plugin. Please ensure that task parameters are uniquely named to avoid conflicts.",
254254
},
255255
TASK_OVERRIDE_PARAMETER_ALREADY_DEFINED: {
256256
number: 210,

0 commit comments

Comments
 (0)