Skip to content

Commit

Permalink
allow a dangling comma after last formal arg of an arrow function (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
imaman authored Nov 12, 2024
1 parent ebdb5ec commit b8ca41b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/septima-lang/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@
- supoort `JSON.parse(s)`
- supoort sorting an array: `[97, 100, 50].sort()`, `['the', 'quick', 'brown', 'fox'].sort((a, b) => a.length - b.length)`
- fix object comparsion

### PR/173

- support block comments `4 + /* now five */ 5`
- support default values for function args: `(a, b = 1000) => a + b`
- inspect the runtime type of a value via `constructor.name`

### PR/174

- allow a dangling comma after last formal arg of an arrow function `(a,b,) => a + b`
3 changes: 3 additions & 0 deletions modules/septima-lang/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ export class Parser {
}

this.scanner.consume(',')
if (this.scanner.consumeIf(')')) {
break
}
}

this.scanner.consume('=>')
Expand Down
8 changes: 8 additions & 0 deletions modules/septima-lang/tests/septima.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ describe('septima', () => {
expect(run(`let five = () => { let two = 2; let three = 3; return three+two }; five()`)).toEqual(5)
expect(run(`let concat = (a,b) => { let u = '_'; return u+a+b+u }; concat('a', 'b')`)).toEqual('_ab_')
})
test('allows a dangling comma after last formal arg', () => {
expect(run(`let f = (a,) => a+1000; f(3)`)).toEqual(1003)
expect(run(`let f = (a,b,) => a+b+1000; f(5,900)`)).toEqual(1905)
})
})
test('can have no args', () => {
expect(run(`let pi = fun() 3.14; 2*pi()`)).toEqual(6.28)
Expand Down Expand Up @@ -613,6 +617,10 @@ describe('septima', () => {
test('when undefined is passed to an arg with a default value, the default value is used', () => {
expect(run(`let f = (a, b = 2000, c = 3) => a+b+c; f(1, undefined, 5)`)).toEqual(2006)
})
test('a dangling comma is allowed after last default value', () => {
expect(run(`let f = (a, b = 2000,) => a+b; f(5)`)).toEqual(2005)
expect(run(`let f = (a, b = 2000,) => a+b; f(5,20)`)).toEqual(25)
})
})
})
describe('array methods', () => {
Expand Down

0 comments on commit b8ca41b

Please sign in to comment.