From 659efe67d7859ad87fa7534d54cd968dfface9e3 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 19 Apr 2024 16:40:45 +0100 Subject: [PATCH] Adding edge case handling to the binding readable/runtime conversion, checking if it is trying to replace a binding which a substring of a helper name, which causes the helper to become un-usable. --- packages/builder/src/dataBinding.js | 19 +++++++++++++++++++ packages/string-templates/src/index.ts | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/dataBinding.js b/packages/builder/src/dataBinding.js index 8fdf71c156..17acd48764 100644 --- a/packages/builder/src/dataBinding.js +++ b/packages/builder/src/dataBinding.js @@ -22,12 +22,14 @@ import { isJSBinding, decodeJSBinding, encodeJSBinding, + getJsHelperList, } from "@budibase/string-templates" import { TableNames } from "./constants" import { JSONUtils, Constants } from "@budibase/frontend-core" import ActionDefinitions from "components/design/settings/controls/ButtonActionEditor/manifest.json" import { environment, licensing } from "stores/portal" import { convertOldFieldFormat } from "components/design/settings/controls/FieldConfiguration/utils" +import { helpersToCompletion } from "components/common/CodeEditor" const { ContextScopes } = Constants @@ -1210,6 +1212,23 @@ const shouldReplaceBinding = (currentValue, from, convertTo, binding) => { if (!currentValue?.includes(from)) { return false } + const helperNames = Object.keys(getJsHelperList()) + const matchedHelperNames = helperNames.filter( + name => name.includes(from) && currentValue.includes(name) + ) + // edge case - if the binding is part of a helper it may accidentally replace it + if (matchedHelperNames.length > 0) { + const indexStart = currentValue.indexOf(from), + indexEnd = indexStart + from.length + for (let helperName of matchedHelperNames) { + const helperIndexStart = currentValue.indexOf(helperName), + helperIndexEnd = helperIndexStart + helperName.length + if (indexStart >= helperIndexStart && indexEnd <= helperIndexEnd) { + return false + } + } + } + if (convertTo === "readableBinding") { // Dont replace if the value already matches the readable binding return currentValue.indexOf(binding.readableBinding) === -1 diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index 847567cb5a..0992813e9d 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -16,7 +16,7 @@ import { setJSRunner, removeJSRunner } from "./helpers/javascript" import manifest from "./manifest.json" import { ProcessOptions } from "./types" -export { helpersToRemoveForJs } from "./helpers/list" +export { helpersToRemoveForJs, getJsHelperList } from "./helpers/list" export { FIND_ANY_HBS_REGEX } from "./utilities" export { setJSRunner, setOnErrorLog } from "./helpers/javascript" export { iifeWrapper } from "./iife"