Skip to content

Commit

Permalink
Merge pull request #800 from erhant/erhant/json5-parser
Browse files Browse the repository at this point in the history
feat: JSON5 support for module parameters
  • Loading branch information
zoeyTM authored Sep 6, 2024
2 parents f3dc8ad + 27f36a8 commit bf7e7fb
Show file tree
Hide file tree
Showing 5 changed files with 836 additions and 817 deletions.
3 changes: 2 additions & 1 deletion packages/hardhat-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"chalk": "^4.0.0",
"debug": "^4.3.2",
"fs-extra": "^10.0.0",
"prompts": "^2.4.2"
"prompts": "^2.4.2",
"json5": "^2.2.3"
}
}
10 changes: 7 additions & 3 deletions packages/hardhat-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "fs-extra";
import { extendConfig, extendEnvironment, scope } from "hardhat/config";
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
import { parse as json5Parse } from "json5";
import path from "path";

import "./type-extensions";
Expand Down Expand Up @@ -241,7 +242,10 @@ ignitionScope
userModule.id,
hre.config.paths.ignition
);
} else if (parametersInput.endsWith(".json")) {
} else if (
parametersInput.endsWith(".json") ||
parametersInput.endsWith(".json5")
) {
parameters = await resolveParametersFromFileName(parametersInput);
} else {
parameters = resolveParametersString(parametersInput);
Expand Down Expand Up @@ -672,7 +676,7 @@ async function resolveConfigPath(
try {
const rawFile = await readFile(filepath);

return JSON.parse(rawFile.toString(), bigintReviver);
return await json5Parse(rawFile.toString(), bigintReviver);
} catch (e) {
if (e instanceof NomicLabsHardhatPluginError) {
throw e;
Expand All @@ -692,7 +696,7 @@ async function resolveConfigPath(

function resolveParametersString(paramString: string): DeploymentParameters {
try {
return JSON.parse(paramString, bigintReviver);
return json5Parse(paramString, bigintReviver);
} catch (e) {
if (e instanceof NomicLabsHardhatPluginError) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* comments allowed in json5
*/

{
// no quotes around keys
LockModule: {
// weird multi-line but should pass
unlockTime: "1\
893\
499\
200\
000n",
lockedAmount: "2000000000000n",
},
}
13 changes: 13 additions & 0 deletions packages/hardhat-plugin/test/module-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ describe("module parameters", () => {
);
});

it("should run if provided with a valid module parameters file in JSON5 format", async function () {
await this.hre.run(
{
scope: "ignition",
task: "deploy",
},
{
modulePath: "./ignition/modules/Lock.ts",
parameters: "./ignition/modules/parameters-json5.json5",
}
);
});

it("should run if provided with a valid module parameters file encoding a bigint as a string", async function () {
await this.hre.run(
{
Expand Down
Loading

0 comments on commit bf7e7fb

Please sign in to comment.