1
- import type { ArgumentValue } from "../../types/arguments.js" ;
1
+ import type {
2
+ ArgumentValue ,
3
+ OptionDefinition ,
4
+ PositionalArgumentDefinition ,
5
+ } from "../../types/arguments.js" ;
2
6
import type { HardhatRuntimeEnvironment } from "../../types/hre.js" ;
3
7
import type {
4
- TaskOptionDefinition ,
5
8
NewTaskActionFunction ,
6
- TaskPositionalArgumentDefinition ,
7
9
Task ,
8
10
TaskActions ,
9
11
TaskArguments ,
10
12
TaskOverrideActionFunction ,
11
- TaskArgumentDefinition ,
12
13
} from "../../types/tasks.js" ;
13
14
14
15
import {
@@ -48,8 +49,8 @@ export class ResolvedTask implements Task {
48
49
id : string [ ] ,
49
50
description : string ,
50
51
action : NewTaskActionFunction | string ,
51
- options : Record < string , TaskOptionDefinition > ,
52
- positionalArguments : TaskPositionalArgumentDefinition [ ] ,
52
+ options : Record < string , OptionDefinition > ,
53
+ positionalArguments : PositionalArgumentDefinition [ ] ,
53
54
pluginId ?: string ,
54
55
) : ResolvedTask {
55
56
return new ResolvedTask (
@@ -68,8 +69,8 @@ export class ResolvedTask implements Task {
68
69
public readonly id : string [ ] ,
69
70
public readonly description : string ,
70
71
public readonly actions : TaskActions ,
71
- public readonly options : Map < string , TaskOptionDefinition > ,
72
- public readonly positionalArguments : TaskPositionalArgumentDefinition [ ] ,
72
+ public readonly options : Map < string , OptionDefinition > ,
73
+ public readonly positionalArguments : PositionalArgumentDefinition [ ] ,
73
74
public readonly pluginId : string | undefined ,
74
75
public readonly subtasks : Map < string , Task > ,
75
76
hre : HardhatRuntimeEnvironment ,
@@ -98,25 +99,34 @@ export class ResolvedTask implements Task {
98
99
} ) ;
99
100
}
100
101
101
- // Normalize arguments into a single iterable
102
- const allArguments : TaskArgumentDefinition [ ] = [
103
- ...this . options . values ( ) ,
104
- ...this . positionalArguments ,
105
- ] ;
106
-
107
102
const providedArgumentNames = new Set ( Object . keys ( taskArguments ) ) ;
108
- for ( const argument of allArguments ) {
103
+
104
+ // Validate and resolve the task options
105
+ for ( const option of this . options . values ( ) ) {
106
+ const value = taskArguments [ option . name ] ;
107
+
108
+ this . #validateArgumentType( option , value ) ;
109
+
110
+ // resolve defaults for optional arguments
111
+ if ( value === undefined ) {
112
+ taskArguments [ option . name ] = option . defaultValue ;
113
+ }
114
+
115
+ providedArgumentNames . delete ( option . name ) ;
116
+ }
117
+
118
+ // Validate and resolve the task positional arguments
119
+ for ( const argument of this . positionalArguments ) {
109
120
const value = taskArguments [ argument . name ] ;
110
121
111
122
this . #validateRequiredArgument( argument , value ) ;
112
- this . #validateArgumentType( argument , value ) ;
123
+ this . #validateArgumentType( argument , value , argument . isVariadic ) ;
113
124
114
125
// resolve defaults for optional arguments
115
126
if ( value === undefined && argument . defaultValue !== undefined ) {
116
127
taskArguments [ argument . name ] = argument . defaultValue ;
117
128
}
118
129
119
- // Remove processed argument from the set
120
130
providedArgumentNames . delete ( argument . name ) ;
121
131
}
122
132
@@ -166,7 +176,7 @@ export class ResolvedTask implements Task {
166
176
* @throws HardhatError if the argument is required and doesn't have a value.
167
177
*/
168
178
#validateRequiredArgument(
169
- argument : TaskArgumentDefinition ,
179
+ argument : PositionalArgumentDefinition ,
170
180
value : ArgumentValue | ArgumentValue [ ] ,
171
181
) {
172
182
if ( argument . defaultValue === undefined && value === undefined ) {
@@ -188,20 +198,15 @@ export class ResolvedTask implements Task {
188
198
* @throws HardhatError if the argument has an invalid type.
189
199
*/
190
200
#validateArgumentType(
191
- argument : TaskArgumentDefinition ,
201
+ argument : OptionDefinition | PositionalArgumentDefinition ,
192
202
value : ArgumentValue | ArgumentValue [ ] ,
203
+ isVariadic : boolean = false ,
193
204
) {
194
205
// skip type validation for optional arguments with undefined value
195
206
if ( value === undefined && argument . defaultValue !== undefined ) {
196
207
return ;
197
208
}
198
209
199
- // check if the argument is variadic
200
- const isPositionalArgument = (
201
- arg : TaskArgumentDefinition ,
202
- ) : arg is TaskPositionalArgumentDefinition => "isVariadic" in arg ;
203
- const isVariadic = isPositionalArgument ( argument ) && argument . isVariadic ;
204
-
205
210
// check if the value is valid for the argument type
206
211
if ( ! isArgumentValueValid ( argument . type , value , isVariadic ) ) {
207
212
throw new HardhatError (
0 commit comments