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 context?: object
) { ) {
opts = { ...defaultOpts, ...opts } opts = { ...defaultOpts, ...opts }
const helpersEnabled = !opts?.noHelpers
// Finalising adds a helper, can't do this with no helpers // Finalising adds a helper, can't do this with no helpers
const key = `${string}-${JSON.stringify(opts)}` const key = `${string}-${JSON.stringify(opts)}`
@ -74,7 +75,7 @@ function createTemplate(
return templateCache[key] return templateCache[key]
} }
const overlappingHelpers = !opts?.noHelpers const overlappingHelpers = helpersEnabled
? findOverlappingHelpers(context) ? findOverlappingHelpers(context)
: [] : []
@ -83,9 +84,9 @@ function createTemplate(
disabledHelpers: overlappingHelpers, disabledHelpers: overlappingHelpers,
}) })
if (context && !opts?.noHelpers) { if (context && helpersEnabled) {
if (overlappingHelpers.length > 0) { if (overlappingHelpers.length > 0) {
for (let block of findHBSBlocks(string)) { for (const block of findHBSBlocks(string)) {
string = string.replace( string = string.replace(
block, block,
prefixStrings(block, overlappingHelpers, "./") prefixStrings(block, overlappingHelpers, "./")

View File

@ -1,19 +1,21 @@
import { LITERAL_MARKER } from "../helpers/constants" import { LITERAL_MARKER } from "../helpers/constants"
export const PostProcessorNames = { export enum PostProcessorNames {
CONVERT_LITERALS: "convert-literals", CONVERT_LITERALS = "convert-literals",
} }
class Postprocessor { type PostprocessorFn = (statement: string) => string
name: string
private fn: any
constructor(name: string, fn: any) { class Postprocessor {
name: PostProcessorNames
private readonly fn: PostprocessorFn
constructor(name: PostProcessorNames, fn: PostprocessorFn) {
this.name = name this.name = name
this.fn = fn this.fn = fn
} }
process(statement: any) { process(statement: string) {
return this.fn(statement) return this.fn(statement)
} }
} }

View File

@ -1,25 +1,28 @@
import { HelperNames } from "../helpers" import { HelperNames } from "../helpers"
import { swapStrings, isAlphaNumeric } from "../utilities" import { swapStrings, isAlphaNumeric } from "../utilities"
import { ProcessOptions } from "../types"
const FUNCTION_CASES = ["#", "else", "/"] const FUNCTION_CASES = ["#", "else", "/"]
export const PreprocessorNames = { export enum PreprocessorNames {
SWAP_TO_DOT: "swap-to-dot-notation", SWAP_TO_DOT = "swap-to-dot-notation",
FIX_FUNCTIONS: "fix-functions", FIX_FUNCTIONS = "fix-functions",
FINALISE: "finalise", FINALISE = "finalise",
NORMALIZE_SPACES: "normalize-spaces", NORMALIZE_SPACES = "normalize-spaces",
} }
type PreprocessorFn = (statement: string, opts?: ProcessOptions) => string
class Preprocessor { class Preprocessor {
name: string name: string
private fn: any private readonly fn: PreprocessorFn
constructor(name: string, fn: any) { constructor(name: PreprocessorNames, fn: PreprocessorFn) {
this.name = name this.name = name
this.fn = fn this.fn = fn
} }
process(fullString: string, statement: string, opts: Object) { process(fullString: string, statement: string, opts: ProcessOptions) {
const output = this.fn(statement, opts) const output = this.fn(statement, opts)
const idx = fullString.indexOf(statement) const idx = fullString.indexOf(statement)
return swapStrings(fullString, idx, statement.length, output) return swapStrings(fullString, idx, statement.length, output)
@ -56,11 +59,9 @@ export const processors = [
}), }),
new Preprocessor( new Preprocessor(
PreprocessorNames.FINALISE, PreprocessorNames.FINALISE,
( (statement: string, opts?: ProcessOptions) => {
statement: string, const noHelpers = opts?.noHelpers
opts: { noHelpers: any; disabledHelpers?: string[] } const helpersEnabled = !noHelpers
) => {
const noHelpers = opts && opts.noHelpers
let insideStatement = statement.slice(2, statement.length - 2) let insideStatement = statement.slice(2, statement.length - 2)
if (insideStatement.charAt(0) === " ") { if (insideStatement.charAt(0) === " ") {
insideStatement = insideStatement.slice(1) insideStatement = insideStatement.slice(1)
@ -77,8 +78,8 @@ export const processors = [
} }
const testHelper = possibleHelper.trim().toLowerCase() const testHelper = possibleHelper.trim().toLowerCase()
if ( if (
!noHelpers && helpersEnabled &&
!opts.disabledHelpers?.includes(testHelper) && !opts?.disabledHelpers?.includes(testHelper) &&
HelperNames().some(option => testHelper === option.toLowerCase()) HelperNames().some(option => testHelper === option.toLowerCase())
) { ) {
insideStatement = `(${insideStatement})` insideStatement = `(${insideStatement})`

View File

@ -505,4 +505,15 @@ describe("helper overlap", () => {
}) })
expect(output).toEqual(["a", "b"]) 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")
})
}) })