forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsExpression.ts
More file actions
148 lines (128 loc) · 3.88 KB
/
jsExpression.ts
File metadata and controls
148 lines (128 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import * as parser from '@babel/parser';
import generate from '@babel/generator';
import traverse from '@babel/traverse';
import * as t from '@babel/types';
import { IPublicTypeJSExpression, IPublicTypeJSFunction, isJSExpression, isJSFunction } from '@alilc/lowcode-types';
import { CodeGeneratorError, IScope } from '../types';
import { transformExpressionLocalRef, ParseError } from './expressionParser';
import { isJSExpressionFn } from './common';
function parseFunction(content: string): t.FunctionExpression | null {
try {
const ast = parser.parse(`(${content});`, {
plugins: [
'jsx',
],
});
let resultNode: t.FunctionExpression | null = null;
traverse(ast, {
FunctionExpression(path) {
resultNode = path.node;
path.stop();
},
});
return resultNode;
} catch (e) {
throw new ParseError(content, e);
}
}
function transformFuncExpr2MethodMember(methodName: string, content: string): string {
const funcNode = parseFunction(content);
if (funcNode) {
const targetNode = t.classMethod(
'method',
(methodName && t.identifier(methodName)) || funcNode.id || t.identifier('notDefineName'),
funcNode.params,
funcNode.body,
undefined,
undefined,
undefined,
funcNode.async || undefined,
);
const { code: resultCode } = generate(targetNode, { sourceMaps: false });
return resultCode;
}
throw new Error('Can not find Function Statement');
}
function getArrowFunction(content: string) {
const funcNode = parseFunction(content);
if (funcNode) {
const targetNode = t.arrowFunctionExpression(
funcNode.params,
funcNode.body,
funcNode.async || undefined,
);
const { code: resultCode } = generate(targetNode, { sourceMaps: false });
return resultCode;
}
throw new Error('Can not find Function Statement');
}
function getBodyStatements(content: string) {
const funcNode = parseFunction(content);
if (funcNode) {
const statements: t.Statement[] = funcNode.body.body;
const targetNode = t.program(statements, undefined, 'module', undefined);
const { code: resultCode } = generate(targetNode, { sourceMaps: false });
return resultCode;
}
throw new Error('Can not find Function Statement');
}
/**
* 是否是广义上的 JSFunction
* @param value
* @returns
*/
export function isBroadJSFunction(value: unknown): boolean {
return isJSExpressionFn(value) || isJSFunction(value);
}
export function generateExpression(value: any, scope: IScope): string {
if (isJSExpression(value)) {
const exprVal = (value as IPublicTypeJSExpression).value.trim();
if (!exprVal) {
return 'null';
}
const afterProcessWithLocals = transformExpressionLocalRef(exprVal, scope);
return afterProcessWithLocals;
}
throw new CodeGeneratorError('Not a JSExpression');
}
function getFunctionSource(cfg: IPublicTypeJSFunction): string {
return cfg.source || cfg.value || cfg.compiled;
}
export function generateFunction(
value: any,
config: {
name?: string;
isMember?: boolean;
isBlock?: boolean;
isArrow?: boolean;
isBindExpr?: boolean;
} = {
name: undefined,
isMember: false,
isBlock: false,
isArrow: false,
isBindExpr: false,
},
) {
if (isBroadJSFunction(value)) {
const functionCfg = value as IPublicTypeJSFunction;
const functionSource = getFunctionSource(functionCfg);
if (config.isMember) {
return transformFuncExpr2MethodMember(config.name || '', functionSource);
}
if (config.isBlock) {
return getBodyStatements(functionSource);
}
if (config.isArrow) {
return getArrowFunction(functionSource);
}
if (config.isBindExpr) {
return `(${functionSource}).bind(this)`;
}
return functionSource;
}
if (isJSExpression(value)) {
return value.value;
}
throw new CodeGeneratorError('Not a JSFunction or JSExpression');
}