added source maps

This commit is contained in:
2026-05-15 18:58:03 -05:00
parent ac745dd4a6
commit e11d9007ec
2 changed files with 30 additions and 19 deletions

View File

@@ -52,7 +52,7 @@ def transpile(source: str, lang_code: str) -> str:
def transpile_file(path: Path) -> str:
"""
Detect language from file extension (.es.py es),
Detect language from file extension (.es.py -> es),
read the file, and return transpiled Python source.
"""
lang_code = _detect_lang(path)
@@ -64,8 +64,33 @@ def transpile_file(path: Path) -> str:
return transpile(source, lang_code)
def run_transpiled(original_path: Path, transpiled: str) -> None:
"""
Execute transpiled source while making tracebacks point
to the original .es.py file, not a temp file.
"""
import linecache
filename = str(original_path.resolve())
# Register original source lines so traceback displays them correctly
original_lines = original_path.read_text(encoding="utf-8").splitlines(keepends=True)
linecache.cache[filename] = (
len(original_lines),
None,
original_lines,
filename,
)
# Compile with original filename — this is what sets it in the traceback
code = compile(transpiled, filename, "exec")
glob = {"__file__": filename, "__name__": "__main__"}
exec(code, glob)
def _detect_lang(path: Path) -> str:
"""Extract lang code from extension, e.g. script.es.py es."""
"""Extract lang code from extension, e.g. script.es.py -> es."""
suffixes = path.suffixes # e.g. ['.es', '.py']
if len(suffixes) >= 2 and suffixes[-1] == ".py":
return suffixes[-2].lstrip(".")