workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: `wrangler types` generates wrong types with dashes, numbers (etc.) in vars, bindings, etc.

Open Cherry opened this issue 1 year ago • 0 comments

Which Cloudflare product(s) does this pertain to?

Wrangler core

What version(s) of the tool(s) are you using?

3.29.0

What version of Node are you using?

20.11.1

What operating system and version are you using?

Windows

Describe the Bug

Observed behavior

With the following wrangler.toml:

compatibility_date = "2022-01-12"
name = "test-name"
main = "./src/index.ts"

[vars]
dashed-var="foo"
"single-quote'"="bar"
'"double-quote"'="baz"
"escaped\"quote"="qux"
"escaped\\backslash"="quux"
123num="quuz'"
123numquote="'''"
true=1
false=42
null=0

Run wrangler types.

The generated types file includes:

interface Env {
	dashed-var: "foo";
	single-quote': "bar";
	"double-quote": "baz";
	escaped"quote: "qux";
	escaped\backslash: "quux";
	123num: "quuz'";
	123numquote: "'''";
	true: "1";
	false: "42";
	null: "0";
	breaking /*: "testing";
}

This is syntactically invalid.

Expected behavior

This should wrap all keys in quotes and handle escaping as needed:

interface Env {
	"dashed-var": "foo";
	"single-quote'": "bar";
	"double-quote": "baz";
	"escaped\"quote": "qux";
	"escaped\\backslash": "quux";
	"123num": "quuz'";
	"123numquote": "'''";
	"true": 1;
	"false": 42;
	"null": 0;
	"breaking": "/*: \"testing\"";
}

Some of these are definitely edge-cases and very non-real-world, but at least the dashes, and variables starting with numbers should definitely be handled. These are all valid vars: https://test-name.jross.workers.dev/

Cherry avatar Feb 25 '24 16:02 Cherry