-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
resolveGlobalArguments implementation & testing #5414
Changes from 6 commits
9bc3a6c
0f5453a
5fc1ba3
02d6b97
d33a223
0eca4fa
22abbd5
665a755
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import type { ParameterValue } from "../types/common.js"; | ||
|
||
import { HardhatError } from "@nomicfoundation/hardhat-errors"; | ||
|
||
import { ParameterType } from "../types/common.js"; | ||
|
||
/** | ||
|
@@ -54,3 +58,99 @@ const parameterTypeValidators: Record< | |
[ParameterType.FLOAT]: (value): value is number => typeof value === "number", | ||
[ParameterType.FILE]: (value): value is string => typeof value === "string", | ||
}; | ||
|
||
/** | ||
* Parses a parameter value from a string to the corresponding type. | ||
*/ | ||
// TODO: this code is duplicated in v-next/hardhat/src/internal/cli/main.ts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think exposing these things from core makes sense. We already expose functions like this. e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. I'll do it on a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue created: #5431 |
||
// we should move it to a shared place and add tests | ||
export function parseParameterValue( | ||
value: string, | ||
type: ParameterType, | ||
name: string, | ||
): ParameterValue { | ||
switch (type) { | ||
case ParameterType.STRING: | ||
case ParameterType.FILE: | ||
return value; | ||
case ParameterType.INT: | ||
return validateAndParseInt(name, value); | ||
case ParameterType.FLOAT: | ||
return validateAndParseFloat(name, value); | ||
case ParameterType.BIGINT: | ||
return validateAndParseBigInt(name, value); | ||
case ParameterType.BOOLEAN: | ||
return validateAndParseBoolean(name, value); | ||
} | ||
} | ||
|
||
function validateAndParseInt(name: string, value: string): number { | ||
const decimalPattern = /^\d+(?:[eE]\d+)?$/; | ||
const hexPattern = /^0[xX][\dABCDEabcde]+$/; | ||
|
||
if (!decimalPattern.test(value) && !hexPattern.test(value)) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, | ||
{ | ||
value, | ||
name, | ||
type: ParameterType.INT, | ||
}, | ||
); | ||
} | ||
|
||
return Number(value); | ||
} | ||
|
||
function validateAndParseFloat(name: string, value: string): number { | ||
const decimalPattern = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]\d+)?$/; | ||
const hexPattern = /^0[xX][\dABCDEabcde]+$/; | ||
|
||
if (!decimalPattern.test(value) && !hexPattern.test(value)) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, | ||
{ | ||
value, | ||
name, | ||
type: ParameterType.FLOAT, | ||
}, | ||
); | ||
} | ||
|
||
return Number(value); | ||
} | ||
|
||
function validateAndParseBigInt(name: string, value: string): bigint { | ||
const decimalPattern = /^\d+(?:n)?$/; | ||
const hexPattern = /^0[xX][\dABCDEabcde]+$/; | ||
|
||
if (!decimalPattern.test(value) && !hexPattern.test(value)) { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, | ||
{ | ||
value, | ||
name, | ||
type: ParameterType.BIGINT, | ||
}, | ||
); | ||
} | ||
|
||
return BigInt(value.replace("n", "")); | ||
} | ||
|
||
function validateAndParseBoolean(name: string, value: string): boolean { | ||
const normalizedValue = value.toLowerCase(); | ||
|
||
if (normalizedValue !== "true" && normalizedValue !== "false") { | ||
throw new HardhatError( | ||
HardhatError.ERRORS.ARGUMENTS.INVALID_VALUE_FOR_TYPE, | ||
{ | ||
value, | ||
name, | ||
type: ParameterType.BOOLEAN, | ||
}, | ||
); | ||
} | ||
|
||
return normalizedValue === "true"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
import type { ParameterType, ParameterTypeToValueType } from "./common.js"; | ||
import type { | ||
ParameterType, | ||
ParameterTypeToValueType, | ||
ParameterValue, | ||
} from "./common.js"; | ||
|
||
/** | ||
* A global option with an associated value and a default if not provided by | ||
|
@@ -25,7 +29,7 @@ export interface GlobalOption<T extends ParameterType = ParameterType> { | |
* Runtime Environment. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- To be used through module augmentation | ||
export interface GlobalOptions {} | ||
export interface GlobalOptions extends Record<string, ParameterValue> {} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will be a bit annoying, as you can do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What motivated it? We may find an alternative There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remember correctly, this was done to avoid an error when assigning the parsed value to the global options. As the interface is empty, this throws a TS error:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 22abbd5 |
||
/** | ||
* An entry in the global options map. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is trying to parse the
userProvidedGlobalOptions
's values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if those are provided, they should already be parsed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 665a755