Skip to content

refactor(interpreter): single scope-carrying evaluator for template generatio - #222

Open
Anbarasu2410 wants to merge 1 commit into
accordproject:mainfrom
Anbarasu2410:refactor/single-scope-evaluator
Open

Anbarasu2410 wants to merge 1 commit into
accordproject:mainfrom
Anbarasu2410:refactor/single-scope-evaluator

Conversation

@Anbarasu2410

Copy link
Copy Markdown

Description

Closes #216.

This PR refactors TemplateMarkInterpreter.generateAgreement from multiple independent whole-tree pre-passes (evaluateUserCode, generateRecursiveBlocks, generateOptionalBlocks, and traverse.map) to a single recursive evaluator evaluate(node, scope, ctx) that carries the data context scope down the AST.

Key Changes

  • Single Scope-Carrying Evaluator: Implemented recursive evaluate(node, scope, ctx) in src/TemplateMarkInterpreter.ts. Each scope-changing node (ListBlockDefinition, ForeachDefinition, JoinDefinition, OptionalDefinition, WithDefinition, ClauseDefinition) creates or updates a Scope instance and evaluates its children within that scope.
  • Removed Pre-passes & Path Hacks: Removed getJsonPath, path-keyed dictionary lookups (Record<string, CiceroMarkNode>), and ancestor-walking guards.
  • Worker Process Compatibility: Implemented getEvaluationData to flatten ancestor scope bindings so compiled TypeScript formula preludes access outer variables across childProcessJavaScriptEvaluation serialization.
  • Nested Lists & Absent Optionals:
    • Nested lists evaluate array items against their local data context rather than throwing on outer data lookups.
    • Lists inside absent optional blocks cleanly evaluate whenNone without error (at root and inside list items).
  • Exact Evaluation Count: Formulas and conditions are evaluated exactly once per scope instance when the evaluator encounters them.
  • Fixtures & Tests:
    • Added test/templates/good/nested-list and test/templates/good/optional-list-absent fixtures and snapshots.
    • Added unit test suite nested list data contexts.
    • Added unit test suite formula and condition evaluation counts.

Verification

Commands run and test results:

  • npm run pretest (licchk, eslint, build): passed with 0 errors.
  • npm test: 19 test suites passed, 200 tests passed, 48 snapshots passed.
  • npm run test:e2e: 6 Playwright E2E browser tests passed.

…eneration

Replaces the multi-pass AST tree generation (evaluateUserCode, generateRecursiveBlocks, generateOptionalBlocks, and traverse.map) in TemplateMarkInterpreter with a single recursive evaluator evaluate(node, scope, ctx) that threads data context scopes down the AST.

- Eliminates getJsonPath, path-keyed result dictionaries, and ancestor-walking guards
- Binds correct item scope for nested list blocks
- Evaluates whenNone cleanly when a list is inside an absent optional block
- Evaluates formulas and conditions exactly once per scope instance
- Adds test fixtures and snapshots for nested lists and absent optional lists

Closes accordproject#216

Signed-off-by: Anbarasu2410 <anbuarasu2017@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor TemplateMarkInterpreter to a single scope-carrying evaluator

1 participant