Skip to content

Commit 6784b3b

Browse files
committed
chore: fix #26
1 parent f781c74 commit 6784b3b

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

packages/core/src/print/gen-enum.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import MagicString from 'magic-string';
22
import { indentPrefix, OUTPUT_TYPE } from '../constant.js';
3+
import protobuf from 'protobufjs';
4+
5+
const { Type } = protobuf;
36

47
/**
58
* generate typescript files from proto info
@@ -8,7 +11,7 @@ import { indentPrefix, OUTPUT_TYPE } from '../constant.js';
811
* @returns
912
*/
1013
export function genEnum(proto, options) {
11-
const { values, comments, name } = proto;
14+
const { values, comments, name, parent } = proto;
1215

1316
const items = Object.keys(values)
1417
.map((key) => ({
@@ -21,17 +24,24 @@ export function genEnum(proto, options) {
2124
const result = new MagicString('');
2225

2326
items.forEach((s) => {
24-
if(s.comment) {
27+
if (s.comment) {
2528
result.append(`//${s.comment}\n`);
2629
}
2730
result.append(`${s.name} = ${s.id},\n`);
2831
});
2932

3033
const prefix = options.outputType === OUTPUT_TYPE.definition ? '' : 'export ';
3134

35+
let interfaceName = name;
36+
37+
// deal with nested type conflict problem
38+
if (parent && parent instanceof Type && parent.name) {
39+
interfaceName = parent.name + interfaceName;
40+
}
41+
3242
result
3343
.indent(indentPrefix)
34-
.prepend(`${prefix}enum ${name} {\n`)
44+
.prepend(`${prefix}enum ${interfaceName} {\n`)
3545
.append('}\n\n');
3646

3747
return {
@@ -60,7 +70,7 @@ export function getJsdocEnum(proto, options) {
6070
const result = new MagicString('');
6171

6272
items.forEach((s) => {
63-
if(s.comment) {
73+
if (s.comment) {
6474
result.append(`//${s.comment}\n`);
6575
}
6676
result.append(`${s.name} = ${s.id},\n`);

packages/core/src/print/gen-service.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export function genService(proto, options) {
5252
const prefix = options.outputType === OUTPUT_TYPE.definition ? '' : 'export ';
5353

5454
if (item.comment) {
55-
result.append(`//${item.comment}\n`);
55+
item.comment.split('\n').forEach((line) => {
56+
result.append(`//${line} \n`);
57+
})
5658
}
5759

5860
result.append(

packages/core/tests/comment/index.spec.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { test } from 'uvu';
44
import * as assert from 'uvu/assert';
55

66

7-
const source = `
7+
const sample1 = `
88
99
message UpdateHostBankAccWalletInfoResp {
1010
// 0:成功  非0:错误码   
@@ -19,12 +19,43 @@ message UpdateHostBankAccWalletInfoResp {
1919
`;
2020

2121
test('Comment of Field type should be converted', () => {
22-
const ts = parseProto(source);
22+
const ts = parseProto(sample1);
2323

2424
assert.match(ts, '//50010 bank account already exist');
2525
assert.match(ts, '//50011: bank account number already exist');
2626
assert.match(ts, '//50012 bank account mismatch the one on');
2727
});
2828

29+
const sample2 = `
30+
syntax = "proto3";
31+
32+
service Greeter {
33+
//hello
34+
//hello1
35+
rpc SayHello (HelloRequest) returns (HelloReply) {}
36+
}
37+
38+
message HelloRequest {
39+
string name = 1;
40+
}
41+
42+
message Teacher {
43+
string name = 1;
44+
}
45+
46+
message HelloReply {
47+
string message = 1;
48+
int32 test = 2;
49+
Teacher teacher = 3;
50+
}
51+
`;
52+
53+
test('Comment of function type should be converted', () => {
54+
const ts = parseProto(sample2);
55+
56+
assert.match(ts, '//hello');
57+
assert.match(ts, '//hello1');
58+
});
59+
2960

3061
test.run();

packages/core/tests/print/nest.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,28 @@ test('nest type6 should be converted', () => {
140140
assert.match(ts, '@typedef {Object} Message1');
141141
});
142142

143+
144+
test('nest type7 should be converted', () => {
145+
const source = `
146+
syntax = "proto3";
147+
message UpdateUserRoleReq {
148+
enum Type {
149+
PLATFORM = 0;
150+
PROJECT = 1;
151+
MODULE = 2;
152+
PLUGIN = 3;
153+
}
154+
int32 user_id = 1;
155+
int32 role_id = 2;
156+
Type type = 3;
157+
}
158+
`;
159+
160+
const ts = parseProto(source);
161+
console.log(ts);
162+
assert.match(ts, 'type?: UpdateUserRoleReqType;');
163+
assert.match(ts, 'enum UpdateUserRoleReqType');
164+
})
165+
143166
test.run();
144167

0 commit comments

Comments
 (0)