mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-24 09:28:31 +00:00
Codehinter fixes main 2.0 (#9333)
* fixes: returning or using reserved keywords in Query is crashing Need discussion on reserved keywords. * fixes: on set/unset app and page variables correctly updatees the hints and references * fixes: single line hinters do not resolve single line js code * fixes: multiline code editor suggestion should popup as per content
This commit is contained in:
parent
afe2cc0dd3
commit
df92d81c78
4 changed files with 54 additions and 7 deletions
|
|
@ -245,7 +245,7 @@
|
|||
|
||||
.cm-tooltip-autocomplete {
|
||||
@extend .cm-base-autocomplete;
|
||||
top: 0px !important;
|
||||
top: content-box !important;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ const resolveMultiDynamicReferences = (code, lookupTable) => {
|
|||
} else {
|
||||
const [resolvedCode] = resolveCode(variableToResolve, {}, true, [], true);
|
||||
|
||||
resolvedValue = resolvedCode;
|
||||
resolvedValue = resolvedValue.replace(variable, resolvedCode);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -210,6 +210,14 @@ const resolveMultiDynamicReferences = (code, lookupTable) => {
|
|||
return resolvedValue;
|
||||
};
|
||||
|
||||
const queryHasStringOtherThanVariable = (query) => {
|
||||
if (query.startsWith('{{') && query.endsWith('}}')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
export const resolveReferences = (query, validationSchema, customResolvers = {}) => {
|
||||
if (!query || typeof query !== 'string') return [false, null, null];
|
||||
let resolvedValue = query;
|
||||
|
|
@ -229,7 +237,7 @@ export const resolveReferences = (query, validationSchema, customResolvers = {})
|
|||
return [valid, errors, newValue, resolvedValue];
|
||||
}
|
||||
|
||||
const hasMultiDynamicVariables = getDynamicVariables(query)?.length > 1;
|
||||
const hasMultiDynamicVariables = queryHasStringOtherThanVariable(query) || getDynamicVariables(query)?.length > 1;
|
||||
|
||||
const { lookupTable } = useResolveStore.getState();
|
||||
if (hasMultiDynamicVariables) {
|
||||
|
|
|
|||
|
|
@ -622,7 +622,11 @@ function executeActionWithDebounce(_ref, event, mode, customVariables) {
|
|||
const key = resolveReferences(event.key, getCurrentState(), undefined, customVariables);
|
||||
const customAppVariables = { ...getCurrentState().variables };
|
||||
delete customAppVariables[key];
|
||||
// useResolveStore.getState().actions.removeAppSuggestions(key);
|
||||
useResolveStore.getState().actions.removeAppSuggestions([`variables.${key}`]);
|
||||
useResolveStore
|
||||
.getState()
|
||||
.actions.updateResolvedRefsOfHints([{ hint: 'variables', newRef: customAppVariables }]);
|
||||
|
||||
return useCurrentStateStore.getState().actions.setCurrentState({
|
||||
variables: customAppVariables,
|
||||
});
|
||||
|
|
@ -635,9 +639,14 @@ function executeActionWithDebounce(_ref, event, mode, customVariables) {
|
|||
...getCurrentState().page.variables,
|
||||
[key]: value,
|
||||
};
|
||||
|
||||
useResolveStore.getState().actions.addAppSuggestions({
|
||||
variables: customPageVariables,
|
||||
page: {
|
||||
...getCurrentState().page,
|
||||
variables: customPageVariables,
|
||||
},
|
||||
});
|
||||
|
||||
return useCurrentStateStore.getState().actions.setCurrentState({
|
||||
page: {
|
||||
...getCurrentState().page,
|
||||
|
|
@ -657,7 +666,23 @@ function executeActionWithDebounce(_ref, event, mode, customVariables) {
|
|||
case 'unset-page-variable': {
|
||||
const key = resolveReferences(event.key, getCurrentState(), undefined, customVariables);
|
||||
const customPageVariables = _.omit(getCurrentState().page.variables, key);
|
||||
// useResolveStore.getState().actions.removeAppSuggestions(key);
|
||||
|
||||
useResolveStore.getState().actions.removeAppSuggestions([`page.variables.${key}`]);
|
||||
|
||||
const pageRef = {
|
||||
page: {
|
||||
...getCurrentState().page,
|
||||
variables: customPageVariables,
|
||||
},
|
||||
};
|
||||
|
||||
const toUpdateRefs = [
|
||||
{ hint: 'page', newRef: pageRef },
|
||||
{ hint: 'page.variables', newRef: customPageVariables },
|
||||
];
|
||||
|
||||
useResolveStore.getState().actions.updateResolvedRefsOfHints(toUpdateRefs);
|
||||
|
||||
return useCurrentStateStore.getState().actions.setCurrentState({
|
||||
page: {
|
||||
...getCurrentState().page,
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import { getWorkspaceIdOrSlugFromURL, getSubpath, returnWorkspaceIdIfNeed } from
|
|||
import { getCookie, eraseCookie } from '@/_helpers/cookie';
|
||||
import { staticDataSources } from '@/Editor/QueryManager/constants';
|
||||
|
||||
const reservedKeyword = ['app', 'window']; //Keywords that slows down the app
|
||||
export function findProp(obj, prop, defval) {
|
||||
if (typeof defval === 'undefined') defval = null;
|
||||
prop = prop.split('.');
|
||||
|
|
@ -158,7 +159,7 @@ export function resolveReferences(
|
|||
forPreviewBox = false
|
||||
) {
|
||||
if (object === '{{{}}}') return '';
|
||||
const reservedKeyword = ['app', 'window']; //Keywords that slows down the app
|
||||
|
||||
object = _.clone(object);
|
||||
const objectType = typeof object;
|
||||
let error;
|
||||
|
|
@ -422,6 +423,19 @@ export function validateEmail(email) {
|
|||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
export async function executeMultilineJS(_ref, code, queryId, isPreview, mode = '', parameters = {}) {
|
||||
if ([...reservedKeyword, 'this'].some((keyword) => code.includes(keyword))) {
|
||||
const message = `Code contains ${reservedKeyword.join(' or ')} or this keywords`;
|
||||
const description = 'Cannot resolve code with reserved keywords in it. Please remove them and try again.';
|
||||
|
||||
return {
|
||||
status: 'failed',
|
||||
data: {
|
||||
message,
|
||||
description,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const currentState = getCurrentState();
|
||||
let result = {},
|
||||
error = null;
|
||||
|
|
|
|||
Loading…
Reference in a new issue