Skip to content

Commit 2cf74cd

Browse files
wukidyClarence-pan
authored andcommitted
fix: fix empty string lost when generate variable
1 parent 1ecb2f3 commit 2cf74cd

2 files changed

Lines changed: 125 additions & 1 deletion

File tree

modules/code-generator/src/utils/compositeType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,14 @@ function generateUnknownType(
134134
};
135135

136136
if (options.handlers?.expression) {
137-
return executeFunctionStack(
137+
const expression = executeFunctionStack(
138138
transValue,
139139
scope,
140140
options.handlers.expression,
141141
generateExpression,
142142
options,
143143
);
144+
return expression || 'undefined';
144145
}
145146
return generateExpression(transValue, scope);
146147
}

modules/code-generator/tests/utils/compositeType.test.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { generateCompositeType } from '../../src/utils/compositeType';
2+
import { parseExpressionConvertThis2Context } from '../../src/utils/expressionParser';
23
import { Scope } from '../../src/utils/Scope';
34

45
test('single line string', () => {
@@ -16,3 +17,125 @@ test('string with single quote', () => {
1617
test('string with double quote', () => {
1718
expect(generateCompositeType('a"bc', Scope.createRootScope())).toEqual('"a\\"bc"');
1819
});
20+
21+
const marcoFactory = () => {
22+
const cases: any[] = [];
23+
24+
const marco = (value: any, cb: (expression: string) => void) => {
25+
cases.push([value, cb]);
26+
};
27+
28+
const start = () => {
29+
test.each(cases)('parse expression %s', (item, cb) => {
30+
const testObj = {
31+
globalConfig: {},
32+
online: [
33+
{
34+
description: '表格(CnTable)的数据源',
35+
initialData: {
36+
type: 'variable',
37+
variable: item,
38+
value: '',
39+
},
40+
somethingelse: 'somethingelse',
41+
},
42+
],
43+
};
44+
const ret = generateCompositeType(testObj, Scope.createRootScope(), {
45+
handlers: {
46+
function: (jsFunc) => parseExpressionConvertThis2Context(jsFunc.value, '_this'),
47+
expression: (jsExpr) => parseExpressionConvertThis2Context(jsExpr.value, '_this'),
48+
},
49+
});
50+
cb(ret);
51+
});
52+
};
53+
54+
return { marco, start };
55+
};
56+
57+
const { marco: testMarco, start: startMarco } = marcoFactory();
58+
59+
/**
60+
* dataSource 为低码编辑器里面数据源的输入
61+
* variable 为 schema 存储的结果
62+
* expect 为出码后期望生产的串
63+
64+
* |dataSource | variable | expect
65+
* |-------------------|----------------------------|--------------
66+
* |"" | "\"\"" | ""
67+
* |"helo world" | "\"hello world\"" | "hello world"
68+
* |true | "true" | true
69+
* |false | "false" | false
70+
* |{"name": gaokai} | "{\"name\": \"cone\"}" | {"name": gaokai}
71+
* | | "" | undefined
72+
* |undefined | "undefined" | undefined
73+
* |null | "null" | null
74+
*/
75+
76+
testMarco('""', (expression) => {
77+
expect(expression).toMatchInlineSnapshot(`
78+
"{\\"globalConfig\\": {},
79+
\\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
80+
\\"initialData\\": \\"\\",
81+
\\"somethingelse\\": \\"somethingelse\\"}]}"
82+
`);
83+
});
84+
85+
testMarco('"hello world"', (expression) => {
86+
expect(expression).toMatchInlineSnapshot(`
87+
"{\\"globalConfig\\": {},
88+
\\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
89+
\\"initialData\\": \\"hello world\\",
90+
\\"somethingelse\\": \\"somethingelse\\"}]}"
91+
`);
92+
});
93+
94+
testMarco('true', (expression) => {
95+
expect(expression).toMatchInlineSnapshot(`
96+
"{\\"globalConfig\\": {},
97+
\\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
98+
\\"initialData\\": true,
99+
\\"somethingelse\\": \\"somethingelse\\"}]}"
100+
`);
101+
});
102+
103+
testMarco('{"name": "cone"}', (expression) => {
104+
expect(expression).toMatchInlineSnapshot(`
105+
"{\\"globalConfig\\": {},
106+
\\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
107+
\\"initialData\\": {
108+
\\"name\\": \\"cone\\"
109+
},
110+
\\"somethingelse\\": \\"somethingelse\\"}]}"
111+
`);
112+
});
113+
114+
testMarco('', (expression) => {
115+
expect(expression).toMatchInlineSnapshot(`
116+
"{\\"globalConfig\\": {},
117+
\\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
118+
\\"initialData\\": undefined,
119+
\\"somethingelse\\": \\"somethingelse\\"}]}"
120+
`);
121+
});
122+
123+
testMarco('undefined', (expression) => {
124+
expect(expression).toMatchInlineSnapshot(`
125+
"{\\"globalConfig\\": {},
126+
\\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
127+
\\"initialData\\": undefined,
128+
\\"somethingelse\\": \\"somethingelse\\"}]}"
129+
`);
130+
});
131+
132+
testMarco('null', (expression) => {
133+
expect(expression).toMatchInlineSnapshot(`
134+
"{\\"globalConfig\\": {},
135+
\\"online\\": [{\\"description\\": \\"表格(CnTable)的数据源\\",
136+
\\"initialData\\": null,
137+
\\"somethingelse\\": \\"somethingelse\\"}]}"
138+
`);
139+
});
140+
141+
startMarco();

0 commit comments

Comments
 (0)