Skip to content

Commit

Permalink
Remove ASCII check
Browse files Browse the repository at this point in the history
Also remove the unnecessary switch statement. This was probably left in accidentally from initial development.

And run prettier.
  • Loading branch information
timostamm committed Nov 1, 2024
1 parent 6137dd4 commit f095a7d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
28 changes: 14 additions & 14 deletions packages/protobuf-test/src/generate-code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ describe("JSON types", () => {
const ok_ts: json_types_ts_json.JsonTypesMessageJson = {
booleanFieldWithCustomName: true,
Foo123_bar$: true,
"foo@":true,
"foo-bar":true,
"1foo":true,
"foo bar":true,
"foo\tbar":true,
"你好":true,
"foo\nbar\\n":true,
"foo@": true,
"foo-bar": true,
"1foo": true,
"foo bar": true,
"foo\tbar": true,
你好: true,
"foo\nbar\\n": true,
doubleField: "Infinity",
bytesField: "aGVsbG8gd29ybGQ=",
int64Field: "123",
Expand All @@ -119,13 +119,13 @@ describe("JSON types", () => {
const ok_js: json_types_js_json.JsonTypesMessageJson = {
booleanFieldWithCustomName: true,
Foo123_bar$: true,
"foo@":true,
"foo-bar":true,
"1foo":true,
"foo bar":true,
"foo\tbar":true,
"你好":true,
"foo\nbar\\n":true,
"foo@": true,
"foo-bar": true,
"1foo": true,
"foo bar": true,
"foo\tbar": true,
你好: true,
"foo\nbar\\n": true,
doubleField: "Infinity",
bytesField: "aGVsbG8gd29ybGQ=",
int64Field: "123",
Expand Down
25 changes: 9 additions & 16 deletions packages/protoc-gen-es/src/protoc-gen-es-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,23 +472,16 @@ function generateMessageJsonShape(f: GeneratedFile, message: DescMessage, target
} else {
f.print(exp, " = {");
for (const field of message.fields) {
switch (field.kind) {
default:
f.print(f.jsDoc(field, " "));
// eslint-disable-next-line no-case-declarations
let jsonName: Printable = field.jsonName;
const startWithNumber = /^[0-9]/;
const containsSpecialChar = /[^a-zA-Z0-9_$]/;
const isAscii = /^[\x00-\x7F]*$/;
if (jsonName === ""
|| startWithNumber.test(jsonName)
|| containsSpecialChar.test(jsonName)
|| !isAscii.test(jsonName)) {
jsonName = f.string(jsonName);
}
f.print(" ", jsonName, "?: ", fieldJsonType(field), ";");
break;
f.print(f.jsDoc(field, " "));
let jsonName: Printable = field.jsonName;
const startWithNumber = /^[0-9]/;
const containsSpecialChar = /[^a-zA-Z0-9_$]/;
if (jsonName === ""
|| startWithNumber.test(jsonName)
|| containsSpecialChar.test(jsonName)) {
jsonName = f.string(jsonName);
}
f.print(" ", jsonName, "?: ", fieldJsonType(field), ";");
if (message.fields.indexOf(field) < message.fields.length - 1) {
f.print();
}
Expand Down

0 comments on commit f095a7d

Please sign in to comment.