fix: fix empty string lost when generate variable

This commit is contained in:
wukidy 2022-10-25 17:43:12 +08:00 committed by 牧毅
parent 1ecb2f3c5a
commit 2cf74cd04b
2 changed files with 125 additions and 1 deletions

View File

@ -134,13 +134,14 @@ function generateUnknownType(
};
if (options.handlers?.expression) {
return executeFunctionStack(
const expression = executeFunctionStack(
transValue,
scope,
options.handlers.expression,
generateExpression,
options,
);
return expression || 'undefined';
}
return generateExpression(transValue, scope);
}

View File

@ -1,4 +1,5 @@
import { generateCompositeType } from '../../src/utils/compositeType';
import { parseExpressionConvertThis2Context } from '../../src/utils/expressionParser';
import { Scope } from '../../src/utils/Scope';
test('single line string', () => {
@ -16,3 +17,125 @@ test('string with single quote', () => {
test('string with double quote', () => {
expect(generateCompositeType('a"bc', Scope.createRootScope())).toEqual('"a\\"bc"');
});
const marcoFactory = () => {
const cases: any[] = [];
const marco = (value: any, cb: (expression: string) => void) => {
cases.push([value, cb]);
};
const start = () => {
test.each(cases)('parse expression %s', (item, cb) => {
const testObj = {
globalConfig: {},
online: [
{
description: '表格CnTable的数据源',
initialData: {
type: 'variable',
variable: item,
value: '',
},
somethingelse: 'somethingelse',
},
],
};
const ret = generateCompositeType(testObj, Scope.createRootScope(), {
handlers: {
function: (jsFunc) => parseExpressionConvertThis2Context(jsFunc.value, '_this'),
expression: (jsExpr) => parseExpressionConvertThis2Context(jsExpr.value, '_this'),
},
});
cb(ret);
});
};
return { marco, start };
};
const { marco: testMarco, start: startMarco } = marcoFactory();
/**
* dataSource
* variable schema
* expect
* |dataSource | variable | expect
* |-------------------|----------------------------|--------------
* |"" | "\"\"" | ""
* |"helo world" | "\"hello world\"" | "hello world"
* |true | "true" | true
* |false | "false" | false
* |{"name": gaokai} | "{\"name\": \"cone\"}" | {"name": gaokai}
* | | "" | undefined
* |undefined | "undefined" | undefined
* |null | "null" | null
*/
testMarco('""', (expression) => {
expect(expression).toMatchInlineSnapshot(`
"{\\"globalConfig\\": {},
\\"online\\": [{\\"description\\": \\"表格CnTable的数据源\\",
\\"initialData\\": \\"\\",
\\"somethingelse\\": \\"somethingelse\\"}]}"
`);
});
testMarco('"hello world"', (expression) => {
expect(expression).toMatchInlineSnapshot(`
"{\\"globalConfig\\": {},
\\"online\\": [{\\"description\\": \\"表格CnTable的数据源\\",
\\"initialData\\": \\"hello world\\",
\\"somethingelse\\": \\"somethingelse\\"}]}"
`);
});
testMarco('true', (expression) => {
expect(expression).toMatchInlineSnapshot(`
"{\\"globalConfig\\": {},
\\"online\\": [{\\"description\\": \\"表格CnTable的数据源\\",
\\"initialData\\": true,
\\"somethingelse\\": \\"somethingelse\\"}]}"
`);
});
testMarco('{"name": "cone"}', (expression) => {
expect(expression).toMatchInlineSnapshot(`
"{\\"globalConfig\\": {},
\\"online\\": [{\\"description\\": \\"表格CnTable的数据源\\",
\\"initialData\\": {
\\"name\\": \\"cone\\"
},
\\"somethingelse\\": \\"somethingelse\\"}]}"
`);
});
testMarco('', (expression) => {
expect(expression).toMatchInlineSnapshot(`
"{\\"globalConfig\\": {},
\\"online\\": [{\\"description\\": \\"表格CnTable的数据源\\",
\\"initialData\\": undefined,
\\"somethingelse\\": \\"somethingelse\\"}]}"
`);
});
testMarco('undefined', (expression) => {
expect(expression).toMatchInlineSnapshot(`
"{\\"globalConfig\\": {},
\\"online\\": [{\\"description\\": \\"表格CnTable的数据源\\",
\\"initialData\\": undefined,
\\"somethingelse\\": \\"somethingelse\\"}]}"
`);
});
testMarco('null', (expression) => {
expect(expression).toMatchInlineSnapshot(`
"{\\"globalConfig\\": {},
\\"online\\": [{\\"description\\": \\"表格CnTable的数据源\\",
\\"initialData\\": null,
\\"somethingelse\\": \\"somethingelse\\"}]}"
`);
});
startMarco();