Merge branch hotfix/code-generator-multi-line-string into release/1.0.0

Title: fix: 🐛 解决多行文本在出码的时候生成的字符串是无效代码的问题 

我们这边有个 schema 里有个多行文本,如:
![image](http://git.cn-hangzhou.oss-cdn.aliyun-inc.com/uploads/ali-lowcode/ali-lowcode-engine/24b255170cc884aac9eeed4fa2019401/image.png)

经过目前的出码模块出码后这块会变成:
![image](http://git.cn-hangzhou.oss-cdn.aliyun-inc.com/uploads/ali-lowcode/ali-lowcode-engine/d16c83a0bf27ec14fecfdb8544e9aff0/image.png)

可以看到缺少对换行等特殊字符的处理。故改之。

Link: https://code.aone.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/codereview/4071413
This commit is contained in:
rongbin.arb 2020-11-11 16:22:49 +08:00
commit eafaedb4dc
2 changed files with 22 additions and 1 deletions

View File

@ -34,7 +34,8 @@ function generateObject(value: CompositeObject, scope: IScope, options: Composit
}
function generateString(value: string): string {
return `'${value}'`;
// 有的字符串里面会有特殊字符,比如换行或引号之类的,这里我们借助 JSON 的字符串转义功能来做下转义并加上双引号
return JSON.stringify(value);
}
function generateNumber(value: number): string {

View File

@ -0,0 +1,20 @@
import test from 'ava';
import { generateCompositeType } from '../../src/utils/compositeType';
import Scope from '../../src/utils/Scope';
test('single line string', (t) => {
t.is(generateCompositeType('ab c', Scope.createRootScope()), '"ab c"');
});
test('multi line string', (t) => {
t.is(generateCompositeType('a\nb\nc', Scope.createRootScope()), '"a\\nb\\nc"');
});
test('string with single quote', (t) => {
t.is(generateCompositeType('a\'bc', Scope.createRootScope()), '"a\'bc"');
});
test('string with double quote', (t) => {
t.is(generateCompositeType('a"bc', Scope.createRootScope()), '"a\\"bc"');
});