Skip to content

Commit 6f33944

Browse files
authored
Merge pull request #5367 from NomicFoundation/refactor-parameter-fns
Refactor parameter fns
2 parents ffae5e4 + 41a7971 commit 6f33944

File tree

5 files changed

+133
-37
lines changed

5 files changed

+133
-37
lines changed

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

+38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ParameterType } from "../types/common.js";
2+
13
/**
24
* Names that can't be used as global- nor task-parameter names.
35
*/
@@ -16,3 +18,39 @@ const VALID_PARAM_NAME_CASING_REGEX = /^[a-z][a-zA-Z0-9]*$/;
1618
export function isValidParamNameCasing(name: string): boolean {
1719
return VALID_PARAM_NAME_CASING_REGEX.test(name);
1820
}
21+
22+
/**
23+
* Checks if a parameter value is valid for a given parameter type.
24+
*
25+
* This function uses a map of validators, where each validator is a function
26+
* that checks if a value is valid for a specific parameter type.
27+
* If the parameter type is variadic, the value is considered valid if it is an
28+
* array and all its elements are valid for the parameter type. An empty array
29+
* is considered invalid.
30+
*/
31+
export function isParameterValueValid(
32+
type: ParameterType,
33+
value: unknown,
34+
isVariadic: boolean = false,
35+
): boolean {
36+
const validator = parameterTypeValidators[type];
37+
38+
if (isVariadic) {
39+
return Array.isArray(value) && value.length > 0 && value.every(validator);
40+
}
41+
42+
return validator(value);
43+
}
44+
45+
const parameterTypeValidators: Record<
46+
ParameterType,
47+
(value: unknown) => boolean
48+
> = {
49+
[ParameterType.STRING]: (value): value is string => typeof value === "string",
50+
[ParameterType.BOOLEAN]: (value): value is boolean =>
51+
typeof value === "boolean",
52+
[ParameterType.INT]: (value): value is number => Number.isInteger(value),
53+
[ParameterType.BIGINT]: (value): value is bigint => typeof value === "bigint",
54+
[ParameterType.FLOAT]: (value): value is number => typeof value === "number",
55+
[ParameterType.FILE]: (value): value is string => typeof value === "string",
56+
};

