From 1043ef82b1e9ceefc3b74fd21eb28e9a740bd1db Mon Sep 17 00:00:00 2001 From: wukidy Date: Tue, 25 Oct 2022 15:41:56 +0800 Subject: [PATCH] fix: add support for jsx expression --- .../src/utils/expressionParser.ts | 6 ++- .../code-generator/src/utils/jsExpression.ts | 7 ++- .../expressionParser/jsExpression.test.ts | 45 +++++++++++++++++++ .../parseExpressionGetKeywords.test.ts | 31 +++++++++++++ 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 modules/code-generator/tests/utils/expressionParser/jsExpression.test.ts create mode 100644 modules/code-generator/tests/utils/expressionParser/parseExpressionGetKeywords.test.ts diff --git a/modules/code-generator/src/utils/expressionParser.ts b/modules/code-generator/src/utils/expressionParser.ts index 58c6e6ca8..cbc950bbc 100644 --- a/modules/code-generator/src/utils/expressionParser.ts +++ b/modules/code-generator/src/utils/expressionParser.ts @@ -161,7 +161,11 @@ export function parseExpressionGetKeywords(expr: string | null | undefined): str try { const keywordVars = new OrderedSet(); - const ast = parser.parse(`!(${expr});`); + const ast = parser.parse(`!(${expr});`, { + plugins: [ + 'jsx', + ], + }); const addIdentifierIfNeeded = (x: Record | number | null | undefined) => { if (typeof x === 'object' && isIdentifier(x) && JS_KEYWORDS.includes(x.name)) { diff --git a/modules/code-generator/src/utils/jsExpression.ts b/modules/code-generator/src/utils/jsExpression.ts index d9fb67892..d6dc8c027 100644 --- a/modules/code-generator/src/utils/jsExpression.ts +++ b/modules/code-generator/src/utils/jsExpression.ts @@ -8,7 +8,12 @@ import { transformExpressionLocalRef, ParseError } from './expressionParser'; function parseFunction(content: string): t.FunctionExpression | null { try { - const ast = parser.parse(`(${content});`); + const ast = parser.parse(`(${content});`, { + plugins: [ + 'jsx', + ], + }); + let resultNode: t.FunctionExpression | null = null; traverse(ast, { FunctionExpression(path) { diff --git a/modules/code-generator/tests/utils/expressionParser/jsExpression.test.ts b/modules/code-generator/tests/utils/expressionParser/jsExpression.test.ts new file mode 100644 index 000000000..8a801fbe8 --- /dev/null +++ b/modules/code-generator/tests/utils/expressionParser/jsExpression.test.ts @@ -0,0 +1,45 @@ +import { generateFunction } from '../../../src/utils/jsExpression'; + +const marcoFactory = () => { + const cases: any[] = []; + + const marco = ( + value: { type: string; value: string }, + config: Record, + expected: any, + ) => { + cases.push([value, config, expected]); + }; + + const start = () => { + test.each(cases)(`after convert this to context "${1}" should be "${3}"`, (a, b, expected) => { + expect(generateFunction(a, b)).toEqual(expected); + }); + }; + + return { marco, start }; +}; + +const { marco: testMarco, start: startMarco } = marcoFactory(); + +// 支持普通函数 +testMarco( + { + type: 'JSFunction', + value: 'function isDisabled(row, rowIndex) { \n \n}', + }, + { isArrow: true }, + '(row, rowIndex) => {}', +); + +// 支持 jsx 表达式 +testMarco( + { + type: 'JSFunction', + value: 'function content() { \n return
我是自定义在div内容123
\n}', + }, + { isArrow: true }, + '() => {\n return
我是自定义在div内容123
;\n}', +); + +startMarco(); diff --git a/modules/code-generator/tests/utils/expressionParser/parseExpressionGetKeywords.test.ts b/modules/code-generator/tests/utils/expressionParser/parseExpressionGetKeywords.test.ts new file mode 100644 index 000000000..5f9cd700b --- /dev/null +++ b/modules/code-generator/tests/utils/expressionParser/parseExpressionGetKeywords.test.ts @@ -0,0 +1,31 @@ +import { parseExpressionGetKeywords } from '../../../src/utils/expressionParser'; + +const marcoFactory = () => { + const cases: any[] = []; + + const marco = (input: string | null, expected: any) => { + cases.push([input, expected]); + }; + + const start = () => { + test.each(cases)( + `after convert this to context "${1}" should be "${2}"`, + (a, expected) => { + expect(parseExpressionGetKeywords(a)).toEqual(expected); + }, + ); + }; + + return { marco, start }; +}; + +const { marco: testMarco, start: startMarco } = marcoFactory(); + +// 支持普通函数 +testMarco('function isDisabled(row) {}', []); +testMarco('function content() { \n return "hello world"\n}', []); + +// 支持 jsx 表达式 +testMarco('function content() { \n return
自定义在div内容123
\n}', []); + +startMarco();