hardhat 3.14.0 → 3.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/src/internal/builtin-plugins/solidity/type-extensions.d.ts +2 -2
  3. package/dist/src/internal/builtin-plugins/solidity-test/edr-artifacts.d.ts.map +1 -1
  4. package/dist/src/internal/builtin-plugins/solidity-test/edr-artifacts.js +1 -1
  5. package/dist/src/internal/builtin-plugins/solidity-test/edr-artifacts.js.map +1 -1
  6. package/dist/src/internal/builtin-plugins/solidity-test/eip712/canonicalize.d.ts +1 -1
  7. package/dist/src/internal/builtin-plugins/solidity-test/eip712/canonicalize.d.ts.map +1 -1
  8. package/dist/src/internal/builtin-plugins/solidity-test/eip712/canonicalize.js +13 -11
  9. package/dist/src/internal/builtin-plugins/solidity-test/eip712/canonicalize.js.map +1 -1
  10. package/dist/src/internal/builtin-plugins/solidity-test/formatters.d.ts +6 -1
  11. package/dist/src/internal/builtin-plugins/solidity-test/formatters.d.ts.map +1 -1
  12. package/dist/src/internal/builtin-plugins/solidity-test/formatters.js +41 -0
  13. package/dist/src/internal/builtin-plugins/solidity-test/formatters.js.map +1 -1
  14. package/dist/src/internal/builtin-plugins/solidity-test/helpers.d.ts +16 -3
  15. package/dist/src/internal/builtin-plugins/solidity-test/helpers.d.ts.map +1 -1
  16. package/dist/src/internal/builtin-plugins/solidity-test/helpers.js +45 -2
  17. package/dist/src/internal/builtin-plugins/solidity-test/helpers.js.map +1 -1
  18. package/dist/src/internal/builtin-plugins/solidity-test/runner.d.ts.map +1 -1
  19. package/dist/src/internal/builtin-plugins/solidity-test/runner.js +18 -1
  20. package/dist/src/internal/builtin-plugins/solidity-test/runner.js.map +1 -1
  21. package/dist/src/internal/builtin-plugins/solidity-test/task-action.d.ts.map +1 -1
  22. package/dist/src/internal/builtin-plugins/solidity-test/task-action.js +15 -16
  23. package/dist/src/internal/builtin-plugins/solidity-test/task-action.js.map +1 -1
  24. package/package.json +2 -2
  25. package/src/internal/builtin-plugins/solidity/type-extensions.ts +2 -2
  26. package/src/internal/builtin-plugins/solidity-test/edr-artifacts.ts +1 -1
  27. package/src/internal/builtin-plugins/solidity-test/eip712/canonicalize.ts +13 -11
  28. package/src/internal/builtin-plugins/solidity-test/formatters.ts +60 -1
  29. package/src/internal/builtin-plugins/solidity-test/helpers.ts +56 -4
  30. package/src/internal/builtin-plugins/solidity-test/runner.ts +33 -1
  31. package/src/internal/builtin-plugins/solidity-test/task-action.ts +20 -20
  32. package/templates/01-node-test-runner-viem/contracts/Counter.sol +1 -1
  33. package/templates/01-node-test-runner-viem/contracts/Counter.t.sol +1 -1
  34. package/templates/01-node-test-runner-viem/hardhat.config.ts +2 -2
  35. package/templates/01-node-test-runner-viem/package.json +2 -2
  36. package/templates/02-mocha-ethers/contracts/Counter.sol +1 -1
  37. package/templates/02-mocha-ethers/contracts/Counter.t.sol +1 -1
  38. package/templates/02-mocha-ethers/hardhat.config.ts +2 -2
  39. package/templates/02-mocha-ethers/package.json +2 -2
  40. package/templates/03-minimal/hardhat.config.ts +1 -1
  41. package/templates/03-minimal/package.json +1 -1
  42. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/constants.d.ts +0 -9
  43. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/constants.d.ts.map +0 -1
  44. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/constants.js +0 -24
  45. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/constants.js.map +0 -1
  46. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/helpers.d.ts +0 -24
  47. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/helpers.d.ts.map +0 -1
  48. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/helpers.js +0 -80
  49. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/helpers.js.map +0 -1
  50. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/index.d.ts +0 -13
  51. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/index.d.ts.map +0 -1
  52. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/index.js +0 -146
  53. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/index.js.map +0 -1
  54. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/parsing.d.ts +0 -19
  55. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/parsing.d.ts.map +0 -1
  56. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/parsing.js +0 -134
  57. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/parsing.js.map +0 -1
  58. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/types.d.ts +0 -10
  59. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/types.d.ts.map +0 -1
  60. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/types.js +0 -2
  61. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/types.js.map +0 -1
  62. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/validation.d.ts +0 -13
  63. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/validation.d.ts.map +0 -1
  64. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/validation.js +0 -96
  65. package/dist/src/internal/builtin-plugins/solidity-test/inline-config/validation.js.map +0 -1
  66. package/src/internal/builtin-plugins/solidity-test/inline-config/constants.ts +0 -25
  67. package/src/internal/builtin-plugins/solidity-test/inline-config/helpers.ts +0 -110
  68. package/src/internal/builtin-plugins/solidity-test/inline-config/index.ts +0 -285
  69. package/src/internal/builtin-plugins/solidity-test/inline-config/parsing.ts +0 -187
  70. package/src/internal/builtin-plugins/solidity-test/inline-config/types.ts +0 -9
  71. package/src/internal/builtin-plugins/solidity-test/inline-config/validation.ts +0 -144
