Add arrow function syntax for inline functions#27
Conversation
Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Sander-Toonen
left a comment
There was a problem hiding this comment.
Update the documentation regarding inline functions.
Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Added documentation for arrow functions in
Commit: 1f9836c |
There was a problem hiding this comment.
Pull request overview
This PR adds arrow function syntax support to the expression parser, enabling concise inline function definitions similar to JavaScript. The implementation is comprehensive with extensive test coverage (35 tests) and proper documentation.
Changes:
- Added lexer support for the
=>arrow operator token - Implemented parser logic to handle single-parameter (
x => expr), multi-parameter ((x, y) => expr), and zero-parameter (() => expr) arrow functions - Added
IARROWinstruction type for arrow function evaluation with proper security handling - Updated expression serialization to correctly output arrow function syntax
- Enhanced language service to recognize and highlight arrow functions
- Documented arrow function syntax with examples in docs/syntax.md
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/parsing/token-stream.ts | Added tryMatchArrowOperator() method to recognize => token in lexer |
| src/parsing/parser.ts | Mapped => operator to fndef configuration option |
| src/parsing/parser-state.ts | Implemented parseArrowFunctionFromParameter() and tryParseArrowFunction() for parsing arrow function syntax with lookahead |
| src/parsing/instruction.ts | Added IARROW instruction type and ArrowFunctionInstruction interface |
| src/core/evaluate.ts | Implemented IARROW evaluation logic with secure function registration using unique counter-based keys |
| src/core/expression-to-string.ts | Added serialization support for arrow functions with proper parenthesization |
| docs/syntax.md | Documented arrow function syntax, examples, and behavior |
| test/functions/functions-arrow.ts | Comprehensive test suite with 35 tests covering various arrow function use cases |
| test/language-service/language-service.ts | Added tests for arrow function syntax highlighting |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Parse the function body | ||
| const bodyInstr: Instruction[] = []; | ||
| this.parseExpression(bodyInstr); |
There was a problem hiding this comment.
Inconsistent parsing method used for zero-parameter arrow functions. Line 221 uses parseExpression(bodyInstr) while lines 190 and 281 correctly use parseConditionalExpression(bodyInstr) for single and multi-parameter arrow functions. This inconsistency means zero-parameter arrow functions would incorrectly consume semicolons in their body, preventing patterns like fn = () => 42; otherCode. The body parsing should use parseConditionalExpression for consistency.
| // Parse the function body | |
| const bodyInstr: Instruction[] = []; | |
| this.parseExpression(bodyInstr); | |
| // Parse the function body. Use parseConditionalExpression so the body is a | |
| // single expression and does not consume semicolons, consistent with the | |
| // non-empty parameter case below. | |
| const bodyInstr: Instruction[] = []; | |
| this.parseConditionalExpression(bodyInstr); |
| assert.deepStrictEqual(parser.evaluate('map(x=>x*2,[1,2,3])'), [2, 4, 6]); | ||
| }); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Missing test coverage for arrow function syntax when fndef operator is disabled. The documentation states that "Arrow functions share the same fndef operator flag as traditional function definitions" (docs/syntax.md:400), but there are no tests verifying that arrow functions are properly rejected when fndef: false. Tests should be added to ensure expressions like x => x * 2 and (a, b) => a + b throw the "Arrow function syntax is not permitted" error when arrow functions are disabled.
Arrow Function Support for Inline Functions
f(x) = expr)=>)x => exprand(x, y) => expr)IARROWinstruction to instruction setOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.