From 78e558c4aa4baa958062cff08d64038195457529 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Thu, 27 Feb 2025 14:16:52 +0100 Subject: [PATCH] Handle conditional arguments --- .../common/CodeEditor/validator/hbs.ts | 33 +++++++++- .../CodeEditor/validator/tests/hbs.spec.ts | 64 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/packages/builder/src/components/common/CodeEditor/validator/hbs.ts b/packages/builder/src/components/common/CodeEditor/validator/hbs.ts index b954bb1e8f..357cb04a86 100644 --- a/packages/builder/src/components/common/CodeEditor/validator/hbs.ts +++ b/packages/builder/src/components/common/CodeEditor/validator/hbs.ts @@ -94,7 +94,15 @@ export function validateHbsTemplate( providedParamsCount++ } - if (providedParamsCount !== expectedArguments.length) { + const optionalArgMatcher = new RegExp(/^\[(.+)\]$/) + const optionalArgs = expectedArguments.filter(a => + optionalArgMatcher.test(a) + ) + + if ( + !optionalArgs.length && + providedParamsCount !== expectedArguments.length + ) { diagnostics.push({ from, to, @@ -105,6 +113,29 @@ export function validateHbsTemplate( ", " )}), but got ${providedParamsCount}.`, }) + } else if (optionalArgs.length) { + const maxArgs = expectedArguments.length + const minArgs = maxArgs - optionalArgs.length + const parameters = expectedArguments + .map(a => { + const test = optionalArgMatcher.exec(a) + if (!test?.[1]) { + return a + } + return `${test[1]} (optional)` + }) + .join(", ") + if ( + minArgs > providedParamsCount || + maxArgs < providedParamsCount + ) { + diagnostics.push({ + from, + to, + severity: "error", + message: `Helper "${helperName}" expects between ${minArgs} to ${expectedArguments.length} parameters (${parameters}), but got ${providedParamsCount}.`, + }) + } } } diff --git a/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts b/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts index 9484c7a4a5..95ec2e540e 100644 --- a/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts +++ b/packages/builder/src/components/common/CodeEditor/validator/tests/hbs.spec.ts @@ -138,5 +138,69 @@ describe("hbs validator", () => { }, ]) }) + + describe("optional parameters", () => { + it("supports empty parameters", () => { + const validators: CodeValidator = { + helperFunction: { + arguments: ["a", "b", "[c]"], + }, + } + const text = "{{ helperFunction 1 99 }}" + + const result = validateHbsTemplate(text, validators) + expect(result).toHaveLength(0) + }) + + it("supports valid parameters", () => { + const validators: CodeValidator = { + helperFunction: { + arguments: ["a", "b", "[c]"], + }, + } + const text = "{{ helperFunction 1 99 'a' }}" + + const result = validateHbsTemplate(text, validators) + expect(result).toHaveLength(0) + }) + + it("returns a valid message on missing parameters", () => { + const validators: CodeValidator = { + helperFunction: { + arguments: ["a", "b", "[c]"], + }, + } + const text = "{{ helperFunction 1 }}" + + const result = validateHbsTemplate(text, validators) + expect(result).toEqual([ + { + from: 0, + message: `Helper "helperFunction" expects between 2 to 3 parameters (a, b, c (optional)), but got 1.`, + severity: "error", + to: 22, + }, + ]) + }) + + it("returns a valid message on too many parameters", () => { + const validators: CodeValidator = { + helperFunction: { + arguments: ["a", "b", "[c]"], + }, + } + const text = "{{ helperFunction 1 2 3 4 }}" + + const result = validateHbsTemplate(text, validators) + expect(result).toEqual([ + { + from: 0, + message: `Helper "helperFunction" expects between 2 to 3 parameters (a, b, c (optional)), but got 4.`, + severity: "error", + to: 28, + }, + ]) + }) + }) }) })