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