@bufbuild/protoc-gen-es 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -124,3 +124,11 @@ By default, we generate JavaScript and TypeScript declaration files, which
124
124
  produces the smallest code size. If you prefer to generate TypeScript, use
125
125
  `target=ts`.
126
126
 
127
+ ### `keep_empty_files=true`
128
+
129
+ By default, [protoc-gen-es](https://www.npmjs.com/package/@bufbuild/protoc-gen-es)
130
+ (and all other plugins based on [@bufbuild/protoplugin](https://www.npmjs.com/package/@bufbuild/protoplugin))
131
+ omit empty files from the plugin output. This option disables pruning of
132
+ empty files, to allow for smooth interoperation with Bazel and similar
133
+ tooling that requires all output files to be declared ahead of time.
134
+ Unless you use Bazel, it is very unlikely that you need this option.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.1.1"
23
+ "@bufbuild/protoplugin": "0.2.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.1.1"
26
+ "@bufbuild/protobuf": "0.2.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -13,15 +13,23 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.declaration = void 0;
16
+ exports.generateDts = void 0;
17
17
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
18
18
  const match_wkt_js_1 = require("./match-wkt.js");
19
- exports.declaration = {
20
- target: "dts",
21
- extension: "_pb.d.ts",
22
- generateEnum,
23
- generateMessage,
24
- };
19
+ function generateDts(schema) {
20
+ for (const file of schema.files) {
21
+ const f = schema.generateFile(file.name + "_pb.d.ts");
22
+ f.preamble(file);
23
+ for (const enumeration of file.enums) {
24
+ generateEnum(schema, f, enumeration);
25
+ }
26
+ for (const message of file.messages) {
27
+ generateMessage(schema, f, message);
28
+ }
29
+ // We do not generate anything for services, and we do not support extensions at this time
30
+ }
31
+ }
32
+ exports.generateDts = generateDts;
25
33
  // prettier-ignore
26
34
  function generateEnum(schema, f, enumeration) {
27
35
  f.print((0, ecmascript_1.makeJsDoc)(enumeration));
@@ -13,16 +13,24 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.generateFieldInfo = exports.javascript = void 0;
16
+ exports.generateFieldInfo = exports.generateJs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
19
  const match_wkt_js_1 = require("./match-wkt.js");
20
- exports.javascript = {
21
- target: "js",
22
- extension: "_pb.js",
23
- generateEnum,
24
- generateMessage,
25
- };
20
+ function generateJs(schema) {
21
+ for (const file of schema.files) {
22
+ const f = schema.generateFile(file.name + "_pb.js");
23
+ f.preamble(file);
24
+ for (const enumeration of file.enums) {
25
+ generateEnum(schema, f, enumeration);
26
+ }
27
+ for (const message of file.messages) {
28
+ generateMessage(schema, f, message);
29
+ }
30
+ // We do not generate anything for services, and we do not support extensions at this time
31
+ }
32
+ }
33
+ exports.generateJs = generateJs;
26
34
  // prettier-ignore
27
35
  function generateEnum(schema, f, enumeration) {
28
36
  const protoN = schema.runtime[enumeration.file.syntax];
@@ -88,11 +96,11 @@ function generateFieldInfo(schema, f, field) {
88
96
  if (field.jsonName !== undefined) {
89
97
  e.push(`jsonName: "`, field.jsonName, `", `);
90
98
  }
91
- switch (field.kind) {
92
- case "scalar_field":
99
+ switch (field.fieldKind) {
100
+ case "scalar":
93
101
  e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
94
102
  break;
95
- case "map_field":
103
+ case "map":
96
104
  e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
97
105
  switch (field.mapValue.kind) {
98
106
  case "scalar":
@@ -106,10 +114,10 @@ function generateFieldInfo(schema, f, field) {
106
114
  break;
107
115
  }
108
116
  break;
109
- case "message_field":
117
+ case "message":
110
118
  e.push(`kind: "message", T: `, field.message, `, `);
111
119
  break;
112
- case "enum_field":
120
+ case "enum":
113
121
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
114
122
  break;
115
123
  }
@@ -19,10 +19,10 @@ function matchWkt(message) {
19
19
  switch (message.typeName) {
20
20
  case "google.protobuf.Any": {
21
21
  const typeUrl = message.fields.find((f) => f.number == 1 &&
22
- f.kind == "scalar_field" &&
22
+ f.fieldKind == "scalar" &&
23
23
  f.scalar === protobuf_1.ScalarType.STRING);
24
24
  const value = message.fields.find((f) => f.number == 2 &&
25
- f.kind == "scalar_field" &&
25
+ f.fieldKind == "scalar" &&
26
26
  f.scalar === protobuf_1.ScalarType.BYTES);
27
27
  if (typeUrl && value) {
28
28
  return {
@@ -35,10 +35,10 @@ function matchWkt(message) {
35
35
  }
36
36
  case "google.protobuf.Timestamp": {
37
37
  const seconds = message.fields.find((f) => f.number == 1 &&
38
- f.kind == "scalar_field" &&
38
+ f.fieldKind == "scalar" &&
39
39
  f.scalar === protobuf_1.ScalarType.INT64);
40
40
  const nanos = message.fields.find((f) => f.number == 2 &&
41
- f.kind == "scalar_field" &&
41
+ f.fieldKind == "scalar" &&
42
42
  f.scalar === protobuf_1.ScalarType.INT32);
43
43
  if (seconds && nanos) {
44
44
  return {
@@ -51,10 +51,10 @@ function matchWkt(message) {
51
51
  }
52
52
  case "google.protobuf.Duration": {
53
53
  const seconds = message.fields.find((f) => f.number == 1 &&
54
- f.kind == "scalar_field" &&
54
+ f.fieldKind == "scalar" &&
55
55
  f.scalar === protobuf_1.ScalarType.INT64);
56
56
  const nanos = message.fields.find((f) => f.number == 2 &&
57
- f.kind == "scalar_field" &&
57
+ f.fieldKind == "scalar" &&
58
58
  f.scalar === protobuf_1.ScalarType.INT32);
59
59
  if (seconds && nanos) {
60
60
  return {
@@ -67,7 +67,7 @@ function matchWkt(message) {
67
67
  }
68
68
  case "google.protobuf.Struct": {
69
69
  const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
70
- if ((fields === null || fields === void 0 ? void 0 : fields.kind) !== "map_field" ||
70
+ if ((fields === null || fields === void 0 ? void 0 : fields.fieldKind) !== "map" ||
71
71
  fields.mapValue.kind !== "message" ||
72
72
  fields.mapValue.message.typeName !== "google.protobuf.Value") {
73
73
  break;
@@ -77,29 +77,29 @@ function matchWkt(message) {
77
77
  case "google.protobuf.Value": {
78
78
  const kind = message.oneofs.find((o) => o.name === "kind");
79
79
  const nullValue = message.fields.find((f) => f.number == 1 && f.oneof === kind);
80
- if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.kind) !== "enum_field" ||
80
+ if ((nullValue === null || nullValue === void 0 ? void 0 : nullValue.fieldKind) !== "enum" ||
81
81
  nullValue.enum.typeName !== "google.protobuf.NullValue") {
82
82
  return undefined;
83
83
  }
84
84
  const numberValue = message.fields.find((f) => f.number == 2 &&
85
- f.kind == "scalar_field" &&
85
+ f.fieldKind == "scalar" &&
86
86
  f.scalar === protobuf_1.ScalarType.DOUBLE &&
87
87
  f.oneof === kind);
88
88
  const stringValue = message.fields.find((f) => f.number == 3 &&
89
- f.kind == "scalar_field" &&
89
+ f.fieldKind == "scalar" &&
90
90
  f.scalar === protobuf_1.ScalarType.STRING &&
91
91
  f.oneof === kind);
92
92
  const boolValue = message.fields.find((f) => f.number == 4 &&
93
- f.kind == "scalar_field" &&
93
+ f.fieldKind == "scalar" &&
94
94
  f.scalar === protobuf_1.ScalarType.BOOL &&
95
95
  f.oneof === kind);
96
96
  const structValue = message.fields.find((f) => f.number == 5 && f.oneof === kind);
97
- if ((structValue === null || structValue === void 0 ? void 0 : structValue.kind) !== "message_field" ||
97
+ if ((structValue === null || structValue === void 0 ? void 0 : structValue.fieldKind) !== "message" ||
98
98
  structValue.message.typeName !== "google.protobuf.Struct") {
99
99
  return undefined;
100
100
  }
101
101
  const listValue = message.fields.find((f) => f.number == 6 && f.oneof === kind);
102
- if ((listValue === null || listValue === void 0 ? void 0 : listValue.kind) !== "message_field" ||
102
+ if ((listValue === null || listValue === void 0 ? void 0 : listValue.fieldKind) !== "message" ||
103
103
  listValue.message.typeName !== "google.protobuf.ListValue") {
104
104
  return undefined;
105
105
  }
@@ -119,7 +119,7 @@ function matchWkt(message) {
119
119
  }
120
120
  case "google.protobuf.ListValue": {
121
121
  const values = message.fields.find((f) => f.number == 1 && f.repeated);
122
- if ((values === null || values === void 0 ? void 0 : values.kind) != "message_field" ||
122
+ if ((values === null || values === void 0 ? void 0 : values.fieldKind) != "message" ||
123
123
  values.message.typeName !== "google.protobuf.Value") {
124
124
  break;
125
125
  }
@@ -127,7 +127,7 @@ function matchWkt(message) {
127
127
  }
128
128
  case "google.protobuf.FieldMask": {
129
129
  const paths = message.fields.find((f) => f.number == 1 &&
130
- f.kind == "scalar_field" &&
130
+ f.fieldKind == "scalar" &&
131
131
  f.scalar === protobuf_1.ScalarType.STRING &&
132
132
  f.repeated);
133
133
  if (paths) {
@@ -148,7 +148,7 @@ function matchWkt(message) {
148
148
  if (!value) {
149
149
  break;
150
150
  }
151
- if (value.kind !== "scalar_field") {
151
+ if (value.fieldKind !== "scalar") {
152
152
  break;
153
153
  }
154
154
  return { typeName: message.typeName, value };
@@ -17,24 +17,12 @@ exports.protocGenEs = void 0;
17
17
  const protoplugin_1 = require("@bufbuild/protoplugin");
18
18
  const typescript_js_1 = require("./typescript.js");
19
19
  const javascript_js_1 = require("./javascript.js");
20
- const declaration_1 = require("./declaration");
20
+ const declaration_js_1 = require("./declaration.js");
21
21
  const package_json_1 = require("../package.json");
22
22
  exports.protocGenEs = (0, protoplugin_1.createEcmaScriptPlugin)({
23
23
  name: "protoc-gen-es",
24
24
  version: `v${String(package_json_1.version)}`,
25
- }, (schema) => {
26
- const targets = [typescript_js_1.typescript, javascript_js_1.javascript, declaration_1.declaration].filter((gen) => schema.targets.includes(gen.target));
27
- for (const target of targets) {
28
- for (const file of schema.files) {
29
- const f = schema.generateFile(file.name + target.extension);
30
- f.preamble(file);
31
- for (const enumeration of file.enums) {
32
- target.generateEnum(schema, f, enumeration);
33
- }
34
- for (const message of file.messages) {
35
- target.generateMessage(schema, f, message);
36
- }
37
- // We do not generate anything for services, and we do not support extensions at this time
38
- }
39
- }
25
+ generateTs: typescript_js_1.generateTs,
26
+ generateJs: javascript_js_1.generateJs,
27
+ generateDts: declaration_js_1.generateDts,
40
28
  });
@@ -13,18 +13,26 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.typescript = void 0;
16
+ exports.generateTs = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
19
19
  const match_wkt_js_1 = require("./match-wkt.js");
20
20
  const javascript_js_1 = require("./javascript.js");
21
21
  const ecmascript_2 = require("@bufbuild/protoplugin/ecmascript");
22
- exports.typescript = {
23
- target: "ts",
24
- extension: "_pb.ts",
25
- generateEnum,
26
- generateMessage,
27
- };
22
+ function generateTs(schema) {
23
+ for (const file of schema.files) {
24
+ const f = schema.generateFile(file.name + "_pb.ts");
25
+ f.preamble(file);
26
+ for (const enumeration of file.enums) {
27
+ generateEnum(schema, f, enumeration);
28
+ }
29
+ for (const message of file.messages) {
30
+ generateMessage(schema, f, message);
31
+ }
32
+ // We do not generate anything for services, and we do not support extensions at this time
33
+ }
34
+ }
35
+ exports.generateTs = generateTs;
28
36
  // prettier-ignore
29
37
  function generateEnum(schema, f, enumeration) {
30
38
  const protoN = schema.runtime[enumeration.file.syntax];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -20,10 +20,10 @@
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protoplugin": "0.1.1"
23
+ "@bufbuild/protoplugin": "0.2.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bufbuild/protobuf": "0.1.1"
26
+ "@bufbuild/protobuf": "0.2.0"
27
27
  },
28
28
  "peerDependenciesMeta": {
29
29
  "@bufbuild/protobuf": {
@@ -18,7 +18,11 @@ import type {
18
18
  DescMessage,
19
19
  DescOneof,
20
20
  } from "@bufbuild/protobuf";
21
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
21
+ import type {
22
+ GeneratedFile,
23
+ Printable,
24
+ Schema,
25
+ } from "@bufbuild/protoplugin/ecmascript";
22
26
  import {
23
27
  getFieldTyping,
24
28
  literalString,
@@ -27,12 +31,19 @@ import {
27
31
  } from "@bufbuild/protoplugin/ecmascript";
28
32
  import { matchWkt } from "./match-wkt.js";
29
33
 
30
- export const declaration = {
31
- target: "dts",
32
- extension: "_pb.d.ts",
33
- generateEnum,
34
- generateMessage,
35
- } as const;
34
+ export function generateDts(schema: Schema) {
35
+ for (const file of schema.files) {
36
+ const f = schema.generateFile(file.name + "_pb.d.ts");
37
+ f.preamble(file);
38
+ for (const enumeration of file.enums) {
39
+ generateEnum(schema, f, enumeration);
40
+ }
41
+ for (const message of file.messages) {
42
+ generateMessage(schema, f, message);
43
+ }
44
+ // We do not generate anything for services, and we do not support extensions at this time
45
+ }
46
+ }
36
47
 
37
48
  // prettier-ignore
38
49
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -120,7 +131,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
120
131
 
121
132
  function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
122
133
  f.print(makeJsDoc(field, " "));
123
- const e: Parameters<typeof f.print> = [];
134
+ const e: Printable = [];
124
135
  e.push(" ", localName(field));
125
136
  const { typing, optional } = getFieldTyping(field, f);
126
137
  if (optional) {
package/src/javascript.ts CHANGED
@@ -14,7 +14,11 @@
14
14
 
15
15
  import type { DescEnum, DescField, DescMessage } from "@bufbuild/protobuf";
16
16
  import { proto2, proto3, ScalarType } from "@bufbuild/protobuf";
17
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
17
+ import type {
18
+ GeneratedFile,
19
+ Printable,
20
+ Schema,
21
+ } from "@bufbuild/protoplugin/ecmascript";
18
22
  import {
19
23
  getFieldExplicitDefaultValue,
20
24
  literalString,
@@ -23,12 +27,19 @@ import {
23
27
  } from "@bufbuild/protoplugin/ecmascript";
24
28
  import { matchWkt } from "./match-wkt.js";
25
29
 
26
- export const javascript = {
27
- target: "js",
28
- extension: "_pb.js",
29
- generateEnum,
30
- generateMessage,
31
- } as const;
30
+ export function generateJs(schema: Schema) {
31
+ for (const file of schema.files) {
32
+ const f = schema.generateFile(file.name + "_pb.js");
33
+ f.preamble(file);
34
+ for (const enumeration of file.enums) {
35
+ generateEnum(schema, f, enumeration);
36
+ }
37
+ for (const message of file.messages) {
38
+ generateMessage(schema, f, message);
39
+ }
40
+ // We do not generate anything for services, and we do not support extensions at this time
41
+ }
42
+ }
32
43
 
33
44
  // prettier-ignore
34
45
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -90,16 +101,16 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
90
101
  // prettier-ignore
91
102
  export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescField) {
92
103
  const protoN = schema.runtime[field.parent.file.syntax];
93
- const e: Parameters<typeof f.print> = [];
104
+ const e: Printable = [];
94
105
  e.push(" { no: ", field.number, `, name: "`, field.name, `", `);
95
106
  if (field.jsonName !== undefined) {
96
107
  e.push(`jsonName: "`, field.jsonName, `", `);
97
108
  }
98
- switch (field.kind) {
99
- case "scalar_field":
109
+ switch (field.fieldKind) {
110
+ case "scalar":
100
111
  e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, ScalarType[field.scalar], ` */, `);
101
112
  break;
102
- case "map_field":
113
+ case "map":
103
114
  e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, ScalarType[field.mapKey], ` */, `);
104
115
  switch (field.mapValue.kind) {
105
116
  case "scalar":
@@ -113,10 +124,10 @@ export function generateFieldInfo(schema: Schema, f: GeneratedFile, field: DescF
113
124
  break;
114
125
  }
115
126
  break;
116
- case "message_field":
127
+ case "message":
117
128
  e.push(`kind: "message", T: `, field.message, `, `);
118
129
  break;
119
- case "enum_field":
130
+ case "enum":
120
131
  e.push(`kind: "enum", T: `, protoN, `.getEnumType(`, field.enum, `), `);
121
132
  break;
122
133
  }
package/src/match-wkt.ts CHANGED
@@ -37,21 +37,21 @@ type DescWkt =
37
37
  }
38
38
  | {
39
39
  typeName: "google.protobuf.Struct";
40
- fields: DescField & { kind: "map_field" };
40
+ fields: DescField & { fieldKind: "map" };
41
41
  }
42
42
  | {
43
43
  typeName: "google.protobuf.Value";
44
44
  kind: DescOneof;
45
- nullValue: DescField & { kind: "enum_field" };
45
+ nullValue: DescField & { fieldKind: "enum" };
46
46
  numberValue: DescField;
47
47
  stringValue: DescField;
48
48
  boolValue: DescField;
49
- structValue: DescField & { kind: "message_field" };
50
- listValue: DescField & { kind: "message_field" };
49
+ structValue: DescField & { fieldKind: "message" };
50
+ listValue: DescField & { fieldKind: "message" };
51
51
  }
52
52
  | {
53
53
  typeName: "google.protobuf.ListValue";
54
- values: DescField & { kind: "message_field" };
54
+ values: DescField & { fieldKind: "message" };
55
55
  }
56
56
  | {
57
57
  typeName: "google.protobuf.FieldMask";
@@ -59,39 +59,39 @@ type DescWkt =
59
59
  }
60
60
  | {
61
61
  typeName: "google.protobuf.DoubleValue";
62
- value: DescField & { kind: "scalar_field" };
62
+ value: DescField & { fieldKind: "scalar" };
63
63
  }
64
64
  | {
65
65
  typeName: "google.protobuf.FloatValue";
66
- value: DescField & { kind: "scalar_field" };
66
+ value: DescField & { fieldKind: "scalar" };
67
67
  }
68
68
  | {
69
69
  typeName: "google.protobuf.Int64Value";
70
- value: DescField & { kind: "scalar_field" };
70
+ value: DescField & { fieldKind: "scalar" };
71
71
  }
72
72
  | {
73
73
  typeName: "google.protobuf.UInt64Value";
74
- value: DescField & { kind: "scalar_field" };
74
+ value: DescField & { fieldKind: "scalar" };
75
75
  }
76
76
  | {
77
77
  typeName: "google.protobuf.Int32Value";
78
- value: DescField & { kind: "scalar_field" };
78
+ value: DescField & { fieldKind: "scalar" };
79
79
  }
80
80
  | {
81
81
  typeName: "google.protobuf.UInt32Value";
82
- value: DescField & { kind: "scalar_field" };
82
+ value: DescField & { fieldKind: "scalar" };
83
83
  }
84
84
  | {
85
85
  typeName: "google.protobuf.BoolValue";
86
- value: DescField & { kind: "scalar_field" };
86
+ value: DescField & { fieldKind: "scalar" };
87
87
  }
88
88
  | {
89
89
  typeName: "google.protobuf.StringValue";
90
- value: DescField & { kind: "scalar_field" };
90
+ value: DescField & { fieldKind: "scalar" };
91
91
  }
92
92
  | {
93
93
  typeName: "google.protobuf.BytesValue";
94
- value: DescField & { kind: "scalar_field" };
94
+ value: DescField & { fieldKind: "scalar" };
95
95
  };
96
96
 
97
97
  export function matchWkt(message: DescMessage): DescWkt | undefined {
@@ -100,13 +100,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
100
100
  const typeUrl = message.fields.find(
101
101
  (f) =>
102
102
  f.number == 1 &&
103
- f.kind == "scalar_field" &&
103
+ f.fieldKind == "scalar" &&
104
104
  f.scalar === ScalarType.STRING
105
105
  );
106
106
  const value = message.fields.find(
107
107
  (f) =>
108
108
  f.number == 2 &&
109
- f.kind == "scalar_field" &&
109
+ f.fieldKind == "scalar" &&
110
110
  f.scalar === ScalarType.BYTES
111
111
  );
112
112
  if (typeUrl && value) {
@@ -122,13 +122,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
122
122
  const seconds = message.fields.find(
123
123
  (f) =>
124
124
  f.number == 1 &&
125
- f.kind == "scalar_field" &&
125
+ f.fieldKind == "scalar" &&
126
126
  f.scalar === ScalarType.INT64
127
127
  );
128
128
  const nanos = message.fields.find(
129
129
  (f) =>
130
130
  f.number == 2 &&
131
- f.kind == "scalar_field" &&
131
+ f.fieldKind == "scalar" &&
132
132
  f.scalar === ScalarType.INT32
133
133
  );
134
134
  if (seconds && nanos) {
@@ -144,13 +144,13 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
144
144
  const seconds = message.fields.find(
145
145
  (f) =>
146
146
  f.number == 1 &&
147
- f.kind == "scalar_field" &&
147
+ f.fieldKind == "scalar" &&
148
148
  f.scalar === ScalarType.INT64
149
149
  );
150
150
  const nanos = message.fields.find(
151
151
  (f) =>
152
152
  f.number == 2 &&
153
- f.kind == "scalar_field" &&
153
+ f.fieldKind == "scalar" &&
154
154
  f.scalar === ScalarType.INT32
155
155
  );
156
156
  if (seconds && nanos) {
@@ -165,7 +165,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
165
165
  case "google.protobuf.Struct": {
166
166
  const fields = message.fields.find((f) => f.number == 1 && !f.repeated);
167
167
  if (
168
- fields?.kind !== "map_field" ||
168
+ fields?.fieldKind !== "map" ||
169
169
  fields.mapValue.kind !== "message" ||
170
170
  fields.mapValue.message.typeName !== "google.protobuf.Value"
171
171
  ) {
@@ -179,7 +179,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
179
179
  (f) => f.number == 1 && f.oneof === kind
180
180
  );
181
181
  if (
182
- nullValue?.kind !== "enum_field" ||
182
+ nullValue?.fieldKind !== "enum" ||
183
183
  nullValue.enum.typeName !== "google.protobuf.NullValue"
184
184
  ) {
185
185
  return undefined;
@@ -187,21 +187,21 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
187
187
  const numberValue = message.fields.find(
188
188
  (f) =>
189
189
  f.number == 2 &&
190
- f.kind == "scalar_field" &&
190
+ f.fieldKind == "scalar" &&
191
191
  f.scalar === ScalarType.DOUBLE &&
192
192
  f.oneof === kind
193
193
  );
194
194
  const stringValue = message.fields.find(
195
195
  (f) =>
196
196
  f.number == 3 &&
197
- f.kind == "scalar_field" &&
197
+ f.fieldKind == "scalar" &&
198
198
  f.scalar === ScalarType.STRING &&
199
199
  f.oneof === kind
200
200
  );
201
201
  const boolValue = message.fields.find(
202
202
  (f) =>
203
203
  f.number == 4 &&
204
- f.kind == "scalar_field" &&
204
+ f.fieldKind == "scalar" &&
205
205
  f.scalar === ScalarType.BOOL &&
206
206
  f.oneof === kind
207
207
  );
@@ -209,7 +209,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
209
209
  (f) => f.number == 5 && f.oneof === kind
210
210
  );
211
211
  if (
212
- structValue?.kind !== "message_field" ||
212
+ structValue?.fieldKind !== "message" ||
213
213
  structValue.message.typeName !== "google.protobuf.Struct"
214
214
  ) {
215
215
  return undefined;
@@ -218,7 +218,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
218
218
  (f) => f.number == 6 && f.oneof === kind
219
219
  );
220
220
  if (
221
- listValue?.kind !== "message_field" ||
221
+ listValue?.fieldKind !== "message" ||
222
222
  listValue.message.typeName !== "google.protobuf.ListValue"
223
223
  ) {
224
224
  return undefined;
@@ -240,7 +240,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
240
240
  case "google.protobuf.ListValue": {
241
241
  const values = message.fields.find((f) => f.number == 1 && f.repeated);
242
242
  if (
243
- values?.kind != "message_field" ||
243
+ values?.fieldKind != "message" ||
244
244
  values.message.typeName !== "google.protobuf.Value"
245
245
  ) {
246
246
  break;
@@ -251,7 +251,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
251
251
  const paths = message.fields.find(
252
252
  (f) =>
253
253
  f.number == 1 &&
254
- f.kind == "scalar_field" &&
254
+ f.fieldKind == "scalar" &&
255
255
  f.scalar === ScalarType.STRING &&
256
256
  f.repeated
257
257
  );
@@ -275,7 +275,7 @@ export function matchWkt(message: DescMessage): DescWkt | undefined {
275
275
  if (!value) {
276
276
  break;
277
277
  }
278
- if (value.kind !== "scalar_field") {
278
+ if (value.fieldKind !== "scalar") {
279
279
  break;
280
280
  }
281
281
  return { typeName: message.typeName, value };
@@ -13,32 +13,15 @@
13
13
  // limitations under the License.
14
14
 
15
15
  import { createEcmaScriptPlugin } from "@bufbuild/protoplugin";
16
- import { typescript } from "./typescript.js";
17
- import { javascript } from "./javascript.js";
18
- import { declaration } from "./declaration";
16
+ import { generateTs } from "./typescript.js";
17
+ import { generateJs } from "./javascript.js";
18
+ import { generateDts } from "./declaration.js";
19
19
  import { version } from "../package.json";
20
20
 
21
- export const protocGenEs = createEcmaScriptPlugin(
22
- {
23
- name: "protoc-gen-es",
24
- version: `v${String(version)}`,
25
- },
26
- (schema) => {
27
- const targets = [typescript, javascript, declaration].filter((gen) =>
28
- schema.targets.includes(gen.target)
29
- );
30
- for (const target of targets) {
31
- for (const file of schema.files) {
32
- const f = schema.generateFile(file.name + target.extension);
33
- f.preamble(file);
34
- for (const enumeration of file.enums) {
35
- target.generateEnum(schema, f, enumeration);
36
- }
37
- for (const message of file.messages) {
38
- target.generateMessage(schema, f, message);
39
- }
40
- // We do not generate anything for services, and we do not support extensions at this time
41
- }
42
- }
43
- }
44
- );
21
+ export const protocGenEs = createEcmaScriptPlugin({
22
+ name: "protoc-gen-es",
23
+ version: `v${String(version)}`,
24
+ generateTs,
25
+ generateJs,
26
+ generateDts,
27
+ });
package/src/typescript.ts CHANGED
@@ -19,7 +19,11 @@ import type {
19
19
  DescOneof,
20
20
  } from "@bufbuild/protobuf";
21
21
  import { ScalarType } from "@bufbuild/protobuf";
22
- import type { GeneratedFile, Schema } from "@bufbuild/protoplugin/ecmascript";
22
+ import type {
23
+ GeneratedFile,
24
+ Printable,
25
+ Schema,
26
+ } from "@bufbuild/protoplugin/ecmascript";
23
27
  import {
24
28
  localName,
25
29
  getFieldIntrinsicDefaultValue,
@@ -30,12 +34,19 @@ import { matchWkt } from "./match-wkt.js";
30
34
  import { generateFieldInfo } from "./javascript.js";
31
35
  import { literalString } from "@bufbuild/protoplugin/ecmascript";
32
36
 
33
- export const typescript = {
34
- target: "ts",
35
- extension: "_pb.ts",
36
- generateEnum,
37
- generateMessage,
38
- } as const;
37
+ export function generateTs(schema: Schema) {
38
+ for (const file of schema.files) {
39
+ const f = schema.generateFile(file.name + "_pb.ts");
40
+ f.preamble(file);
41
+ for (const enumeration of file.enums) {
42
+ generateEnum(schema, f, enumeration);
43
+ }
44
+ for (const message of file.messages) {
45
+ generateMessage(schema, f, message);
46
+ }
47
+ // We do not generate anything for services, and we do not support extensions at this time
48
+ }
49
+ }
39
50
 
40
51
  // prettier-ignore
41
52
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
@@ -145,7 +156,7 @@ function generateOneof(schema: Schema, f: GeneratedFile, oneof: DescOneof) {
145
156
 
146
157
  function generateField(schema: Schema, f: GeneratedFile, field: DescField) {
147
158
  f.print(makeJsDoc(field, " "));
148
- const e: Parameters<typeof f.print> = [];
159
+ const e: Printable = [];
149
160
  e.push(" ", localName(field));
150
161
  const { defaultValue, typingInferrable } =
151
162
  getFieldIntrinsicDefaultValue(field);