Skip to content

Commit

Permalink
fix: solve line break in annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
PanPanZou committed Jul 5, 2024
1 parent e7e602b commit 5e015ca
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
63 changes: 45 additions & 18 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,9 @@ class Visitor {
return item.text.text;
});

var descriptionText = description ? _escape(description.text.text.trimEnd()) : '';
var summaryText = summary ? _escape(summary.text.text.trimEnd()) : '';
var returnText = _return ? _return.text.text.trimEnd() : '';
let hasNextSection = false;
if (deprecated) {
let deprecatedText = deprecated.text.text.trimEnd();
let deprecatedText = _escape(deprecated.text.text).trimEnd();
deprecatedText.split('\n').forEach((line, index, array) => {
if(index === 0) {
this.emit(`// Deprecated: ${line}\n`, level);
Expand All @@ -403,7 +400,8 @@ class Visitor {
});
hasNextSection = true;
}
if (summaryText !== '') {
if (summary) {
let summaryText = _escape(summary.text.text).trimEnd();
if (hasNextSection) {
this.emit(`// \n`, level);
}
Expand All @@ -417,7 +415,8 @@ class Visitor {
});
hasNextSection = true;
}
if (descriptionText !== '') {
if (description) {
let descriptionText = _escape(description.text.text).trimEnd();
if (hasNextSection) {
this.emit(`// \n`, level);
}
Expand All @@ -435,30 +434,58 @@ class Visitor {
if (hasNextSection) {
this.emit(`// \n`, level);
}
params.forEach((item, index) => {
this.emit(`// @param ${item.name} - ${item.text}`, level);
if (index < params.length - 1) {
this.emit(`// \n`, level);
}
params.forEach((item, i) => {
this.emit(`// @param ${item.name} - `, level);
const items = item.text.trimEnd().split('\n');
items.forEach((line, j) => {
if (j === 0) {
this.emit(`${line}\n`);
} else {
this.emit(`// ${line}\n`, level);
}
if (j < items.length - 1 || (j === items.length - 1 && i < params.length -1)) {
this.emit(`// \n`, level);
}
});
});
hasNextSection = true;
}
if (returnText !== '') {
if (_return) {
let returnText =_escape(_return.text.text).trimEnd();
if (hasNextSection) {
this.emit(`// \n`, level);
}
this.emit(`// @return ${returnText}\n`, level);
this.emit(`// @return `, level);
const returns = returnText.split('\n');
returns.forEach((line, index) => {
if (index === 0) {
this.emit(`${line}\n`);
} else {
this.emit(`// ${line}\n`, level);
}
if (index < returns.length - 1) {
this.emit(`// \n`, level);
}
});
hasNextSection = true;
}
if (throws.length > 0) {
if (hasNextSection) {
this.emit(`// \n`, level);
}
throws.forEach((item, index) => {
this.emit(`// @throws ${item}`, level);
if (index < throws.length - 1) {
this.emit(`// \n`, level);
}
throws.forEach((item, i) => {
this.emit(`// @throws `, level);
const items = item.trimEnd().split('\n');
items.forEach((line, j) => {
if (j === 0) {
this.emit(`${line}\n`);
} else {
this.emit(`// ${line}\n`, level);
}
if (j < items.length - 1 || (j === items.length - 1 && i < params.length -1)) {
this.emit(`// \n`, level);
}
});
});
}
}
Expand Down
12 changes: 9 additions & 3 deletions test/fixtures/annotation/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func TestFuncWithAnnotation1 (test *string, _test *string) (_err error) {
// @return void
//
// @throws InternalError Server error. 500 服务器端出现未知异常。
//
func TestFuncWithAnnotation2 (test *string, _test *string) (_err error) {
// empty comment1
// empty comment2
Expand All @@ -146,14 +147,19 @@ func TestFuncWithAnnotation2 (test *string, _test *string) (_err error) {
//
// @param test - string param1
//
// param test for line break.
//
// @param _test - string param2
//
// @return void
//
// return test for line break.
//
// @throws InternalError Server error. 500 服务器端出现未知异常。
func TestFuncWithAnnotation3 (test *string, _test *string) (_err error) {
// empty comment1
// empty comment2
//
// throws test for line break.
//
func LineBreakAnnotation (test *string, _test *string) (_err error) {
return _err
}

8 changes: 5 additions & 3 deletions test/fixtures/annotation/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ static async function testFuncWithAnnotation2(test: string, _test: string): void
* deprecated test for line break.
*
* @param test string param1
* param test for line break.
* @param _test string param2
* @return void
* return test for line break.
* @throws InternalError Server error. 500 服务器端出现未知异常。
* throws test for line break.
*/
static async function testFuncWithAnnotation3(test: string, _test: string): void {
// empty comment1
// empty comment2
static async function lineBreakAnnotation(test: string, _test: string): void {
}

3 changes: 3 additions & 0 deletions test/fixtures/interface/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// This file is auto-generated, don't edit it. Thanks.
// Description:
//
//
package client

import (
Expand Down

0 comments on commit 5e015ca

Please sign in to comment.