Skip to content

Commit 4dbbcb6

Browse files
simplify addition of .js extension
1 parent aac2ca1 commit 4dbbcb6

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

v-next/hardhat-typechain/src/internal/generate-types.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,13 @@ export function addJsExtensionsIfNeeded(content: string): string {
125125
// import * from "npmPackage"
126126
// will not be converted because the import path does not starts with a "."
127127
const jsExtensionRegex =
128-
/^import\s+.*?\s+from\s+(['"])\.[^'"]*(?<!\.js)\1;$/gm;
128+
/^import\s+.*?\s+from\s+(['"])\.[^'"]*(?<!\.js)\1;?$/gm;
129+
130+
return content.replace(jsExtensionRegex, (match) => {
131+
const insertIndex = match.includes(";")
132+
? match.length - 2
133+
: match.length - 1;
129134

130-
content = content.replace(jsExtensionRegex, (match) => {
131-
const insertIndex = match.lastIndexOf(";") - 1;
132135
return match.slice(0, insertIndex) + "/index.js" + match.slice(insertIndex);
133136
});
134-
135-
// Special scenario for ".d.ts" files: These files lack semicolons at the end,
136-
// and imports do not include the ".js" extension.
137-
// Handle this separately to improve code readability.
138-
// Example:
139-
// import { ethers } from 'ethers' // Not modified
140-
// import * as Contracts from "." // Modified to: import * as Contracts from "./index.js"
141-
return content.replaceAll(`from "."`, `from "./index.js"`);
142137
}

v-next/hardhat-typechain/test/index.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
11
import assert from "node:assert/strict";
2+
import path from "node:path";
23
import { before, describe, it } from "node:test";
34

45
import { createHardhatRuntimeEnvironment } from "@ignored/hardhat-vnext/hre";
5-
import { exists, remove } from "@ignored/hardhat-vnext-utils/fs";
6+
import { exists, readUtf8File, remove } from "@ignored/hardhat-vnext-utils/fs";
67
import { useFixtureProject } from "@nomicfoundation/hardhat-test-utils";
78

89
describe("hardhat-typechain", () => {
10+
describe("check that types are generated correctly", () => {
11+
const projectFolder = "generate-types";
12+
13+
useFixtureProject(projectFolder);
14+
15+
before(async () => {
16+
await remove(`${process.cwd()}/types`);
17+
});
18+
19+
it("should generate the types", async () => {
20+
// Check that the types are generated with the expected addition of the "/index.js" extensions
21+
// and the v3 modules
22+
23+
const hardhatConfig = await import(
24+
// eslint-disable-next-line import/no-relative-packages -- allow for fixture projects
25+
`./fixture-projects/${projectFolder}/hardhat.config.js`
26+
);
27+
28+
const hre = await createHardhatRuntimeEnvironment(hardhatConfig.default);
29+
30+
assert.equal(await exists(`${process.cwd()}/types`), false);
31+
32+
await hre.tasks.getTask("clean").run();
33+
34+
await hre.tasks.getTask("compile").run({});
35+
36+
const content = await readUtf8File(
37+
path.join(process.cwd(), "types", "ethers-contracts", "hardhat.d.ts"),
38+
);
39+
40+
// The overload target the v3 hardhat ethers package
41+
assert.equal(
42+
content.includes(
43+
`declare module "@ignored/hardhat-vnext-ethers/types" {`,
44+
),
45+
true,
46+
);
47+
48+
// The import should be from the v3 hardhat ethers package
49+
assert.equal(
50+
content.includes(`from "@ignored/hardhat-vnext-ethers/types";`),
51+
true,
52+
);
53+
54+
// A relative import should have the ".js" extension
55+
assert.equal(
56+
content.includes(`import * as Contracts from "./index.js"`),
57+
true,
58+
);
59+
60+
// The import from a npm package should have ".js" extensions
61+
assert.equal(content.includes(`import { ethers } from 'ethers'`), true);
62+
});
63+
});
64+
965
describe("generate types from contracts", () => {
1066
const projectFolder = "generate-types";
1167

v-next/hardhat-typechain/test/internal/js-extensions.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ import * from "./index.js";`,
3131

3232
it("should correctly add the '/index.js' - simulate .d.ts files", () => {
3333
const output = addJsExtensionsIfNeeded(`
34-
import { ethers } from "ethers"; // Not modified
35-
import * from "." // modified`);
34+
import { ethers } from "ethers";
35+
import * from "."`);
3636

3737
assert.equal(
3838
output,
3939
`
40-
import { ethers } from "ethers"; // Not modified
41-
import * from "./index.js" // modified`,
40+
import { ethers } from "ethers";
41+
import * from "./index.js"`,
4242
);
4343
});
4444

0 commit comments

Comments
 (0)