diff --git a/packages/string-templates/src/index.ts b/packages/string-templates/src/index.ts index 4a24328265..8d5fe4c16d 100644 --- a/packages/string-templates/src/index.ts +++ b/packages/string-templates/src/index.ts @@ -65,6 +65,7 @@ function createTemplate( context?: object ) { opts = { ...defaultOpts, ...opts } + const helpersEnabled = !opts?.noHelpers // Finalising adds a helper, can't do this with no helpers const key = `${string}-${JSON.stringify(opts)}` @@ -74,7 +75,7 @@ function createTemplate( return templateCache[key] } - const overlappingHelpers = !opts?.noHelpers + const overlappingHelpers = helpersEnabled ? findOverlappingHelpers(context) : [] @@ -83,9 +84,9 @@ function createTemplate( disabledHelpers: overlappingHelpers, }) - if (context && !opts?.noHelpers) { + if (context && helpersEnabled) { if (overlappingHelpers.length > 0) { - for (let block of findHBSBlocks(string)) { + for (const block of findHBSBlocks(string)) { string = string.replace( block, prefixStrings(block, overlappingHelpers, "./") diff --git a/packages/string-templates/src/processors/postprocessor.ts b/packages/string-templates/src/processors/postprocessor.ts index b8d99682b1..6ddc0e67cd 100644 --- a/packages/string-templates/src/processors/postprocessor.ts +++ b/packages/string-templates/src/processors/postprocessor.ts @@ -1,19 +1,21 @@ import { LITERAL_MARKER } from "../helpers/constants" -export const PostProcessorNames = { - CONVERT_LITERALS: "convert-literals", +export enum PostProcessorNames { + CONVERT_LITERALS = "convert-literals", } -class Postprocessor { - name: string - private fn: any +type PostprocessorFn = (statement: string) => string - constructor(name: string, fn: any) { +class Postprocessor { + name: PostProcessorNames + private readonly fn: PostprocessorFn + + constructor(name: PostProcessorNames, fn: PostprocessorFn) { this.name = name this.fn = fn } - process(statement: any) { + process(statement: string) { return this.fn(statement) } } diff --git a/packages/string-templates/src/processors/preprocessor.ts b/packages/string-templates/src/processors/preprocessor.ts index 95a4d5500d..97e5c56fcc 100644 --- a/packages/string-templates/src/processors/preprocessor.ts +++ b/packages/string-templates/src/processors/preprocessor.ts @@ -1,25 +1,28 @@ import { HelperNames } from "../helpers" import { swapStrings, isAlphaNumeric } from "../utilities" +import { ProcessOptions } from "../types" const FUNCTION_CASES = ["#", "else", "/"] -export const PreprocessorNames = { - SWAP_TO_DOT: "swap-to-dot-notation", - FIX_FUNCTIONS: "fix-functions", - FINALISE: "finalise", - NORMALIZE_SPACES: "normalize-spaces", +export enum PreprocessorNames { + SWAP_TO_DOT = "swap-to-dot-notation", + FIX_FUNCTIONS = "fix-functions", + FINALISE = "finalise", + NORMALIZE_SPACES = "normalize-spaces", } +type PreprocessorFn = (statement: string, opts?: ProcessOptions) => string + class Preprocessor { name: string - private fn: any + private readonly fn: PreprocessorFn - constructor(name: string, fn: any) { + constructor(name: PreprocessorNames, fn: PreprocessorFn) { this.name = name this.fn = fn } - process(fullString: string, statement: string, opts: Object) { + process(fullString: string, statement: string, opts: ProcessOptions) { const output = this.fn(statement, opts) const idx = fullString.indexOf(statement) return swapStrings(fullString, idx, statement.length, output) @@ -56,11 +59,9 @@ export const processors = [ }), new Preprocessor( PreprocessorNames.FINALISE, - ( - statement: string, - opts: { noHelpers: any; disabledHelpers?: string[] } - ) => { - const noHelpers = opts && opts.noHelpers + (statement: string, opts?: ProcessOptions) => { + const noHelpers = opts?.noHelpers + const helpersEnabled = !noHelpers let insideStatement = statement.slice(2, statement.length - 2) if (insideStatement.charAt(0) === " ") { insideStatement = insideStatement.slice(1) @@ -77,8 +78,8 @@ export const processors = [ } const testHelper = possibleHelper.trim().toLowerCase() if ( - !noHelpers && - !opts.disabledHelpers?.includes(testHelper) && + helpersEnabled && + !opts?.disabledHelpers?.includes(testHelper) && 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 566be2b02b..12de4f1c29 100644 --- a/packages/string-templates/test/helpers.spec.ts +++ b/packages/string-templates/test/helpers.spec.ts @@ -505,4 +505,15 @@ describe("helper overlap", () => { }) expect(output).toEqual(["a", "b"]) }) + + it("should work as expected when no helpers are set", async () => { + const output = await processString( + "{{ sum }}", + { + sum: "a", + }, + { noHelpers: true } + ) + expect(output).toEqual("a") + }) })