Skip to content

Add expression level resolver callback#42

Merged
Sander-Toonen merged 1 commit intomasterfrom
feature/evaluator-callback
Apr 11, 2026
Merged

Add expression level resolver callback#42
Sander-Toonen merged 1 commit intomasterfrom
feature/evaluator-callback

Conversation

@Sander-Toonen
Copy link
Copy Markdown

No description provided.

Copilot AI review requested due to automatic review settings April 11, 2026 19:59
Copy link
Copy Markdown

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 support for passing a per-evaluation variable resolver callback into Expression.evaluate() and the Parser.evaluate() convenience method, enabling different variable-resolution behavior per call without mutating parser.resolve.

Changes:

  • Introduces and exports a VariableResolver type and threads an optional resolver through evaluation.
  • Updates evaluation logic to prefer per-call resolver, then fall back to parser.resolve.
  • Adds tests and documentation describing the new per-call resolver behavior and precedence.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/expression/expression-advanced.ts Adds Vitest coverage for per-call variable resolver precedence and propagation.
src/types/values.ts Adds exported VariableResolver type.
src/parsing/parser.ts Updates instance Parser.evaluate() to accept and forward a per-call resolver.
src/core/expression.ts Updates Expression.evaluate() signature/docs and forwards resolver into evaluator.
src/core/evaluate.ts Threads resolver through evaluation loop and variable resolution path.
index.ts Re-exports VariableResolver from public API.
docs/parser.md Documents resolver arg on evaluate() and suggests when to use it.
docs/expression.md Documents Expression.evaluate(values, resolver) and resolver precedence.
docs/enhancements.md Notes per-call resolver as an enhancement.
docs/advanced-features.md Adds “Per-Expression Variable Resolver” section with examples and precedence rules.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/evaluate.ts
ExpressionValidator.validateAllowedFunction(resolvedValue, expr.functions, expr.toString());
nstack.push(resolvedValue);
return true;
}
Comment thread src/parsing/parser.ts
Comment on lines +315 to 317
evaluate(expr: string, variables?: Values, resolver?: VariableResolver): Value | Promise<Value> {
return this.parse(expr).evaluate(variables, resolver);
}
Comment on lines +251 to +257
it('should prefer the per-call resolver over the parser-level resolver', () => {
const parser = new Parser();
(parser as any).resolve = (token: string) =>
token === '$a' ? { value: 10 } : undefined;
const resolver: VariableResolver = (token) =>
token === '$a' ? { value: 1 } : undefined;
expect(parser.parse('$a').evaluate({}, resolver)).toBe(1);
Comment on lines +260 to +266
it('should fall back to the parser-level resolver when the per-call resolver returns undefined', () => {
const parser = new Parser();
(parser as any).resolve = (token: string) =>
token === '$b' ? { value: 4 } : undefined;
const resolver: VariableResolver = (token) =>
token === '$a' ? { value: 3 } : undefined;
expect(parser.parse('$a + $b').evaluate({}, resolver)).toBe(7);
@Sander-Toonen Sander-Toonen merged commit 32b5bbe into master Apr 11, 2026
12 checks passed
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