From df92d81c782d273474b2f24e8deb2eae5029ebbf Mon Sep 17 00:00:00 2001 From: Arpit Date: Tue, 9 Apr 2024 13:52:02 +0530 Subject: [PATCH] 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 --- frontend/src/Editor/CodeEditor/styles.scss | 2 +- frontend/src/Editor/CodeEditor/utils.js | 12 +++++++-- frontend/src/_helpers/appUtils.js | 31 +++++++++++++++++++--- frontend/src/_helpers/utils.js | 16 ++++++++++- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/frontend/src/Editor/CodeEditor/styles.scss b/frontend/src/Editor/CodeEditor/styles.scss index f032ee08b0..a5dd955ec0 100644 --- a/frontend/src/Editor/CodeEditor/styles.scss +++ b/frontend/src/Editor/CodeEditor/styles.scss @@ -245,7 +245,7 @@ .cm-tooltip-autocomplete { @extend .cm-base-autocomplete; - top: 0px !important; + top: content-box !important; } } diff --git a/frontend/src/Editor/CodeEditor/utils.js b/frontend/src/Editor/CodeEditor/utils.js index b068ddb487..dc0ea6811e 100644 --- a/frontend/src/Editor/CodeEditor/utils.js +++ b/frontend/src/Editor/CodeEditor/utils.js @@ -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) { diff --git a/frontend/src/_helpers/appUtils.js b/frontend/src/_helpers/appUtils.js index 309281afc2..c9fb7d7a1f 100644 --- a/frontend/src/_helpers/appUtils.js +++ b/frontend/src/_helpers/appUtils.js @@ -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, diff --git a/frontend/src/_helpers/utils.js b/frontend/src/_helpers/utils.js index 70aa1bdae4..bd63b34908 100644 --- a/frontend/src/_helpers/utils.js +++ b/frontend/src/_helpers/utils.js @@ -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;