Skip to content

Add arrow function syntax for inline functions#27

Merged
Sander-Toonen merged 4 commits intomasterfrom
copilot/add-inline-function-support
Jan 23, 2026
Merged

Add arrow function syntax for inline functions#27
Sander-Toonen merged 4 commits intomasterfrom
copilot/add-inline-function-support

Conversation

Copy link

Copilot AI commented Jan 23, 2026

Arrow Function Support for Inline Functions

  • Understand the existing inline function syntax (f(x) = expr)
  • Add lexer support for arrow token (=>)
  • Modify parser to parse arrow function syntax (x => expr and (x, y) => expr)
  • Add IARROW instruction to instruction set
  • Add evaluation logic for arrow functions
  • Add expression-to-string support for arrow functions
  • Ensure arrow functions work as arguments to higher-order functions (map, filter, fold)
  • Update language service to handle arrow function syntax (highlighting works)
  • Add extensive unit tests for arrow functions (35 tests)
  • Run existing tests to ensure no regression (all 1166 tests pass)
  • Run code review and address feedback
  • Run security checks (CodeQL - no alerts)
  • Update documentation with arrow function syntax
Original prompt

Users of the library expact inline functions to work with arrow functions. Add support for this. This should work: "map(x => x.prop > 2, items)" when multiple arguments are needed parentheses are requires such as: "some sort((x, y) => x.age > y.age, users)" Ensure all existing functionality keeps working as is. Add extensive unit test coverage for the arrow functions. Also ensure that the language server can handle this.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 23, 2026 15:08
Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for inline functions with arrow functions Add arrow function syntax for inline functions Jan 23, 2026
Copilot AI requested a review from Sander-Toonen January 23, 2026 15:13
Copy link

@Sander-Toonen Sander-Toonen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the documentation regarding inline functions.

Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 23, 2026

Update the documentation regarding inline functions.

Added documentation for arrow functions in docs/syntax.md:

  • Added => operator to the operator precedence table
  • Added new "Arrow Functions" subsection under "Function Definitions" with examples for:
    • Single parameter syntax (x => expr)
    • Multiple parameters syntax ((x, y) => expr)
    • Zero parameters (() => expr)
    • Variable assignment
    • Nested arrow functions
    • Complex expressions with member access and operators

Commit: 1f9836c

Copilot AI requested a review from Sander-Toonen January 23, 2026 15:38
@Sander-Toonen Sander-Toonen marked this pull request as ready for review January 23, 2026 15:47
Copilot AI review requested due to automatic review settings January 23, 2026 15:47
@Sander-Toonen Sander-Toonen merged commit 30c411e into master Jan 23, 2026
8 checks passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 IARROW instruction 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.

Comment on lines +219 to +221
// Parse the function body
const bodyInstr: Instruction[] = [];
this.parseExpression(bodyInstr);
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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);

Copilot uses AI. Check for mistakes.
assert.deepStrictEqual(parser.evaluate('map(x=>x*2,[1,2,3])'), [2, 4, 6]);
});
});
});
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants