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:
Arpit 2024-04-09 13:52:02 +05:30 committed by GitHub
parent afe2cc0dd3
commit df92d81c78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 54 additions and 7 deletions

View file

@ -245,7 +245,7 @@
.cm-tooltip-autocomplete {
@extend .cm-base-autocomplete;
top: 0px !important;
top: content-box !important;
}
}

View file

@ -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) {

View file

@ -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,

View file

@ -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;