From 8addb77e9008a4d5562f5a1725c8b1274fc6828e Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 2 Nov 2025 16:34:59 -0800 Subject: [PATCH] make defaults work, like magic --- src/compiler/tests/compiler.test.ts | 16 ++++++++++++++++ src/compiler/utils.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/compiler/tests/compiler.test.ts b/src/compiler/tests/compiler.test.ts index 2f5dcac..3ed7836 100644 --- a/src/compiler/tests/compiler.test.ts +++ b/src/compiler/tests/compiler.test.ts @@ -282,3 +282,19 @@ describe('dot get', () => { expect(`a = 1; arr = array 'a' 'b' 'c'; arr.(1 + a)`).toEvaluateTo('c', { array }) }) }) + +describe('default params', () => { + test('parses function with single default parameter', () => { + expect('add1 = do x=1: x + 1 end; add1').toEvaluateTo(2) + expect('add1 = do x=1: x + 1 end; add1 5').toEvaluateTo(6) + }) + + test('parses function with multiple default parameters', () => { + expect(`weird = do x='something' y=true: [x y] end; weird`).toEvaluateTo(['something', true]) + }) + + test('parses function with mixed parameters', () => { + expect('multiply = do x y=5: x * y end; multiply 5').toEvaluateTo(25) + expect('multiply = do x y=5: x * y end; multiply 5 2').toEvaluateTo(10) + }) +}) \ No newline at end of file diff --git a/src/compiler/utils.ts b/src/compiler/utils.ts index 1f8f0c1..6151563 100644 --- a/src/compiler/utils.ts +++ b/src/compiler/utils.ts @@ -99,9 +99,9 @@ export const getFunctionDefParts = (node: SyntaxNode, input: string) => { } const paramNames = getAllChildren(paramsNode).map((param) => { - if (param.type.id !== terms.Identifier) { + if (param.type.id !== terms.Identifier && param.type.id !== terms.NamedParam) { throw new CompilerError( - `FunctionDef params must be Identifier, got ${param.type.name}`, + `FunctionDef params must be Identifier or NamedParam, got ${param.type.name}`, param.from, param.to )