PR comments.

This commit is contained in:
mike12345567 2024-08-15 11:04:57 +01:00
parent 3ed2fbbcb4
commit bbc33d37e6
4 changed files with 40 additions and 25 deletions

View File

@ -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, "./")

View File

@ -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)
}
}

View File

@ -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})`

View File

@ -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")
})
})