@@ -1,110 +0,0 @@
1
- import type { RawInlineOverride } from "./types.js";
2
- import type { TestFunctionConfigOverride } from "@nomicfoundation/edr";
3
-
4
- import { assertHardhatInvariant } from "@nomicfoundation/hardhat-errors";
5
- import { bytesIncludesUtf8String } from "@nomicfoundation/hardhat-utils/bytes";
6
-
7
- import { getFullyQualifiedName } from "../../../../utils/contract-names.js";
8
-
9
- import {
10
- HARDHAT_CONFIG_PREFIX,
11
- FORGE_CONFIG_PREFIX,
12
- KEY_TYPES,
13
- } from "./constants.js";
14
-
15
- /**
16
- * Returns true if the build info bytes contain either of the inline config
17
- * prefixes.
18
- */
19
- export function buildInfoContainsInlineConfig(
20
- buildInfoBytes: Uint8Array,
21
- ): boolean {
22
- return (
23
- bytesIncludesUtf8String(buildInfoBytes, HARDHAT_CONFIG_PREFIX) ||
24
- bytesIncludesUtf8String(buildInfoBytes, FORGE_CONFIG_PREFIX)
25
- );
26
- }
27
-
28
- /**
29
- * Finds the function selector for a given function name in the methodIdentifiers
30
- * map. Matches the first entry whose signature starts with `functionName(`.
31
- * Returns the selector prefixed with "0x".
32
- */
33
- export function resolveFunctionSelector(
34
- methodIdentifiers: Record<string, string>,
35
- functionName: string,
36
- ): string | undefined {
37
- const prefix = `${functionName}(`;
38
-
39
- for (const [signature, selector] of Object.entries(methodIdentifiers)) {
40
- if (signature.startsWith(prefix)) {
41
- return `0x${selector}`;
42
- }
43
- }
44
-
45
- return undefined;
46
- }
47
-
48
- /**
49
- * Converts a validated set of raw overrides into a TestFunctionConfigOverride
50
- * object.
51
- */
52
- export function buildConfigOverride(
53
- overrides: RawInlineOverride[],
54
- ): TestFunctionConfigOverride {
55
- const config: Record<string, unknown> = {};
56
- const fuzz: Record<string, unknown> = {};
57
- const invariant: Record<string, unknown> = {};
58
-
59
- for (const override of overrides) {
60
- const expectedType = KEY_TYPES[override.key];
61
- let parsed: number | boolean | string;
62
- if (expectedType === "number") {
63
- parsed = Number(override.rawValue);
64
- } else if (expectedType === "boolean") {
65
- parsed = override.rawValue.toLowerCase() === "true";
66
- } else if (expectedType === "string") {
67
- // Validation has already proven the surrounding double quotes.
68
- parsed = override.rawValue.slice(1, -1);
69
- } else {
70
- assertHardhatInvariant(
71
- false,
72
- `Unhandled inline config value type for key ${override.key}`,
73
- );
74
- }
75
-
76
- const dotIndex = override.key.indexOf(".");
77
- if (dotIndex === -1) {
78
- // Top-level key, like "allowInternalExpectRevert"
79
- config[override.key] = parsed;
80
- } else {
81
- const section = override.key.slice(0, dotIndex);
82
- const field = override.key.slice(dotIndex + 1);
83
- const target = section === "fuzz" ? fuzz : invariant;
84
- target[field] = field === "timeout" ? { time: parsed } : parsed;
85
- }
86
- }
87
-
88
- if (Object.keys(fuzz).length > 0) {
89
- config.fuzz = fuzz;
90
- }
91
- if (Object.keys(invariant).length > 0) {
92
- config.invariant = invariant;
93
- }
94
-
95
- /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions --
96
- Cast is safe because we have validated all keys and value types in the previous step. */
97
- return config as TestFunctionConfigOverride;
98
- }
99
-
100
- /**
101
- * Constructs a fully qualified function name, in the format
102
- * "source.sol:ContractName#functionName".
103
- */
104
- export function getFunctionFqn(
105
- inputSourceName: string,
106
- contractName: string,
107
- functionName: string,
108
- ): string {
109
- return `${getFullyQualifiedName(inputSourceName, contractName)}#${functionName}`;
110
- }
@@ -1,285 +0,0 @@
1
- import type { SolidityBuildInfoOutput } from "../../../../types/solidity/solidity-artifacts.js";
2
- import type {
3
- BuildInfoAndOutput,
4
- EdrArtifactWithMetadata,
5
- } from "../edr-artifacts.js";
6
- import type { RawInlineOverride } from "./types.js";
7
- import type { ArtifactId, TestFunctionOverride } from "@nomicfoundation/edr";
8
-
9
- import {
10
- HardhatError,
11
- assertHardhatInvariant,
12
- } from "@nomicfoundation/hardhat-errors";
13
- import { parseJsonBytes } from "@nomicfoundation/hardhat-utils/bytes";
14
- import { ensureError } from "@nomicfoundation/hardhat-utils/error";
15
-
16
- import { getFullyQualifiedName } from "../../../../utils/contract-names.js";
17
-
18
- import {
19
- buildInfoContainsInlineConfig,
20
- resolveFunctionSelector,
21
- buildConfigOverride,
22
- getFunctionFqn,
23
- } from "./helpers.js";
24
- import { extractInlineConfigFromAst } from "./parsing.js";
25
- import { validateInlineOverrides } from "./validation.js";
26
-
27
- export type { RawInlineOverride } from "./types.js";
28
- export {
29
- buildInfoContainsInlineConfig,
30
- resolveFunctionSelector,
31
- buildConfigOverride,
32
- getFunctionFqn,
33
- } from "./helpers.js";
34
- export {
35
- extractInlineConfigFromAst,
36
- extractDocText,
37
- parseInlineConfigLine,
38
- } from "./parsing.js";
39
- export { validateInlineOverrides } from "./validation.js";
40
-
41
- interface CollectedOverrides {
42
- overrides: RawInlineOverride[];
43
- artifactIdsByFqn: Map<string, ArtifactId>;
44
- methodIdentifiersByContract: Map<string, Record<string, string>>;
45
- }
46
-
47
- /**
48
- * Extracts per-test inline configuration overrides from the NatSpec comments
49
- * in the solc AST. It only extracts them from the build info where each
50
- * test artifact's file was compiled as a root file.
51
- */
52
- export async function getTestFunctionOverrides(
53
- testSuiteArtifacts: EdrArtifactWithMetadata[],
54
- buildInfosAndOutputs: BuildInfoAndOutput[],
55
- ): Promise<TestFunctionOverride[]> {
56
- const allRawOverrides = await collectRawOverrides(
57
- testSuiteArtifacts,
58
- buildInfosAndOutputs,
59
- );
60
-
61
- validateInlineOverrides(allRawOverrides.overrides);
62
-
63
- return buildTestFunctionOverrides(allRawOverrides);
64
- }
65
-
66
- async function collectRawOverrides(
67
- testSuiteArtifacts: EdrArtifactWithMetadata[],
68
- buildInfosAndOutputs: BuildInfoAndOutput[],
69
- ): Promise<CollectedOverrides> {
70
- const overrides: RawInlineOverride[] = [];
71
- const methodIdentifiersByContract = new Map<string, Record<string, string>>();
72
-
73
- // Note: We group the artifacts by their build info, so that we only process
74
- // the relevant build infos, and only the root files of each of them.
75
- //
76
- // The last part is important, as a test file can be present in multiple build
77
- // infos in the presence of partial recompilations
78
- //
79
- // At the same time, the same root file could have produced multiple test
80
- // artifacts, so we need a two-level map to avoid processing the root files
81
- // multiple times.
82
-
83
- // Build lookup structures for fast access
84
- const artifactsGroupedByBuildInfo = new Map<
85
- /* buildInfoId */ string,
86
- Map</* inputSourceName */ string, EdrArtifactWithMetadata[]>
87
- >();
88
-
89
- const artifactIdsByFqn = new Map<string, ArtifactId>();
90
- const buildInfoAndOutputById: Map<string, BuildInfoAndOutput> = new Map(
91
- buildInfosAndOutputs.map((bio) => [bio.buildInfoId, bio]),
92
- );
93
-
94
- for (const edrArtifactWithMetadata of testSuiteArtifacts) {
95
- const fqn = getFullyQualifiedName(
96
- edrArtifactWithMetadata.edrArtifact.id.source,
97
- edrArtifactWithMetadata.edrArtifact.id.name,
98
- );
99
-
100
- const buildInfoId = edrArtifactWithMetadata.buildInfoId;
101
- let artifactsBySource = artifactsGroupedByBuildInfo.get(buildInfoId);
102
-
103
- if (artifactsBySource === undefined) {
104
- artifactsBySource = new Map();
105
- artifactsGroupedByBuildInfo.set(buildInfoId, artifactsBySource);
106
- }
107
-
108
- let artifacts = artifactsBySource.get(
109
- edrArtifactWithMetadata.edrArtifact.id.source,
110
- );
111
- if (artifacts === undefined) {
112
- artifacts = [];
113
- artifactsBySource.set(
114
- edrArtifactWithMetadata.edrArtifact.id.source,
115
- artifacts,
116
- );
117
- }
118
-
119
- artifacts.push(edrArtifactWithMetadata);
120
- artifactIdsByFqn.set(fqn, edrArtifactWithMetadata.edrArtifact.id);
121
- }
122
-
123
- for (const [
124
- buildInfoId,
125
- artifactsBySource,
126
- ] of artifactsGroupedByBuildInfo.entries()) {
127
- const buildInfoAndOutput = buildInfoAndOutputById.get(buildInfoId);
128
- if (buildInfoAndOutput === undefined) {
129
- // We can throw for this error for the first artifact with this build info
130
- // as all of them have the same problem.
131
- const artifacts = artifactsBySource.values().next().value;
132
-
133
- assertHardhatInvariant(
134
- artifacts !== undefined && artifacts.length > 0,
135
- "An artifact must be present for the build info",
136
- );
137
-
138
- const anyArtifact = artifacts[0];
139
-
140
- const fqn = getFullyQualifiedName(
141
- anyArtifact.userSourceName,
142
- anyArtifact.edrArtifact.id.name,
143
- );
144
-
145
- throw new HardhatError(
146
- HardhatError.ERRORS.CORE.SOLIDITY_TESTS
147
- .BUILD_INFO_NOT_FOUND_FOR_CONTRACT,
148
- {
149
- fqn,
150
- },
151
- );
152
- }
153
-
154
- if (!buildInfoContainsInlineConfig(buildInfoAndOutput.buildInfo)) {
155
- continue;
156
- }
157
-
158
- let buildInfoOutput: SolidityBuildInfoOutput;
159
- try {
160
- buildInfoOutput = await parseJsonBytes<SolidityBuildInfoOutput>(
161
- buildInfoAndOutput.output,
162
- );
163
- } catch (error) {
164
- ensureError(error);
165
- throw new HardhatError(
166
- HardhatError.ERRORS.CORE.SOLIDITY.BUILD_INFO_OUTPUT_PARSE_ERROR,
167
- { buildInfoId },
168
- error,
169
- );
170
- }
171
-
172
- for (const [
173
- inputSourceName,
174
- sourceArtifacts,
175
- ] of artifactsBySource.entries()) {
176
- const contractNames = new Set(
177
- sourceArtifacts.map((a) => a.edrArtifact.id.name),
178
- );
179
-
180
- const source = buildInfoOutput.output.sources[inputSourceName];
181
- const extracted = extractInlineConfigFromAst(
182
- source.ast,
183
- inputSourceName,
184
- contractNames,
185
- );
186
- overrides.push(...extracted);
187
-
188
- for (const artifact of sourceArtifacts) {
189
- const contractName = artifact.edrArtifact.id.name;
190
- const fqn = getFullyQualifiedName(inputSourceName, contractName);
191
-
192
- const methodIdentifiers =
193
- buildInfoOutput.output.contracts?.[inputSourceName][contractName]?.evm
194
- ?.methodIdentifiers;
195
-
196
- methodIdentifiersByContract.set(fqn, methodIdentifiers ?? {});
197
- }
198
- }
199
- }
200
-
201
- return { overrides, artifactIdsByFqn, methodIdentifiersByContract };
202
- }
203
-
204
- function buildTestFunctionOverrides(
205
- collected: CollectedOverrides,
206
- ): TestFunctionOverride[] {
207
- const { overrides, artifactIdsByFqn, methodIdentifiersByContract } =
208
- collected;
209
-
210
- // Group overrides by function. When the AST provides a functionSelector
211
- // (public/external functions in solc >= 0.6.0), use it to distinguish
212
- // overloaded functions. Otherwise fall back to function name only.
213
- const overridesByFunction = new Map<string, RawInlineOverride[]>();
214
- for (const override of overrides) {
215
- const functionFqn = getFunctionFqn(
216
- override.inputSourceName,
217
- override.contractName,
218
- override.functionName,
219
- );
220
- const groupKey =
221
- override.functionSelector !== undefined
222
- ? `${functionFqn}#${override.functionSelector}`
223
- : functionFqn;
224
- const existing = overridesByFunction.get(groupKey);
225
- if (existing === undefined) {
226
- overridesByFunction.set(groupKey, [override]);
227
- } else {
228
- existing.push(override);
229
- }
230
- }
231
-
232
- // Build TestFunctionOverride objects
233
- const testFunctionOverrides: TestFunctionOverride[] = [];
234
- for (const [_groupKey, groupOverrides] of overridesByFunction.entries()) {
235
- const firstOverride = groupOverrides[0];
236
- const functionFqn = getFunctionFqn(
237
- firstOverride.inputSourceName,
238
- firstOverride.contractName,
239
- firstOverride.functionName,
240
- );
241
- const contractFqn = `${firstOverride.inputSourceName}:${firstOverride.contractName}`;
242
-
243
- const artifactId = artifactIdsByFqn.get(contractFqn);
244
- assertHardhatInvariant(
245
- artifactId !== undefined,
246
- `Missing artifact id for "${contractFqn}"`,
247
- );
248
-
249
- // Use the AST-provided selector when available, otherwise fall back to
250
- // resolving via methodIdentifiers.
251
- let selector: string | undefined;
252
- if (firstOverride.functionSelector !== undefined) {
253
- selector = `0x${firstOverride.functionSelector}`;
254
- } else {
255
- const methodIdentifiers = methodIdentifiersByContract.get(contractFqn);
256
- assertHardhatInvariant(
257
- methodIdentifiers !== undefined,
258
- `Missing method identifiers for "${contractFqn}"`,
259
- );
260
-
261
- selector = resolveFunctionSelector(
262
- methodIdentifiers,
263
- firstOverride.functionName,
264
- );
265
- }
266
-
267
- if (selector === undefined) {
268
- throw new HardhatError(
269
- HardhatError.ERRORS.CORE.SOLIDITY_TESTS
270
- .INLINE_CONFIG_UNRESOLVED_SELECTOR,
271
- { functionFqn },
272
- );
273
- }
274
-
275
- testFunctionOverrides.push({
276
- identifier: {
277
- contractArtifact: artifactId,
278
- functionSelector: selector,
279
- },
280
- config: buildConfigOverride(groupOverrides),
281
- });
282
- }
283
-
284
- return testFunctionOverrides;
285
- }
@@ -1,187 +0,0 @@
1
- import type { RawInlineOverride } from "./types.js";
2
-
3
- import { HardhatError } from "@nomicfoundation/hardhat-errors";
4
- import { isObject } from "@nomicfoundation/hardhat-utils/lang";
5
- import {
6
- kebabToCamelCase,
7
- snakeToCamelCase,
8
- } from "@nomicfoundation/hardhat-utils/string";
9
-
10
- import {
11
- HARDHAT_CONFIG_PREFIX,
12
- FORGE_CONFIG_PREFIX,
13
- TOP_LEVEL_KEYS,
14
- } from "./constants.js";
15
- import { getFunctionFqn } from "./helpers.js";
16
-
17
- /**
18
- * Extracts raw inline config overrides from a solc AST for a single source
19
- * file.
20
- */
21
- export function extractInlineConfigFromAst(
22
- ast: unknown,
23
- inputSourceName: string,
24
- contractNames: Set<string>,
25
- ): RawInlineOverride[] {
26
- if (!isObject(ast) || ast.nodeType !== "SourceUnit") {
27
- return [];
28
- }
29
-
30
- const results: RawInlineOverride[] = [];
31
- const nodes: unknown[] = Array.isArray(ast.nodes) ? ast.nodes : [];
32
- for (const node of nodes) {
33
- if (!isObject(node) || node.nodeType !== "ContractDefinition") {
34
- continue;
35
- }
36
-
37
- const contractName = node.name;
38
- if (typeof contractName !== "string") {
39
- continue;
40
- }
41
-
42
- if (!contractNames.has(contractName)) {
43
- continue;
44
- }
45
-
46
- const members: unknown[] = Array.isArray(node.nodes) ? node.nodes : [];
47
- for (const member of members) {
48
- if (!isObject(member) || member.nodeType !== "FunctionDefinition") {
49
- continue;
50
- }
51
-
52
- const fnName = member.name;
53
- if (
54
- typeof fnName !== "string" ||
55
- (!fnName.startsWith("test") && !fnName.startsWith("invariant"))
56
- ) {
57
- continue;
58
- }
59
-
60
- const fnSelector =
61
- typeof member.functionSelector === "string"
62
- ? member.functionSelector
63
- : undefined;
64
-
65
- const docText = extractDocText(member.documentation);
66
- if (docText === undefined) {
67
- continue;
68
- }
69
-
70
- for (const line of docText.split("\n")) {
71
- const parsed = parseInlineConfigLine(
72
- line,
73
- inputSourceName,
74
- contractName,
75
- fnName,
76
- );
77
- if (parsed !== undefined) {
78
- parsed.functionSelector = fnSelector;
79
- results.push(parsed);
80
- }
81
- }
82
- }
83
- }
84
-
85
- return results;
86
- }
87
-
88
- /**
89
- * Extracts the documentation text from a FunctionDefinition's documentation
90
- * field, which can be a StructuredDocumentation node, a plain string, or null.
91
- */
92
- export function extractDocText(doc: unknown): string | undefined {
93
- if (
94
- isObject(doc) &&
95
- doc.nodeType === "StructuredDocumentation" &&
96
- typeof doc.text === "string"
97
- ) {
98
- return doc.text;
99
- } else if (typeof doc === "string") {
100
- return doc;
101
- } else {
102
- return undefined;
103
- }
104
- }
105
-
106
- /**
107
- * Parses a single line from a NatSpec comment and returns a RawInlineOverride
108
- * if the line contains a valid inline config directive. The line must start
109
- * with either "hardhat-config:" or "forge-config:", followed by a key=value pair.
110
- * Returns undefined if the line does not contain an inline config directive.
111
- */
112
- export function parseInlineConfigLine(
113
- line: string,
114
- inputSourceName: string,
115
- contractName: string,
116
- functionName: string,
117
- ): RawInlineOverride | undefined {
118
- // Strip leading whitespace and optional leading '*' from NatSpec text.
119
- // Solc's StructuredDocumentation.text has delimiters (///, /**, */) removed.
120
- // For /// comments, text has leading whitespace; for /** */ blocks, interior
121
- // lines may start with " * ". The regex handles both styles.
122
- const trimmedLine = line.replace(/^\s*\*?\s*/, "");
123
- const functionFqn = getFunctionFqn(
124
- inputSourceName,
125
- contractName,
126
- functionName,
127
- );
128
-
129
- let keyValueSegment: string;
130
-
131
- if (trimmedLine.startsWith(HARDHAT_CONFIG_PREFIX)) {
132
- keyValueSegment = trimmedLine.slice(HARDHAT_CONFIG_PREFIX.length).trim();
133
- } else if (trimmedLine.startsWith(FORGE_CONFIG_PREFIX)) {
134
- keyValueSegment = trimmedLine.slice(FORGE_CONFIG_PREFIX.length).trim();
135
- } else {
136
- return undefined;
137
- }
138
-
139
- const eqIndex = keyValueSegment.indexOf("=");
140
- if (eqIndex === -1) {
141
- throw new HardhatError(
142
- HardhatError.ERRORS.CORE.SOLIDITY_TESTS.INLINE_CONFIG_INVALID_SYNTAX,
143
- {
144
- line: trimmedLine,
145
- functionFqn,
146
- },
147
- );
148
- }
149
-
150
- const rawKey = keyValueSegment.slice(0, eqIndex).trim();
151
- let parsedKey = rawKey;
152
- const rawValue = keyValueSegment.slice(eqIndex + 1).trim();
153
-
154
- // Detect profile prefix: if the first dot-segment is NOT a known top-level
155
- // category, treat it as a profile name.
156
- const firstDot = rawKey.indexOf(".");
157
- if (firstDot !== -1) {
158
- const firstSegment = rawKey.slice(0, firstDot);
159
- if (!TOP_LEVEL_KEYS.includes(firstSegment)) {
160
- // It's a profile. Validate it.
161
- const profile = firstSegment;
162
- if (profile !== "default") {
163
- throw new HardhatError(
164
- HardhatError.ERRORS.CORE.SOLIDITY_TESTS
165
- .INLINE_CONFIG_UNSUPPORTED_PROFILE,
166
- {
167
- profile,
168
- functionFqn,
169
- },
170
- );
171
- }
172
- // Strip the "default." prefix
173
- parsedKey = rawKey.slice(firstDot + 1);
174
- }
175
- }
176
-
177
- parsedKey = snakeToCamelCase(kebabToCamelCase(parsedKey));
178
-
179
- return {
180
- inputSourceName,
181
- contractName,
182
- functionName,
183
- key: parsedKey,
184
- rawKey,
185
- rawValue,
186
- };
187
- }
@@ -1,9 +0,0 @@
1
- export interface RawInlineOverride {
2
- inputSourceName: string;
3
- contractName: string;
4
- functionName: string;
5
- functionSelector?: string; // from AST, hex without 0x prefix
6
- key: string; // parsed camelCase key, without profile prefix
7
- rawKey: string; // original key as written by the user, for error messages
8
- rawValue: string;
9
- }