make defaults work, like magic
This commit is contained in:
parent
1791e5a6c7
commit
8addb77e90
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user