Repository navigation
Description
Part of the fold-instead-of-run epic (link below). Cold-handoff issue — self-contained.
Context
chant build currently runs modules in Node to get resource objects. This issue builds the pure-static alternative for leaf resources: a fold(node, scope) that reduces a TypeScript expression AST to a value with no execution. Use the existing typescript compiler API (already a dep; EVL rules use it). Single-file only — cross-file is #2.
Scope
Implement fold(node: ts.Expression, scope): FoldedValue covering exactly the epic's supported subset, and foldResource(newExpr, scope) → { __resource: TypeName, props }. No CallExpression case — a call throws a located FoldError. let/var, computed keys, spread-from-call, etc. → FoldError.
Reference implementation (verified against typescript@5.9.3)
This runs today and produces correct output with no module execution. Port it into packages/core/src/fold/ with types + tests:
function fold(node, consts) {
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) return fold(node.expression, consts);
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
if (ts.isNumericLiteral(node)) return Number(node.text);
if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
if (node.kind === ts.SyntaxKind.NullKeyword) return null;
if (ts.isIdentifier(node) && node.text === "undefined") return undefined;
if (ts.isTemplateExpression(node)) { let o = node.head.text; for (const s of node.templateSpans) o += String(fold(s.expression, consts)) + s.literal.text; return o; }
if (ts.isObjectLiteralExpression(node)) { const obj = {}; for (const p of node.properties) {
if (ts.isPropertyAssignment(p)) obj[propName(p.name)] = fold(p.initializer, consts);
else if (ts.isShorthandPropertyAssignment(p)) obj[p.name.text] = fold(p.name, consts);
else if (ts.isSpreadAssignment(p)) { const src = fold(p.expression, consts); if (src == null || typeof src !== "object") throw new FoldError("spread source not an object"); Object.assign(obj, src); }
else throw new FoldError("unsupported object member"); } return obj; }
if (ts.isArrayLiteralExpression(node)) { const a = []; for (const el of node.elements) { if (ts.isSpreadElement(el)) a.push(...fold(el.expression, consts)); else a.push(fold(el, consts)); } return a; }
if (ts.isIdentifier(node)) { if (!consts.has(node.text)) throw new FoldError(`unresolved identifier: ${node.text}`); return fold(consts.get(node.text), consts); }
if (ts.isPropertyAccessExpression(node)) { if (ts.isIdentifier(node.expression) && resolvesToResource(consts, node.expression)) return { __ref: node.expression.text, attr: node.name.text }; const o = fold(node.expression, consts); return o == null ? undefined : o[node.name.text]; }
if (ts.isPrefixUnaryExpression(node)) { const v = fold(node.operand, consts); if (node.operator === ts.SyntaxKind.ExclamationToken) return !v; if (node.operator === ts.SyntaxKind.MinusToken) return -v; throw new FoldError("unsupported unary"); }
if (ts.isBinaryExpression(node)) { /* &&, ||, ?? short-circuit; + - * / === !== > < >= <= */ }
if (ts.isConditionalExpression(node)) return fold(node.condition, consts) ? fold(node.whenTrue, consts) : fold(node.whenFalse, consts);
if (ts.isCallExpression(node)) throw new FoldError(`function call as a value is not foldable: ${node.expression.getText()}(...)`);
throw new FoldError(`unsupported expression: ${ts.SyntaxKind[node.kind]}`);
}(Full binary-operator block + collectConsts/propName/resolvesToResource helpers are in the prototype; reproduce them.)
Acceptance criteria
-
foldlives inpackages/core/src/fold/, typed, noanyleaks in the public API. - A test corpus folds each supported construct to the expected value (templates/interpolation, spread,
??, ternary, arithmetic, unary,as/satisfies, nested objects/arrays). - Every unsupported construct throws a
FoldErrorcarrying the node position (line/col fromsourceFile), and a call's message names the callee. -
FoldErrormessages align 1:1 with the corresponding EVL rule id (coordinate with Temporal lexicon #6). - Zero module execution — assert by folding a source whose top level would throw if run.
Out of scope
Cross-file imports (#2), symbolic refs beyond the single-file case + intrinsics (#3), composites (#5).
Epic: #1019
Activity
epic: fold TypeScript-as-Data instead of running it — #1026,#1021,#1022,
lex00 commentedon Jul 26, 2026
Shipped in
a6c2cffb("epic: fold TypeScript-as-Data instead of running it — #1026,#1021,#1022,#1024,#1025 + #1032"). The squash message listed the children without closing keywords, so none of them auto-closed and they stayed open after the epic #1019 was closed. Closing as bookkeeping — the work is onmainand verified: fold coverage 15/98 at that commit, drift 0 across the 98-entry differential.