Skip to content

Commit c7cc2a2

Browse files
飞澋yndu13
authored andcommitted
fix: solve backslash in comment and string expr
1 parent 98b5b43 commit c7cc2a2

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

lib/generator.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const {
2020
_subModelName,
2121
remove,
2222
_upperFirst,
23-
md2Html
23+
md2Html,
24+
_doc
2425
} = require('./helper');
2526

2627
function collectionType(name) {
@@ -851,7 +852,7 @@ class Visitor {
851852
if (description) {
852853
const descriptions = md2Html(description).trimEnd().split('\n');
853854
for (let j = 0; j < descriptions.length; j++) {
854-
this.emit(` * ${descriptions[j]}\n`, level);
855+
this.emit(` * ${_doc(descriptions[j])}\n`, level);
855856
}
856857
hasNextSection = true;
857858
}
@@ -862,7 +863,7 @@ class Visitor {
862863
const examples = md2Html(example).trimEnd().split('\n');
863864
this.emit(' * <strong>example:</strong>\n', level);
864865
for (let j = 0; j < examples.length; j++) {
865-
this.emit(` * ${examples[j]}\n`, level);
866+
this.emit(` * ${_doc(examples[j])}\n`, level);
866867
}
867868
hasNextSection = true;
868869
}
@@ -891,7 +892,7 @@ class Visitor {
891892
}
892893
this.emit(' */\n', level);
893894
}
894-
this.emit(`@NameInMap("${realName}")\n`, level);
895+
this.emit(`@NameInMap("${_doc(realName)}")\n`, level);
895896
if (deprecated === 'true') {
896897
this.emit(`@Deprecated\n`, level);
897898
}

lib/helper.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,18 @@ function _string(str) {
160160
if (str.string === '""') {
161161
return '\\"\\"';
162162
}
163+
164+
// process \n in string expr
165+
str.string = str.string.replace(/\\/g, '\\\\').replace(/\n/g, '\\n');
163166
return str.string.replace(/([^\\])"+|^"/g, function (str) {
164167
return str.replace(/"/g, '\\"');
165168
});
166169
}
170+
function _doc(str) {
171+
return str.replace(/\\/g, '\\\\');
172+
}
167173

168174
module.exports = {
169175
_name, _type, _escape, _string,
170-
_lowerFirst, _subModelName, remove, _upperFirst, md2Html
176+
_lowerFirst, _subModelName, remove, _upperFirst, md2Html, _doc
171177
};

test/fixtures/model/NewMyModel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public NewMyModelObject getObject() {
2222
}
2323

2424
public static class NewMyModelObject extends TeaModel {
25+
/**
26+
* <strong>example:</strong>
27+
* <p>example.com\\wuying\\users</p>
28+
*/
2529
@NameInMap("Name")
2630
@Validation(required = true)
2731
public String name;

test/fixtures/model/main.dara

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ model MyModel = {
4444

4545
model NewMyModel = {
4646
object: {
47-
name: string(name='Name'),
47+
name: string(name='Name', example='example.com\\wuying\\users'),
4848
num?: integer(name='Num'),
4949
},
5050
}

0 commit comments

Comments
 (0)