v-next/core/src/internal/tasks/builders.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import type {
1414

1515
import { HardhatError } from "@nomicfoundation/hardhat-errors";
1616

17-
import { ParameterType, isParameterValueValid } from "../../types/common.js";
17+
import { ParameterType } from "../../types/common.js";
1818
import { TaskDefinitionType } from "../../types/tasks.js";
1919
import {
2020
RESERVED_PARAMETER_NAMES,
21+
isParameterValueValid,
2122
isValidParamNameCasing,
2223
} from "../parameters.js";
2324

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
import { HardhatError } from "@nomicfoundation/hardhat-errors";
1515
import { ensureError } from "@nomicfoundation/hardhat-utils/error";
1616

17-
import { isParameterValueValid } from "../../types/common.js";
17+
import { isParameterValueValid } from "../parameters.js";
1818

1919
import { formatTaskId } from "./utils.js";
2020

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

-35
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,3 @@ export type ParameterValue =
4444
*/
4545
export type ParameterTypeToValueType<T extends ParameterType> =
4646
ParameterToValueTypeMap[T];
47-
48-
const parameterTypeValidators: Record<
49-
ParameterType,
50-
(value: unknown) => boolean
51-
> = {
52-
[ParameterType.STRING]: (value): value is string => typeof value === "string",
53-
[ParameterType.BOOLEAN]: (value): value is boolean =>
54-
typeof value === "boolean",
55-
[ParameterType.INT]: (value): value is number => Number.isInteger(value),
56-
[ParameterType.BIGINT]: (value): value is bigint => typeof value === "bigint",
57-
[ParameterType.FLOAT]: (value): value is number => typeof value === "number",
58-
[ParameterType.FILE]: (value): value is string => typeof value === "string",
59-
};
60-
61-
/**
62-
* Checks if a parameter value is valid for a given parameter type.
63-
*
64-
* This function uses a map of validators, where each validator is a function
65-
* that checks if a value is valid for a specific parameter type.
66-
* If the parameter type is variadic, the value is considered valid if it is an
67-
* array and all its elements are valid for the parameter type.
68-
*/
69-
export function isParameterValueValid(
70-
type: ParameterType,
71-
value: unknown,
72-
isVariadic: boolean = false,
73-
): boolean {
74-
const validator = parameterTypeValidators[type];
75-
76-
if (isVariadic) {
77-
return Array.isArray(value) && value.every(validator);
78-
}
79-
80-
return validator(value);
81-
}
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { ParameterType } from "../../src/config.js";
5+
import {
6+
isValidParamNameCasing,
7+
isParameterValueValid,
8+
} from "../../src/internal/parameters.js";
9+
10+
describe("Parameters", () => {
11+
describe("isValidParamNameCasing", () => {
12+
it("should return true for valid parameter names", () => {
13+
assert.equal(isValidParamNameCasing("a"), true);
14+
assert.equal(isValidParamNameCasing("aa"), true);
15+
assert.equal(isValidParamNameCasing("aA"), true);
16+
assert.equal(isValidParamNameCasing("a1"), true);
17+
assert.equal(isValidParamNameCasing("foo"), true);
18+
assert.equal(isValidParamNameCasing("fooBar"), true);
19+
assert.equal(isValidParamNameCasing("foo123"), true);
20+
});
21+
22+
it("should return false for invalid parameter names", () => {
23+
assert.equal(isValidParamNameCasing("A"), false);
24+
assert.equal(isValidParamNameCasing("1"), false);
25+
assert.equal(isValidParamNameCasing("-"), false);
26+
assert.equal(isValidParamNameCasing("Foo"), false);
27+
assert.equal(isValidParamNameCasing("123Foo"), false);
28+
assert.equal(isValidParamNameCasing("foo-bar"), false);
29+
});
30+
});
31+
32+
describe("isParameterValueValid", () => {
33+
it("should validate string parameters", () => {
34+
assert.equal(isParameterValueValid(ParameterType.STRING, "foo"), true);
35+
assert.equal(isParameterValueValid(ParameterType.STRING, 123), false);
36+
});
37+
38+
it("should validate boolean parameters", () => {
39+
assert.equal(isParameterValueValid(ParameterType.BOOLEAN, true), true);
40+
assert.equal(isParameterValueValid(ParameterType.BOOLEAN, "true"), false);
41+
});
42+
43+
it("should validate int parameters", () => {
44+
assert.equal(isParameterValueValid(ParameterType.INT, 123), true);
45+
assert.equal(isParameterValueValid(ParameterType.INT, 123.45), false);
46+
assert.equal(isParameterValueValid(ParameterType.INT, 123n), false);
47+
});
48+
49+
it("should validate bigint parameters", () => {
50+
assert.equal(
51+
isParameterValueValid(ParameterType.BIGINT, BigInt(123)),
52+
true,
53+
);
54+
assert.equal(isParameterValueValid(ParameterType.BIGINT, 123n), true);
55+
assert.equal(isParameterValueValid(ParameterType.BIGINT, 123), false);
56+
});
57+
58+
it("should validate float parameters", () => {
59+
assert.equal(isParameterValueValid(ParameterType.FLOAT, 123), true);
60+
assert.equal(isParameterValueValid(ParameterType.FLOAT, 123.45), true);
61+
assert.equal(isParameterValueValid(ParameterType.FLOAT, 123n), false);
62+
});
63+
64+
it("should validate file parameters", () => {
65+
assert.equal(isParameterValueValid(ParameterType.FILE, "foo.txt"), true);
66+
assert.equal(
67+
isParameterValueValid(ParameterType.FILE, "random string"),
68+
true,
69+
);
70+
assert.equal(isParameterValueValid(ParameterType.FILE, 123), false);
71+
});
72+
73+
it("should validate variadic parameters", () => {
74+
assert.equal(
75+
isParameterValueValid(ParameterType.STRING, ["foo", "bar"], true),
76+
true,
77+
);
78+
assert.equal(
79+
isParameterValueValid(ParameterType.BIGINT, [123n, BigInt(123)], true),
80+
true,
81+
);
82+
assert.equal(
83+
isParameterValueValid(ParameterType.STRING, [], true),
84+
false,
85+
);
86+
assert.equal(
87+
isParameterValueValid(ParameterType.STRING, ["foo", 123], true),
88+
false,
89+
);
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)