From d1712bda5275e57b2496999d135d06000367a232 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 30 Jan 2024 17:37:09 +0100 Subject: [PATCH 1/4] Remove helpers not available in js --- .../src/components/common/bindings/BindingPicker.svelte | 7 ++++--- packages/builder/src/constants/completions.js | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/components/common/bindings/BindingPicker.svelte b/packages/builder/src/components/common/bindings/BindingPicker.svelte index 93d9d62021..1f35e3fbab 100644 --- a/packages/builder/src/components/common/bindings/BindingPicker.svelte +++ b/packages/builder/src/components/common/bindings/BindingPicker.svelte @@ -47,9 +47,10 @@ }) $: filteredHelpers = helpers?.filter(helper => { return ( - !search || - helper.label.match(searchRgx) || - helper.description.match(searchRgx) + (!search || + helper.label.match(searchRgx) || + helper.description.match(searchRgx)) && + (mode.name !== "javascript" || !helper.requiresBlock) ) }) diff --git a/packages/builder/src/constants/completions.js b/packages/builder/src/constants/completions.js index 32de934324..ab0e5fd52d 100644 --- a/packages/builder/src/constants/completions.js +++ b/packages/builder/src/constants/completions.js @@ -11,6 +11,7 @@ export function handlebarsCompletions() { label: helperName, displayText: helperName, description: helperConfig.description, + requiresBlock: helperConfig.requiresBlock, })) ) } From 70d49bbd6b25eac519b670b236b805279eab1b58 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 31 Jan 2024 09:59:59 +0100 Subject: [PATCH 2/4] Renames --- .../builder/src/components/common/bindings/BindingPicker.svelte | 2 +- packages/builder/src/constants/completions.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/components/common/bindings/BindingPicker.svelte b/packages/builder/src/components/common/bindings/BindingPicker.svelte index 1f35e3fbab..d85a0a2bbb 100644 --- a/packages/builder/src/components/common/bindings/BindingPicker.svelte +++ b/packages/builder/src/components/common/bindings/BindingPicker.svelte @@ -50,7 +50,7 @@ (!search || helper.label.match(searchRgx) || helper.description.match(searchRgx)) && - (mode.name !== "javascript" || !helper.requiresBlock) + (mode.name !== "javascript" || helper.allowsJs) ) }) diff --git a/packages/builder/src/constants/completions.js b/packages/builder/src/constants/completions.js index ab0e5fd52d..4af0bad38d 100644 --- a/packages/builder/src/constants/completions.js +++ b/packages/builder/src/constants/completions.js @@ -11,7 +11,7 @@ export function handlebarsCompletions() { label: helperName, displayText: helperName, description: helperConfig.description, - requiresBlock: helperConfig.requiresBlock, + allowsJs: !helperConfig.requiresBlock, })) ) } From e5d5dea5e626cb593281fb0bb47ce6a17e4dc123 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 31 Jan 2024 10:00:41 +0100 Subject: [PATCH 3/4] Renames --- packages/string-templates/src/conversion/index.js | 4 ++-- packages/string-templates/src/helpers/index.js | 4 ++-- packages/string-templates/src/helpers/javascript.js | 4 ++-- packages/string-templates/src/helpers/list.js | 2 +- packages/string-templates/test/manifest.spec.js | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/string-templates/src/conversion/index.js b/packages/string-templates/src/conversion/index.js index 30e2510b55..10aaef0d2f 100644 --- a/packages/string-templates/src/conversion/index.js +++ b/packages/string-templates/src/conversion/index.js @@ -1,4 +1,4 @@ -const { getHelperList } = require("../helpers") +const { getJsHelperList } = require("../helpers") function getLayers(fullBlock) { let layers = [] @@ -109,7 +109,7 @@ module.exports.convertHBSBlock = (block, blockNumber) => { const layers = getLayers(block) let value = null - const list = getHelperList() + const list = getJsHelperList() for (let layer of layers) { const parts = splitBySpace(layer) if (value || parts.length > 1 || list[parts[0]]) { diff --git a/packages/string-templates/src/helpers/index.js b/packages/string-templates/src/helpers/index.js index bed3d0c3e3..5e6dcbd2b9 100644 --- a/packages/string-templates/src/helpers/index.js +++ b/packages/string-templates/src/helpers/index.js @@ -7,7 +7,7 @@ const { HelperFunctionBuiltin, LITERAL_MARKER, } = require("./constants") -const { getHelperList } = require("./list") +const { getJsHelperList } = require("./list") const HTML_SWAPS = { "<": "<", @@ -97,4 +97,4 @@ module.exports.unregisterAll = handlebars => { externalHandlebars.unregisterAll(handlebars) } -module.exports.getHelperList = getHelperList +module.exports.getJsHelperList = getJsHelperList diff --git a/packages/string-templates/src/helpers/javascript.js b/packages/string-templates/src/helpers/javascript.js index 0b63400deb..eff125dd72 100644 --- a/packages/string-templates/src/helpers/javascript.js +++ b/packages/string-templates/src/helpers/javascript.js @@ -1,7 +1,7 @@ const { atob } = require("../utilities") const cloneDeep = require("lodash.clonedeep") const { LITERAL_MARKER } = require("../helpers/constants") -const { getHelperList } = require("./list") +const { getJsHelperList } = require("./list") // The method of executing JS scripts depends on the bundle being built. // This setter is used in the entrypoint (either index.cjs or index.mjs). @@ -49,7 +49,7 @@ module.exports.processJS = (handlebars, context) => { // app context. const sandboxContext = { $: path => getContextValue(path, cloneDeep(context)), - helpers: getHelperList(), + helpers: getJsHelperList(), } // Create a sandbox with our context and run the JS diff --git a/packages/string-templates/src/helpers/list.js b/packages/string-templates/src/helpers/list.js index c10a732371..edbdcbdf05 100644 --- a/packages/string-templates/src/helpers/list.js +++ b/packages/string-templates/src/helpers/list.js @@ -5,7 +5,7 @@ let helpers = undefined const helpersToRemove = ["sortBy"] -module.exports.getHelperList = () => { +module.exports.getJsHelperList = () => { if (helpers) { return helpers } diff --git a/packages/string-templates/test/manifest.spec.js b/packages/string-templates/test/manifest.spec.js index 7c794d8c23..3e39d775f5 100644 --- a/packages/string-templates/test/manifest.spec.js +++ b/packages/string-templates/test/manifest.spec.js @@ -24,7 +24,7 @@ const { } = require("../src/index.cjs") const tk = require("timekeeper") -const { getHelperList } = require("../src/helpers") +const { getJsHelperList } = require("../src/helpers") tk.freeze("2021-01-21T12:00:00") @@ -109,7 +109,7 @@ describe("manifest", () => { }) describe("can be parsed and run as js", () => { - const jsHelpers = getHelperList() + const jsHelpers = getJsHelperList() const jsExamples = Object.keys(examples).reduce((acc, v) => { acc[v] = examples[v].filter(([key]) => jsHelpers[key]) return acc From cd4fccbd6e06c012908537d0dacb19a5181bcfd2 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 31 Jan 2024 10:34:49 +0100 Subject: [PATCH 4/4] Allow excluding js helpers --- packages/builder/src/constants/completions.js | 6 ++++-- packages/string-templates/src/helpers/list.js | 5 +++-- packages/string-templates/src/index.cjs | 1 + packages/string-templates/src/index.js | 2 ++ packages/string-templates/src/index.mjs | 1 + 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/builder/src/constants/completions.js b/packages/builder/src/constants/completions.js index 4af0bad38d..e539a8084a 100644 --- a/packages/builder/src/constants/completions.js +++ b/packages/builder/src/constants/completions.js @@ -1,4 +1,4 @@ -import { getManifest } from "@budibase/string-templates" +import { getManifest, helpersToRemoveForJs } from "@budibase/string-templates" export function handlebarsCompletions() { const manifest = getManifest() @@ -11,7 +11,9 @@ export function handlebarsCompletions() { label: helperName, displayText: helperName, description: helperConfig.description, - allowsJs: !helperConfig.requiresBlock, + allowsJs: + !helperConfig.requiresBlock && + !helpersToRemoveForJs.includes(helperName), })) ) } diff --git a/packages/string-templates/src/helpers/list.js b/packages/string-templates/src/helpers/list.js index edbdcbdf05..e956958865 100644 --- a/packages/string-templates/src/helpers/list.js +++ b/packages/string-templates/src/helpers/list.js @@ -3,7 +3,8 @@ const helperList = require("@budibase/handlebars-helpers") let helpers = undefined -const helpersToRemove = ["sortBy"] +const helpersToRemoveForJs = ["sortBy"] +module.exports.helpersToRemoveForJs = helpersToRemoveForJs module.exports.getJsHelperList = () => { if (helpers) { @@ -25,7 +26,7 @@ module.exports.getJsHelperList = () => { helpers[key] = externalHandlebars.addedHelpers[key] } - for (const toRemove of helpersToRemove) { + for (const toRemove of helpersToRemoveForJs) { delete helpers[toRemove] } Object.freeze(helpers) diff --git a/packages/string-templates/src/index.cjs b/packages/string-templates/src/index.cjs index aedb7fc052..7d3aa02be8 100644 --- a/packages/string-templates/src/index.cjs +++ b/packages/string-templates/src/index.cjs @@ -20,6 +20,7 @@ module.exports.findHBSBlocks = templates.findHBSBlocks module.exports.convertToJS = templates.convertToJS module.exports.setJSRunner = templates.setJSRunner module.exports.FIND_ANY_HBS_REGEX = templates.FIND_ANY_HBS_REGEX +module.exports.helpersToRemoveForJs = templates.helpersToRemoveForJs if (!process.env.NO_JS) { const { VM } = require("vm2") diff --git a/packages/string-templates/src/index.js b/packages/string-templates/src/index.js index 63da7fde4d..eb9a8ac81d 100644 --- a/packages/string-templates/src/index.js +++ b/packages/string-templates/src/index.js @@ -10,6 +10,7 @@ const { } = require("./utilities") const { convertHBSBlock } = require("./conversion") const javascript = require("./helpers/javascript") +const { helpersToRemoveForJs } = require("./helpers/list") const hbsInstance = handlebars.create() registerAll(hbsInstance) @@ -394,3 +395,4 @@ module.exports.convertToJS = hbs => { } module.exports.FIND_ANY_HBS_REGEX = FIND_ANY_HBS_REGEX +module.exports.helpersToRemoveForJs = helpersToRemoveForJs diff --git a/packages/string-templates/src/index.mjs b/packages/string-templates/src/index.mjs index 43cda8183f..ad93a7bed4 100644 --- a/packages/string-templates/src/index.mjs +++ b/packages/string-templates/src/index.mjs @@ -21,6 +21,7 @@ export const findHBSBlocks = templates.findHBSBlocks export const convertToJS = templates.convertToJS export const setJSRunner = templates.setJSRunner export const FIND_ANY_HBS_REGEX = templates.FIND_ANY_HBS_REGEX +export const helpersToRemoveForJs = templates.helpersToRemoveForJs if (process && !process.env.NO_JS) { /**