Skip to content

Commit 453e069

Browse files
wukidyLeoYuan
authored andcommitted
fix: add support for jsx expression
1 parent e0e537a commit 453e069

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ export function parseExpressionGetKeywords(expr: string | null | undefined): str
161161
try {
162162
const keywordVars = new OrderedSet<string>();
163163

164-
const ast = parser.parse(`!(${expr});`);
164+
const ast = parser.parse(`!(${expr});`, {
165+
plugins: [
166+
'jsx',
167+
],
168+
});
165169

166170
const addIdentifierIfNeeded = (x: Record<string, unknown> | number | null | undefined) => {
167171
if (typeof x === 'object' && isIdentifier(x) && JS_KEYWORDS.includes(x.name)) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import { transformExpressionLocalRef, ParseError } from './expressionParser';
88

99
function parseFunction(content: string): t.FunctionExpression | null {
1010
try {
11-
const ast = parser.parse(`(${content});`);
11+
const ast = parser.parse(`(${content});`, {
12+
plugins: [
13+
'jsx',
14+
],
15+
});
16+
1217
let resultNode: t.FunctionExpression | null = null;
1318
traverse(ast, {
1419
FunctionExpression(path) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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();

0 commit comments

Comments
 (0)