From 70ba459f7077774aef32d189c63809356fc9f63b Mon Sep 17 00:00:00 2001 From: brontolosone <177225737+brontolosone@users.noreply.github.com> Date: Fri, 25 Oct 2024 18:16:10 +0000 Subject: [PATCH] throw when calling distance() with 0 arguments --- .../src/evaluator/functions/FunctionImplementation.ts | 8 +++++++- packages/xpath/src/functions/xforms/geo.ts | 2 +- packages/xpath/test/xforms/{area.test.ts => geo.test.ts} | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) rename packages/xpath/test/xforms/{area.test.ts => geo.test.ts} (99%) diff --git a/packages/xpath/src/evaluator/functions/FunctionImplementation.ts b/packages/xpath/src/evaluator/functions/FunctionImplementation.ts index 8b973d665..6d164f7cd 100644 --- a/packages/xpath/src/evaluator/functions/FunctionImplementation.ts +++ b/packages/xpath/src/evaluator/functions/FunctionImplementation.ts @@ -39,7 +39,7 @@ export class InvalidArgumentError extends Error { } } -export type ParameterArityType = 'optional' | 'required' | 'variadic'; +export type ParameterArityType = 'optional' | 'required' | 'variadic' | 'variadic+' ; export type ParameterTypeHint = // | 'lazy' // TODO: it might be good to *explicitly* mark certain parameters @@ -127,6 +127,12 @@ export class FunctionImplementation { max: Infinity, }; + case 'variadic+': + return { + min: acc.min + 1, + max: Infinity, + }; + default: throw new UnreachableError(arityType); } diff --git a/packages/xpath/src/functions/xforms/geo.ts b/packages/xpath/src/functions/xforms/geo.ts index 1414847cd..48146ea4c 100644 --- a/packages/xpath/src/functions/xforms/geo.ts +++ b/packages/xpath/src/functions/xforms/geo.ts @@ -202,7 +202,7 @@ const sum = (values: readonly number[]) => { export const distance = new NumberFunction( 'distance', - [{ arityType: 'variadic' }], + [{ arityType: 'variadic+' }], (context, args) => { const lines = evaluateLines(context, args); diff --git a/packages/xpath/test/xforms/area.test.ts b/packages/xpath/test/xforms/geo.test.ts similarity index 99% rename from packages/xpath/test/xforms/area.test.ts rename to packages/xpath/test/xforms/geo.test.ts index 1adcf1da7..8d9f5eae8 100644 --- a/packages/xpath/test/xforms/area.test.ts +++ b/packages/xpath/test/xforms/geo.test.ts @@ -43,7 +43,7 @@ describe('distance() and area() functions', () => { }); }); - describe('area with nodes', () => { + describe('area and distance with nodes', () => { beforeEach(() => { testContext = createXFormsTestContext(`