@bufbuild/protoplugin
Advanced tools
Comparing version 2.0.0-alpha.1 to 2.0.0-alpha.2
@@ -67,2 +67,5 @@ "use strict"; | ||
} | ||
for (const o of (0, source_code_info_js_1.getFeatureOptionStrings)(file)) { | ||
builder.push(`// option ${o};\n`); | ||
} | ||
builder.push("/* eslint-disable */\n"); | ||
@@ -69,0 +72,0 @@ if (tsNoCheck) { |
@@ -18,2 +18,3 @@ "use strict"; | ||
const source_code_info_js_1 = require("../source-code-info.js"); | ||
const reflect_1 = require("@bufbuild/protobuf/reflect"); | ||
function createJsDocTextFromDesc(desc) { | ||
@@ -54,2 +55,11 @@ const comments = (0, source_code_info_js_1.getComments)(desc); | ||
break; | ||
case "message": | ||
case "enum": { | ||
text += `@generated from ${desc.toString()}`; | ||
const featureOptions = (0, source_code_info_js_1.getFeatureOptionStrings)(desc); | ||
if (featureOptions.length > 0) { | ||
text += `\n@generated with ${featureOptions.length > 1 ? "options" : "option"} ${featureOptions.join(", ")}`; | ||
} | ||
break; | ||
} | ||
default: | ||
@@ -59,10 +69,12 @@ text += `@generated from ${desc.toString()}`; | ||
} | ||
let deprecated = desc.deprecated; | ||
let deprecated; | ||
switch (desc.kind) { | ||
case "enum": | ||
case "message": | ||
case "service": | ||
deprecated = deprecated || desc.file.deprecated; | ||
case "field": | ||
case "enum_value": | ||
case "rpc": | ||
deprecated = desc.deprecated; | ||
break; | ||
default: | ||
deprecated = | ||
desc.deprecated || (0, reflect_1.parentTypes)(desc).some((d) => d.deprecated); | ||
break; | ||
@@ -69,0 +81,0 @@ } |
@@ -24,3 +24,3 @@ "use strict"; | ||
const rewriteImports = []; | ||
let importExtension = ".js"; | ||
let importExtension = ""; | ||
let jsImportStyle = "module"; | ||
@@ -27,0 +27,0 @@ const rawParameters = []; |
@@ -95,4 +95,12 @@ "use strict"; | ||
} | ||
// Our goal is to provide options with source retention to plugin authors. | ||
// CodeGeneratorRequest.proto_file elides options with source retention for | ||
// files to generate. For these files, we take the file from source_file_descriptors, | ||
// which does include options with source retention. | ||
const allProtoWithSourceOptions = request.protoFile.map((protoFile) => { | ||
const sourceFile = request.sourceFileDescriptors.find((s) => s.name == protoFile.name); | ||
return sourceFile !== null && sourceFile !== void 0 ? sourceFile : protoFile; | ||
}); | ||
const registry = (0, reflect_1.createFileRegistry)((0, protobuf_1.create)(wkt_1.FileDescriptorSetDesc, { | ||
file: request.protoFile, | ||
file: allProtoWithSourceOptions, | ||
})); | ||
@@ -99,0 +107,0 @@ const allFiles = []; |
@@ -1,2 +0,2 @@ | ||
import type { DescField, DescEnumValue, DescExtension, DescComments, AnyDesc, DescFile } from "@bufbuild/protobuf"; | ||
import { type AnyDesc, type DescComments, type DescEnumValue, type DescExtension, type DescField, type DescFile } from "@bufbuild/protobuf"; | ||
/** | ||
@@ -15,2 +15,7 @@ * Get comments on the package element in the protobuf source. | ||
/** | ||
* Get feature options set on the element in the protobuf source. This returns | ||
* compact (e.g. fields) or regular options (e.g. files) as an array of strings. | ||
*/ | ||
export declare function getFeatureOptionStrings(desc: AnyDesc): string[]; | ||
/** | ||
* Return a string that matches the definition of a field in the protobuf | ||
@@ -17,0 +22,0 @@ * source. Does not take custom options into account. |
@@ -16,3 +16,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDeclarationString = exports.getComments = exports.getSyntaxComments = exports.getPackageComments = void 0; | ||
exports.getDeclarationString = exports.getFeatureOptionStrings = exports.getComments = exports.getSyntaxComments = exports.getPackageComments = void 0; | ||
const protobuf_1 = require("@bufbuild/protobuf"); | ||
@@ -26,3 +26,3 @@ const reflect_1 = require("@bufbuild/protobuf/reflect"); | ||
return findComments(desc.proto.sourceCodeInfo, [ | ||
FieldNumber.FileDescriptorProto_Package, | ||
wkt_1.FileDescriptorProtoDesc.field.package.number, | ||
]); | ||
@@ -36,3 +36,3 @@ } | ||
return findComments(desc.proto.sourceCodeInfo, [ | ||
FieldNumber.FileDescriptorProto_Syntax, | ||
wkt_1.FileDescriptorProtoDesc.field.syntax.number, | ||
]); | ||
@@ -52,7 +52,7 @@ } | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_EnumType, | ||
wkt_1.DescriptorProtoDesc.field.enumType.number, | ||
desc.parent.proto.enumType.indexOf(desc.proto), | ||
] | ||
: [ | ||
FieldNumber.FileDescriptorProto_EnumType, | ||
wkt_1.FileDescriptorProtoDesc.field.enumType.number, | ||
desc.file.proto.enumType.indexOf(desc.proto), | ||
@@ -65,3 +65,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_OneofDecl, | ||
wkt_1.DescriptorProtoDesc.field.oneofDecl.number, | ||
desc.parent.proto.oneofDecl.indexOf(desc.proto), | ||
@@ -75,7 +75,7 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_NestedType, | ||
wkt_1.DescriptorProtoDesc.field.nestedType.number, | ||
desc.parent.proto.nestedType.indexOf(desc.proto), | ||
] | ||
: [ | ||
FieldNumber.FileDescriptorProto_MessageType, | ||
wkt_1.FileDescriptorProtoDesc.field.messageType.number, | ||
desc.file.proto.messageType.indexOf(desc.proto), | ||
@@ -88,3 +88,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.EnumDescriptorProto_Value, | ||
wkt_1.EnumDescriptorProtoDesc.field.value.number, | ||
desc.parent.proto.value.indexOf(desc.proto), | ||
@@ -97,3 +97,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_Field, | ||
wkt_1.DescriptorProtoDesc.field.field.number, | ||
desc.parent.proto.field.indexOf(desc.proto), | ||
@@ -107,7 +107,7 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_Extension, | ||
wkt_1.DescriptorProtoDesc.field.extension.number, | ||
desc.parent.proto.extension.indexOf(desc.proto), | ||
] | ||
: [ | ||
FieldNumber.FileDescriptorProto_Extension, | ||
wkt_1.FileDescriptorProtoDesc.field.extension.number, | ||
desc.file.proto.extension.indexOf(desc.proto), | ||
@@ -119,3 +119,3 @@ ]; | ||
path = [ | ||
FieldNumber.FileDescriptorProto_Service, | ||
wkt_1.FileDescriptorProtoDesc.field.service.number, | ||
desc.file.proto.service.indexOf(desc.proto), | ||
@@ -128,3 +128,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.ServiceDescriptorProto_Method, | ||
wkt_1.ServiceDescriptorProtoDesc.field.method.number, | ||
desc.parent.proto.method.indexOf(desc.proto), | ||
@@ -139,2 +139,26 @@ ]; | ||
/** | ||
* Get feature options set on the element in the protobuf source. This returns | ||
* compact (e.g. fields) or regular options (e.g. files) as an array of strings. | ||
*/ | ||
function getFeatureOptionStrings(desc) { | ||
var _a, _b; | ||
const strings = []; | ||
const features = (_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.features; | ||
if (features !== undefined) { | ||
const r = (0, reflect_1.reflect)(wkt_1.FeatureSetDesc, features); | ||
for (const f of r.fields) { | ||
if (f.fieldKind != "enum" || !r.isSet(f)) { | ||
continue; | ||
} | ||
const val = r.get(f); | ||
const name = (_b = f.enum.values.find((v) => v.number == val)) === null || _b === void 0 ? void 0 : _b.name; | ||
if (name !== undefined) { | ||
strings.push(`features.${f.name} = ${name}`); | ||
} | ||
} | ||
} | ||
return strings; | ||
} | ||
exports.getFeatureOptionStrings = getFeatureOptionStrings; | ||
/** | ||
* Return a string that matches the definition of a field in the protobuf | ||
@@ -152,3 +176,2 @@ * source. Does not take custom options into account. | ||
} | ||
const file = desc.kind === "extension" ? desc.file : desc.parent.file; | ||
const parts = []; | ||
@@ -168,8 +191,6 @@ function typeName(f) { | ||
case "message": | ||
if (file.edition === wkt_1.Edition.EDITION_PROTO2 && | ||
(0, protobuf_1.isFieldSet)(wkt_1.FieldDescriptorProtoDesc, desc.proto, "label") && | ||
desc.proto.label == wkt_1.FieldDescriptorProto_Label.REQUIRED) { | ||
if (fieldHasRequiredKeyword(desc)) { | ||
parts.push("required"); | ||
} | ||
if (desc.optional) { | ||
if (fieldHasOptionalKeyword(desc)) { | ||
parts.push("optional"); | ||
@@ -193,6 +214,6 @@ } | ||
if (protoOptions !== undefined && | ||
(0, protobuf_1.isFieldSet)(wkt_1.FieldOptionsDesc, protoOptions, "packed")) { | ||
(0, protobuf_1.isFieldSet)(protoOptions, wkt_1.FieldOptionsDesc.field.packed)) { | ||
options.push(`packed = ${protoOptions.packed.toString()}`); | ||
} | ||
if ((0, protobuf_1.isFieldSet)(wkt_1.FieldDescriptorProtoDesc, desc.proto, "defaultValue")) { | ||
if ((0, protobuf_1.isFieldSet)(desc.proto, wkt_1.FieldDescriptorProtoDesc.field.defaultValue)) { | ||
let defaultValue = desc.proto.defaultValue; | ||
@@ -209,9 +230,10 @@ if (desc.proto.type == wkt_1.FieldDescriptorProto_Type.BYTES || | ||
if (protoOptions !== undefined && | ||
(0, protobuf_1.isFieldSet)(wkt_1.FieldOptionsDesc, protoOptions, "jstype")) { | ||
(0, protobuf_1.isFieldSet)(protoOptions, wkt_1.FieldOptionsDesc.field.jstype)) { | ||
options.push(`jstype = ${wkt_1.FieldOptions_JSType[protoOptions.jstype]}`); | ||
} | ||
if (protoOptions !== undefined && | ||
(0, protobuf_1.isFieldSet)(wkt_1.FieldOptionsDesc, protoOptions, "deprecated")) { | ||
(0, protobuf_1.isFieldSet)(protoOptions, wkt_1.FieldOptionsDesc.field.deprecated)) { | ||
options.push(`deprecated = true`); | ||
} | ||
options.push(...getFeatureOptionStrings(desc)); | ||
if (options.length > 0) { | ||
@@ -224,2 +246,27 @@ parts.push("[" + options.join(", ") + "]"); | ||
/** | ||
* Whether this field was declared with `required` in the protobuf source. | ||
*/ | ||
function fieldHasRequiredKeyword(field) { | ||
const edition = (field.kind == "extension" ? field.file : field.parent.file) | ||
.edition; | ||
return (edition == wkt_1.Edition.EDITION_PROTO2 && | ||
field.proto.label == wkt_1.FieldDescriptorProto_Label.REQUIRED); | ||
} | ||
/** | ||
* Whether this field was declared with `optional` in the protobuf source. | ||
* Note that message fields are always optional. It is impossible to determine | ||
* whether the keyword was used. | ||
*/ | ||
function fieldHasOptionalKeyword(field) { | ||
const edition = (field.kind == "extension" ? field.file : field.parent.file) | ||
.edition; | ||
if (edition == wkt_1.Edition.EDITION_PROTO2) { | ||
return (!field.oneof && field.proto.label == wkt_1.FieldDescriptorProto_Label.OPTIONAL); | ||
} | ||
if (edition == wkt_1.Edition.EDITION_PROTO3) { | ||
return field.proto.proto3Optional; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Find comments. | ||
@@ -243,6 +290,6 @@ */ | ||
leadingDetached: location.leadingDetachedComments, | ||
leading: (0, protobuf_1.isFieldSet)(wkt_1.SourceCodeInfo_LocationDesc, location, "leadingComments") | ||
leading: (0, protobuf_1.isFieldSet)(location, wkt_1.SourceCodeInfo_LocationDesc.field.leadingComments) | ||
? location.leadingComments | ||
: undefined, | ||
trailing: (0, protobuf_1.isFieldSet)(wkt_1.SourceCodeInfo_LocationDesc, location, "trailingComments") | ||
trailing: (0, protobuf_1.isFieldSet)(location, wkt_1.SourceCodeInfo_LocationDesc.field.trailingComments) | ||
? location.trailingComments | ||
@@ -258,21 +305,1 @@ : undefined, | ||
} | ||
/** | ||
* The following field numbers are used to find comments in | ||
* google.protobuf.SourceCodeInfo. | ||
*/ | ||
var FieldNumber; | ||
(function (FieldNumber) { | ||
FieldNumber[FieldNumber["FileDescriptorProto_Package"] = 2] = "FileDescriptorProto_Package"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_MessageType"] = 4] = "FileDescriptorProto_MessageType"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_EnumType"] = 5] = "FileDescriptorProto_EnumType"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_Service"] = 6] = "FileDescriptorProto_Service"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_Extension"] = 7] = "FileDescriptorProto_Extension"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_Syntax"] = 12] = "FileDescriptorProto_Syntax"; | ||
FieldNumber[FieldNumber["DescriptorProto_Field"] = 2] = "DescriptorProto_Field"; | ||
FieldNumber[FieldNumber["DescriptorProto_NestedType"] = 3] = "DescriptorProto_NestedType"; | ||
FieldNumber[FieldNumber["DescriptorProto_EnumType"] = 4] = "DescriptorProto_EnumType"; | ||
FieldNumber[FieldNumber["DescriptorProto_Extension"] = 6] = "DescriptorProto_Extension"; | ||
FieldNumber[FieldNumber["DescriptorProto_OneofDecl"] = 8] = "DescriptorProto_OneofDecl"; | ||
FieldNumber[FieldNumber["EnumDescriptorProto_Value"] = 2] = "EnumDescriptorProto_Value"; | ||
FieldNumber[FieldNumber["ServiceDescriptorProto_Method"] = 2] = "ServiceDescriptorProto_Method"; | ||
})(FieldNumber || (FieldNumber = {})); |
@@ -15,3 +15,3 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
import { Edition } from "@bufbuild/protobuf/wkt"; | ||
import { getPackageComments, getSyntaxComments } from "../source-code-info.js"; | ||
import { getPackageComments, getSyntaxComments, getFeatureOptionStrings, } from "../source-code-info.js"; | ||
export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) { | ||
@@ -65,2 +65,5 @@ const builder = []; | ||
} | ||
for (const o of getFeatureOptionStrings(file)) { | ||
builder.push(`// option ${o};\n`); | ||
} | ||
builder.push("/* eslint-disable */\n"); | ||
@@ -67,0 +70,0 @@ if (tsNoCheck) { |
@@ -14,3 +14,4 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
// limitations under the License. | ||
import { getComments, getDeclarationString } from "../source-code-info.js"; | ||
import { getComments, getDeclarationString, getFeatureOptionStrings, } from "../source-code-info.js"; | ||
import { parentTypes } from "@bufbuild/protobuf/reflect"; | ||
export function createJsDocTextFromDesc(desc) { | ||
@@ -51,2 +52,11 @@ const comments = getComments(desc); | ||
break; | ||
case "message": | ||
case "enum": { | ||
text += `@generated from ${desc.toString()}`; | ||
const featureOptions = getFeatureOptionStrings(desc); | ||
if (featureOptions.length > 0) { | ||
text += `\n@generated with ${featureOptions.length > 1 ? "options" : "option"} ${featureOptions.join(", ")}`; | ||
} | ||
break; | ||
} | ||
default: | ||
@@ -56,10 +66,12 @@ text += `@generated from ${desc.toString()}`; | ||
} | ||
let deprecated = desc.deprecated; | ||
let deprecated; | ||
switch (desc.kind) { | ||
case "enum": | ||
case "message": | ||
case "service": | ||
deprecated = deprecated || desc.file.deprecated; | ||
case "field": | ||
case "enum_value": | ||
case "rpc": | ||
deprecated = desc.deprecated; | ||
break; | ||
default: | ||
deprecated = | ||
desc.deprecated || parentTypes(desc).some((d) => d.deprecated); | ||
break; | ||
@@ -66,0 +78,0 @@ } |
@@ -21,3 +21,3 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
const rewriteImports = []; | ||
let importExtension = ".js"; | ||
let importExtension = ""; | ||
let jsImportStyle = "module"; | ||
@@ -24,0 +24,0 @@ const rawParameters = []; |
@@ -91,4 +91,12 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
} | ||
// Our goal is to provide options with source retention to plugin authors. | ||
// CodeGeneratorRequest.proto_file elides options with source retention for | ||
// files to generate. For these files, we take the file from source_file_descriptors, | ||
// which does include options with source retention. | ||
const allProtoWithSourceOptions = request.protoFile.map((protoFile) => { | ||
const sourceFile = request.sourceFileDescriptors.find((s) => s.name == protoFile.name); | ||
return sourceFile !== null && sourceFile !== void 0 ? sourceFile : protoFile; | ||
}); | ||
const registry = createFileRegistry(create(FileDescriptorSetDesc, { | ||
file: request.protoFile, | ||
file: allProtoWithSourceOptions, | ||
})); | ||
@@ -95,0 +103,0 @@ const allFiles = []; |
@@ -1,2 +0,2 @@ | ||
import type { DescField, DescEnumValue, DescExtension, DescComments, AnyDesc, DescFile } from "@bufbuild/protobuf"; | ||
import { type AnyDesc, type DescComments, type DescEnumValue, type DescExtension, type DescField, type DescFile } from "@bufbuild/protobuf"; | ||
/** | ||
@@ -15,2 +15,7 @@ * Get comments on the package element in the protobuf source. | ||
/** | ||
* Get feature options set on the element in the protobuf source. This returns | ||
* compact (e.g. fields) or regular options (e.g. files) as an array of strings. | ||
*/ | ||
export declare function getFeatureOptionStrings(desc: AnyDesc): string[]; | ||
/** | ||
* Return a string that matches the definition of a field in the protobuf | ||
@@ -17,0 +22,0 @@ * source. Does not take custom options into account. |
@@ -14,5 +14,5 @@ // Copyright 2021-2024 Buf Technologies, Inc. | ||
// limitations under the License. | ||
import { isFieldSet } from "@bufbuild/protobuf"; | ||
import { ScalarType, protoCamelCase } from "@bufbuild/protobuf/reflect"; | ||
import { Edition, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldDescriptorProtoDesc, FieldOptions_JSType, FieldOptionsDesc, SourceCodeInfo_LocationDesc, } from "@bufbuild/protobuf/wkt"; | ||
import { isFieldSet, } from "@bufbuild/protobuf"; | ||
import { protoCamelCase, reflect, ScalarType, } from "@bufbuild/protobuf/reflect"; | ||
import { Edition, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldDescriptorProtoDesc, FieldOptions_JSType, FieldOptionsDesc, FeatureSetDesc, SourceCodeInfo_LocationDesc, FileDescriptorProtoDesc, DescriptorProtoDesc, EnumDescriptorProtoDesc, ServiceDescriptorProtoDesc, } from "@bufbuild/protobuf/wkt"; | ||
/** | ||
@@ -23,3 +23,3 @@ * Get comments on the package element in the protobuf source. | ||
return findComments(desc.proto.sourceCodeInfo, [ | ||
FieldNumber.FileDescriptorProto_Package, | ||
FileDescriptorProtoDesc.field.package.number, | ||
]); | ||
@@ -32,3 +32,3 @@ } | ||
return findComments(desc.proto.sourceCodeInfo, [ | ||
FieldNumber.FileDescriptorProto_Syntax, | ||
FileDescriptorProtoDesc.field.syntax.number, | ||
]); | ||
@@ -47,7 +47,7 @@ } | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_EnumType, | ||
DescriptorProtoDesc.field.enumType.number, | ||
desc.parent.proto.enumType.indexOf(desc.proto), | ||
] | ||
: [ | ||
FieldNumber.FileDescriptorProto_EnumType, | ||
FileDescriptorProtoDesc.field.enumType.number, | ||
desc.file.proto.enumType.indexOf(desc.proto), | ||
@@ -60,3 +60,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_OneofDecl, | ||
DescriptorProtoDesc.field.oneofDecl.number, | ||
desc.parent.proto.oneofDecl.indexOf(desc.proto), | ||
@@ -70,7 +70,7 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_NestedType, | ||
DescriptorProtoDesc.field.nestedType.number, | ||
desc.parent.proto.nestedType.indexOf(desc.proto), | ||
] | ||
: [ | ||
FieldNumber.FileDescriptorProto_MessageType, | ||
FileDescriptorProtoDesc.field.messageType.number, | ||
desc.file.proto.messageType.indexOf(desc.proto), | ||
@@ -83,3 +83,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.EnumDescriptorProto_Value, | ||
EnumDescriptorProtoDesc.field.value.number, | ||
desc.parent.proto.value.indexOf(desc.proto), | ||
@@ -92,3 +92,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_Field, | ||
DescriptorProtoDesc.field.field.number, | ||
desc.parent.proto.field.indexOf(desc.proto), | ||
@@ -102,7 +102,7 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.DescriptorProto_Extension, | ||
DescriptorProtoDesc.field.extension.number, | ||
desc.parent.proto.extension.indexOf(desc.proto), | ||
] | ||
: [ | ||
FieldNumber.FileDescriptorProto_Extension, | ||
FileDescriptorProtoDesc.field.extension.number, | ||
desc.file.proto.extension.indexOf(desc.proto), | ||
@@ -114,3 +114,3 @@ ]; | ||
path = [ | ||
FieldNumber.FileDescriptorProto_Service, | ||
FileDescriptorProtoDesc.field.service.number, | ||
desc.file.proto.service.indexOf(desc.proto), | ||
@@ -123,3 +123,3 @@ ]; | ||
...getComments(desc.parent).sourcePath, | ||
FieldNumber.ServiceDescriptorProto_Method, | ||
ServiceDescriptorProtoDesc.field.method.number, | ||
desc.parent.proto.method.indexOf(desc.proto), | ||
@@ -133,2 +133,25 @@ ]; | ||
/** | ||
* Get feature options set on the element in the protobuf source. This returns | ||
* compact (e.g. fields) or regular options (e.g. files) as an array of strings. | ||
*/ | ||
export function getFeatureOptionStrings(desc) { | ||
var _a, _b; | ||
const strings = []; | ||
const features = (_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.features; | ||
if (features !== undefined) { | ||
const r = reflect(FeatureSetDesc, features); | ||
for (const f of r.fields) { | ||
if (f.fieldKind != "enum" || !r.isSet(f)) { | ||
continue; | ||
} | ||
const val = r.get(f); | ||
const name = (_b = f.enum.values.find((v) => v.number == val)) === null || _b === void 0 ? void 0 : _b.name; | ||
if (name !== undefined) { | ||
strings.push(`features.${f.name} = ${name}`); | ||
} | ||
} | ||
} | ||
return strings; | ||
} | ||
/** | ||
* Return a string that matches the definition of a field in the protobuf | ||
@@ -146,3 +169,2 @@ * source. Does not take custom options into account. | ||
} | ||
const file = desc.kind === "extension" ? desc.file : desc.parent.file; | ||
const parts = []; | ||
@@ -162,8 +184,6 @@ function typeName(f) { | ||
case "message": | ||
if (file.edition === Edition.EDITION_PROTO2 && | ||
isFieldSet(FieldDescriptorProtoDesc, desc.proto, "label") && | ||
desc.proto.label == FieldDescriptorProto_Label.REQUIRED) { | ||
if (fieldHasRequiredKeyword(desc)) { | ||
parts.push("required"); | ||
} | ||
if (desc.optional) { | ||
if (fieldHasOptionalKeyword(desc)) { | ||
parts.push("optional"); | ||
@@ -187,6 +207,6 @@ } | ||
if (protoOptions !== undefined && | ||
isFieldSet(FieldOptionsDesc, protoOptions, "packed")) { | ||
isFieldSet(protoOptions, FieldOptionsDesc.field.packed)) { | ||
options.push(`packed = ${protoOptions.packed.toString()}`); | ||
} | ||
if (isFieldSet(FieldDescriptorProtoDesc, desc.proto, "defaultValue")) { | ||
if (isFieldSet(desc.proto, FieldDescriptorProtoDesc.field.defaultValue)) { | ||
let defaultValue = desc.proto.defaultValue; | ||
@@ -203,9 +223,10 @@ if (desc.proto.type == FieldDescriptorProto_Type.BYTES || | ||
if (protoOptions !== undefined && | ||
isFieldSet(FieldOptionsDesc, protoOptions, "jstype")) { | ||
isFieldSet(protoOptions, FieldOptionsDesc.field.jstype)) { | ||
options.push(`jstype = ${FieldOptions_JSType[protoOptions.jstype]}`); | ||
} | ||
if (protoOptions !== undefined && | ||
isFieldSet(FieldOptionsDesc, protoOptions, "deprecated")) { | ||
isFieldSet(protoOptions, FieldOptionsDesc.field.deprecated)) { | ||
options.push(`deprecated = true`); | ||
} | ||
options.push(...getFeatureOptionStrings(desc)); | ||
if (options.length > 0) { | ||
@@ -217,2 +238,27 @@ parts.push("[" + options.join(", ") + "]"); | ||
/** | ||
* Whether this field was declared with `required` in the protobuf source. | ||
*/ | ||
function fieldHasRequiredKeyword(field) { | ||
const edition = (field.kind == "extension" ? field.file : field.parent.file) | ||
.edition; | ||
return (edition == Edition.EDITION_PROTO2 && | ||
field.proto.label == FieldDescriptorProto_Label.REQUIRED); | ||
} | ||
/** | ||
* Whether this field was declared with `optional` in the protobuf source. | ||
* Note that message fields are always optional. It is impossible to determine | ||
* whether the keyword was used. | ||
*/ | ||
function fieldHasOptionalKeyword(field) { | ||
const edition = (field.kind == "extension" ? field.file : field.parent.file) | ||
.edition; | ||
if (edition == Edition.EDITION_PROTO2) { | ||
return (!field.oneof && field.proto.label == FieldDescriptorProto_Label.OPTIONAL); | ||
} | ||
if (edition == Edition.EDITION_PROTO3) { | ||
return field.proto.proto3Optional; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Find comments. | ||
@@ -236,6 +282,6 @@ */ | ||
leadingDetached: location.leadingDetachedComments, | ||
leading: isFieldSet(SourceCodeInfo_LocationDesc, location, "leadingComments") | ||
leading: isFieldSet(location, SourceCodeInfo_LocationDesc.field.leadingComments) | ||
? location.leadingComments | ||
: undefined, | ||
trailing: isFieldSet(SourceCodeInfo_LocationDesc, location, "trailingComments") | ||
trailing: isFieldSet(location, SourceCodeInfo_LocationDesc.field.trailingComments) | ||
? location.trailingComments | ||
@@ -251,21 +297,1 @@ : undefined, | ||
} | ||
/** | ||
* The following field numbers are used to find comments in | ||
* google.protobuf.SourceCodeInfo. | ||
*/ | ||
var FieldNumber; | ||
(function (FieldNumber) { | ||
FieldNumber[FieldNumber["FileDescriptorProto_Package"] = 2] = "FileDescriptorProto_Package"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_MessageType"] = 4] = "FileDescriptorProto_MessageType"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_EnumType"] = 5] = "FileDescriptorProto_EnumType"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_Service"] = 6] = "FileDescriptorProto_Service"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_Extension"] = 7] = "FileDescriptorProto_Extension"; | ||
FieldNumber[FieldNumber["FileDescriptorProto_Syntax"] = 12] = "FileDescriptorProto_Syntax"; | ||
FieldNumber[FieldNumber["DescriptorProto_Field"] = 2] = "DescriptorProto_Field"; | ||
FieldNumber[FieldNumber["DescriptorProto_NestedType"] = 3] = "DescriptorProto_NestedType"; | ||
FieldNumber[FieldNumber["DescriptorProto_EnumType"] = 4] = "DescriptorProto_EnumType"; | ||
FieldNumber[FieldNumber["DescriptorProto_Extension"] = 6] = "DescriptorProto_Extension"; | ||
FieldNumber[FieldNumber["DescriptorProto_OneofDecl"] = 8] = "DescriptorProto_OneofDecl"; | ||
FieldNumber[FieldNumber["EnumDescriptorProto_Value"] = 2] = "EnumDescriptorProto_Value"; | ||
FieldNumber[FieldNumber["ServiceDescriptorProto_Method"] = 2] = "ServiceDescriptorProto_Method"; | ||
})(FieldNumber || (FieldNumber = {})); |
{ | ||
"name": "@bufbuild/protoplugin", | ||
"version": "2.0.0-alpha.1", | ||
"version": "2.0.0-alpha.2", | ||
"license": "(Apache-2.0 AND BSD-3-Clause)", | ||
@@ -39,3 +39,3 @@ "description": "Helps to create your own Protocol Buffers code generators.", | ||
"dependencies": { | ||
"@bufbuild/protobuf": "2.0.0-alpha.1", | ||
"@bufbuild/protobuf": "2.0.0-alpha.2", | ||
"@typescript/vfs": "^1.4.0", | ||
@@ -42,0 +42,0 @@ "typescript": "4.5.2" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
195943
4944
+ Added@bufbuild/protobuf@2.0.0-alpha.2(transitive)
- Removed@bufbuild/protobuf@2.0.0-alpha.1(transitive)