Debian Codegraph @

node/@bufbuild/protobuf/dist/esm/reflect/names.js

raw
1 // Copyright 2021-2026 Buf Technologies, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 /**
15 * Return a fully-qualified name for a Protobuf descriptor.
16 * For a file descriptor, return the original file path.
17 *
18 * See https://protobuf.com/docs/language-spec#fully-qualified-names
19 */
20 export function qualifiedName(desc) {
21 switch (desc.kind) {
22 case "field":
23 case "oneof":
24 case "rpc":
25 return desc.parent.typeName + "." + desc.name;
26 case "enum_value": {
27 const p = desc.parent.parent
28 ? desc.parent.parent.typeName
29 : desc.parent.file.proto.package;
30 return p + (p.length > 0 ? "." : "") + desc.name;
31 }
32 case "service":
33 case "message":
34 case "enum":
35 case "extension":
36 return desc.typeName;
37 case "file":
38 return desc.proto.name;
39 }
40 }
41 /**
42 * Converts snake_case to protoCamelCase according to the convention
43 * used by protoc to convert a field name to a JSON name.
44 *
45 * See https://protobuf.com/docs/language-spec#default-json-names
46 *
47 * The function protoSnakeCase provides the reverse.
48 */
49 export function protoCamelCase(snakeCase) {
50 let capNext = false;
51 const b = [];
52 for (let i = 0; i < snakeCase.length; i++) {
53 let c = snakeCase.charAt(i);
54 switch (c) {
55 case "_":
56 capNext = true;
57 break;
58 case "0":
59 case "1":
60 case "2":
61 case "3":
62 case "4":
63 case "5":
64 case "6":
65 case "7":
66 case "8":
67 case "9":
68 b.push(c);
69 capNext = false;
70 break;
71 default:
72 if (capNext) {
73 capNext = false;
74 c = c.toUpperCase();
75 }
76 b.push(c);
77 break;
78 }
79 }
80 return b.join("");
81 }
82 /**
83 * Converts protoCamelCase to snake_case.
84 *
85 * This function is the reverse of function protoCamelCase. Note that some names
86 * are not reversible - for example, "foo__bar" -> "fooBar" -> "foo_bar".
87 */
88 export function protoSnakeCase(lowerCamelCase) {
89 return lowerCamelCase.replace(/[A-Z]/g, (letter) => "_" + letter.toLowerCase());
90 }
91 /**
92 * Names that cannot be used for object properties because they are reserved
93 * by built-in JavaScript properties.
94 */
95 const reservedObjectProperties = new Set([
96 // names reserved by JavaScript
97 "constructor",
98 "toString",
99 "toJSON",
100 "valueOf",
101 ]);
102 /**
103 * Escapes names that are reserved for ECMAScript built-in object properties.
104 *
105 * Also see safeIdentifier() from @bufbuild/protoplugin.
106 */
107 export function safeObjectProperty(name) {
108 return reservedObjectProperties.has(name) ? name + "$" : name;
109 }