@bufbuild/protoc-gen-es 1.8.0 → 1.9.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.
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -16,15 +16,15 @@
16
16
  },
17
17
  "scripts": {
18
18
  "clean": "rm -rf ./dist/cjs/*",
19
- "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs"
19
+ "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/cjs"
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protobuf": "^1.8.0",
24
- "@bufbuild/protoplugin": "1.8.0"
23
+ "@bufbuild/protobuf": "^1.9.0",
24
+ "@bufbuild/protoplugin": "1.9.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@bufbuild/protobuf": "1.8.0"
27
+ "@bufbuild/protobuf": "1.9.0"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@bufbuild/protobuf": {
@@ -37,7 +37,7 @@ exports.generateDts = generateDts;
37
37
  // prettier-ignore
38
38
  function generateEnum(schema, f, enumeration) {
39
39
  f.print(f.jsDoc(enumeration));
40
- f.print("export declare enum ", enumeration, " {");
40
+ f.print(f.exportDecl("declare enum", (0, ecmascript_1.localName)(enumeration)), " {");
41
41
  for (const value of enumeration.values) {
42
42
  if (enumeration.values.indexOf(value) > 0) {
43
43
  f.print();
@@ -52,8 +52,9 @@ function generateEnum(schema, f, enumeration) {
52
52
  function generateMessage(schema, f, message) {
53
53
  const protoN = (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
54
54
  const { PartialMessage, FieldList, Message, PlainMessage, BinaryReadOptions, JsonReadOptions, JsonValue } = schema.runtime;
55
+ const m = (0, ecmascript_1.localName)(message);
55
56
  f.print(f.jsDoc(message));
56
- f.print("export declare class ", message, " extends ", Message, "<", message, "> {");
57
+ f.print(f.exportDecl("declare class", m), " extends ", Message, "<", m, "> {");
57
58
  for (const member of message.members) {
58
59
  switch (member.kind) {
59
60
  case "oneof":
@@ -65,7 +66,7 @@ function generateMessage(schema, f, message) {
65
66
  }
66
67
  f.print();
67
68
  }
68
- f.print(" constructor(data?: ", PartialMessage, "<", message, ">);");
69
+ f.print(" constructor(data?: ", PartialMessage, "<", m, ">);");
69
70
  f.print();
70
71
  generateWktMethods(schema, f, message);
71
72
  f.print(" static readonly runtime: typeof ", protoN, ";");
@@ -75,13 +76,13 @@ function generateMessage(schema, f, message) {
75
76
  //f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
76
77
  f.print();
77
78
  generateWktStaticMethods(schema, f, message);
78
- f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", message, ";");
79
+ f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", m, ";");
79
80
  f.print();
80
- f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", message, ";");
81
+ f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", m, ";");
81
82
  f.print();
82
- f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", message, ";");
83
+ f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", m, ";");
83
84
  f.print();
84
- f.print(" static equals(a: ", message, " | ", PlainMessage, "<", message, "> | undefined, b: ", message, " | ", PlainMessage, "<", message, "> | undefined): boolean;");
85
+ f.print(" static equals(a: ", m, " | ", PlainMessage, "<", m, "> | undefined, b: ", m, " | ", PlainMessage, "<", m, "> | undefined): boolean;");
85
86
  f.print("}");
86
87
  f.print();
87
88
  for (const nestedEnum of message.nestedEnums) {
@@ -125,8 +126,9 @@ function generateField(schema, f, field) {
125
126
  // prettier-ignore
126
127
  function generateExtension(schema, f, ext) {
127
128
  const { typing } = (0, util_js_1.getFieldTypeInfo)(ext);
129
+ const e = f.import(ext.extendee).toTypeOnly();
128
130
  f.print(f.jsDoc(ext));
129
- f.print(f.exportDecl("declare const", ext), ": ", schema.runtime.Extension, "<", ext.extendee, ", ", typing, ">;");
131
+ f.print(f.exportDecl("declare const", (0, ecmascript_1.localName)(ext)), ": ", schema.runtime.Extension, "<", e, ", ", typing, ">;");
130
132
  f.print();
131
133
  }
132
134
  // prettier-ignore
@@ -43,15 +43,12 @@ function generateEnum(schema, f, enumeration) {
43
43
  f.print(f.exportDecl("const", enumeration), " = /*@__PURE__*/ ", protoN, ".makeEnum(");
44
44
  f.print(` "`, enumeration.typeName, `",`);
45
45
  f.print(` [`);
46
- if (enumeration.sharedPrefix === undefined) {
47
- for (const value of enumeration.values) {
46
+ for (const value of enumeration.values) {
47
+ if ((0, ecmascript_1.localName)(value) === value.name) {
48
48
  f.print(" {no: ", value.number, ", name: ", f.string(value.name), "},");
49
49
  }
50
- }
51
- else {
52
- for (const value of enumeration.values) {
53
- const localName = value.name.substring(enumeration.sharedPrefix.length);
54
- f.print(" {no: ", value.number, ", name: ", f.string(value.name), ", localName: ", f.string(localName), "},");
50
+ else {
51
+ f.print(" {no: ", value.number, ", name: ", f.string(value.name), ", localName: ", f.string((0, ecmascript_1.localName)(value)), "},");
55
52
  }
56
53
  }
57
54
  f.print(` ],`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bufbuild/protoc-gen-es",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Protocol Buffers code generator for ECMAScript",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -16,15 +16,15 @@
16
16
  },
17
17
  "scripts": {
18
18
  "clean": "rm -rf ./dist/cjs/*",
19
- "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --module commonjs --outDir ./dist/cjs"
19
+ "build": "../../node_modules/typescript/bin/tsc --project tsconfig.json --outDir ./dist/cjs"
20
20
  },
21
21
  "preferUnplugged": true,
22
22
  "dependencies": {
23
- "@bufbuild/protobuf": "^1.8.0",
24
- "@bufbuild/protoplugin": "1.8.0"
23
+ "@bufbuild/protobuf": "^1.9.0",
24
+ "@bufbuild/protoplugin": "1.9.0"
25
25
  },
26
26
  "peerDependencies": {
27
- "@bufbuild/protobuf": "1.8.0"
27
+ "@bufbuild/protobuf": "1.9.0"
28
28
  },
29
29
  "peerDependenciesMeta": {
30
30
  "@bufbuild/protobuf": {
@@ -48,7 +48,7 @@ export function generateDts(schema: Schema) {
48
48
  // prettier-ignore
49
49
  function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
50
50
  f.print(f.jsDoc(enumeration));
51
- f.print("export declare enum ", enumeration, " {");
51
+ f.print(f.exportDecl("declare enum", localName(enumeration)), " {");
52
52
  for (const value of enumeration.values) {
53
53
  if (enumeration.values.indexOf(value) > 0) {
54
54
  f.print();
@@ -72,8 +72,9 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
72
72
  JsonReadOptions,
73
73
  JsonValue
74
74
  } = schema.runtime;
75
+ const m = localName(message);
75
76
  f.print(f.jsDoc(message));
76
- f.print("export declare class ", message, " extends ", Message, "<", message, "> {");
77
+ f.print(f.exportDecl("declare class", m), " extends ", Message, "<", m, "> {");
77
78
  for (const member of message.members) {
78
79
  switch (member.kind) {
79
80
  case "oneof":
@@ -85,7 +86,7 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
85
86
  }
86
87
  f.print();
87
88
  }
88
- f.print(" constructor(data?: ", PartialMessage, "<", message, ">);");
89
+ f.print(" constructor(data?: ", PartialMessage, "<", m, ">);");
89
90
  f.print();
90
91
  generateWktMethods(schema, f, message);
91
92
  f.print(" static readonly runtime: typeof ", protoN, ";");
@@ -95,13 +96,13 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
95
96
  //f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
96
97
  f.print();
97
98
  generateWktStaticMethods(schema, f, message);
98
- f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", message, ";")
99
+ f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", m, ";")
99
100
  f.print()
100
- f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", message, ";")
101
+ f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", m, ";")
101
102
  f.print()
102
- f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", message, ";")
103
+ f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", m, ";")
103
104
  f.print()
104
- f.print(" static equals(a: ", message, " | ", PlainMessage, "<", message, "> | undefined, b: ", message, " | ", PlainMessage, "<", message, "> | undefined): boolean;")
105
+ f.print(" static equals(a: ", m, " | ", PlainMessage, "<", m, "> | undefined, b: ", m, " | ", PlainMessage, "<", m, "> | undefined): boolean;")
105
106
  f.print("}")
106
107
  f.print()
107
108
  for (const nestedEnum of message.nestedEnums) {
@@ -151,8 +152,9 @@ function generateExtension(
151
152
  ext: DescExtension,
152
153
  ) {
153
154
  const { typing } = getFieldTypeInfo(ext);
155
+ const e = f.import(ext.extendee).toTypeOnly();
154
156
  f.print(f.jsDoc(ext));
155
- f.print(f.exportDecl("declare const", ext), ": ", schema.runtime.Extension, "<", ext.extendee, ", ", typing, ">;");
157
+ f.print(f.exportDecl("declare const", localName(ext)), ": ", schema.runtime.Extension, "<", e, ", ", typing, ">;");
156
158
  f.print();
157
159
  }
158
160
 
package/src/javascript.ts CHANGED
@@ -59,14 +59,11 @@ function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
59
59
  f.print(f.exportDecl("const", enumeration), " = /*@__PURE__*/ ", protoN, ".makeEnum(")
60
60
  f.print(` "`, enumeration.typeName, `",`)
61
61
  f.print(` [`)
62
- if (enumeration.sharedPrefix === undefined) {
63
- for (const value of enumeration.values) {
62
+ for (const value of enumeration.values) {
63
+ if (localName(value) === value.name) {
64
64
  f.print(" {no: ", value.number, ", name: ", f.string(value.name), "},")
65
- }
66
- } else {
67
- for (const value of enumeration.values) {
68
- const localName = value.name.substring(enumeration.sharedPrefix.length);
69
- f.print(" {no: ", value.number, ", name: ", f.string(value.name), ", localName: ", f.string(localName), "},")
65
+ } else {
66
+ f.print(" {no: ", value.number, ", name: ", f.string(value.name), ", localName: ", f.string(localName(value)), "},")
70
67
  }
71
68
  }
72
69
  f.print(` ],`)
package/tsconfig.json CHANGED
@@ -2,6 +2,11 @@
2
2
  "files": ["src/protoc-gen-es-plugin.ts"],
3
3
  "extends": "../../tsconfig.base.json",
4
4
  "compilerOptions": {
5
- "resolveJsonModule": true
5
+ // We import the plugin's version number from package.json
6
+ "resolveJsonModule": true,
7
+ // This package is CommonJS
8
+ "verbatimModuleSyntax": false,
9
+ "module": "CommonJS",
10
+ "moduleResolution": "Node10"
6
11
  }
7
12
  }