Skip to content

Commit

Permalink
feature: comment multiline
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonxiang committed Aug 26, 2024
1 parent aa23e15 commit 8acd5b2
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
25 changes: 21 additions & 4 deletions packages/core/src/print/gen-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ function getType(type, paramValue) {
return TYPE_REFLECTION[type];
}
if(paramValue.parent?.nested) {
return paramValue.parent.name + type;
try {
if(paramValue.parent.lookupTypeOrEnum(type)) {
return paramValue.parent.name + type;
}
} catch (error) {
if (error instanceof Error) {
console.log('lookup protobuf type error:' + error.message);
} else {
console.log('lookup protobuf type error: Unknown error');
}

}
return type;
}

return type;
Expand Down Expand Up @@ -119,6 +131,12 @@ export function genType(proto, options) {
items.params.forEach((item) => {
const optionalChar = item.required ? '' : '?';

if (item.comment) {
item.comment.split('\n').forEach((line) => {
result.append(`//${line} \n`);
})
}

if (item.repeated) {
result.append(`${item.name}${optionalChar}: ${item.type}[];`);
} else if (item.keyType) {
Expand All @@ -128,16 +146,15 @@ export function genType(proto, options) {
} else {
result.append(`${item.name}${optionalChar}: ${item.type};`);
}
if (item.comment) {
result.append(` //${item.comment}`);
}

result.append('\n');
});

const prefix = options.isDefinition ? '' : 'export ';

let interfaceName = items.name;

// deal with nested type conflict problem
if(parent && parent instanceof Type && parent.name) {
interfaceName = parent.name + interfaceName;
}
Expand Down
30 changes: 30 additions & 0 deletions packages/core/tests/comment/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { parseProto } from '../../src/index.js';
import { test } from 'uvu';
import * as assert from 'uvu/assert';


const source = `
message UpdateHostBankAccWalletInfoResp {
// 0:成功  非0:错误码   
//50010 bank account already exist
//50011: bank account number already exist but the bank name or bank account name mismatch
//50012 bank account mismatch the one on bank side
optional ErrorCode errorCode = 1;
optional string errorMsg = 2;
optional UpdateHostBankAccWalletInfoRespData data = 3;
}
`;

test('Comment of Field type should be converted', () => {
const ts = parseProto(source);

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


test.run();
42 changes: 41 additions & 1 deletion packages/core/tests/print/nest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,44 @@ test('nest type4 should be converted', () => {
assert.match(ts, '@typedef {Object} Message2');
});

test.run();
test('nest type5 should be converted', () => {
const source = `
syntax = "proto3";
message Message1 {
message Data {
int32 id = 1;
}
Data data = 1;
Finder finder = 2;
}
`;
const ts = parseProto(source);
assert.match(ts, 'interface Message1');
assert.match(ts, 'data?: Message1Data');
assert.match(ts, 'finder?: Finder');
assert.match(ts, 'interface Message1Data');
assert.match(ts, 'id?: number');
});

test('nest type6 should be converted', () => {
const source = `
syntax = "proto3";
message Message1 {
message Data {
int32 id = 1;
}
Data data = 1;
Finder finder = 2;
}
`;
const ts = parseProto(source, { isJsdoc: true });
assert.match(ts, '@typedef {Object} Message1Data');
assert.match(ts, '* @prop {Finder=} finder ');
assert.match(ts, '@typedef {Object} Message1');
});

test.run();

0 comments on commit 8acd5b2

Please sign in to comment.