From cc2a2963060ab3a739d1ef718d123e8d1bd62a97 Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 25 Apr 2026 19:10:57 +0000 Subject: [PATCH] HS: sourceInfo API (sourceFor / lineFor / node-get) --- lib/hyperscript/runtime.sx | 54 +++++++++++++++++++++++++++++ shared/static/wasm/sx/hs-runtime.sx | 54 +++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/lib/hyperscript/runtime.sx b/lib/hyperscript/runtime.sx index 4daa71d9..460f52cd 100644 --- a/lib/hyperscript/runtime.sx +++ b/lib/hyperscript/runtime.sx @@ -2525,3 +2525,57 @@ (fn (fn-name args) (let ((fn (host-global fn-name))) (if fn (host-call-fn fn args) nil)))) + +;; ── SourceInfo API ──────────────────────────────────────────────── + +(define + hs-source-for + (fn + (node) + (substring (get node :src) (get node :start) (get node :end)))) + +(define + hs-line-for + (fn + (node) + (let + ((lines (split (get node :src) "\n")) + (line-idx (- (get node :line) 1))) + (if (< line-idx (len lines)) (nth lines line-idx) "")))) + +(define + hs-node-get + (fn + (node key) + (get (get node :fields) key))) + +(define + hs-src + (fn (src-str) + (hs-source-for (hs-parse-ast src-str)))) + +(define + hs-src-at + (fn + (src-str path) + (define + walk + (fn + (node keys) + (if (or (nil? keys) (= (len keys) 0)) + node + (walk (hs-node-get node (first keys)) (rest keys))))) + (hs-source-for (walk (hs-parse-ast src-str) path)))) + +(define + hs-line-at + (fn + (src-str path) + (define + walk + (fn + (node keys) + (if (or (nil? keys) (= (len keys) 0)) + node + (walk (hs-node-get node (first keys)) (rest keys))))) + (hs-line-for (walk (hs-parse-ast src-str) path)))) diff --git a/shared/static/wasm/sx/hs-runtime.sx b/shared/static/wasm/sx/hs-runtime.sx index 4daa71d9..460f52cd 100644 --- a/shared/static/wasm/sx/hs-runtime.sx +++ b/shared/static/wasm/sx/hs-runtime.sx @@ -2525,3 +2525,57 @@ (fn (fn-name args) (let ((fn (host-global fn-name))) (if fn (host-call-fn fn args) nil)))) + +;; ── SourceInfo API ──────────────────────────────────────────────── + +(define + hs-source-for + (fn + (node) + (substring (get node :src) (get node :start) (get node :end)))) + +(define + hs-line-for + (fn + (node) + (let + ((lines (split (get node :src) "\n")) + (line-idx (- (get node :line) 1))) + (if (< line-idx (len lines)) (nth lines line-idx) "")))) + +(define + hs-node-get + (fn + (node key) + (get (get node :fields) key))) + +(define + hs-src + (fn (src-str) + (hs-source-for (hs-parse-ast src-str)))) + +(define + hs-src-at + (fn + (src-str path) + (define + walk + (fn + (node keys) + (if (or (nil? keys) (= (len keys) 0)) + node + (walk (hs-node-get node (first keys)) (rest keys))))) + (hs-source-for (walk (hs-parse-ast src-str) path)))) + +(define + hs-line-at + (fn + (src-str path) + (define + walk + (fn + (node keys) + (if (or (nil? keys) (= (len keys) 0)) + node + (walk (hs-node-get node (first keys)) (rest keys))))) + (hs-line-for (walk (hs-parse-ast src-str) path))))