Skip to content

Commit

Permalink
Generate type imports when possible for .d.ts (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm committed Apr 10, 2024
1 parent 4969053 commit 5cd5156
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions packages/protobuf-test/src/gen/js/extra/enum_pb.d.ts

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.

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.

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.

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.

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

18 changes: 10 additions & 8 deletions packages/protoc-gen-es/src/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function generateDts(schema: Schema) {
// prettier-ignore
function generateEnum(schema: Schema, f: GeneratedFile, enumeration: DescEnum) {
f.print(f.jsDoc(enumeration));
f.print("export declare enum ", enumeration, " {");
f.print(f.exportDecl("declare enum", localName(enumeration)), " {");
for (const value of enumeration.values) {
if (enumeration.values.indexOf(value) > 0) {
f.print();
Expand All @@ -72,8 +72,9 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
JsonReadOptions,
JsonValue
} = schema.runtime;
const m = localName(message);
f.print(f.jsDoc(message));
f.print("export declare class ", message, " extends ", Message, "<", message, "> {");
f.print(f.exportDecl("declare class", m), " extends ", Message, "<", m, "> {");
for (const member of message.members) {
switch (member.kind) {
case "oneof":
Expand All @@ -85,7 +86,7 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
}
f.print();
}
f.print(" constructor(data?: ", PartialMessage, "<", message, ">);");
f.print(" constructor(data?: ", PartialMessage, "<", m, ">);");
f.print();
generateWktMethods(schema, f, message);
f.print(" static readonly runtime: typeof ", protoN, ";");
Expand All @@ -95,13 +96,13 @@ function generateMessage(schema: Schema, f: GeneratedFile, message: DescMessage)
//f.print(" static readonly options: { readonly [extensionName: string]: ", rt.JsonValue, " } = {};")
f.print();
generateWktStaticMethods(schema, f, message);
f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", message, ";")
f.print(" static fromBinary(bytes: Uint8Array, options?: Partial<", BinaryReadOptions, ">): ", m, ";")
f.print()
f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", message, ";")
f.print(" static fromJson(jsonValue: ", JsonValue, ", options?: Partial<", JsonReadOptions, ">): ", m, ";")
f.print()
f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", message, ";")
f.print(" static fromJsonString(jsonString: string, options?: Partial<", JsonReadOptions, ">): ", m, ";")
f.print()
f.print(" static equals(a: ", message, " | ", PlainMessage, "<", message, "> | undefined, b: ", message, " | ", PlainMessage, "<", message, "> | undefined): boolean;")
f.print(" static equals(a: ", m, " | ", PlainMessage, "<", m, "> | undefined, b: ", m, " | ", PlainMessage, "<", m, "> | undefined): boolean;")
f.print("}")
f.print()
for (const nestedEnum of message.nestedEnums) {
Expand Down Expand Up @@ -151,8 +152,9 @@ function generateExtension(
ext: DescExtension,
) {
const { typing } = getFieldTypeInfo(ext);
const e = f.import(ext.extendee).toTypeOnly();
f.print(f.jsDoc(ext));
f.print(f.exportDecl("declare const", ext), ": ", schema.runtime.Extension, "<", ext.extendee, ", ", typing, ">;");
f.print(f.exportDecl("declare const", localName(ext)), ": ", schema.runtime.Extension, "<", e, ", ", typing, ">;");
f.print();
}

Expand Down

0 comments on commit 5cd5156

Please sign in to comment.