forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
459 lines (382 loc) · 14 KB
/
utils.js
File metadata and controls
459 lines (382 loc) · 14 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import { useResolveStore } from '@/_stores/resolverStore';
import moment from 'moment';
import _, { isEmpty } from 'lodash';
import { useCurrentStateStore } from '@/_stores/currentStateStore';
import { any } from 'superstruct';
import { generateSchemaFromValidationDefinition, validate } from '../component-properties-validation';
import {
hasCircularDependency,
resolveReferences as olderResolverMethod,
removeNestedDoubleCurlyBraces,
} from '@/_helpers/utils';
import { validateMultilineCode } from '@/_helpers/utility';
const acorn = require('acorn');
const acorn_code = `
const array = [1, 2, 3];
const string = "hello";
const object = {};
const boolean = true;
const number = 1;
`;
const ast = acorn.parse(acorn_code, { ecmaVersion: 2020 });
export const getCurrentNodeType = (node) => Object.prototype.toString.call(node).slice(8, -1);
function traverseAST(node, callback) {
callback(node);
for (let key in node) {
if (node[key] && typeof node[key] === 'object') {
traverseAST(node[key], callback);
}
}
}
function getMethods(type) {
const arrayMethods = Object.getOwnPropertyNames(Array.prototype).filter(
(p) => typeof Array.prototype[p] === 'function'
);
const stringMethods = Object.getOwnPropertyNames(String.prototype).filter(
(p) => typeof String.prototype[p] === 'function'
);
const objectMethods = Object.getOwnPropertyNames(Object.prototype).filter(
(p) => typeof Object.prototype[p] === 'function'
);
const booleanMethods = Object.getOwnPropertyNames(Boolean.prototype).filter(
(p) => typeof Boolean.prototype[p] === 'function'
);
const numberMethods = Object.getOwnPropertyNames(Number.prototype).filter(
(p) => typeof Number.prototype[p] === 'function'
);
switch (type) {
case 'Array':
return arrayMethods;
case 'String':
return stringMethods;
case 'Object':
return objectMethods;
case 'Boolean':
return booleanMethods;
case 'Number':
return numberMethods;
default:
return [];
}
}
function inferType(node) {
if (node.type === 'ArrayExpression') {
return 'Array';
} else if (node.type === 'Literal') {
if (typeof node.value === 'string') {
return 'String';
} else if (typeof node.value === 'number') {
return 'Number';
} else if (typeof node.value === 'boolean') {
return 'Boolean';
}
} else if (node.type === 'ObjectExpression') {
return 'Object';
}
return null;
}
export const createJavaScriptSuggestions = () => {
const allMethods = {};
traverseAST(ast, (node) => {
if (node.type === 'VariableDeclarator' && node.id.type === 'Identifier') {
const type = inferType(node.init);
if (type) {
allMethods[node.id.name] = {
type: type,
methods: getMethods(type),
};
}
}
});
return allMethods;
};
const resolveWorkspaceVariables = (query) => {
let resolvedStr = query;
let error = null;
let valid = false;
// Resolve %%object%%
const serverRegex = /(%%.+?%%)/g;
const serverMatches = resolvedStr.match(serverRegex);
if (serverMatches) {
serverMatches.forEach((serverMatch) => {
const code = serverMatch.replace(/%%/g, '');
if (code.includes('server.') && !/^server\.[A-Za-z0-9]+$/.test(code)) {
resolvedStr = resolvedStr.replace(serverMatch, 'HiddenEnvironmentVariable');
} else {
const resolvedCode = resolveCode(code);
resolvedStr = resolvedStr.replace(serverMatch, resolvedCode);
}
});
valid = true;
}
return [valid, error, resolvedStr];
};
function resolveCode(code, customObjects = {}, withError = false, reservedKeyword, isJsCode) {
let result = '';
let error;
// dont resolve if code starts with "queries." and ends with "run()"
if (code.startsWith('queries.') && code.endsWith('run()')) {
error = `Cannot resolve function call ${code}`;
} else {
try {
const state = useCurrentStateStore.getState();
const evalFunction = Function(
[
'variables',
'components',
'queries',
'globals',
'page',
'client',
'server',
'constants',
'moment',
'_',
...Object.keys(customObjects),
reservedKeyword,
],
`return ${code}`
);
result = evalFunction(
isJsCode ? state?.variables : undefined,
isJsCode ? state?.components : undefined,
isJsCode ? state?.queries : undefined,
isJsCode ? state?.globals : undefined,
isJsCode ? state?.page : undefined,
isJsCode ? undefined : state?.client,
isJsCode ? undefined : state?.server,
state?.constants, // Passing constants as an argument allows the evaluated code to access and utilize the constants value correctly.
moment,
_,
...Object.values(customObjects),
null
);
} catch (err) {
error = err.toString();
}
}
if (withError) return [result, error];
return result;
}
function getDynamicVariables(text) {
/* eslint-disable no-useless-escape */
const matchedParams = text.match(/\{\{(.*?)\}\}/g) || text.match(/\%\%(.*?)\%\%/g);
return matchedParams;
}
const resolveMultiDynamicReferences = (code, lookupTable, queryHasJSCode, customResolvers = {}) => {
try {
let resolvedValue = code;
const isComponentValue = code.includes('components.') || code.includes('queries.') || false;
const allDynamicVariables = getDynamicVariables(code) || [];
let isJSCodeResolver = queryHasJSCode && (allDynamicVariables.length === 1 || allDynamicVariables.length === 0);
if (!isJSCodeResolver) {
allDynamicVariables.forEach((variable) => {
const variableToResolve = removeNestedDoubleCurlyBraces(variable);
const { toResolveReference } = inferJSExpAndReferences(variableToResolve, lookupTable.hints);
if (!isComponentValue && toResolveReference && lookupTable.hints.has(toResolveReference)) {
const idToLookUp = lookupTable.hints.get(variableToResolve);
const res = lookupTable.resolvedRefs.get(idToLookUp);
resolvedValue = resolvedValue.replace(variable, res);
} else {
const [resolvedCode] = resolveCode(variableToResolve, customResolvers, true, [], true);
resolvedValue = resolvedValue.replace(variable, resolvedCode);
}
});
} else {
const variableToResolve = removeNestedDoubleCurlyBraces(code);
const [resolvedCode] = resolveCode(variableToResolve, customResolvers, true, [], true);
resolvedValue = typeof resolvedCode === 'string' ? resolvedValue.replace(code, resolvedCode) : resolvedCode;
}
return resolvedValue;
} catch (error) {
console.error('Error resolving code', error);
}
};
const queryHasStringOtherThanVariable = (query) => {
const startsWithDoubleCurly = query.startsWith('{{');
const endsWithDoubleCurly = query.endsWith('}}');
if (startsWithDoubleCurly && endsWithDoubleCurly) {
const content = query.slice(2, -2).trim();
if (content.includes(' ')) {
return true;
}
//* Check if the content includes a template literal
//!Note: Do not delete this regex, it is used to check if the content includes a template literal
//used for cases like {{queries.runjs1.data[0][`${components.textinput1.value}`]}}
const templateLiteralRegex = /\$\{[^}]+\}/;
return templateLiteralRegex.test(content);
}
return false;
};
export const resolveReferences = (query, validationSchema, customResolvers = {}) => {
if (typeof query === 'number') {
return [true, null, query];
}
if (query !== '' && (!query || typeof query !== 'string')) {
// fallback to old resolver for non-string values
const resolvedValue = olderResolverMethod(query);
return [true, null, resolvedValue];
}
let resolvedValue = query;
let error = null;
//Todo : remove resolveWorkspaceVariables when workspace variables are removed
if (query?.startsWith('%%') && query?.endsWith('%%')) {
return resolveWorkspaceVariables(query);
}
if (query?.startsWith('{{') && query?.endsWith('}}')) {
const { status, data } = validateMultilineCode(query);
if (status === 'failed') {
const errMessage = `${data.message} - ${data.description}`;
return [false, errMessage, query, query];
}
}
if ((!validationSchema || isEmpty(validationSchema)) && (!query?.includes('{{') || !query?.includes('}}'))) {
return [true, error, resolvedValue];
}
if (validationSchema && !query?.includes('{{') && !query?.includes('}}')) {
const [valid, errors, newValue] = validateComponentProperty(query, validationSchema);
return [valid, errors, newValue, resolvedValue];
}
const queryHasJSCode = queryHasStringOtherThanVariable(query);
let useJSResolvers = queryHasJSCode || getDynamicVariables(query)?.length > 1;
if (
!queryHasJSCode &&
getDynamicVariables(query)?.length === 1 &&
((!query.startsWith('{{') && query.includes('{{')) || (query.startsWith('{{') && !query.endsWith('}}')))
) {
useJSResolvers = true;
}
const { lookupTable } = useResolveStore.getState();
if (useJSResolvers) {
resolvedValue = resolveMultiDynamicReferences(query, lookupTable, queryHasJSCode, customResolvers);
} else {
let value = removeNestedDoubleCurlyBraces(query);
if (value.startsWith('#') || value.includes('table-')) {
value = JSON.stringify(value);
}
const { toResolveReference, jsExpression, jsExpMatch } =
lookupTable.hints || lookupTable.hints.has
? inferJSExpAndReferences(value, lookupTable.hints)
: { toResolveReference: null, jsExpression: null, jsExpMatch: null };
if (!jsExpMatch && toResolveReference && lookupTable.hints.has(toResolveReference)) {
const idToLookUp = lookupTable.hints.get(toResolveReference);
resolvedValue = lookupTable.resolvedRefs.get(idToLookUp);
if (jsExpression) {
let jscode = value;
if (!Array.isArray(resolvedValue) && typeof resolvedValue !== 'object' && resolvedValue !== null) {
jscode = value.replace(toResolveReference, resolvedValue).replace(toResolveReference, `'${resolvedValue}'`);
resolvedValue = resolveCode(jscode, customResolvers);
} else {
const [resolvedCode, errorRef] = resolveCode(value, customResolvers, true, [], true);
resolvedValue = resolvedCode;
error = errorRef || null;
}
}
} else {
const [resolvedCode, errorRef] = resolveCode(value, customResolvers, true, [], true);
resolvedValue = resolvedCode;
error = errorRef || null;
}
}
if (!validationSchema || isEmpty(validationSchema)) {
return [true, error, resolvedValue, resolvedValue];
}
if (error) {
return [false, error, query, query];
}
if (hasCircularDependency(resolvedValue)) {
return [false, `${resolvedValue} has circular dependency, unable to resolve`, query, query];
}
if (validationSchema) {
const [valid, errors, newValue] = validateComponentProperty(resolvedValue, validationSchema);
return [valid, errors, newValue, resolvedValue];
}
};
export const paramValidation = (expectedType, value) => {
const type = getCurrentNodeType(value)?.toLowerCase();
return type === expectedType;
};
const inferJSExpAndReferences = (code, hintsMap) => {
if (!code) return { toResolveReference: null, jsExpression: null };
//check starts with JS expression like JSON.parse or JSON.stringify !
const jsExpRegex = /(JSON\..+?\(.+?\))/g;
const jsExpMatch = code.match(jsExpRegex)?.[0];
if (jsExpMatch) {
return { toResolveReference: null, jsExpression: null, jsExpMatch };
}
// Split the code into segments using '.' as a delimiter
const segments = code.split('.');
let referenceChain = '';
let jsExpression = '';
for (let i = 0; i < segments.length; i++) {
const segment = segments[i];
const potentialReference = referenceChain ? referenceChain + '.' + segment : segment;
// Check if the potential reference exists in hintsMap
if (hintsMap.has && hintsMap.has(potentialReference)) {
// If it does, update the referenceChain
referenceChain = potentialReference;
} else {
// If it doesn't, treat the rest as a JS expression
jsExpression = segments.slice(i).join('.');
break;
}
}
return {
toResolveReference: referenceChain || null,
jsExpression: jsExpression || null,
};
};
export const FxParamTypeMapping = Object.freeze({
text: 'Text',
string: 'Text',
color: 'Color',
json: 'Json',
code: 'Code',
toggle: 'Toggle',
select: 'Select',
alignButtons: 'AlignButtons',
number: 'Number',
boxShadow: 'BoxShadow',
clientServerSwitch: 'ClientServerSwitch',
switch: 'Switch',
checkbox: 'Checkbox',
slider: 'Slider',
input: 'Input',
icon: 'Icon',
visibility: 'Visibility',
numberInput: 'NumberInput',
tableRowHeightInput: 'TableRowHeightInput',
});
export function computeCoercion(oldValue, newValue) {
const oldValueType = Array.isArray(oldValue) ? 'array' : typeof oldValue;
const newValueType = Array.isArray(newValue) ? 'array' : typeof newValue;
if (oldValueType === newValueType) {
if (JSON.stringify(oldValue) !== JSON.stringify(newValue)) {
return [` → ${JSON.stringify(newValue)}`, newValueType, oldValueType];
}
} else {
return [` → ${JSON.stringify(newValue)}`, newValueType, oldValueType];
}
return ['', newValueType, oldValueType];
}
export const validateComponentProperty = (resolvedValue, validation) => {
const validationDefinition = validation?.schema;
const defaultValue = validation?.defaultValue;
const schema = _.isUndefined(validationDefinition)
? any()
: generateSchemaFromValidationDefinition(validationDefinition);
return validate(resolvedValue, schema, defaultValue, true);
};
export function hasDeepChildren(obj, currentDepth = 1, maxDepth = 3) {
if (currentDepth > maxDepth) {
return true;
}
for (const key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
if (hasDeepChildren(obj[key], currentDepth + 1, maxDepth)) {
return true;
}
}
}
return false;
}