Skip to content

Commit

Permalink
Fix test (#6923)
Browse files Browse the repository at this point in the history
* Fix test

* chore: add changeset

* chore: the actual fix

* fix: add extra line in test

* chore: hide eslint rule from output as it's too noisy
  • Loading branch information
andyjessop authored Oct 8, 2024
1 parent 32b8a35 commit 1320f20
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-lies-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore: adds eslint-disable for ESLint error on empty typescript interface in workers-configuration.d.ts
5 changes: 3 additions & 2 deletions packages/wrangler/src/__tests__/type-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,9 @@ describe("generateTypes()", () => {
);

await runWrangler("types");
expect(fs.readFileSync("./worker-configuration.d.ts", "utf-8")).toMatch(
/interface Env \{\s*\}/

expect(fs.readFileSync("./worker-configuration.d.ts", "utf-8")).toContain(
`// eslint-disable-next-line @typescript-eslint/no-empty-interface,@typescript-eslint/no-empty-object-type\ninterface Env {\n}`
);
expect(std.out).toMatchInlineSnapshot(`
"Generating project types...
Expand Down
50 changes: 37 additions & 13 deletions packages/wrangler/src/type-generation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,34 +488,58 @@ function writeDTSFile({
}
}

let combinedTypeStrings = "";
if (formatType === "modules") {
combinedTypeStrings += `interface ${envInterface} {${envTypeStructure
.map((value) => `\n\t${value}`)
.join("")}\n}\n${modulesTypeStructure.join("\n")}`;
} else {
combinedTypeStrings += `export {};\ndeclare global {\n${envTypeStructure
.map((value) => `\tconst ${value}`)
.join("\n")}\n}\n${modulesTypeStructure.join("\n")}`;
}

const wranglerCommandUsed = ["wrangler", ...process.argv.slice(2)].join(" ");

const typesHaveBeenFound =
envTypeStructure.length || modulesTypeStructure.length;

if (formatType === "modules" || typesHaveBeenFound) {
const { fileContent, consoleOutput } = generateTypeStrings(
formatType,
envInterface,
envTypeStructure,
modulesTypeStructure
);

fs.writeFileSync(
path,
[
`// Generated by Wrangler by running \`${wranglerCommandUsed}\``,
"",
combinedTypeStrings,
fileContent,
].join("\n")
);

logger.log(`Generating project types...\n`);
logger.log(combinedTypeStrings);
logger.log(consoleOutput);
}
}

function generateTypeStrings(
formatType: string,
envInterface: string,
envTypeStructure: string[],
modulesTypeStructure: string[]
): { fileContent: string; consoleOutput: string } {
let baseContent = "";
let eslintDisable = "";

if (formatType === "modules") {
if (envTypeStructure.length === 0) {
eslintDisable =
"// eslint-disable-next-line @typescript-eslint/no-empty-interface,@typescript-eslint/no-empty-object-type\n";
}
baseContent = `interface ${envInterface} {${envTypeStructure.map((value) => `\n\t${value}`).join("")}\n}`;
} else {
baseContent = `export {};\ndeclare global {\n${envTypeStructure.map((value) => `\tconst ${value}`).join("\n")}\n}`;
}

const modulesContent = modulesTypeStructure.join("\n");

return {
fileContent: `${eslintDisable}${baseContent}\n${modulesContent}`,
consoleOutput: `${baseContent}\n${modulesContent}`,
};
}

/**
Expand Down

0 comments on commit 1320f20

Please sign in to comment.