diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index 291807d5f0..4a24328265 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -47,7 +47,10 @@ function testObject(object: any) { } } -function findOverlappingHelpers(context: object) { +function findOverlappingHelpers(context?: object) { + if (!context) { + return [] + } const contextKeys = Object.keys(context) return contextKeys.filter(key => helperNames.includes(key)) } diff --git a/packages/string-templates/src/processors/preprocessor.ts b/packages/string-templates/src/processors/preprocessor.ts index ee76b5dbcf..95a4d5500d 100644 --- a/packages/string-templates/src/processors/preprocessor.ts +++ b/packages/string-templates/src/processors/preprocessor.ts @@ -13,12 +13,10 @@ export const PreprocessorNames = { class Preprocessor { name: string private fn: any - private helperNames: string[] constructor(name: string, fn: any) { this.name = name this.fn = fn - this.helperNames = HelperNames() } process(fullString: string, statement: string, opts: Object) { @@ -81,7 +79,7 @@ export const processors = [ if ( !noHelpers && !opts.disabledHelpers?.includes(testHelper) && - this.helperNames.some(option => testHelper === option.toLowerCase()) + HelperNames().some(option => testHelper === option.toLowerCase()) ) { insideStatement = `(${insideStatement})` } diff --git a/packages/string-templates/test/helpers.spec.ts b/packages/string-templates/test/helpers.spec.ts index 5f1855535d..566be2b02b 100644 --- a/packages/string-templates/test/helpers.spec.ts +++ b/packages/string-templates/test/helpers.spec.ts @@ -483,3 +483,26 @@ describe("uuid", () => { expect(output).toMatch(UUID_REGEX) }) }) + +describe("helper overlap", () => { + it("should use context over helpers (regex test helper)", async () => { + const output = await processString("{{ test }}", { test: "a" }) + expect(output).toEqual("a") + }) + + it("should use helper if no sum in context, return the context value otherwise", async () => { + const hbs = "{{ sum 1 2 }}" + const output = await processString(hbs, {}) + expect(output).toEqual("3") + const secondaryOutput = await processString(hbs, { sum: "a" }) + expect(secondaryOutput).toEqual("a") + }) + + it("should handle multiple cases", async () => { + const output = await processString("{{ literal (split test sum) }}", { + test: "a-b", + sum: "-", + }) + expect(output).toEqual(["a", "b"]) + }) +})