Skip to content

Commit

Permalink
Support experimental editions in @bufbuild/protoplugin (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored Dec 20, 2023
1 parent 289559e commit 91e72d4
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 42 deletions.
6 changes: 4 additions & 2 deletions packages/protoplugin-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"clean": "rm -rf ./dist/cjs/* ./dist/esm/* ./dist/types/*",
"build": "npm run build:esm+types",
"build:esm+types": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module ES2015 --verbatimModuleSyntax --outDir ./dist/esm --declaration --declarationDir ./dist/types",
"generate": "buf generate",
"pregenerate": "rm -rf src/gen/*/*",
"generate": "protoc --es_out=src/gen --es_opt=ts_nocheck=false,target=ts --proto_path=proto option-enum.proto option-message.proto",
"postgenerate": "license-header src/gen",
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest"
},
"type": "module",
Expand All @@ -15,7 +17,7 @@
"default": "./dist/esm/index.js"
},
"dependencies": {
"@bufbuild/buf": "^1.28.1",
"upstream-protobuf": "*",
"@bufbuild/protoplugin": "1.6.0"
}
}
4 changes: 2 additions & 2 deletions packages/protoplugin-test/src/custom-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import type { DescFile } from "@bufbuild/protobuf";
import { createDescriptorSet, ScalarType } from "@bufbuild/protobuf";
import { UpstreamProtobuf } from "upstream-protobuf";
import { readFileSync } from "node:fs";
import { OptionEnum } from "./gen/proto/option-enum_pb.js";
import { OptionMessage } from "./gen/proto/option-message_pb.js";
import { OptionEnum } from "./gen/option-enum_pb.js";
import { OptionMessage } from "./gen/option-message_pb.js";

