From c857bacafa6ba20f1a21df12726475f451b6d12a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 22 Jul 2024 16:26:59 +0100 Subject: [PATCH 1/3] Fix for #14201, adds the test helper into the manifest correctly. --- .../scripts/gen-collection-info.ts | 22 ++++------------- .../string-templates/src/helpers/constants.ts | 16 +++++++++++++ .../string-templates/src/helpers/external.ts | 24 ++++--------------- packages/string-templates/src/manifest.json | 20 ++++++++++++++++ 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/packages/string-templates/scripts/gen-collection-info.ts b/packages/string-templates/scripts/gen-collection-info.ts index d176665a5b..8b3825bf5c 100644 --- a/packages/string-templates/scripts/gen-collection-info.ts +++ b/packages/string-templates/scripts/gen-collection-info.ts @@ -1,4 +1,7 @@ -import { HelperFunctionBuiltin } from "../src/helpers/constants" +import { + HelperFunctionBuiltin, + EXTERNAL_FUNCTION_COLLECTIONS, +} from "../src/helpers/constants" import { readFileSync, writeFileSync } from "fs" import { marked } from "marked" import { join, dirname } from "path" @@ -14,21 +17,6 @@ type HelperInfo = { tags?: any[] } -/** - * full list of supported helpers can be found here: - * https://github.com/budibase/handlebars-helpers - */ - -const COLLECTIONS = [ - "math", - "array", - "number", - "url", - "string", - "comparison", - "object", - "uuid", -] const FILENAME = join(__dirname, "..", "src", "manifest.json") const outputJSON: any = {} const ADDED_HELPERS = { @@ -140,7 +128,7 @@ const excludeFunctions: Record = { string: ["raw"] } */ function run() { const foundNames: string[] = [] - for (let collection of COLLECTIONS) { + for (let collection of EXTERNAL_FUNCTION_COLLECTIONS) { const collectionFile = readFileSync( `${dirname( require.resolve("@budibase/handlebars-helpers") diff --git a/packages/string-templates/src/helpers/constants.ts b/packages/string-templates/src/helpers/constants.ts index ffccd36ab0..ee84a1dc47 100644 --- a/packages/string-templates/src/helpers/constants.ts +++ b/packages/string-templates/src/helpers/constants.ts @@ -15,6 +15,22 @@ export const HelperFunctionBuiltin = [ "with", ] +/** + * full list of supported helpers can be found here: + * https://github.com/Budibase/handlebars-helpers + */ +export const EXTERNAL_FUNCTION_COLLECTIONS = [ + "math", + "array", + "number", + "url", + "string", + "comparison", + "object", + "regex", + "uuid", +] + export const HelperFunctionNames = { OBJECT: "object", ALL: "all", diff --git a/packages/string-templates/src/helpers/external.ts b/packages/string-templates/src/helpers/external.ts index 36d5ac0f54..da4af779f3 100644 --- a/packages/string-templates/src/helpers/external.ts +++ b/packages/string-templates/src/helpers/external.ts @@ -2,26 +2,12 @@ import helpers from "@budibase/handlebars-helpers" import { date, duration } from "./date" -import { HelperFunctionBuiltin } from "./constants" +import { + HelperFunctionBuiltin, + EXTERNAL_FUNCTION_COLLECTIONS, +} from "./constants" import Handlebars from "handlebars" -/** - * full list of supported helpers can be found here: - * https://github.com/Budibase/handlebars-helpers - */ - -const EXTERNAL_FUNCTION_COLLECTIONS = [ - "math", - "array", - "number", - "url", - "string", - "comparison", - "object", - "regex", - "uuid", -] - const ADDED_HELPERS = { date: date, duration: duration, @@ -40,7 +26,7 @@ export function registerAll(handlebars: typeof Handlebars) { let hbsHelperInfo = helpers[collection]() for (let entry of Object.entries(hbsHelperInfo)) { const name = entry[0] - // skip built in functions and ones seen already + // skip built-in functions and ones seen already if ( HelperFunctionBuiltin.indexOf(name) !== -1 || externalNames.indexOf(name) !== -1 diff --git a/packages/string-templates/src/manifest.json b/packages/string-templates/src/manifest.json index 2af84caa08..7931c60641 100644 --- a/packages/string-templates/src/manifest.json +++ b/packages/string-templates/src/manifest.json @@ -1312,6 +1312,26 @@ "requiresBlock": false } }, + "regex": { + "toRegex": { + "args": [ + "str" + ], + "numArgs": 1, + "example": "{{toRegex 'foo'}} -> /foo/", + "description": "

Convert the given string to a regular expression.

\n", + "requiresBlock": false + }, + "test": { + "args": [ + "str" + ], + "numArgs": 1, + "example": "{{test 'foobar' (toRegex 'foo')}} -> true", + "description": "

Returns true if the given str matches the given regex. A regex can be passed on the context, or using the toRegex helper as a subexpression.

\n", + "requiresBlock": false + } + }, "uuid": { "uuid": { "args": [], From b611b0da3332ed7cdf0514edd99092da06abf9b2 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 22 Jul 2024 18:12:12 +0100 Subject: [PATCH 2/3] Fixing JS generation ordering. --- packages/string-templates/src/conversion/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/string-templates/src/conversion/index.ts b/packages/string-templates/src/conversion/index.ts index 7eb5ea71af..25700951c9 100644 --- a/packages/string-templates/src/conversion/index.ts +++ b/packages/string-templates/src/conversion/index.ts @@ -58,7 +58,7 @@ function buildList(parts: string[], value: any) { if (!value) { return parts.length > 1 ? `${build()}` : build() } else { - return parts.length === 0 ? value : `${value}, ${build()}` + return parts.length === 0 ? value : `${build()}, ${value}` } } From e34257a018f8fd47a3069c4d2186a739fd491220 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 22 Jul 2024 18:22:04 +0100 Subject: [PATCH 3/3] One more fix. --- packages/string-templates/test/hbsToJs.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/string-templates/test/hbsToJs.spec.ts b/packages/string-templates/test/hbsToJs.spec.ts index ea375064a1..ae071cfe37 100644 --- a/packages/string-templates/test/hbsToJs.spec.ts +++ b/packages/string-templates/test/hbsToJs.spec.ts @@ -93,10 +93,10 @@ describe("Test that the string processing works correctly", () => { it("should handle a complex statement", () => { const response = convertToJS( - "This is the average: {{ join ( avg val1 val2 val3 ) val4 }}" + "This is the average: {{ join val1 ( avg val2 val3 val4 ) }}" ) checkLines(response, [ - 'const var1 = helpers.join(helpers.avg($("val1"), $("val2"), $("val3")), $("val4"));', + 'const var1 = helpers.join($("val1"), helpers.avg($("val2"), $("val3"), $("val4")));', "return `This is the average: ${var1}`;", ]) }) @@ -119,10 +119,10 @@ describe("Test that the string processing works correctly", () => { it("should handle multiple complex statements", () => { const response = convertToJS( - "average: {{ avg ( abs val1 ) val2 }} add: {{ add 1 2 }}" + "average: {{ avg val1 ( abs val2 ) }} add: {{ add 1 2 }}" ) checkLines(response, [ - 'const var1 = helpers.avg(helpers.abs($("val1")), $("val2"));', + 'const var1 = helpers.avg($("val1"), helpers.abs($("val2")));', "const var2 = helpers.add(1, 2);", "return `average: ${var1} add: ${var2}`;", ])