Skip to content

Commit c5c3bea

Browse files
fix improve and simplify types generation logic
1 parent ef45746 commit c5c3bea

File tree

2 files changed

+109
-118
lines changed

2 files changed

+109
-118
lines changed

packages/wrangler/src/__tests__/type-generation.test.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as fs from "fs";
22
import * as TOML from "@iarna/toml";
33
import {
44
constructTSModuleGlob,
5-
constructType,
65
constructTypeKey,
76
generateImportSpecifier,
87
isValidIdentifier,
@@ -65,43 +64,6 @@ describe("constructTSModuleGlob() should return a valid TS glob ", () => {
6564
});
6665
});
6766

68-
describe("constructType", () => {
69-
it("should return a valid type", () => {
70-
expect(constructType("valid", "string")).toBe("valid: string;");
71-
expect(constructType("valid123", "string")).toBe("valid123: string;");
72-
expect(constructType("valid_123", "string")).toBe("valid_123: string;");
73-
expect(constructType("valid_123_", "string")).toBe("valid_123_: string;");
74-
expect(constructType("_valid_123_", "string")).toBe("_valid_123_: string;");
75-
expect(constructType("_valid_123_", "string")).toBe("_valid_123_: string;");
76-
77-
expect(constructType("123invalid", "string")).toBe('"123invalid": string;');
78-
expect(constructType("invalid-123", "string")).toBe(
79-
'"invalid-123": string;'
80-
);
81-
expect(constructType("invalid 123", "string")).toBe(
82-
'"invalid 123": string;'
83-
);
84-
85-
expect(constructType("valid", 'a"', false)).toBe('valid: "a\\"";');
86-
expect(constructType("valid", "a\\", false)).toBe('valid: "a\\\\";');
87-
expect(constructType("valid", "a\\b", false)).toBe('valid: "a\\\\b";');
88-
expect(constructType("valid", 'a\\b"', false)).toBe('valid: "a\\\\b\\"";');
89-
90-
expect(constructType("valid", 1)).toBe("valid: 1;");
91-
expect(constructType("valid", 12345)).toBe("valid: 12345;");
92-
expect(constructType("valid", true)).toBe("valid: true;");
93-
expect(constructType("valid", false)).toBe("valid: false;");
94-
});
95-
});
96-
97-
describe("constructType with multiline strings", () => {
98-
it("should correctly escape newlines in string values", () => {
99-
const multilineString = "This is a\nmulti-line\nstring";
100-
const expected = `valid: "This is a\\nmulti-line\\nstring";`;
101-
expect(constructType("valid", multilineString, false)).toBe(expected);
102-
});
103-
});
104-
10567
describe("generateImportSpecifier", () => {
10668
it("should generate a relative import specifier", () => {
10769
expect(generateImportSpecifier("/app/types.ts", "/app/index.ts")).toBe(
@@ -663,6 +625,46 @@ describe("generateTypes()", () => {
663625
`);
664626
});
665627

628+
it("various different types of vars", async () => {
629+
fs.writeFileSync(
630+
"./wrangler.toml",
631+
TOML.stringify({
632+
vars: {
633+
"var-a": '"a\\""',
634+
"var-a-1": '"a\\\\"',
635+
"var-a-b": '"a\\\\b"',
636+
"var-a-b-": '"a\\\\b\\""',
637+
1: 1,
638+
12345: 12345,
639+
true: true,
640+
false: false,
641+
"multi\nline\nvar": "this\nis\na\nmulti\nline\nvariable!",
642+
},
643+
} as TOML.JsonMap),
644+
"utf-8"
645+
);
646+
await runWrangler("types");
647+
648+
expect(std.out).toMatchInlineSnapshot(`
649+
"Generating project types...
650+
651+
interface Env {
652+
\\"1\\": 1;
653+
\\"12345\\": 12345;
654+
\\"var-a\\": \\"/\\"a///\\"/\\"\\";
655+
\\"var-a-1\\": \\"/\\"a/////\\"\\";
656+
\\"var-a-b\\": \\"/\\"a////b/\\"\\";
657+
\\"var-a-b-\\": \\"/\\"a////b///\\"/\\"\\";
658+
true: true;
659+
false: false;
660+
\\"multi
661+
line
662+
var\\": \\"this/nis/na/nmulti/nline/nvariable!\\";
663+
}
664+
"
665+
`);
666+
});
667+
666668
describe("vars present in multiple environments", () => {
667669
beforeEach(() => {
668670
fs.writeFileSync(
@@ -861,7 +863,7 @@ describe("generateTypes()", () => {
861863
});
862864
});
863865

864-
it("should allow multiple customization to be applied together", async () => {
866+
it("should allow multiple customizations to be applied together", async () => {
865867
fs.writeFileSync(
866868
"./wrangler.toml",
867869
TOML.stringify({

0 commit comments

Comments
 (0)