From 9fb21c7ba609dc6b2f8b7fa079372f420c7e290b Mon Sep 17 00:00:00 2001 From: DavidGracias Date: Fri, 19 Sep 2025 18:15:01 -0400 Subject: [PATCH] fix(core/formatters): correctly handle post-processing of strings (#389) Co-authored-by: David Garcia --- packages/core/src/formatters/base.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/core/src/formatters/base.ts b/packages/core/src/formatters/base.ts index 37edff35..6bffd66d 100644 --- a/packages/core/src/formatters/base.ts +++ b/packages/core/src/formatters/base.ts @@ -311,7 +311,22 @@ export abstract class BaseFormatter { } protected async compileTemplate(str: string): Promise { - if (!str) return () => ''; + const compiledHelper = await this.compileTemplateHelper(str); + return (parseValue: ParseValue) => { + const resultStr = compiledHelper(parseValue); + // final post-processing of the result string + return resultStr + .replace(/\\n/g, '\n') + .split('\n') + .filter( + (line) => line.trim() !== '' && !line.includes('{tools.removeLine}') + ) + .join('\n') + .replace(/\{tools.newLine\}/g, '\n'); + } + } + + protected async compileTemplateHelper(str: string): Promise { const re = this.regexBuilder.buildRegexExpression(); let matches: RegExpExecArray | null; @@ -395,10 +410,10 @@ export abstract class BaseFormatter { // CHECK TRUE/FALSE logic: compile the true/false templates and apply them to the resolved variable if (matches.groups.mod_check !== undefined) { - const check_trueFn = await this.compileTemplate( + const check_trueFn = await this.compileTemplateHelper( matches?.groups?.mod_check_true ?? '' ); - const check_falseFn = await this.compileTemplate( + const check_falseFn = await this.compileTemplateHelper( matches?.groups?.mod_check_false ?? '' ); @@ -444,13 +459,6 @@ export abstract class BaseFormatter { } return resultStr - .replace(/\\n/g, '\n') - .split('\n') - .filter( - (line) => line.trim() !== '' && !line.includes('{tools.removeLine}') - ) - .join('\n') - .replace(/\{tools.newLine\}/g, '\n'); }; }