Add expression level resolver callback#42
Merged
Sander-Toonen merged 1 commit intomasterfrom Apr 11, 2026
Merged
Conversation
There was a problem hiding this comment.
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
VariableResolvertype 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.
| ExpressionValidator.validateAllowedFunction(resolvedValue, expr.functions, expr.toString()); | ||
| nstack.push(resolvedValue); | ||
| return true; | ||
| } |
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.