HS: evalStatically throws for non-literals (+3 tests)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-26 12:54:06 +00:00
parent 6a1cbdcbdb
commit bb18c05083
2 changed files with 44 additions and 9 deletions

View File

@@ -2474,6 +2474,20 @@ def generate_eval_only_test(test, idx):
expected_sx = js_val_to_sx(be_match.group(1))
assertions.append(f' (assert= (eval-hs "{hs_expr}") {expected_sx})')
# Pattern 2d: evalStatically() + toMatch(/cannot be evaluated statically/)
# Handles: try { _hyperscript.parse("expr").evalStatically(); } catch(e) { return e.message; }
# followed by: expect(msg).toMatch(/cannot be evaluated statically/)
# Uses guard directly because try-call in hs-run-filtered.js is a registration stub
# and assert-throws cannot catch exceptions during test execution.
if not assertions:
if 'evalStatically' in body and 'cannot be evaluated statically' in body:
for m in re.finditer(
r'_hyperscript\.parse\((["\x27])(.+?)\1\)\.evalStatically\(\)',
body
):
hs_expr = extract_hs_expr(m.group(2))
assertions.append(f' (guard (_e (true nil)) (hs-eval-statically "{hs_expr}") (error "hs-eval-statically did not throw for: {hs_expr}"))')
# Pattern 2e: run() with side-effects on window, checked via
# const X = await evaluate(() => <js-expr>); expect(X).toBe(val)
# The const holds the evaluated JS expr, not the run() return value,
@@ -2535,7 +2549,7 @@ def generate_eval_only_test(test, idx):
body, re.DOTALL
):
hs_expr = extract_hs_expr(m.group(2))
assertions.append(f' (assert-throws (eval-hs "{hs_expr}"))')
assertions.append(f' (assert-throws (fn () (eval-hs "{hs_expr}")))')
if not assertions:
return None # Can't convert this body pattern
@@ -2901,6 +2915,17 @@ output.append(' (nth _e 1)')
output.append(' (raise _e))))')
output.append(' (handler me-val))))))')
output.append('')
output.append(';; Evaluate a HS expression using evalStatically semantics:')
output.append(';; only literal values (numbers, strings, booleans, null, time units)')
output.append(';; succeed — any other expression raises "cannot be evaluated statically".')
output.append('(define hs-eval-statically')
output.append(' (fn (src)')
output.append(' (let ((ast (hs-compile src)))')
output.append(' (if (or (number? ast) (string? ast) (boolean? ast)')
output.append(' (and (list? ast) (= (first ast) (quote null-literal))))')
output.append(' (eval-hs src)')
output.append(' (raise "cannot be evaluated statically")))))')
output.append('')
# Group by category
categories = OrderedDict()