From 2ec16a9dfe88cff0b2246e2ab458fd5665d0983b Mon Sep 17 00:00:00 2001
From: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
Date: Mon, 16 Nov 2026 16:12:33 -0800
Subject: [PATCH 1/3] Add AST panel to Playground v2
Match the v1 AST viewer to help users understand parser output
for the active file.
---
packages/playground-v2/src/ast-types.ts | 8 +
packages/playground-v2/src/index.html | 13 +
packages/playground-v2/src/main.ts | 241 ++++++++++++++++++
.../playground-v2/src/native-compiler.test.ts | 13 +
packages/playground-v2/src/native-compiler.ts | 148 ++++++++++-
packages/playground-v2/src/native.ts | 13 +
packages/playground-v2/src/native.worker.ts | 21 +-
packages/playground-v2/src/strada.ts | 16 ++
packages/playground-v2/src/strada.worker.ts | 91 +++++++
packages/playground-v2/src/styles.css | 126 +++++++++
10 files changed, 675 insertions(+), 15 deletions(-)
create mode 100644 packages/playground-v2/src/ast-types.ts
diff --git a/packages/playground-v2/src/ast-types.ts b/packages/playground-v2/src/ast-types.ts
new file mode 100644
index 000000000000..c48cea7b1434
--- /dev/null
+++ b/packages/playground-v2/src/ast-types.ts
@@ -0,0 +1,8 @@
+export type AstNode = {
+ detail: string
+ hasChildren: boolean
+ id: string
+ kind: string
+ start: number
+ end: number
+}
diff --git a/packages/playground-v2/src/index.html b/packages/playground-v2/src/index.html
index 3c93de346730..5d2b9db8e19c 100644
--- a/packages/playground-v2/src/index.html
+++ b/packages/playground-v2/src/index.html
@@ -124,6 +124,19 @@
Emit
+
+
+
+
Waiting for the compiler.
+
+
+
Problems
diff --git a/packages/playground-v2/src/main.ts b/packages/playground-v2/src/main.ts
index f7b31df12421..84ff69397083 100644
--- a/packages/playground-v2/src/main.ts
+++ b/packages/playground-v2/src/main.ts
@@ -35,6 +35,7 @@ import { configureExports, decodeCompressedHash } from "./exporter"
import { resolveSymlinks, validateProjectSymlinks, type Symlinks } from "./symlinks"
import { isSourceLanguage, languageForFile, sourceLanguages } from "./editor-languages"
import { StradaBackend } from "./strada"
+import type { AstNode } from "./ast-types"
import { NativeBackend } from "./native"
import { PackageTypeAcquirer, stradaDiscovery, type AcquisitionResult, type DiscoverDependencies } from "./ata"
import { acquisitionOptions } from "./ata/config"
@@ -94,6 +95,7 @@ type VersionedProjectState = ProjectState & {
}
type LayoutState = {
+ astVisible: boolean
emitVisible: boolean
filesVisible: boolean
filesWidth: number
@@ -192,6 +194,18 @@ const toggleOutputButton = getElement("toggle-output-button")
const showFilesButton = getElement("show-files-button")
const showOutputButton = getElement("show-output-button")
const toggleEmitButton = getElement("toggle-emit-button")
+const toggleAstButton = getElement("toggle-ast-button")
+const astPanel = getElement("ast-panel")
+const astMessage = getElement("ast-message")
+const astTree = getElement("ast-tree")
+let astRefreshTimer = 0
+let astCursorTimer = 0
+let astRenderVersion = 0
+let astCursorVersion = 0
+let astDecorations: string[] = []
+let astDecorationModel: monaco.editor.ITextModel | undefined
+let astRootNode: AstNode | undefined
+const astEntries = new Map()
const toggleRunOutputButton = getElement("toggle-run-output-button")
const navigateBackButton = getElement("navigate-back-button")
const currentFile = getElement("current-file")
@@ -441,10 +455,12 @@ inputEditor.onDidChangeModel(() => {
trackedEditorLocation = nextLocation
updateActiveFile()
updateNavigationButtons()
+ scheduleAstRefresh()
})
inputEditor.onDidChangeCursorSelection(() => {
trackedEditorLocation = getEditorLocation()
scheduleProjectPersistence()
+ scheduleAstCursorFollow()
})
window.addEventListener("pagehide", () => {
if (playgroundSettings.saveToUrl) persistProjectState()
@@ -478,6 +494,7 @@ refreshCompilerOverrides()
function loadLayoutState(): LayoutState {
const defaults: LayoutState = {
+ astVisible: true,
emitVisible: true,
filesVisible: true,
filesWidth: 208,
@@ -488,6 +505,7 @@ function loadLayoutState(): LayoutState {
try {
const stored = JSON.parse(localStorage.getItem(layoutStorageKey) ?? "{}")
return {
+ astVisible: typeof stored.astVisible === "boolean" ? stored.astVisible : defaults.astVisible,
emitVisible: typeof stored.emitVisible === "boolean" ? stored.emitVisible : defaults.emitVisible,
filesVisible: typeof stored.filesVisible === "boolean" ? stored.filesVisible : defaults.filesVisible,
filesWidth: typeof stored.filesWidth === "number" ? stored.filesWidth : defaults.filesWidth,
@@ -674,6 +692,7 @@ function applyLayoutState() {
workspace.dataset.filesCollapsed = String(!layoutState.filesVisible)
workspace.dataset.outputCollapsed = String(!layoutState.outputVisible)
emitOutput.hidden = !layoutState.emitVisible
+ astPanel.dataset.collapsed = String(!layoutState.astVisible)
runOutput.dataset.collapsed = String(!layoutState.runVisible)
showFilesButton.hidden = layoutState.filesVisible
@@ -686,6 +705,10 @@ function applyLayoutState() {
toggleEmitButton.title = layoutState.emitVisible ? "Collapse Emit" : "Expand Emit"
toggleEmitButton.setAttribute("aria-label", toggleEmitButton.title)
toggleEmitButton.setAttribute("aria-expanded", String(layoutState.emitVisible))
+ toggleAstButton.textContent = layoutState.astVisible ? "▾" : "▸"
+ toggleAstButton.title = layoutState.astVisible ? "Collapse AST" : "Expand AST"
+ toggleAstButton.setAttribute("aria-label", toggleAstButton.title)
+ toggleAstButton.setAttribute("aria-expanded", String(layoutState.astVisible))
toggleRunOutputButton.textContent = layoutState.runVisible ? "▾" : "▸"
toggleRunOutputButton.title = layoutState.runVisible ? "Collapse Run output" : "Expand Run output"
toggleRunOutputButton.setAttribute("aria-label", toggleRunOutputButton.title)
@@ -864,6 +887,11 @@ toggleFilesButton.addEventListener("click", () => toggleWorkspacePanel("files"))
showFilesButton.addEventListener("click", () => toggleWorkspacePanel("files"))
toggleOutputButton.addEventListener("click", () => toggleWorkspacePanel("output"))
showOutputButton.addEventListener("click", () => toggleWorkspacePanel("output"))
+toggleAstButton.addEventListener("click", () => {
+ layoutState.astVisible = !layoutState.astVisible
+ applyLayoutState()
+ persistLayoutState()
+})
toggleEmitButton.addEventListener("click", () => {
layoutState.emitVisible = !layoutState.emitVisible
applyLayoutState()
@@ -956,6 +984,7 @@ async function initializeNativeCompiler() {
discoverDependencies = file => backend.discover(file)
compileActiveProject = compileNativeProject
compilerReady = true
+ scheduleAstRefresh()
startLanguageServer(module, libFiles)
await compileNativeProject()
void refreshTypeAcquisition()
@@ -1035,6 +1064,7 @@ async function initializeStradaCompiler(requestedVersion: string) {
compileActiveProject = compileStradaProject
compilerReady = true
lspReady = true
+ scheduleAstRefresh()
await compileStradaProject()
void refreshTypeAcquisition()
renderStatus()
@@ -2300,6 +2330,215 @@ function diagnosticSeverity(category: number) {
}
}
+type AstTreeEntry = {
+ element: HTMLLIElement
+ node: AstNode
+ row: HTMLDivElement
+ toggle?: HTMLButtonElement
+}
+
+function scheduleAstRefresh() {
+ window.clearTimeout(astRefreshTimer)
+ astRefreshTimer = window.setTimeout(() => void refreshAstTree(), 180)
+}
+
+async function refreshAstTree() {
+ const version = ++astRenderVersion
+ astCursorVersion++
+ clearAstDecoration()
+ astEntries.clear()
+ astRootNode = undefined
+ astTree.replaceChildren()
+ astTree.hidden = true
+ astMessage.hidden = false
+ const model = inputEditor.getModel()
+ if (!model || !isSourceLanguage(model.getLanguageId())) {
+ astMessage.textContent = "AST view is available for TypeScript and JavaScript files."
+ return
+ }
+ if (!compilerReady) {
+ astMessage.textContent = "Waiting for the compiler."
+ return
+ }
+ astMessage.textContent = "Loading AST…"
+ try {
+ const root = await requestAstRoot(model.uri.path)
+ if (version !== astRenderVersion || inputEditor.getModel() !== model) return
+ if (!root) {
+ astMessage.textContent = "AST is unavailable for this file with the selected compiler."
+ return
+ }
+ astRootNode = root
+ astTree.hidden = false
+ astMessage.hidden = true
+ renderAstEntry(astTree, root)
+ await followAstCursor(version)
+ } catch (error) {
+ if (version !== astRenderVersion) return
+ const reason = error instanceof Error ? error.message.split("\n", 1)[0] : String(error)
+ astMessage.textContent = `AST view unavailable: ${reason}`
+ }
+}
+
+function nativeAstInput() {
+ return {
+ configFileName,
+ files: compilerFileContents(),
+ sourceFiles: [...projectModels]
+ .filter(([, model]) => isSourceLanguage(model.getLanguageId()))
+ .map(([fileName]) => fileName),
+ symlinks: projectSymlinks,
+ }
+}
+
+function requestAstRoot(fileName: string) {
+ if (useNativeCompiler) return nativeBackend?.astRoot(nativeAstInput(), fileName)
+ return stradaBackend?.astRoot(fileName)
+}
+
+function requestAstChildren(fileName: string, id: string) {
+ if (useNativeCompiler) return nativeBackend?.astChildren(fileName, id)
+ return stradaBackend?.astChildren(fileName, id)
+}
+
+function requestAstPath(fileName: string, position: number) {
+ if (useNativeCompiler) return nativeBackend?.astPath(fileName, position)
+ return stradaBackend?.astPath(fileName, position)
+}
+
+function renderAstEntry(parent: HTMLElement, node: AstNode) {
+ const element = document.createElement("li")
+ element.className = "ast-node"
+ element.setAttribute("role", "treeitem")
+ element.setAttribute("aria-level", String(node.id === "" ? 1 : node.id.split(".").length + 1))
+ if (node.hasChildren) element.setAttribute("aria-expanded", "false")
+ const row = document.createElement("div")
+ row.className = "ast-node-row"
+ const toggle = node.hasChildren ? document.createElement("button") : undefined
+ if (toggle) {
+ toggle.className = "ast-node-toggle"
+ toggle.type = "button"
+ toggle.setAttribute("aria-label", `Expand ${node.kind}`)
+ toggle.appendChild(createText("span", "▸", "ast-node-arrow"))
+ toggle.addEventListener("click", () => void toggleAstEntry(node, toggle, element))
+ toggle.addEventListener("pointerenter", () => highlightAstNode(node))
+ toggle.addEventListener("focus", () => highlightAstNode(node))
+ row.appendChild(toggle)
+ } else {
+ row.appendChild(createText("span", "", "ast-node-arrow"))
+ }
+ const select = document.createElement("button")
+ select.className = "ast-node-select"
+ select.type = "button"
+ select.title = `${node.kind}${node.detail ? ` · ${node.detail}` : ""}`
+ select.appendChild(createText("span", node.kind, "ast-node-kind"))
+ if (node.detail) select.appendChild(createText("span", node.detail, "ast-node-detail"))
+ select.addEventListener("click", () => selectAstNode(node))
+ select.addEventListener("pointerenter", () => highlightAstNode(node))
+ select.addEventListener("focus", () => highlightAstNode(node))
+ row.addEventListener("pointerleave", clearAstDecoration)
+ row.addEventListener("focusout", event => {
+ if (!row.contains(event.relatedTarget as Node | null)) clearAstDecoration()
+ })
+ row.appendChild(select)
+ element.appendChild(row)
+ parent.appendChild(element)
+ astEntries.set(node.id, { element, node, row, toggle })
+}
+
+async function toggleAstEntry(node: AstNode, toggle: HTMLButtonElement, element: HTMLLIElement) {
+ const expanded = element.getAttribute("aria-expanded") === "true"
+ const existingChildren = element.querySelector(".ast-node-children")
+ if (expanded) {
+ element.setAttribute("aria-expanded", "false")
+ toggle.setAttribute("aria-label", `Expand ${node.kind}`)
+ toggle.querySelector(".ast-node-arrow")!.textContent = "▸"
+ if (existingChildren) existingChildren.hidden = true
+ return
+ }
+ element.setAttribute("aria-expanded", "true")
+ toggle.setAttribute("aria-label", `Collapse ${node.kind}`)
+ toggle.querySelector(".ast-node-arrow")!.textContent = "▾"
+ if (existingChildren) {
+ existingChildren.hidden = false
+ return
+ }
+ const model = inputEditor.getModel()
+ if (!model) return
+ const version = astRenderVersion
+ const children = await requestAstChildren(model.uri.path, node.id)
+ if (
+ !children ||
+ version !== astRenderVersion ||
+ !element.isConnected ||
+ element.getAttribute("aria-expanded") !== "true"
+ )
+ return
+ const list = document.createElement("ul")
+ list.className = "ast-node-children"
+ list.setAttribute("role", "group")
+ for (const child of children) renderAstEntry(list, child)
+ element.appendChild(list)
+ element.dataset.loaded = "true"
+}
+
+function selectAstNode(node: AstNode) {
+ const model = inputEditor.getModel()
+ if (!model) return
+ const end = Math.min(model.getValueLength(), Math.max(node.start + 1, node.end))
+ const range = spanRange(model, node.start, end)
+ inputEditor.setSelection(range)
+ inputEditor.revealRangeInCenter(range, monaco.editor.ScrollType.Immediate)
+ inputEditor.focus()
+}
+
+function highlightAstNode(node: AstNode) {
+ const model = inputEditor.getModel()
+ if (!model || node.end <= node.start) return clearAstDecoration()
+ if (astDecorationModel && astDecorationModel !== model) clearAstDecoration()
+ const range = spanRange(model, node.start, node.end)
+ astDecorations = model.deltaDecorations(astDecorations, [
+ {
+ range,
+ options: { inlineClassName: "ast-source-highlight" },
+ },
+ ])
+ astDecorationModel = model
+}
+
+function clearAstDecoration() {
+ if (!astDecorationModel || astDecorations.length === 0) return
+ astDecorations = astDecorationModel.deltaDecorations(astDecorations, [])
+ astDecorationModel = undefined
+}
+
+function scheduleAstCursorFollow() {
+ window.clearTimeout(astCursorTimer)
+ astCursorTimer = window.setTimeout(() => void followAstCursor(astRenderVersion), 80)
+}
+
+async function followAstCursor(version: number) {
+ const cursorVersion = ++astCursorVersion
+ const model = inputEditor.getModel()
+ const position = inputEditor.getPosition()
+ if (!model || !position || !astRootNode) return
+ const path = await requestAstPath(model.uri.path, model.getOffsetAt(position))
+ if (!path || version !== astRenderVersion || cursorVersion !== astCursorVersion || inputEditor.getModel() !== model)
+ return
+ for (let index = 0; index < path.length - 1; index++) {
+ const current = astEntries.get(path[index].id)
+ if (!current) return
+ if (current.element.getAttribute("aria-expanded") !== "true" && current.toggle)
+ await toggleAstEntry(current.node, current.toggle, current.element)
+ }
+ for (const { row } of astEntries.values()) row.classList.remove("ast-node-active")
+ const active = path.length ? astEntries.get(path[path.length - 1].id) : undefined
+ if (active) {
+ active.row.classList.add("ast-node-active")
+ active.row.scrollIntoView({ block: "nearest" })
+ }
+}
+
async function renderEmittedFiles() {
const renderVersion = ++emitRenderVersion
emitOutput.replaceChildren()
@@ -2577,6 +2816,7 @@ function updateActiveFile() {
else button.removeAttribute("aria-current")
}
if (projectModel) persistProjectState()
+ scheduleAstRefresh()
}
function restoreProjectSelection(state: ProjectState) {
@@ -2628,6 +2868,7 @@ function registerProjectModel(model: monaco.editor.ITextModel) {
nativeCompileVersion++
stradaCompileVersion++
scheduleProjectPersistence()
+ scheduleAstRefresh()
refreshCompilerOverrides()
synchronizeLinkedProjectFiles()
scheduleTypeAcquisition()
diff --git a/packages/playground-v2/src/native-compiler.test.ts b/packages/playground-v2/src/native-compiler.test.ts
index ad6ba2999363..1ea2a3d3fc7a 100644
--- a/packages/playground-v2/src/native-compiler.test.ts
+++ b/packages/playground-v2/src/native-compiler.test.ts
@@ -782,6 +782,19 @@ test("native compiler reuses snapshots across edits, types, and configuration ch
assert.match(first.outputFiles["/workspace/src/index.js"], /42/)
assert(first.typeQueries[source].some(query => query.label === "42"))
+ const astRoot = compiler.astRoot(input, source)
+ assert.equal(astRoot?.kind, "SourceFile")
+ assert.equal(astRoot?.hasChildren, true)
+ const declarationPath = compiler.astPath(source, input.files[source].indexOf("answer"))
+ assert.equal(declarationPath.at(-1)?.kind, "Identifier")
+ assert.equal(declarationPath.at(-1)?.detail, '"answer"')
+ const rootChildren = compiler.astChildren(source, astRoot!.id)
+ assert(rootChildren.length > 0, JSON.stringify(rootChildren))
+ const outsideSource = "/workspace/outside.ts"
+ input.files[outsideSource] = "const outside = true;"
+ assert.equal(compiler.astRoot(input, outsideSource)?.kind, "SourceFile")
+ delete input.files[outsideSource]
+
input.files[source] = 'const answer: number = "wrong";\n// ^?\n'
const changed = compiler.compile(input)
assert(changed.diagnostics.some(diagnostic => diagnostic.code === 2322))
diff --git a/packages/playground-v2/src/native-compiler.ts b/packages/playground-v2/src/native-compiler.ts
index 45db1f90548b..31997a58e90c 100644
--- a/packages/playground-v2/src/native-compiler.ts
+++ b/packages/playground-v2/src/native-compiler.ts
@@ -1,9 +1,10 @@
import type { API, Diagnostic, FileNotifications, Snapshot } from "@typescript/typescript/sync"
import type * as Ast from "@typescript/typescript/ast"
-import type { Node } from "@typescript/typescript/ast"
+import type { Node, SourceFile as AstSourceFile } from "@typescript/typescript/ast"
+import type { AstNode } from "./ast-types"
import type { WasmTransport } from "@typescript/typescript/wasm"
import { TextDocument } from "vscode-languageserver-textdocument"
-import { corsaDiscovery, type Dependency, type SourceFile } from "./ata"
+import { corsaDiscovery, type Dependency, type SourceFile as DependencySourceFile } from "./ata"
import type { Symlinks } from "./symlinks"
export type NativeCompileInput = {
@@ -29,17 +30,19 @@ export class NativeCompiler {
private snapshot: Snapshot | undefined
private readonly files = new Map()
private symlinkKey = ""
+ private lastInput: NativeCompileInput | undefined
private readonly discoverDependencies: ReturnType
- constructor(private readonly api: API, private readonly transport: WasmTransport, ast: typeof Ast) {
+ constructor(private readonly api: API, private readonly transport: WasmTransport, private readonly ast: typeof Ast) {
this.discoverDependencies = corsaDiscovery(api, ast)
}
- discover(file: SourceFile): readonly Dependency[] {
+ discover(file: DependencySourceFile): readonly Dependency[] {
return this.discoverDependencies(file)
}
- compile(input: NativeCompileInput): NativeCompileResult {
+ private prepareProgram(input: NativeCompileInput) {
+ this.lastInput = input
const symlinkKey = JSON.stringify(input.symlinks ?? {})
if (this.snapshot && this.symlinkKey !== symlinkKey) {
this.snapshot.dispose()
@@ -64,16 +67,19 @@ export class NativeCompiler {
deleted.push(fileName)
}
const notifications: FileNotifications = { changed, created, deleted }
+ const hasChanges = changed.length > 0 || created.length > 0 || deleted.length > 0
const next = this.snapshot
- ? this.snapshot.update({
- ensurePrograms: true,
- fileNotifications: notifications,
- fileSystem: {
- kind: "layer",
- files: Object.fromEntries([...changed, ...created].map(fileName => [fileName, input.files[fileName]])),
- removedPaths: deleted,
- },
- })
+ ? hasChanges
+ ? this.snapshot.update({
+ ensurePrograms: true,
+ fileNotifications: notifications,
+ fileSystem: {
+ kind: "layer",
+ files: Object.fromEntries([...changed, ...created].map(fileName => [fileName, input.files[fileName]])),
+ removedPaths: deleted,
+ },
+ })
+ : this.snapshot
: this.api.createSnapshot({
openProjects: [input.configFileName],
fileSystem: { kind: "layer", files: input.files, symlinks },
@@ -96,6 +102,11 @@ export class NativeCompiler {
projectReferences: parsed.projectReferences,
configFileParsingDiagnostics: parsed.errors,
})
+ return { config, next, parsed, program, configuredProgram }
+ }
+
+ compile(input: NativeCompileInput): NativeCompileResult {
+ const { config, next, parsed, program, configuredProgram } = this.prepareProgram(input)
try {
const emit = program.emitToString()
const diagnostics = [
@@ -133,6 +144,68 @@ export class NativeCompiler {
}
}
+ astRoot(input: NativeCompileInput, fileName: string): AstNode | undefined {
+ return this.withAstSource(input, fileName, sourceFile =>
+ describeAstNode(sourceFile, sourceFile, [], this.ast.SyntaxKind)
+ )
+ }
+
+ astChildren(fileName: string, id: string): AstNode[] {
+ if (!this.lastInput) return []
+ const path = parseAstPath(id)
+ return (
+ this.withAstSource(this.lastInput, fileName, sourceFile => {
+ const node = nodeAtPath(sourceFile, path)
+ return node
+ ? astChildren(node).map(({ child, index }) =>
+ describeAstNode(child, sourceFile, [...path, index], this.ast.SyntaxKind)
+ )
+ : []
+ }) ?? []
+ )
+ }
+
+ astPath(fileName: string, position: number): AstNode[] {
+ if (!this.lastInput) return []
+ return (
+ this.withAstSource(this.lastInput, fileName, sourceFile => {
+ const path: Node[] = [sourceFile]
+ let node: Node = sourceFile
+ let indices: number[] = []
+ while (true) {
+ const child = astChildren(node).find(
+ ({ child }) => position >= child.getStart(sourceFile) && position <= child.getEnd()
+ )
+ if (!child) break
+ node = child.child
+ indices = [...indices, child.index]
+ path.push(node)
+ }
+ return path.map((entry, index) =>
+ describeAstNode(entry, sourceFile, indices.slice(0, index), this.ast.SyntaxKind)
+ )
+ }) ?? []
+ )
+ }
+
+ private withAstSource(input: NativeCompileInput, fileName: string, callback: (sourceFile: AstSourceFile) => T) {
+ const { next, parsed, program, configuredProgram } = this.prepareProgram(input)
+ let inferred: Snapshot | undefined
+ try {
+ let sourceFile = program.getSourceFile(fileName)
+ if (!sourceFile && input.files[fileName] !== undefined) {
+ inferred = next.update({
+ createPrograms: [{ rootFiles: [fileName], compilerOptions: { ...parsed.options, allowJs: true } }],
+ })
+ sourceFile = inferred.operation.createdPrograms?.[0]?.getSourceFile(fileName)
+ }
+ return sourceFile ? callback(sourceFile) : undefined
+ } finally {
+ inferred?.dispose()
+ if (!configuredProgram) program.dispose()
+ }
+ }
+
close() {
this.snapshot?.dispose()
this.snapshot = undefined
@@ -173,6 +246,53 @@ export class NativeCompiler {
}
}
+function parseAstPath(id: string) {
+ return id === "" ? [] : id.split(".").map(Number)
+}
+
+function nodeAtPath(sourceFile: Node, path: number[]) {
+ let node = sourceFile
+ for (const index of path) {
+ const child = astChildren(node).find(candidate => candidate.index === index)
+ if (!child) return undefined
+ node = child.child
+ }
+ return node
+}
+
+function astChildren(node: Node) {
+ const children: Array<{ child: Node; index: number }> = []
+ node.forEachChild(child => {
+ children.push({ child, index: children.length })
+ })
+ return children
+}
+
+function describeAstNode(
+ node: Node,
+ sourceFile: AstSourceFile,
+ path: number[],
+ syntaxKind: typeof Ast.SyntaxKind
+): AstNode {
+ const properties = node as Node & { text?: string; escapedText?: string }
+ const text =
+ typeof properties.text === "string"
+ ? properties.text
+ : typeof properties.escapedText === "string"
+ ? properties.escapedText
+ : ""
+ const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : ""
+ const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : ""
+ return {
+ detail: [preview, flags].filter(Boolean).join(" · "),
+ end: node.getEnd(),
+ hasChildren: node.forEachChild(() => true) === true,
+ id: path.join("."),
+ kind: syntaxKind[node.kind] ?? String(node.kind),
+ start: node.getStart(sourceFile),
+ }
+}
+
function hasTypeQuery(source: string | undefined): source is string {
return source !== undefined && /^\s*\/\/\s*\^\?\s*$/m.test(source)
}
diff --git a/packages/playground-v2/src/native.ts b/packages/playground-v2/src/native.ts
index 384b40ea3ab6..974587837d8c 100644
--- a/packages/playground-v2/src/native.ts
+++ b/packages/playground-v2/src/native.ts
@@ -1,5 +1,6 @@
import type { Dependency, SourceFile } from "./ata"
import type { NativeCompileInput, NativeCompileResult } from "./native-compiler"
+import type { AstNode } from "./ast-types"
import type { CompilerModules } from "./compiler-distribution"
type PendingRequest = {
@@ -62,6 +63,18 @@ export class NativeBackend {
return this.request("discover", file)
}
+ astRoot(input: NativeCompileInput, fileName: string) {
+ return this.request("astRoot", { input, fileName })
+ }
+
+ astChildren(fileName: string, id: string) {
+ return this.request("astChildren", { fileName, id })
+ }
+
+ astPath(fileName: string, position: number) {
+ return this.request("astPath", { fileName, position })
+ }
+
compile(input: NativeCompileInput): Promise {
return new Promise((resolve, reject) => {
if (this.queuedCompile) {
diff --git a/packages/playground-v2/src/native.worker.ts b/packages/playground-v2/src/native.worker.ts
index 38b8c0632ff5..a2665e6299ac 100644
--- a/packages/playground-v2/src/native.worker.ts
+++ b/packages/playground-v2/src/native.worker.ts
@@ -12,6 +12,9 @@ type Message =
args: { module: WebAssembly.Module; libraries: Record; modules: CompilerModules }
}
| { id: number; method: "compile"; args: NativeCompileInput }
+ | { id: number; method: "astRoot"; args: { input: NativeCompileInput; fileName: string } }
+ | { id: number; method: "astChildren"; args: { fileName: string; id: string } }
+ | { id: number; method: "astPath"; args: { fileName: string; position: number } }
| { id: number; method: "discover"; args: SourceFile }
let compiler: NativeCompiler | undefined
@@ -37,7 +40,23 @@ async function handle(message: Message) {
result = { diagnosticCategories: sync.DiagnosticCategory }
} else {
if (!compiler) throw new Error("Native compiler worker is not initialized")
- result = message.method === "compile" ? compiler.compile(message.args) : compiler.discover(message.args)
+ switch (message.method) {
+ case "compile":
+ result = compiler.compile(message.args)
+ break
+ case "astRoot":
+ result = compiler.astRoot(message.args.input, message.args.fileName)
+ break
+ case "astChildren":
+ result = compiler.astChildren(message.args.fileName, message.args.id)
+ break
+ case "astPath":
+ result = compiler.astPath(message.args.fileName, message.args.position)
+ break
+ case "discover":
+ result = compiler.discover(message.args)
+ break
+ }
}
self.postMessage({ id: message.id, result })
} catch (error) {
diff --git a/packages/playground-v2/src/strada.ts b/packages/playground-v2/src/strada.ts
index ee2fe0cd1649..9f26441bbc73 100644
--- a/packages/playground-v2/src/strada.ts
+++ b/packages/playground-v2/src/strada.ts
@@ -1,5 +1,6 @@
import { monaco } from "./tsgo-lsp"
import type { StradaLibraryProgress } from "./strada-libraries"
+import type { AstNode } from "./ast-types"
import { isSourceLanguage, languageForFile, sourceLanguages } from "./editor-languages"
import { resolveSymlinks, type Symlinks } from "./symlinks"
@@ -152,6 +153,21 @@ export class StradaBackend {
return this.#request("readFile", { fileName })
}
+ async astRoot(fileName: string) {
+ await this.updateFiles()
+ return this.#request("astRoot", { fileName })
+ }
+
+ async astChildren(fileName: string, id: string) {
+ await this.updateFiles()
+ return this.#request("astChildren", { fileName, id })
+ }
+
+ async astPath(fileName: string, position: number) {
+ await this.updateFiles()
+ return this.#request("astPath", { fileName, position })
+ }
+
async #formatOptions(model: monaco.editor.ITextModel) {
const options = model.getOptions()
return {
diff --git a/packages/playground-v2/src/strada.worker.ts b/packages/playground-v2/src/strada.worker.ts
index 4ca72ded9527..a84b8f06f3b1 100644
--- a/packages/playground-v2/src/strada.worker.ts
+++ b/packages/playground-v2/src/strada.worker.ts
@@ -1,4 +1,5 @@
import type { StradaLoadingProgress } from "./strada"
+import type { AstNode } from "./ast-types"
import { loadStradaLibraries } from "./strada-libraries"
import { createStradaFileSystem, normalizeStradaPath as normalizePath, parseStradaConfig } from "./strada-project"
import { resolveSymlinks, type Symlinks } from "./symlinks"
@@ -176,11 +177,101 @@ async function dispatch(method: string, args: any) {
}
case "readFile":
return readFile(args.fileName)
+ case "astRoot":
+ return serializeAstRoot(args.fileName)
+ case "astChildren":
+ return serializeAstChildren(args.fileName, args.id)
+ case "astPath":
+ return serializeAstPath(args.fileName, args.position)
default:
throw new Error(`Unknown Strada request: ${method}`)
}
}
+function serializeAstRoot(fileName: string): AstNode | undefined {
+ const { service, fileName: resolvedFileName } = languageServiceForFile(fileName)
+ const sourceFile = service.getProgram()?.getSourceFile(resolvedFileName)
+ return sourceFile ? describeAstNode(sourceFile, sourceFile, "") : undefined
+}
+
+function serializeAstChildren(fileName: string, id: string): AstNode[] {
+ const sourceFile = sourceFileForAst(fileName)
+ if (!sourceFile) return []
+ const path = parseAstPath(id)
+ const node = nodeAtPath(sourceFile, path)
+ return node ? astChildren(node).map(({ child, index }) => describeAstNode(child, sourceFile, [...path, index])) : []
+}
+
+function serializeAstPath(fileName: string, position: number): AstNode[] {
+ const sourceFile = sourceFileForAst(fileName)
+ if (!sourceFile) return []
+ const path: import("typescript").Node[] = [sourceFile]
+ let node: import("typescript").Node = sourceFile
+ let indices: number[] = []
+ while (true) {
+ const child = astChildren(node).find(
+ ({ child }) => position >= child.getStart(sourceFile) && position <= child.getEnd()
+ )
+ if (!child) break
+ node = child.child
+ indices = [...indices, child.index]
+ path.push(node)
+ }
+ return path.map((entry, index) => describeAstNode(entry, sourceFile, indices.slice(0, index)))
+}
+
+function sourceFileForAst(fileName: string): import("typescript").SourceFile | undefined {
+ const { service, fileName: resolvedFileName } = languageServiceForFile(fileName)
+ return service.getProgram()?.getSourceFile(resolvedFileName) as import("typescript").SourceFile | undefined
+}
+
+function parseAstPath(id: string) {
+ return id === "" ? [] : id.split(".").map(Number)
+}
+
+function nodeAtPath(sourceFile: import("typescript").SourceFile, path: number[]) {
+ let node: import("typescript").Node = sourceFile
+ for (const index of path) {
+ const child = astChildren(node).find(candidate => candidate.index === index)
+ if (!child) return undefined
+ node = child.child
+ }
+ return node
+}
+
+function astChildren(node: import("typescript").Node) {
+ const children: Array<{ child: import("typescript").Node; index: number }> = []
+ node.forEachChild(child => {
+ children.push({ child, index: children.length })
+ })
+ return children
+}
+
+function describeAstNode(
+ node: import("typescript").Node,
+ sourceFile: import("typescript").SourceFile,
+ path: number[] | ""
+): AstNode {
+ const kind = ts.SyntaxKind[node.kind] ?? String(node.kind)
+ const properties = node as import("typescript").Node & { text?: string; escapedText?: string }
+ const text =
+ typeof properties.text === "string"
+ ? properties.text
+ : typeof properties.escapedText === "string"
+ ? properties.escapedText
+ : ""
+ const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : ""
+ const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : ""
+ return {
+ detail: [preview, flags].filter(Boolean).join(" · "),
+ end: node.getEnd(),
+ hasChildren: node.forEachChild(() => true) === true,
+ id: Array.isArray(path) ? path.join(".") : path,
+ kind,
+ start: node.getStart(sourceFile),
+ }
+}
+
function updateFiles(
nextFiles: Record,
deleted: readonly string[],
diff --git a/packages/playground-v2/src/styles.css b/packages/playground-v2/src/styles.css
index 0fdd72c9eed0..3bf6df85d846 100644
--- a/packages/playground-v2/src/styles.css
+++ b/packages/playground-v2/src/styles.css
@@ -1040,6 +1040,132 @@ code {
font-size: 0.8rem;
}
+.ast-panel {
+ flex: 0 1 38%;
+ min-height: 0;
+ display: flex;
+ flex-direction: column;
+ border-top: 1px solid var(--border);
+ background: var(--panel);
+}
+
+.ast-panel[data-collapsed="true"] .ast-tree-container {
+ display: none;
+}
+
+.ast-panel-header {
+ position: sticky;
+ top: 0;
+ z-index: 1;
+ min-height: 2.75rem;
+ padding: 0.45rem 0.65rem;
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ border-bottom: 1px solid var(--border);
+ background: var(--sidebar);
+}
+
+.ast-panel-header h2 {
+ margin: 0;
+ font-size: 0.8rem;
+ font-weight: 600;
+}
+
+.ast-panel-header > span {
+ color: var(--muted);
+ font-size: 0.72rem;
+}
+
+.ast-tree-container {
+ min-height: 0;
+ max-height: 18rem;
+ padding: 0.35rem 0.35rem 0.5rem;
+ overflow: auto;
+}
+
+.ast-tree,
+.ast-node-children {
+ margin: 0;
+ padding: 0;
+ list-style: none;
+}
+
+.ast-node-children {
+ margin-left: 0.85rem;
+ padding-left: 0.3rem;
+ border-left: 1px solid var(--border);
+}
+
+.ast-node-row {
+ min-height: 1.65rem;
+ display: flex;
+ align-items: center;
+ gap: 0.2rem;
+}
+
+.ast-node-row.ast-node-active,
+.ast-node-select:hover {
+ background: var(--hover);
+}
+
+.ast-node-toggle,
+.ast-node-select {
+ min-height: 1.65rem;
+ border: 0;
+ color: inherit;
+ background: transparent;
+ cursor: pointer;
+ font: inherit;
+ text-align: left;
+}
+
+.ast-node-toggle {
+ flex: 0 0 1.1rem;
+ padding: 0;
+}
+
+.ast-node-select {
+ min-width: 0;
+ padding: 0.15rem 0.25rem;
+ display: flex;
+ align-items: center;
+ gap: 0.35rem;
+}
+
+.ast-node-toggle:focus-visible,
+.ast-node-select:focus-visible {
+ outline: 2px solid var(--accent);
+ outline-offset: -2px;
+}
+
+.ast-node-arrow {
+ display: block;
+ color: var(--muted);
+ font-size: 0.7rem;
+ text-align: center;
+}
+
+.ast-node-kind {
+ flex: 0 0 auto;
+ font-family: Hack, monospace;
+ font-size: 0.7rem;
+}
+
+.ast-node-detail {
+ min-width: 0;
+ color: var(--muted);
+ font-family: Hack, monospace;
+ font-size: 0.65rem;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+}
+
+.ast-source-highlight {
+ background: color-mix(in srgb, var(--accent) 25%, transparent);
+}
+
.run-output {
flex: 0 0 auto;
max-height: 12rem;
From a0bbad5406e9a6479f0af4590541a5e74454c79c Mon Sep 17 00:00:00 2001
From: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
Date: Mon, 16 Nov 2026 16:28:33 -0800
Subject: [PATCH 2/3] Keep Playground v2 AST panel coherent and idle when
hidden
Child and cursor lookups could resolve against a newer AST than the
rendered root once a compile or edit changed the worker's sources, and
the hidden panel still queried the worker on every edit and cursor
move. Snapshot the tree per root request so lookups stay tied to the
rendered source, and query only while the panel is visible.
Start the panel collapsed like the v1 AST tab, surface AST failures in
the panel, keep a clicked node selected instead of jumping to its
deepest descendant, and support arrow-key tree navigation. Enum
reverse mappings also reported marker aliases such as FirstStatement,
so node kinds now use their declared names.
---
packages/playground-v2/src/ast-tree.ts | 111 +++++++
packages/playground-v2/src/ast-types.ts | 8 -
packages/playground-v2/src/index.html | 4 +-
packages/playground-v2/src/main.ts | 310 +++++++++++++-----
.../playground-v2/src/native-compiler.test.ts | 26 +-
packages/playground-v2/src/native-compiler.ts | 104 +-----
packages/playground-v2/src/native.ts | 12 +-
packages/playground-v2/src/native.worker.ts | 8 +-
packages/playground-v2/src/strada.ts | 14 +-
packages/playground-v2/src/strada.worker.ts | 95 +-----
10 files changed, 400 insertions(+), 292 deletions(-)
create mode 100644 packages/playground-v2/src/ast-tree.ts
delete mode 100644 packages/playground-v2/src/ast-types.ts
diff --git a/packages/playground-v2/src/ast-tree.ts b/packages/playground-v2/src/ast-tree.ts
new file mode 100644
index 000000000000..d3c818b28032
--- /dev/null
+++ b/packages/playground-v2/src/ast-tree.ts
@@ -0,0 +1,111 @@
+export type AstNode = {
+ detail: string
+ hasChildren: boolean
+ id: string
+ kind: string
+ start: number
+ end: number
+}
+
+export type AstSnapshot = {
+ revision: number
+ root: AstNode
+}
+
+export type AstSyntaxNode = {
+ kind: number
+ flags?: number
+ forEachChild(callback: (child: any) => T | undefined): T | undefined
+ getEnd(): number
+}
+
+// Enum reverse mappings resolve to the marker aliases declared last
+// (FirstStatement for VariableStatement), so keep the first declared name.
+export function syntaxKindNames(syntaxKind: Record) {
+ const names = new Map()
+ for (const [name, value] of Object.entries(syntaxKind)) {
+ if (typeof value === "number" && !names.has(value)) names.set(value, name)
+ }
+ return (kind: number) => names.get(kind)
+}
+
+type CachedAstNode = AstNode & { children: CachedAstNode[] }
+
+// Snapshots the tree so child and path requests keep resolving against the
+// source text the root was built from, even after later compiles.
+export class AstCache {
+ private revision = 0
+ private readonly nodes = new Map()
+
+ store(
+ root: T | undefined,
+ kindName: (kind: number) => string | undefined,
+ start: (node: T) => number
+ ): AstSnapshot | undefined {
+ this.revision++
+ this.nodes.clear()
+ if (!root) return undefined
+ const cachedRoot = describe(root, "", kindName, start)
+ this.nodes.set("", cachedRoot)
+ const pending: Array<[T, CachedAstNode]> = [[root, cachedRoot]]
+ while (pending.length) {
+ const [node, cached] = pending.pop()!
+ node.forEachChild((child: T) => {
+ const id = cached.id === "" ? String(cached.children.length) : `${cached.id}.${cached.children.length}`
+ const entry = describe(child, id, kindName, start)
+ cached.children.push(entry)
+ this.nodes.set(id, entry)
+ pending.push([child, entry])
+ return undefined
+ })
+ cached.hasChildren = cached.children.length > 0
+ }
+ return { revision: this.revision, root: summarize(cachedRoot) }
+ }
+
+ children(revision: number, id: string): AstNode[] | undefined {
+ if (revision !== this.revision) return undefined
+ return this.nodes.get(id)?.children.map(summarize)
+ }
+
+ path(revision: number, position: number): AstNode[] | undefined {
+ if (revision !== this.revision) return undefined
+ let node = this.nodes.get("")
+ const path: AstNode[] = []
+ while (node) {
+ path.push(summarize(node))
+ node = node.children.find(child => position >= child.start && position <= child.end)
+ }
+ return path
+ }
+}
+
+function describe(
+ node: T,
+ id: string,
+ kindName: (kind: number) => string | undefined,
+ start: (node: T) => number
+): CachedAstNode {
+ const properties = node as T & { text?: unknown; escapedText?: unknown }
+ const text =
+ typeof properties.text === "string"
+ ? properties.text
+ : typeof properties.escapedText === "string"
+ ? properties.escapedText
+ : ""
+ const preview = text ? JSON.stringify(text.length > 60 ? `${text.slice(0, 57)}…` : text) : ""
+ const flags = typeof node.flags === "number" && node.flags !== 0 ? `flags=${node.flags}` : ""
+ return {
+ children: [],
+ detail: [preview, flags].filter(Boolean).join(" · "),
+ end: node.getEnd(),
+ hasChildren: false,
+ id,
+ kind: kindName(node.kind) ?? String(node.kind),
+ start: start(node),
+ }
+}
+
+function summarize({ children: _children, ...node }: CachedAstNode): AstNode {
+ return node
+}
diff --git a/packages/playground-v2/src/ast-types.ts b/packages/playground-v2/src/ast-types.ts
deleted file mode 100644
index c48cea7b1434..000000000000
--- a/packages/playground-v2/src/ast-types.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-export type AstNode = {
- detail: string
- hasChildren: boolean
- id: string
- kind: string
- start: number
- end: number
-}
diff --git a/packages/playground-v2/src/index.html b/packages/playground-v2/src/index.html
index 5d2b9db8e19c..4c8840b52bee 100644
--- a/packages/playground-v2/src/index.html
+++ b/packages/playground-v2/src/index.html
@@ -124,10 +124,10 @@ Emit
-