GeezerVison is showing github.com — fix: add support for jsx expression · ikvm/lowcode-engine@1043ef8
File treeExpand file treeCollapse file tree Open diff view settings tests/utils/expressionParser
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/expressionParser.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,7 +161,11 @@ export function parseExpressionGetKeywords(expr: string | null | undefined): str | 161 | 161 | try { | 162 | 162 | const keywordVars = new OrderedSet<string>(); | 163 | 163 |
| 164 | | - const ast = parser.parse(`!(${expr});`); | | 164 | + const ast = parser.parse(`!(${expr});`, { | | 165 | + plugins: [ | | 166 | + 'jsx', | | 167 | + ], | | 168 | + }); | 165 | 169 |
| 166 | 170 | const addIdentifierIfNeeded = (x: Record<string, unknown> | number | null | undefined) => { | 167 | 171 | if (typeof x === 'object' && isIdentifier(x) && JS_KEYWORDS.includes(x.name)) { | |
Collapse file Copy file name to clipboardExpand all lines: modules/code-generator/src/utils/jsExpression.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,12 @@ import { transformExpressionLocalRef, ParseError } from './expressionParser'; | 8 | 8 |
| 9 | 9 | function parseFunction(content: string): t.FunctionExpression | null { | 10 | 10 | try { | 11 | | - const ast = parser.parse(`(${content});`); | | 11 | + const ast = parser.parse(`(${content});`, { | | 12 | + plugins: [ | | 13 | + 'jsx', | | 14 | + ], | | 15 | + }); | | 16 | + | 12 | 17 | let resultNode: t.FunctionExpression | null = null; | 13 | 18 | traverse(ast, { | 14 | 19 | FunctionExpression(path) { | |
Collapse file Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { generateFunction } from '../../../src/utils/jsExpression'; | | 2 | + | | 3 | +const marcoFactory = () => { | | 4 | + const cases: any[] = []; | | 5 | + | | 6 | + const marco = ( | | 7 | + value: { type: string; value: string }, | | 8 | + config: Record<string, string | boolean>, | | 9 | + expected: any, | | 10 | + ) => { | | 11 | + cases.push([value, config, expected]); | | 12 | + }; | | 13 | + | | 14 | + const start = () => { | | 15 | + test.each(cases)(`after convert this to context "${1}" should be "${3}"`, (a, b, expected) => { | | 16 | + expect(generateFunction(a, b)).toEqual(expected); | | 17 | + }); | | 18 | + }; | | 19 | + | | 20 | + return { marco, start }; | | 21 | +}; | | 22 | + | | 23 | +const { marco: testMarco, start: startMarco } = marcoFactory(); | | 24 | + | | 25 | +// 支持普通函数 | | 26 | +testMarco( | | 27 | + { | | 28 | + type: 'JSFunction', | | 29 | + value: 'function isDisabled(row, rowIndex) { \n \n}', | | 30 | + }, | | 31 | + { isArrow: true }, | | 32 | + '(row, rowIndex) => {}', | | 33 | +); | | 34 | + | | 35 | +// 支持 jsx 表达式 | | 36 | +testMarco( | | 37 | + { | | 38 | + type: 'JSFunction', | | 39 | + value: 'function content() { \n return <div>我是自定义在div内容123</div> \n}', | | 40 | + }, | | 41 | + { isArrow: true }, | | 42 | + '() => {\n return <div>我是自定义在div内容123</div>;\n}', | | 43 | +); | | 44 | + | | 45 | +startMarco(); |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { parseExpressionGetKeywords } from '../../../src/utils/expressionParser'; | | 2 | + | | 3 | +const marcoFactory = () => { | | 4 | + const cases: any[] = []; | | 5 | + | | 6 | + const marco = (input: string | null, expected: any) => { | | 7 | + cases.push([input, expected]); | | 8 | + }; | | 9 | + | | 10 | + const start = () => { | | 11 | + test.each(cases)( | | 12 | + `after convert this to context "${1}" should be "${2}"`, | | 13 | + (a, expected) => { | | 14 | + expect(parseExpressionGetKeywords(a)).toEqual(expected); | | 15 | + }, | | 16 | + ); | | 17 | + }; | | 18 | + | | 19 | + return { marco, start }; | | 20 | +}; | | 21 | + | | 22 | +const { marco: testMarco, start: startMarco } = marcoFactory(); | | 23 | + | | 24 | +// 支持普通函数 | | 25 | +testMarco('function isDisabled(row) {}', []); | | 26 | +testMarco('function content() { \n return "hello world"\n}', []); | | 27 | + | | 28 | +// 支持 jsx 表达式 | | 29 | +testMarco('function content() { \n return <div>自定义在div内容123</div> \n}', []); | | 30 | + | | 31 | +startMarco(); |
|