Skip to content

Commit

Permalink
Fix: Correctly escape newlines in constructType function (#5955)
Browse files Browse the repository at this point in the history
* Fix: Escape newlines in multi-line strings for `wrangler types` generation

* Add test for handling multiline strings in `constructType` function

* Add changeset for newline escape fix in constructType function

* fix: Simplify `constructType` function by using JSON.stringify for value escaping

* Update packages/wrangler/src/type-generation.ts

Remove unnecessary quotation marks around JSON.stringify output

Co-authored-by: Somhairle MacLeòid <[email protected]>

* fix changeset formatting

* fix type-gen escaping tests

---------

Co-authored-by: Somhairle MacLeòid <[email protected]>
Co-authored-by: Peter Bacon Darwin <[email protected]>
  • Loading branch information
3 people authored Jul 15, 2024
1 parent fa1016c commit db11a0f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/shy-planets-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: correctly escape newlines in `constructType` function for multiline strings

This fix ensures that multiline strings are correctly handled by the `constructType` function. Newlines are now properly escaped to prevent invalid JavaScript code generation when using the `wrangler types` command. This improves robustness and prevents errors related to multiline string handling in environment variables and other configuration settings.
14 changes: 11 additions & 3 deletions packages/wrangler/src/__tests__/type-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ describe("constructType", () => {
);

expect(constructType("valid", 'a"', false)).toBe('valid: "a\\"";');
expect(constructType("valid", "a\\", false)).toBe('valid: "a\\";');
expect(constructType("valid", "a\\b", false)).toBe('valid: "a\\b";');
expect(constructType("valid", 'a\\b"', false)).toBe('valid: "a\\b\\"";');
expect(constructType("valid", "a\\", false)).toBe('valid: "a\\\\";');
expect(constructType("valid", "a\\b", false)).toBe('valid: "a\\\\b";');
expect(constructType("valid", 'a\\b"', false)).toBe('valid: "a\\\\b\\"";');

expect(constructType("valid", 1)).toBe("valid: 1;");
expect(constructType("valid", 12345)).toBe("valid: 12345;");
Expand All @@ -76,6 +76,14 @@ describe("constructType", () => {
});
});

describe("constructType with multiline strings", () => {
it("should correctly escape newlines in string values", () => {
const multilineString = "This is a\nmulti-line\nstring";
const expected = `valid: "This is a\\nmulti-line\\nstring";`;
expect(constructType("valid", multilineString, false)).toBe(expected);
});
});

const bindingsConfigMock: Omit<
EnvironmentNonInheritable,
"define" | "tail_consumers" | "constellation" | "cloudchamber"
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/type-generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function constructType(
if (useRawVal) {
return `${typeKey}: ${value};`;
}
return `${typeKey}: "${value.replace(/"/g, '\\"')}";`;
return `${typeKey}: ${JSON.stringify(value)};`;
}
if (typeof value === "number" || typeof value === "boolean") {
return `${typeKey}: ${value};`;
Expand Down

0 comments on commit db11a0f

Please sign in to comment.