From 5cbcdacd3b9fac0aeb5fce571fc05ed64ccc692b Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Wed, 19 Aug 2026 20:15:09 +0100 Subject: [PATCH] Add function to tighten parentheses around EJS output and corresponding test This update introduces a new function, `tightenParenthesesAroundEjs`, which removes unnecessary spaces inside parentheses surrounding EJS output in formatted templates. Additionally, a test case is added to ensure that the formatting behaves as expected, enhancing the overall template content formatting functionality. --- src/templates/__tests__/templateformatter.test.js | 10 ++++++++++ src/templates/templateformatter.js | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/templates/__tests__/templateformatter.test.js b/src/templates/__tests__/templateformatter.test.js index 3ef4b27..5a8b655 100644 --- a/src/templates/__tests__/templateformatter.test.js +++ b/src/templates/__tests__/templateformatter.test.js @@ -49,4 +49,14 @@ describe('formatTemplateContent', () => { expect(result.content).toContain('<%# comment %>'); }); + + it('should not add spaces inside parentheses around EJS output', () => { + const input = + '<%= courierService.name %> ( <%= courier.name %> )'; + const result = formatTemplateContent(input); + + expect(result.content).toContain(' (<%= courier.name %>)'); + expect(result.content).not.toMatch(/\(\s+<%/); + expect(result.content).not.toMatch(/%>\s+\)/); + }); }); diff --git a/src/templates/templateformatter.js b/src/templates/templateformatter.js index 9dd84aa..6182ff9 100644 --- a/src/templates/templateformatter.js +++ b/src/templates/templateformatter.js @@ -65,6 +65,10 @@ function restoreEjsBlocks(content, ejsBlocks) { ); } +function tightenParenthesesAroundEjs(content) { + return content.replace(/\(\s+(<%)/g, '($1').replace(/(%>)\s+\)/g, '$1)'); +} + export function formatTemplateContent(content) { if (content == null || typeof content !== 'string') { return { error: 'Content is required and must be a string.', code: 400 }; @@ -78,6 +82,7 @@ export function formatTemplateContent(content) { const { masked, ejsBlocks } = maskEjsBlocks(content); let formatted = beautifyHtml(masked, HTML_BEAUTIFY_OPTIONS); formatted = restoreEjsBlocks(formatted, ejsBlocks); + formatted = tightenParenthesesAroundEjs(formatted); return { content: formatted.trimEnd() }; } catch (error) { return { error: error.message || 'Failed to format template content.', code: 400 };