forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompositeType.ts
More file actions
226 lines (195 loc) · 5.95 KB
/
compositeType.ts
File metadata and controls
226 lines (195 loc) · 5.95 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {
CompositeArray,
CompositeValue,
CompositeObject,
JSFunction,
JSExpression,
isJSExpression,
isJSFunction,
isJSSlot,
JSSlot,
} from '@alilc/lowcode-types';
import _ from 'lodash';
import { IScope, CompositeValueGeneratorOptions, CodeGeneratorError } from '../types';
import { generateExpression, generateFunction } from './jsExpression';
import { generateJsSlot } from './jsSlot';
import { executeFunctionStack } from './aopHelper';
import { parseExpressionGetKeywords } from './expressionParser';
interface ILegaoVariable {
type: 'variable';
value: string;
variable: string;
}
function isVariable(v: any): v is ILegaoVariable {
if (_.isObject(v) && (v as ILegaoVariable).type === 'variable') {
return true;
}
return false;
}
interface DataSource {
type: 'DataSource';
id: string;
}
/**
* 判断是否是数据源类型
*/
function isDataSource(v: unknown): v is DataSource {
return typeof v === 'object' && v != null && (v as Partial<DataSource>).type === 'DataSource';
}
function generateArray(
value: CompositeArray,
scope: IScope,
options: CompositeValueGeneratorOptions = {},
): string {
const body = value.map((v) => generateUnknownType(v, scope, options)).join(',');
return `[${body}]`;
}
function generateObject(
value: CompositeObject,
scope: IScope,
options: CompositeValueGeneratorOptions = {},
): string {
if (value.type === 'i18n') {
// params 可能会绑定变量,这里需要处理下
if (value.params && typeof value.params === 'object') {
return `this._i18nText(${generateUnknownType(_.omit(value, 'type'), scope, options)})`;
}
return `this._i18nText(${JSON.stringify(_.omit(value, 'type'))})`; // TODO: 优化:这里可以考虑提取成个常量...
}
const body = Object.keys(value)
.map((key) => {
const propName = JSON.stringify(key);
const v = generateUnknownType(value[key], scope, options);
return `${propName}: ${v}`;
})
.join(',\n');
return `{${body}}`;
}
function generateString(value: string): string {
// 有的字符串里面会有特殊字符,比如换行或引号之类的,这里我们借助 JSON 的字符串转义功能来做下转义并加上双引号
return JSON.stringify(value);
}
function generateNumber(value: number): string {
return String(value);
}
function generateBool(value: boolean): string {
return value ? 'true' : 'false';
}
function genFunction(value: JSFunction): string {
const globalVars = parseExpressionGetKeywords(value.value);
if (globalVars.includes('arguments')) {
return generateFunction(value, { isBindExpr: true });
}
return generateFunction(value, { isArrow: true });
}
function genJsSlot(value: JSSlot, scope: IScope, options: CompositeValueGeneratorOptions = {}) {
if (options.nodeGenerator) {
return generateJsSlot(value, scope, options.nodeGenerator);
}
return '';
}
function generateUnknownType(
value: CompositeValue,
scope: IScope,
options: CompositeValueGeneratorOptions = {},
): string {
if (_.isUndefined(value)) {
return 'undefined';
}
if (_.isNull(value)) {
return 'null';
}
if (_.isArray(value)) {
if (options.handlers?.array) {
return executeFunctionStack(value, scope, options.handlers.array, generateArray, options);
}
return generateArray(value, scope, options);
}
// FIXME: 这个是临时方案
// 在遇到 type variable 私有类型时,转换为 JSExpression
if (isVariable(value)) {
const transValue: JSExpression = {
type: 'JSExpression',
value: value.variable,
};
if (options.handlers?.expression) {
return executeFunctionStack(
transValue,
scope,
options.handlers.expression,
generateExpression,
options,
);
}
return generateExpression(transValue, scope);
}
if (isJSExpression(value)) {
if (options.handlers?.expression) {
return executeFunctionStack(
value,
scope,
options.handlers.expression,
generateExpression,
options,
);
}
return generateExpression(value, scope);
}
if (isJSFunction(value)) {
if (options.handlers?.function) {
return executeFunctionStack(value, scope, options.handlers.function, genFunction, options);
}
return genFunction(value);
}
if (isJSSlot(value)) {
if (options.handlers?.slot) {
return executeFunctionStack(value, scope, options.handlers.slot, genJsSlot, options);
}
return genJsSlot(value, scope, options);
}
if (isDataSource(value)) {
return generateUnknownType(
{
type: 'JSExpression',
value: `this.dataSourceMap[${JSON.stringify(value.id)}]`,
},
scope,
options,
);
}
if (_.isObject(value)) {
if (options.handlers?.object) {
return executeFunctionStack(value, scope, options.handlers.object, generateObject, options);
}
return generateObject(value as CompositeObject, scope, options);
}
if (_.isString(value)) {
if (options.handlers?.string) {
return executeFunctionStack(value, scope, options.handlers.string, generateString, options);
}
return generateString(value);
}
if (_.isNumber(value)) {
if (options.handlers?.number) {
return executeFunctionStack(value, scope, options.handlers.number, generateNumber, options);
}
return generateNumber(value);
}
if (_.isBoolean(value)) {
if (options.handlers?.boolean) {
return executeFunctionStack(value, scope, options.handlers.boolean, generateBool, options);
}
return generateBool(value);
}
throw new CodeGeneratorError('Meet unknown composite value type');
}
// 这一层曾经是对产出做最外层包装的,但其实包装逻辑不应该属于这一层
// 这一层先不去掉,做冗余,方便后续重构
export function generateCompositeType(
value: CompositeValue,
scope: IScope,
options: CompositeValueGeneratorOptions = {},
): string {
const result = generateUnknownType(value, scope, options);
return result;
}