@@ -18,13 +18,14 @@ import { HardhatError } from "@ignored/hardhat-vnext-errors";
18
18
19
19
import { ArgumentType } from "../../types/arguments.js" ;
20
20
import { TaskDefinitionType } from "../../types/tasks.js" ;
21
- import {
22
- RESERVED_ARGUMENT_NAMES ,
23
- isArgumentValueValid ,
24
- isArgumentNameValid ,
25
- } from "../arguments.js" ;
26
21
27
- import { formatTaskId , isValidActionUrl } from "./utils.js" ;
22
+ import { formatTaskId } from "./utils.js" ;
23
+ import {
24
+ validateAction ,
25
+ validateId ,
26
+ validateOption ,
27
+ validatePositionalArgument ,
28
+ } from "./validations.js" ;
28
29
29
30
export class EmptyTaskDefinitionBuilderImplementation
30
31
implements EmptyTaskDefinitionBuilder
@@ -34,11 +35,7 @@ export class EmptyTaskDefinitionBuilderImplementation
34
35
#description: string ;
35
36
36
37
constructor ( id : string | string [ ] , description : string = "" ) {
37
- if ( id . length === 0 ) {
38
- throw new HardhatError (
39
- HardhatError . ERRORS . TASK_DEFINITIONS . EMPTY_TASK_ID ,
40
- ) ;
41
- }
38
+ validateId ( id ) ;
42
39
43
40
this . #id = Array . isArray ( id ) ? id : [ id ] ;
44
41
this . #description = description ;
@@ -72,11 +69,7 @@ export class NewTaskDefinitionBuilderImplementation
72
69
#action?: NewTaskActionFunction | string ;
73
70
74
71
constructor ( id : string | string [ ] , description : string = "" ) {
75
- if ( id . length === 0 ) {
76
- throw new HardhatError (
77
- HardhatError . ERRORS . TASK_DEFINITIONS . EMPTY_TASK_ID ,
78
- ) ;
79
- }
72
+ validateId ( id ) ;
80
73
81
74
this . #id = Array . isArray ( id ) ? id : [ id ] ;
82
75
this . #description = description ;
@@ -88,14 +81,7 @@ export class NewTaskDefinitionBuilderImplementation
88
81
}
89
82
90
83
public setAction ( action : NewTaskActionFunction | string ) : this {
91
- if ( typeof action === "string" && ! isValidActionUrl ( action ) ) {
92
- throw new HardhatError (
93
- HardhatError . ERRORS . TASK_DEFINITIONS . INVALID_FILE_ACTION ,
94
- {
95
- action,
96
- } ,
97
- ) ;
98
- }
84
+ validateAction ( action ) ;
99
85
100
86
this . #action = action ;
101
87
@@ -115,45 +101,17 @@ export class NewTaskDefinitionBuilderImplementation
115
101
} ) : this {
116
102
const argumentType = type ?? ArgumentType . STRING ;
117
103
118
- if ( ! isArgumentNameValid ( name ) ) {
119
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . INVALID_NAME , {
120
- name,
121
- } ) ;
122
- }
123
-
124
- if ( this . #usedNames. has ( name ) ) {
125
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . DUPLICATED_NAME , {
126
- name,
127
- } ) ;
128
- }
129
-
130
- if ( RESERVED_ARGUMENT_NAMES . has ( name ) ) {
131
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . RESERVED_NAME , {
132
- name,
133
- } ) ;
134
- }
135
-
136
- if ( ! isArgumentValueValid ( argumentType , defaultValue ) ) {
137
- throw new HardhatError (
138
- HardhatError . ERRORS . TASK_DEFINITIONS . INVALID_VALUE_FOR_TYPE ,
139
- {
140
- value : defaultValue ,
141
- name : "defaultValue" ,
142
- type : argumentType ,
143
- task : formatTaskId ( this . #id) ,
144
- } ,
145
- ) ;
146
- }
147
-
148
- this . #usedNames. add ( name ) ;
149
-
150
- this . #options[ name ] = {
104
+ const optionDefinition = {
151
105
name,
152
106
description,
153
107
type : argumentType ,
154
108
defaultValue,
155
109
} ;
156
110
111
+ validateOption ( optionDefinition , this . #usedNames, this . #id) ;
112
+
113
+ this . #options[ name ] = optionDefinition ;
114
+
157
115
return this ;
158
116
}
159
117
@@ -223,69 +181,23 @@ export class NewTaskDefinitionBuilderImplementation
223
181
} ) : this {
224
182
const argumentType = type ?? ArgumentType . STRING ;
225
183
226
- if ( ! isArgumentNameValid ( name ) ) {
227
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . INVALID_NAME , {
228
- name,
229
- } ) ;
230
- }
231
-
232
- if ( this . #usedNames. has ( name ) ) {
233
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . DUPLICATED_NAME , {
234
- name,
235
- } ) ;
236
- }
237
-
238
- if ( RESERVED_ARGUMENT_NAMES . has ( name ) ) {
239
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . RESERVED_NAME , {
240
- name,
241
- } ) ;
242
- }
243
-
244
- if ( defaultValue !== undefined ) {
245
- if ( ! isArgumentValueValid ( argumentType , defaultValue , isVariadic ) ) {
246
- throw new HardhatError (
247
- HardhatError . ERRORS . TASK_DEFINITIONS . INVALID_VALUE_FOR_TYPE ,
248
- {
249
- value : defaultValue ,
250
- name : "defaultValue" ,
251
- type : argumentType ,
252
- task : formatTaskId ( this . #id) ,
253
- } ,
254
- ) ;
255
- }
256
- }
257
-
258
- if ( this . #positionalArgs. length > 0 ) {
259
- const lastArg = this . #positionalArgs[ this . #positionalArgs. length - 1 ] ;
260
-
261
- if ( lastArg . isVariadic ) {
262
- throw new HardhatError (
263
- HardhatError . ERRORS . TASK_DEFINITIONS . POSITIONAL_ARG_AFTER_VARIADIC ,
264
- {
265
- name,
266
- } ,
267
- ) ;
268
- }
269
-
270
- if ( lastArg . defaultValue !== undefined && defaultValue === undefined ) {
271
- throw new HardhatError (
272
- HardhatError . ERRORS . TASK_DEFINITIONS . REQUIRED_ARG_AFTER_OPTIONAL ,
273
- {
274
- name,
275
- } ,
276
- ) ;
277
- }
278
- }
279
-
280
- this . #usedNames. add ( name ) ;
281
-
282
- this . #positionalArgs. push ( {
184
+ const positionalArgDef = {
283
185
name,
284
186
description,
285
187
type : argumentType ,
286
188
defaultValue,
287
189
isVariadic,
288
- } ) ;
190
+ } ;
191
+
192
+ const lastArg = this . #positionalArgs. at ( - 1 ) ;
193
+ validatePositionalArgument (
194
+ positionalArgDef ,
195
+ this . #usedNames,
196
+ this . #id,
197
+ lastArg ,
198
+ ) ;
199
+
200
+ this . #positionalArgs. push ( positionalArgDef ) ;
289
201
290
202
return this ;
291
203
}
@@ -303,11 +215,7 @@ export class TaskOverrideDefinitionBuilderImplementation
303
215
#action?: TaskOverrideActionFunction | string ;
304
216
305
217
constructor ( id : string | string [ ] ) {
306
- if ( id . length === 0 ) {
307
- throw new HardhatError (
308
- HardhatError . ERRORS . TASK_DEFINITIONS . EMPTY_TASK_ID ,
309
- ) ;
310
- }
218
+ validateId ( id ) ;
311
219
312
220
this . #id = Array . isArray ( id ) ? id : [ id ] ;
313
221
}
@@ -318,14 +226,7 @@ export class TaskOverrideDefinitionBuilderImplementation
318
226
}
319
227
320
228
public setAction ( action : TaskOverrideActionFunction | string ) : this {
321
- if ( typeof action === "string" && ! isValidActionUrl ( action ) ) {
322
- throw new HardhatError (
323
- HardhatError . ERRORS . TASK_DEFINITIONS . INVALID_FILE_ACTION ,
324
- {
325
- action,
326
- } ,
327
- ) ;
328
- }
229
+ validateAction ( action ) ;
329
230
330
231
this . #action = action ;
331
232
@@ -345,43 +246,21 @@ export class TaskOverrideDefinitionBuilderImplementation
345
246
} ) : this {
346
247
const argumentType = type ?? ArgumentType . STRING ;
347
248
348
- if ( ! isArgumentNameValid ( name ) ) {
349
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . INVALID_NAME , {
350
- name,
351
- } ) ;
352
- }
353
-
354
- if ( name in this . #options) {
355
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . DUPLICATED_NAME , {
356
- name,
357
- } ) ;
358
- }
359
-
360
- if ( RESERVED_ARGUMENT_NAMES . has ( name ) ) {
361
- throw new HardhatError ( HardhatError . ERRORS . ARGUMENTS . RESERVED_NAME , {
362
- name,
363
- } ) ;
364
- }
365
-
366
- if ( ! isArgumentValueValid ( argumentType , defaultValue ) ) {
367
- throw new HardhatError (
368
- HardhatError . ERRORS . TASK_DEFINITIONS . INVALID_VALUE_FOR_TYPE ,
369
- {
370
- value : defaultValue ,
371
- name : "defaultValue" ,
372
- type : argumentType ,
373
- task : formatTaskId ( this . #id) ,
374
- } ,
375
- ) ;
376
- }
377
-
378
- this . #options[ name ] = {
249
+ const optionDefinition = {
379
250
name,
380
251
description,
381
252
type : argumentType ,
382
253
defaultValue,
383
254
} ;
384
255
256
+ validateOption (
257
+ optionDefinition ,
258
+ new Set ( Object . keys ( this . #options) ) ,
259
+ this . #id,
260
+ ) ;
261
+
262
+ this . #options[ name ] = optionDefinition ;
263
+
385
264
return this ;
386
265
}
387
266
0 commit comments