describe("custom options", () => {
describe("findCustomScalarOption on file descriptor", () => {
Expand Down
1 change: 1 addition & 0 deletions packages/protoplugin-test/src/file-preamble.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ describe("file preamble", () => {
}) {
return await createTestPluginAndRun({
...opt,
supportsEditions: true,
generateAny(f, schema) {
f.preamble(schema.files[0]);
f.print(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions packages/protoplugin-test/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ type CreateTestPluginAndRunOptions<ReturnLinesOfFirstFile extends boolean | unde
&
{
proto: string | Record<string, string>;
filesToGenerate?: string[];
parameter?: string;
name?: PluginInit["name"];
version?: PluginInit["version"];
parseOption?: PluginInit["parseOption"];
supportsEditions?: PluginInit["supportsEditions"];
featureSetDefaults?: PluginInit["featureSetDefaults"];
}
&
(
Expand All @@ -66,9 +69,7 @@ export async function createTestPluginAndRun(
) {
const protoFiles =
typeof opt.proto == "string" ? { "x.proto": opt.proto } : opt.proto;
const reqBytes = await upstream.createCodeGeneratorRequest(protoFiles, {
parameter: opt.parameter,
});
const reqBytes = await upstream.createCodeGeneratorRequest(protoFiles, opt);
const req = CodeGeneratorRequest.fromBinary(reqBytes);
let plugin: Plugin;
const defaultPluginInit = {
Expand Down
126 changes: 126 additions & 0 deletions packages/protoplugin-test/src/plugin-edition-support.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { describe, expect, test } from "@jest/globals";
import { createTestPluginAndRun } from "./helpers.js";
import {
CodeGeneratorRequest,
CodeGeneratorResponse,
CodeGeneratorResponse_Feature,
Edition,
FeatureSetDefaults,
} from "@bufbuild/protobuf";
import { UpstreamProtobuf } from "upstream-protobuf";
import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";

describe("editions support in plugins", () => {
describe("with default setup", () => {
test("does not set SUPPORTS_EDITIONS", async () => {
const res = await createTestPluginAndRun({
proto: `syntax="proto3";`,
generateAny() {},
});
expect(supportsEditions(res)).toBe(false);
});
});

describe("with opt-in to editions", () => {
test("does set SUPPORTS_EDITIONS", async () => {
const res = await createTestPluginAndRun({
proto: `syntax="proto3";`,
supportsEditions: true,
generateAny() {},
});
expect(supportsEditions(res)).toBe(true);
});
test("generates edition 2023", async () => {
const res = await createTestPluginAndRun({
proto: `edition="2023";`,
supportsEditions: true,
generateAny: (f) => f.print("// placeholder"),
});
expect(res.file.length).toBeGreaterThanOrEqual(1);
});
});

describe("with own feature-set defaults", () => {
test.each([`syntax="proto3"`, `syntax="proto2"`, `edition="2023"`])(
"generates %s",
async (syntax) => {
const upstream = new UpstreamProtobuf();
const featureSetDefaults = FeatureSetDefaults.fromBinary(
await upstream.getFeatureSetDefaults("PROTO2", "2023"),
);
const res = await createTestPluginAndRun({
proto: syntax + ";",
supportsEditions: true,
featureSetDefaults,
generateAny: (f) => f.print("// placeholder"),
});
expect(res.file.length).toBeGreaterThanOrEqual(1);
},
);
test("raises error for unsupported edition from the past", async () => {
const upstream = new UpstreamProtobuf();
const featureSetDefaults = FeatureSetDefaults.fromBinary(
await upstream.getFeatureSetDefaults("PROTO3", "2023"),
);
const resPromise = createTestPluginAndRun({
proto: `syntax="proto2";`,
supportsEditions: true,
featureSetDefaults,
generateAny() {},
});
await expect(resPromise).rejects.toThrow(
/^Edition EDITION_PROTO2 is earlier than the minimum supported edition EDITION_PROTO3$/,
);
});
test("raises error for unsupported edition from the future", async () => {
const upstream = new UpstreamProtobuf();
const reqBytes = await upstream.createCodeGeneratorRequest(
`edition="2023";`, // we're going to modify this
);
const req = CodeGeneratorRequest.fromBinary(reqBytes);
req.parameter = "target=ts";
expect(req.protoFile.length).toBe(1);
req.protoFile[0].edition = Edition.EDITION_99999_TEST_ONLY;
const plugin = createEcmaScriptPlugin({
name: "test",
version: "v1",
generateTs() {},
});
expect(() => plugin.run(req)).toThrow(
/^Edition EDITION_99999_TEST_ONLY is later than the maximum supported edition EDITION_2023$/,
);
});
});

describe("with own feature-set defaults but without opt-in to editions", () => {
test("does not set SUPPORTS_EDITIONS", async () => {
const res = await createTestPluginAndRun({
proto: `syntax="proto3";`,
generateAny() {},
});
expect(supportsEditions(res)).toBe(false);
});
});

function supportsEditions(res: CodeGeneratorResponse): boolean {
const f = res.supportedFeatures ?? 0n;
return (
(f & BigInt(CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS)) ===
BigInt(CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS)
);
}
});
41 changes: 35 additions & 6 deletions packages/protoplugin/src/create-es-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type { FileInfo } from "./ecmascript/generated-file.js";
import type { Plugin } from "./plugin.js";
import { transpile } from "./ecmascript/transpile.js";
import { parseParameter } from "./ecmascript/parameter.js";
import type { FeatureSetDefaults } from "@bufbuild/protobuf";
import {
CodeGeneratorResponse,
CodeGeneratorResponse_Feature,
Expand All @@ -40,6 +41,22 @@ interface PluginInit {
*/
parseOption?: (key: string, value: string) => void;

/**
* Whether the plugin supports editions.
*/
supportsEditions?: boolean;

/**
* By default, plugins support all editions supported by createDescriptorSet()
* from @bufbuild/protobuf, if supportsEditions is enabled.
*
* This option can be used to limit support for specific editions, by providing
* your own google.protobuf.FeatureSetDefaults generated by protoc with the
* flags --experimental_edition_defaults_out, --experimental_edition_defaults_minimum,
* and --experimental_edition_defaults_maximum.
*/
featureSetDefaults?: FeatureSetDefaults;

/**
* A function which will generate TypeScript files based on proto input.
* This function will be invoked by the plugin framework when the plugin runs.
Expand Down Expand Up @@ -106,7 +123,13 @@ export function createEcmaScriptPlugin(init: PluginInit): Plugin {
version: init.version,
run(req) {
const parameter = parseParameter(req.parameter, init.parseOption);
const schema = createSchema(req, parameter, init.name, init.version);
const schema = createSchema(
req,
parameter,
init.name,
init.version,
init.featureSetDefaults,
);

const targetTs = schema.targets.includes("ts");
const targetJs = schema.targets.includes("js");
Expand Down Expand Up @@ -185,16 +208,22 @@ export function createEcmaScriptPlugin(init: PluginInit): Plugin {
files.push(...transpiledFiles);
}

return toResponse(files);
return toResponse(files, init.supportsEditions ?? false);
},
};
}

function toResponse(files: FileInfo[]): CodeGeneratorResponse {
function toResponse(
files: FileInfo[],
supportsEditions: boolean,
): CodeGeneratorResponse {
let supportedFeatures: number = CodeGeneratorResponse_Feature.PROTO3_OPTIONAL;
if (supportsEditions) {
supportedFeatures =
supportedFeatures | CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS;
}
return new CodeGeneratorResponse({
supportedFeatures: protoInt64.parse(
CodeGeneratorResponse_Feature.PROTO3_OPTIONAL,
),
supportedFeatures: protoInt64.parse(supportedFeatures),
file: files.map((f) => {
if (f.preamble !== undefined) {
f.content = f.preamble + "\n" + f.content;
Expand Down
19 changes: 4 additions & 15 deletions packages/protoplugin/src/ecmascript/generated-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
} from "@bufbuild/protobuf";
import type { ImportSymbol } from "./import-symbol.js";
import { createImportSymbol } from "./import-symbol.js";
import { literalString, makeFilePreamble } from "./gencommon.js";
import { literalString } from "./gencommon.js";
import type { RuntimeImports } from "./runtime-imports.js";
import { makeImportPathRelative } from "./import-path.js";
import type { ExportDeclaration } from "./export-declaration.js";
Expand Down Expand Up @@ -167,8 +167,8 @@ export interface GeneratedFileController extends GeneratedFile {
}

type CreateTypeImportFn = (desc: DescMessage | DescEnum) => ImportSymbol;

type RewriteImportPathFn = (path: string) => string;
type CreatePreambleFn = (descFile: DescFile) => string;

export function createGeneratedFile(
name: string,
Expand All @@ -177,24 +177,13 @@ export function createGeneratedFile(
rewriteImportPath: RewriteImportPathFn,
createTypeImport: CreateTypeImportFn,
runtimeImports: RuntimeImports,
preambleSettings: {
pluginName: string;
pluginVersion: string;
pluginParameter: string;
tsNocheck: boolean;
},
createPreamble: CreatePreambleFn,
): GeneratedFileController {
let preamble: string | undefined;
const el: El[] = [];
return {
preamble(file) {
preamble = makeFilePreamble(
file,
preambleSettings.pluginName,
preambleSettings.pluginVersion,
preambleSettings.pluginParameter,
preambleSettings.tsNocheck,
);
preamble = createPreamble(file);
},
print(
printableOrFragments?: Printable | TemplateStringsArray,
Expand Down
27 changes: 19 additions & 8 deletions packages/protoplugin/src/ecmascript/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import type {
DescMessage,
DescriptorSet,
} from "@bufbuild/protobuf";
import { codegenInfo, createDescriptorSet } from "@bufbuild/protobuf";
import {
codegenInfo,
createDescriptorSet,
FeatureSetDefaults,
} from "@bufbuild/protobuf";
import type {
FileInfo,
GeneratedFile,
Expand All @@ -35,6 +39,7 @@ import {
rewriteImportPath,
} from "./import-path.js";
import { ParsedParameter } from "./parameter";
import { makeFilePreamble } from "./gencommon";

/**
* Schema describes the files and types that the plugin is requested to
Expand Down Expand Up @@ -82,8 +87,11 @@ export function createSchema(
parameter: ParsedParameter,
pluginName: string,
pluginVersion: string,
featureSetDefaults: FeatureSetDefaults | undefined,
): SchemaController {
const descriptorSet = createDescriptorSet(request.protoFile);
const descriptorSet = createDescriptorSet(request.protoFile, {
featureSetDefaults,
});
const filesToGenerate = findFilesToGenerate(descriptorSet, request);
const runtime = createRuntimeImports(parameter.bootstrapWkt);
const createTypeImport = (desc: DescMessage | DescEnum): ImportSymbol => {
Expand All @@ -95,6 +103,14 @@ export function createSchema(
);
return createImportSymbol(name, from);
};
const createPreamble = (descFile: DescFile) =>
makeFilePreamble(
descFile,
pluginName,
pluginVersion,
parameter.sanitizedParameter,
parameter.tsNocheck,
);
let target: Target | undefined;
const generatedFiles: GeneratedFileController[] = [];
return {
Expand All @@ -121,12 +137,7 @@ export function createSchema(
),
createTypeImport,
runtime,
{
pluginName,
pluginVersion,
pluginParameter: parameter.sanitizedParameter,
tsNocheck: parameter.tsNocheck,
},
createPreamble,
);
generatedFiles.push(genFile);
return genFile;
Expand Down

0 comments on commit 91e72d4

Please sign in to comment.