The VS Code of video editing, built for agents.
TypeScript
Integrate ts-morph for JSX editing and refactor source handling
konstantin-paulus81a480610d8e
diff --git a/apps/desktop/package.json b/apps/desktop/package.jsonindex e34a0576..3bef4ebe 100644--- a/apps/desktop/package.json+++ b/apps/desktop/package.json@@ -40,6 +40,7 @@"babel-preset-solid": "^1.9.12","esbuild": "^0.28.1","sqids": "^0.3.0",+ "ts-morph": "^28.0.0","update-electron-app": "^3.0.0"}}diff --git a/apps/desktop/src/edit.ts b/apps/desktop/src/edit.tsnew file mode 100644index 00000000..b443dcbb--- /dev/null+++ b/apps/desktop/src/edit.ts@@ -0,0 +1,499 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++// The write direction of a project's JSX: `applyEdits` puts a value the editor+// arrived at back into the attribute that produced it, and `stampProject`+// gives elements the durable names that survive edits made to the file in+// between.+//+// Elements are addressed the way `./source` stamps them: by position in+// document order until they have an `id`. Both directions walk every JSX+// element a file has, so the nth tag here is the nth stamp there — one order,+// two parsers.+//+// ts-morph owns the parsing and the printing. It edits the syntax tree and+// re-prints only what changed, so a write touches the attribute it was asked+// to and leaves the rest of the file byte for byte as its author wrote it.++import { readdir, readFile, writeFile } from "node:fs/promises";+import { join } from "node:path";++import Sqids from "sqids";+import { Project, SyntaxKind } from "ts-morph";++import { ID_ATTR, formatSource, isCompositionTag, parseSource } from "@diffusionstudio/jsx";++import type { PropValue } from "@diffusionstudio/jsx";+import type { JsxAttribute, JsxOpeningElement, JsxSelfClosingElement, Node, SourceFile } from "ts-morph";++export type { PropValue };++export interface SourceContext {+ /** Absolute path of the project folder. */+ dir: string;+ /** Called with the project-relative path of every file written. */+ onWrite?: (file: string) => void;+}++/** Overwrites props of the element named by `source` (a `SOURCE_ATTR` value). */+export interface SourceEdit {+ source: string;+ props: Record<string, PropValue>;+}++export interface WriteResult {+ /** Sources that could not be written, as `id` or `id (prop)`. */+ skipped: string[];+ /**+ * Elements that earned a name in this write, as `old source id -> new one`.+ * The canvas re-stamps its entities with these, so identity does not have to+ * wait for a recompile.+ */+ ids?: Record<string, string>;+ error?: string;+}++/** The opening half of a JSX element — where its attributes live. */+type JsxTag = JsxOpeningElement | JsxSelfClosingElement;++const SOURCE_FILE = /\.[jt]sx?$/;++const sqids = new Sqids({ minLength: 2 });++const absolute = (dir: string, file: string): string => join(dir, ...file.split("/"));++// ---------------------------------------------------------------------------+// The numbering++/**+ * Every JSX element in document order — the same sequence `./source` numbers,+ * so the nth entry here is the element the nth position refers to. Walked+ * rather than collected: `getDescendants` would wrap every token in the file+ * to find the handful that are elements.+ */+function tags(sourceFile: SourceFile): JsxTag[] {+ const found: JsxTag[] = [];+ sourceFile.forEachDescendant((node: Node) => {+ if (node.isKind(SyntaxKind.JsxSelfClosingElement)) found.push(node);+ else if (node.isKind(SyntaxKind.JsxElement)) found.push(node.getOpeningElement());+ });+ return found;+}++const tagName = (tag: JsxTag): string => tag.getTagNameNode().getText();++const attributeOf = (tag: JsxTag, name: string): JsxAttribute | undefined =>+ tag.getAttribute(name)?.asKind(SyntaxKind.JsxAttribute);++const idOf = (tag: JsxTag): string | undefined =>+ attributeOf(tag, ID_ATTR)?.getInitializer()?.asKind(SyntaxKind.StringLiteral)?.getLiteralValue();++/**+ * Ids live in text, which can be copied, so two elements can end up claiming+ * one. An ambiguous id resolves to nothing and the write is reported instead.+ */+function findTag(sourceFile: SourceFile, locator: number | string): JsxTag | undefined {+ const all = tags(sourceFile);+ if (typeof locator === "number") return all[locator];++ const matches = all.filter((tag) => idOf(tag) === locator);+ return matches.length === 1 ? matches[0] : undefined;+}++const idsIn = (sourceFile: SourceFile): Set<string> =>+ new Set(tags(sourceFile).flatMap((tag) => idOf(tag) ?? []));++function counterOf(id: string): number {+ const decoded = sqids.decode(id);+ return decoded.length === 1 && Number.isInteger(decoded[0]) ? decoded[0]! : 0;+}++function idAllocator(taken: Set<string>): () => string {+ let counter = 0;++ for (const id of taken) {+ counter = Math.max(counter, counterOf(id));+ }++ return () => {+ while (true) {+ const id = sqids.encode([++counter]);+ if (taken.has(id)) continue;+ taken.add(id);+ return id;+ }+ };+}++// ---------------------------------------------------------------------------+// Values++const round = (value: number): number => Math.round(value * 100) / 100;++const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;++/** A prop value as the JavaScript that would have produced it. */+function literalText(value: PropValue): string {+ if (typeof value === "string") return JSON.stringify(value);+ if (typeof value === "number") return String(round(value));+ if (typeof value === "boolean" || value === null) return String(value);+ if (Array.isArray(value)) return `[${value.map(literalText).join(", ")}]`;++ const entries = Object.entries(value).map(+ ([key, nested]) => `${IDENTIFIER.test(key) ? key : JSON.stringify(key)}: ${literalText(nested)}`,+ );+ return entries.length ? `{ ${entries.join(", ")} }` : "{}";+}++/**+ * What a quoted JSX attribute cannot carry as itself: the quote that would end+ * it, the `&` that would start an entity, and the control characters that have+ * no spelling there — an attribute string is raw text, not JavaScript, so a+ * backslash escapes nothing.+ */+const UNQUOTABLE = /["&\p{Cc}\u2028\u2029]/u;++/**+ * A prop value as it appears after `=` in a JSX attribute. Strings are written+ * the way they are read — `fill="#ff0000"` — and fall back to an expression,+ * where JavaScript's escapes are available again, when they cannot be.+ */+const initializerText = (value: PropValue): string =>+ typeof value === "string" && !UNQUOTABLE.test(value)+ ? `"${value}"`+ : `{${literalText(value)}}`;++/** Writes a prop onto a tag as one would write it: `muted`, not `muted={true}`. */+function setProp(tag: JsxTag, name: string, value: PropValue): void {+ const attribute = attributeOf(tag, name);++ if (value === true) {+ if (attribute) attribute.removeInitializer();+ else tag.addAttribute({ name });+ return;+ }++ if (attribute) attribute.setInitializer(initializerText(value));+ else tag.addAttribute({ name, initializer: initializerText(value) });+}++/** Whether an expression is a value rather than a way of computing one. */+function isLiteral(node: Node): boolean {+ if (+ node.isKind(SyntaxKind.StringLiteral) ||+ node.isKind(SyntaxKind.NumericLiteral) ||+ node.isKind(SyntaxKind.TrueKeyword) ||+ node.isKind(SyntaxKind.FalseKeyword) ||+ node.isKind(SyntaxKind.NullKeyword)+ ) {+ return true;+ }++ if (node.isKind(SyntaxKind.PrefixUnaryExpression)) {+ const operator = node.getOperatorToken();+ const signed = operator === SyntaxKind.MinusToken || operator === SyntaxKind.PlusToken;+ return signed && isLiteral(node.getOperand());+ }++ if (node.isKind(SyntaxKind.ArrayLiteralExpression)) {+ return node.getElements().every(isLiteral);+ }++ if (node.isKind(SyntaxKind.ObjectLiteralExpression)) {+ return node.getProperties().every((property) => {+ const assignment = property.asKind(SyntaxKind.PropertyAssignment);+ if (!assignment || assignment.getNameNode().isKind(SyntaxKind.ComputedPropertyName)) return false;++ const initializer = assignment.getInitializer();+ return initializer !== undefined && isLiteral(initializer);+ });+ }++ return false;+}++/**+ * A prop is only written back when the source holds a plain literal. Anything+ * else — a signal, a prop of the surrounding component, an expression — is+ * someone's reactivity, and a drag has no business overwriting it.+ */+function isWritable(attribute: JsxAttribute): boolean {+ const initializer = attribute.getInitializer();+ if (!initializer) return true;+ if (initializer.isKind(SyntaxKind.StringLiteral)) return true;+ if (!initializer.isKind(SyntaxKind.JsxExpression)) return false;++ const expression = initializer.getExpression();+ return expression !== undefined && isLiteral(expression);+}++// ---------------------------------------------------------------------------+// Files++/** Every project source file, project-relative and `/`-separated. */+async function sourceFiles(dir: string, prefix = ""): Promise<string[]> {+ let entries;+ try {+ entries = await readdir(prefix ? join(dir, ...prefix.split("/")) : dir, { withFileTypes: true });+ } catch {+ return [];+ }++ const files: string[] = [];+ for (const entry of entries) {+ if (entry.name.startsWith(".") || entry.name === "node_modules") continue;+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;+ if (entry.isDirectory()) files.push(...(await sourceFiles(dir, path)));+ else if (SOURCE_FILE.test(entry.name)) files.push(path);+ }+ return files;+}++/** A file as it was read, alongside the tree edits are made against. */+interface OpenFile {+ sourceFile: SourceFile;+ text: string;+}++/** An edit resolved to the element it names within its file. */+interface FileEdit {+ locator: number | string;+ edit: SourceEdit;+}++/** What one file's edits came to. */+interface FileWrite {+ skipped: string[];+ ids: Record<string, string>;+}++/**+ * One write against a project's sources. ts-morph parses into a file system of+ * its own: bytes come off disk and go back to it here, and every file a write+ * touches is read once, edited in place, and saved at the end.+ *+ * One instance is one operation — construct it, run it, discard it. That is+ * what keeps a later write from editing a tree parsed before the project+ * changed underneath it.+ */+class SourceWriter {+ // Nothing here asks a question about types, so the standard library is never+ // loaded: parsing it costs more than everything else this class does.+ private readonly project = new Project({+ useInMemoryFileSystem: true,+ skipLoadingLibFiles: true,+ });+ private readonly files = new Map<string, OpenFile>();+ private readonly context: SourceContext;++ public constructor(context: SourceContext) {+ this.context = context;+ }++ /** Reads a file into the project, or does nothing if it cannot be read. */+ private async load(path: string): Promise<void> {+ if (this.files.has(path)) return;++ let text: string;+ try {+ text = await readFile(absolute(this.context.dir, path), "utf8");+ } catch {+ return;+ }++ // Created under the name it has in the project, so the extension still+ // decides the dialect: in `.ts`, `<T>value` is a type assertion rather+ // than an element.+ const sourceFile = this.project.createSourceFile(`/${path}`, text, { overwrite: true });+ this.files.set(path, { sourceFile, text });+ }++ /**+ * Drops every file the parser had to guess at, since re-printing one would+ * hand back source no one wrote — a file mid-edit is not a file to write to.+ *+ * Checked in one pass because ts-morph builds a program to answer for a+ * file, and that program is rebuilt every time another file arrives or one+ * is edited: asking per file would cost a program per file.+ */+ private dropUnparsed(paths: Iterable<string> = this.files.keys()): void {+ const program = this.project.getProgram();+ for (const path of [...paths]) {+ const open = this.files.get(path);+ if (!open) continue;+ if (program.getSyntacticDiagnostics(open.sourceFile).length) this.discard(path);+ }+ }++ /** Drops a file from the write set, leaving what is on disk untouched. */+ private discard(path: string): void {+ const open = this.files.get(path);+ if (!open) return;+ this.files.delete(path);+ this.project.removeSourceFile(open.sourceFile);+ }++ /** Writes back the files an edit actually changed, and only those. */+ private async save(): Promise<void> {+ for (const [path, open] of this.files) {+ const text = open.sourceFile.getFullText();+ if (text === open.text) continue;+ open.text = text;++ // Claimed before the write lands: a watcher must never see this change+ // arrive without knowing whose it was.+ this.context.onWrite?.(path);+ await writeFile(absolute(this.context.dir, path), text, "utf8");+ }+ }++ /**+ * Names every composition element in the project, so identity does not have+ * to be earned one write at a time: without an id, an element is only a+ * position, and any edit that adds an element above it moves it. Idempotent —+ * a project where nothing is missing an id is not written to, which is what+ * makes this safe to run before every compile.+ */+ public async stampProject(paths: string[]): Promise<void> {+ for (const path of paths) await this.load(path);+ this.dropUnparsed();++ for (const path of [...this.files.keys()]) this.stampFile(path);++ // Nothing ts-morph will not vouch for reaches the disk.+ this.dropUnparsed();+ await this.save();+ }++ private stampFile(path: string): void {+ const sourceFile = this.files.get(path)!.sourceFile;+ const nextId = idAllocator(idsIn(sourceFile));++ // Every id in one manipulation: each tag says where its own name goes —+ // after the attributes it already has — and the file is reparsed once+ // rather than once per element.+ const changes = tags(sourceFile).flatMap((tag) => {+ if (!isCompositionTag(tagName(tag)) || idOf(tag)) return [];++ const at = (tag.getAttributes().at(-1) ?? tag.getTagNameNode()).getEnd();+ return [{ span: { start: at, length: 0 }, newText: ` ${ID_ATTR}="${nextId()}"` }];+ });++ if (!changes.length) return;++ try {+ sourceFile.applyTextChanges(changes);+ } catch {+ this.discard(path);+ }+ }++ /**+ * Writes prop values back into the JSX that produced them. Props whose source+ * is an expression rather than a literal are reported as skipped and left+ * alone; an element that is written to and has no id gets one, so that the+ * next edit to renumber the file cannot strand it.+ */+ public async applyEdits(edits: SourceEdit[]): Promise<WriteResult> {+ const skipped: string[] = [];+ const ids: Record<string, string> = {};++ // Grouped by file: one parse and one write per file, however many elements+ // of it an edit touched.+ const byFile = new Map<string, FileEdit[]>();+ for (const edit of edits) {+ const location = parseSource(edit.source);+ if (!location) {+ skipped.push(edit.source);+ continue;+ }+ byFile.set(location.file, [+ ...(byFile.get(location.file) ?? []),+ { locator: location.locator, edit },+ ]);+ }++ for (const file of byFile.keys()) await this.load(file);+ this.dropUnparsed(byFile.keys());++ for (const [file, entries] of byFile) {+ const sourceFile = this.files.get(file)?.sourceFile;+ const written = sourceFile ? this.editFile(file, sourceFile, entries) : undefined;++ // All or nothing per file: a file whose tree an edit left in a state+ // ts-morph will not vouch for is dropped rather than half written.+ if (!written) {+ this.discard(file);+ skipped.push(...entries.map((entry) => entry.edit.source));+ continue;+ }++ skipped.push(...written.skipped);+ Object.assign(ids, written.ids);+ }++ await this.save();+ return { skipped, ...(Object.keys(ids).length ? { ids } : {}) };+ }++ /**+ * One file's worth of edits, applied to the tree: the ids it handed out and+ * the sources it would not write, or undefined if the file is to be left+ * alone entirely.+ */+ private editFile(file: string, sourceFile: SourceFile, entries: FileEdit[]): FileWrite | undefined {+ const nextId = idAllocator(idsIn(sourceFile));+ const skipped: string[] = [];+ const ids: Record<string, string> = {};++ try {+ for (const { locator, edit } of entries) {+ let wrote = false;++ for (const [name, value] of Object.entries(edit.props)) {+ // Re-resolved per prop: editing one attribute forgets its siblings.+ const tag = findTag(sourceFile, locator);+ if (!tag) {+ skipped.push(edit.source);+ break;+ }++ const attribute = attributeOf(tag, name);+ if (attribute && !isWritable(attribute)) {+ skipped.push(`${edit.source} (${name})`);+ continue;+ }++ setProp(tag, name, value);+ wrote = true;+ }++ // Named only once something was written: failing to write to an+ // element is no reason to touch the file.+ const tag = wrote ? findTag(sourceFile, locator) : undefined;+ if (!tag || idOf(tag)) continue;++ const id = nextId();+ tag.addAttribute({ name: ID_ATTR, initializer: `"${id}"` });+ ids[edit.source] = formatSource(file, id);+ }+ } catch {+ return undefined;+ }++ // The tree an edit leaves behind answers for itself before it is printed.+ this.dropUnparsed([file]);+ return this.files.has(file) ? { skipped, ids } : undefined;+ }+}++/** Stamps every source file of the project. Writes nothing to a fully named project. */+export async function stampProject(context: SourceContext): Promise<void> {+ await new SourceWriter(context).stampProject(await sourceFiles(context.dir));+}++/** Writes values the editor arrived at back into the JSX that produced them. */+export function applyEdits(context: SourceContext, edits: SourceEdit[]): Promise<WriteResult> {+ return new SourceWriter(context).applyEdits(edits);+}diff --git a/apps/desktop/src/main-channels.ts b/apps/desktop/src/main-channels.tsindex bc0c3b5a..094f5131 100644--- a/apps/desktop/src/main-channels.ts+++ b/apps/desktop/src/main-channels.ts@@ -10,7 +10,7 @@// CLI traffic uses a separate wire pair (CLI_WIRE in @diffusionstudio/cli/protocol);// main forwards it opaquely without inspecting channel names.import type { LogEntry, ScreenshotResult } from "@diffusionstudio/cli/protocol";-import type { SourceEdit, WriteResult } from "./source";+import type { SourceEdit, WriteResult } from "./edit";export const MAIN_WIRE = {REQUEST: "main:request",diff --git a/apps/desktop/src/projects.ts b/apps/desktop/src/projects.tsindex 2d2cdf5e..1cfd79e1 100644--- a/apps/desktop/src/projects.ts+++ b/apps/desktop/src/projects.ts@@ -24,9 +24,10 @@ import type { BuildOptions, Plugin } from "esbuild";import { mainBridge } from "./main-manager";import { MAIN_CHANNELS } from "./main-channels";-import { applyEdits, sourcePlugin, stampProject } from "./source";+import { applyEdits, stampProject } from "./edit";+import { sourcePlugin } from "./source";import type { CompileResult, ProjectInfo, SourceEdit, WriteResult } from "./main-channels";-import type { SourceContext } from "./source";+import type { SourceContext } from "./edit";/** Entry points looked up in a project folder, in order of preference. */export const ENTRY_FILES = ["index.tsx", "index.ts", "index.jsx", "index.js"];@@ -321,9 +322,8 @@ function solidLoader(root: string): Plugin {};}-/** The `./source` context for a project folder, wired to the watcher's self-write log. */+/** The `./edit` context for a project folder, wired to the watcher's self-write log. */const sourceContext = (dir: string): SourceContext => ({- babel: load<Babel>("@babel/core"),dir,onWrite: (file) => markSelfWrite(dir, file),});diff --git a/apps/desktop/src/source.ts b/apps/desktop/src/source.tsindex 13ab7425..a8ac2880 100644--- a/apps/desktop/src/source.ts+++ b/apps/desktop/src/source.ts@@ -2,117 +2,25 @@* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */-// The two directions of a project's JSX, in one module.+// The read direction of a project's JSX: a compile-time stamp recording where+// every composition element came from, so the entity the renderer builds from+// it knows its own origin.//-// Reading: `sourcePlugin` stamps every composition element with where it came-// from, so the entity the renderer builds from it knows its own origin.-// Writing: `applyEdits` puts a value the editor arrived at back into the-// attribute that produced it, and `stampProject` gives elements the durable-// names that survive edits made to the file in between.-//-// Both directions number a file's JSX elements in document order, and an-// element is known by its position there until it has an `id`. If the two ever-// disagreed about the order, a drag would write to the wrong element — which-// is why they are here together and share one traversal (`eachElement`) over-// one parser, rather than agreeing by convention.--import { readdir, readFile, writeFile } from "node:fs/promises";-import { join } from "node:path";--import Sqids from "sqids";--import { ID_ATTR, SOURCE_ATTR, formatSource, isCompositionTag, parseSource } from "@diffusionstudio/jsx";+// The numbering here and the one `./edit` walks with ts-morph must produce the+// same sequence — two parsers, two libraries, one order. If they ever+// disagreed, a drag would write to the wrong element; that invariant is why+// these two modules are read together.-import type { PropValue } from "@diffusionstudio/jsx";-import type { NodePath, PluginObj, TransformOptions, types as t } from "@babel/core";+import { ID_ATTR, SOURCE_ATTR, formatSource, isCompositionTag } from "@diffusionstudio/jsx";-export type { PropValue };--/** The parts of `@babel/core` this module needs; the caller owns loading it. */-export type SourceBabel = Pick<typeof import("@babel/core"), "parseSync" | "traverse">;+import type { PluginObj } from "@babel/core";/** What babel hands a plugin factory. */type BabelApi = { types: typeof import("@babel/core").types };-export interface SourceContext {- babel: SourceBabel;- /** Absolute path of the project folder. */- dir: string;- /** Called with the project-relative path of every file written. */- onWrite?: (file: string) => void;-}--/** Overwrites props of the element named by `source` (a `SOURCE_ATTR` value). */-export interface SourceEdit {- source: string;- props: Record<string, PropValue>;-}--export interface WriteResult {- /** Sources that could not be written, as `id` or `id (prop)`. */- skipped: string[];- /**- * Elements that earned a name in this write, as `old source id -> new one`.- * The canvas re-stamps its entities with these, so identity does not have to- * wait for a recompile.- */- ids?: Record<string, string>;- error?: string;-}--const SOURCE_FILE = /\.[jt]sx?$/;--const sqids = new Sqids({ minLength: 2 });--// ----------------------------------------------------------------------------// The numbering--/**- * Every JSX element a traversal reaches, in document order. Components and DOM- * tags are counted but not stamped: a position means "the nth element in this- * file", and skipping some would make that depend on which ones are- * composition elements today.- */-function eachElement(- traverse: (visitor: { JSXElement(path: NodePath<t.JSXElement>): void }) => void,- visit: (node: t.JSXElement, index: number) => void,-): void {- let index = 0;- traverse({- JSXElement(path) {- visit(path.node, index++);- },- });-}--/** The tag as written, or undefined for the member/namespaced forms no tag has. */-function tagName(node: t.JSXElement): string | undefined {- const name = node.openingElement.name;- return name.type === "JSXIdentifier" ? name.name : undefined;-}--function attributeOf(node: t.JSXElement, name: string): t.JSXAttribute | undefined {- for (const attribute of node.openingElement.attributes) {- if (attribute.type !== "JSXAttribute") continue;- if (attribute.name.type === "JSXIdentifier" && attribute.name.name === name) return attribute;- }- return undefined;-}--function idOf(node: t.JSXElement): string | undefined {- const value = attributeOf(node, ID_ATTR)?.value;- return value?.type === "StringLiteral" ? value.value : undefined;-}--// ----------------------------------------------------------------------------// Reading: the compile-time stamp-/*** Stamps every composition element with the location of its JSX source, so* entities on the canvas can be traced back to the code that produced them.- * An element that names itself with `id` keeps that name; the position is the- * fallback for elements nothing has had to write to yet. The id is removed on- * the way through, so it never reaches a host as a prop.*/export function sourcePlugin({ types }: BabelApi, { file }: { file: string }): PluginObj {return {@@ -121,361 +29,44 @@ export function sourcePlugin({ types }: BabelApi, { file }: { file: string }): P// Stamped up front: Solid's transform replaces whole JSX trees, so by the// time a nested element would be visited normally it no longer exists.Program(program) {- eachElement(- (visitor) => program.traverse(visitor),- (node, index) => {- const tag = tagName(node);- if (tag === undefined || !isCompositionTag(tag)) return;+ // Components and DOM tags are counted but not stamped: a position means+ // "the nth element in this file", and skipping some would make that+ // depend on which ones are composition elements today.+ let index = 0;++ program.traverse({+ JSXElement(path) {+ const position = index++;++ const opening = path.node.openingElement;+ const name = opening.name;+ if (name.type !== "JSXIdentifier" || !isCompositionTag(name.name)) return;++ // An element that names itself keeps that name; the position is the+ // fallback for elements nothing has had to write to yet. The id is+ // removed on the way through, so it never reaches a host as a prop.+ let locator: string | number = position;+ opening.attributes = opening.attributes.filter((attribute) => {+ if (+ attribute.type !== "JSXAttribute" ||+ attribute.name.type !== "JSXIdentifier" ||+ attribute.name.name !== ID_ATTR+ ) {+ return true;+ }+ if (attribute.value?.type === "StringLiteral") locator = attribute.value.value;+ return false;+ });- const id = idOf(node);- const opening = node.openingElement;- opening.attributes = opening.attributes.filter(- (attribute) =>- !(- attribute.type === "JSXAttribute" &&- attribute.name.type === "JSXIdentifier" &&- attribute.name.name === ID_ATTR- ),- );opening.attributes.push(types.jsxAttribute(types.jsxIdentifier(SOURCE_ATTR),- types.stringLiteral(formatSource(file, id ?? index)),+ types.stringLiteral(formatSource(file, locator)),),);},- );+ });},},};}--// ----------------------------------------------------------------------------// Writing: values back into attributes--const round = (value: number): number => Math.round(value * 100) / 100;--const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;--/** A prop value as the JavaScript that would have produced it. */-function literalText(value: PropValue): string {- if (typeof value === "string") return JSON.stringify(value);- if (typeof value === "number") return String(round(value));- if (typeof value === "boolean" || value === null) return String(value);- if (Array.isArray(value)) return `[${value.map(literalText).join(", ")}]`;-- const entries = Object.entries(value).map(- ([key, nested]) => `${IDENTIFIER.test(key) ? key : JSON.stringify(key)}: ${literalText(nested)}`,- );- return entries.length ? `{ ${entries.join(", ")} }` : "{}";-}--/** A prop value as it appears after `=` in a JSX attribute. */-export function initializerText(value: PropValue): string {- return typeof value === "string" ? JSON.stringify(value) : `{${literalText(value)}}`;-}--/** `muted`, not `muted={true}` — an attribute as one would write it. */-function attributeText(name: string, value: PropValue): string {- if (typeof value === "boolean") return value ? name : `${name}={false}`;- return `${name}=${initializerText(value)}`;-}--/** Whether an expression is a value rather than a way of computing one. */-function isLiteral(node: t.Node): boolean {- switch (node.type) {- case "StringLiteral":- case "NumericLiteral":- case "BooleanLiteral":- case "NullLiteral":- return true;- case "UnaryExpression":- return (node.operator === "-" || node.operator === "+") && isLiteral(node.argument);- case "ArrayExpression":- return node.elements.every((element) => element !== null && isLiteral(element));- case "ObjectExpression":- return node.properties.every(- (property) =>- property.type === "ObjectProperty" && !property.computed && isLiteral(property.value),- );- default:- return false;- }-}--/**- * A prop is only written back when the source holds a plain literal. Anything- * else — a signal, a prop of the surrounding component, an expression — is- * someone's reactivity, and a drag has no business overwriting it.- */-function isWritable(attribute: t.JSXAttribute): boolean {- const value = attribute.value;- if (!value) return true;- if (value.type === "StringLiteral") return true;- if (value.type !== "JSXExpressionContainer") return false;- return value.expression.type !== "JSXEmptyExpression" && isLiteral(value.expression);-}--// ----------------------------------------------------------------------------// Files--/** Text edits over one file's source, applied together so ranges stay valid. */-class Splices {- // Keyed by range, so writing the same attribute twice in one batch replaces- // it once rather than splicing over a range that is no longer there.- private readonly replacements = new Map<string, { start: number; end: number; text: string }>();- private readonly insertions = new Map<number, string[]>();-- public replace(node: { start?: number | null; end?: number | null }, text: string): void {- if (typeof node.start !== "number" || typeof node.end !== "number") return;- this.replacements.set(`${node.start}:${node.end}`, { start: node.start, end: node.end, text });- }-- /**- * Adds an attribute to an element, after the ones it already has — where one- * would have written it. That position is the end of an existing attribute- * rather than the inside of one, so it never lands within a replacement; and- * since edits apply back to front, an insertion sitting exactly on the end of- * an attribute being replaced still ends up after it.- */- public addAttribute(node: t.JSXElement, text: string): void {- const attributes = node.openingElement.attributes;- const at = attributes[attributes.length - 1]?.end ?? node.openingElement.name.end;- if (typeof at !== "number") return;- this.insertions.set(at, [...(this.insertions.get(at) ?? []), text]);- }-- public get empty(): boolean {- return !this.replacements.size && !this.insertions.size;- }-- /** Applies back to front, so earlier offsets are still the ones parsed. */- public apply(source: string): string {- const all = [- ...this.replacements.values(),- ...[...this.insertions].map(([at, texts]) => ({- start: at,- end: at,- text: texts.map((text) => ` ${text}`).join(""),- })),- ].sort((a, b) => b.start - a.start);-- let text = source;- for (const splice of all) {- text = text.slice(0, splice.start) + splice.text + text.slice(splice.end);- }- return text;- }-}--/** One file, parsed, with its elements in the order both directions number them. */-interface ParsedFile {- source: string;- elements: t.JSXElement[];-}--const absolute = (dir: string, file: string): string => join(dir, ...file.split("/"));--async function parseFile(context: SourceContext, file: string): Promise<ParsedFile | undefined> {- let source: string;- try {- source = await readFile(absolute(context.dir, file), "utf8");- } catch {- return undefined;- }-- const options: TransformOptions = {- filename: absolute(context.dir, file),- babelrc: false,- configFile: false,- sourceType: "module",- // JSX everywhere but in `.ts`, where `<T>value` is a type assertion- // instead — the same reading babel's TypeScript preset picks by extension.- parserOpts: { plugins: file.endsWith(".ts") ? ["typescript"] : ["jsx", "typescript"] },- };-- let ast: ReturnType<SourceBabel["parseSync"]>;- try {- ast = context.babel.parseSync(source, options);- } catch {- // A file mid-edit does not parse; the next change brings another chance.- return undefined;- }- if (!ast) return undefined;-- const elements: t.JSXElement[] = [];- eachElement(- (visitor) => context.babel.traverse(ast as t.Node, visitor),- (node) => elements.push(node),- );- return { source, elements };-}--async function save(context: SourceContext, file: string, text: string): Promise<void> {- // Claimed before the write lands: a watcher must never see this change- // arrive without knowing whose it was.- context.onWrite?.(file);- await writeFile(absolute(context.dir, file), text, "utf8");-}--/**- * Ids live in text, which can be copied, so two elements can end up claiming- * one. An ambiguous id resolves to nothing and the write is reported instead.- */-function findElement(elements: t.JSXElement[], locator: number | string): t.JSXElement | undefined {- if (typeof locator === "number") return elements[locator];- const matches = elements.filter((element) => idOf(element) === locator);- return matches.length === 1 ? matches[0] : undefined;-}--function counterOf(id: string): number {- const decoded = sqids.decode(id);- return decoded.length === 1 && Number.isInteger(decoded[0]) ? decoded[0]! : 0;-}--function idAllocator(taken: Set<string>): () => string {- let counter = 0;-- for (const id of taken) {- counter = Math.max(counter, counterOf(id));- }-- return () => {- while (true) {- const id = sqids.encode([++counter]);- if (taken.has(id)) continue;- taken.add(id);- return id;- }- };-}--const idsIn = (elements: t.JSXElement[]): Set<string> =>- new Set(elements.flatMap((element) => idOf(element) ?? []));--// ----------------------------------------------------------------------------// Naming--/** Every project source file, project-relative and `/`-separated. */-async function sourceFiles(dir: string, prefix = ""): Promise<string[]> {- let entries;- try {- entries = await readdir(prefix ? join(dir, ...prefix.split("/")) : dir, { withFileTypes: true });- } catch {- return [];- }-- const files: string[] = [];- for (const entry of entries) {- if (entry.name.startsWith(".") || entry.name === "node_modules") continue;- const path = prefix ? `${prefix}/${entry.name}` : entry.name;- if (entry.isDirectory()) files.push(...(await sourceFiles(dir, path)));- else if (SOURCE_FILE.test(entry.name)) files.push(path);- }- return files;-}--/**- * Names every composition element in a file, so identity does not have to be- * earned one write at a time: without an id, an element is only a position,- * and any edit that adds an element above it moves it. Idempotent — a file- * where nothing is missing an id is not written to, which is what makes this- * safe to run before every compile.- */-async function stampFile(context: SourceContext, file: string): Promise<void> {- const parsed = await parseFile(context, file);- if (!parsed) return;-- const nextId = idAllocator(idsIn(parsed.elements));- const splices = new Splices();-- for (const element of parsed.elements) {- const tag = tagName(element);- if (tag === undefined || !isCompositionTag(tag) || idOf(element)) continue;-- splices.addAttribute(element, `${ID_ATTR}="${nextId()}"`);- }-- if (splices.empty) return;- await save(context, file, splices.apply(parsed.source));-}--/** Stamps every source file of the project. Writes nothing to a fully named project. */-export async function stampProject(context: SourceContext): Promise<void> {- for (const file of await sourceFiles(context.dir)) {- await stampFile(context, file);- }-}--// ----------------------------------------------------------------------------// Prop writes--/**- * Writes prop values back into the JSX that produced them. Props whose source- * is an expression rather than a literal are reported as skipped and left- * alone; an element that is written to and has no id gets one, so that the- * next edit to renumber the file cannot strand it.- */-export async function applyEdits(context: SourceContext, edits: SourceEdit[]): Promise<WriteResult> {- const skipped: string[] = [];- const ids: Record<string, string> = {};-- // Grouped by file: one parse and one write per file, however many elements- // of it an edit touched.- const byFile = new Map<string, { locator: number | string; edit: SourceEdit }[]>();- for (const edit of edits) {- const location = parseSource(edit.source);- if (!location) {- skipped.push(edit.source);- continue;- }- byFile.set(location.file, [- ...(byFile.get(location.file) ?? []),- { locator: location.locator, edit },- ]);- }-- for (const [file, entries] of byFile) {- const parsed = await parseFile(context, file);- if (!parsed) {- skipped.push(...entries.map((entry) => entry.edit.source));- continue;- }-- const nextId = idAllocator(idsIn(parsed.elements));- const splices = new Splices();-- for (const { locator, edit } of entries) {- const element = findElement(parsed.elements, locator);- if (!element) {- skipped.push(edit.source);- continue;- }-- let wrote = false;- for (const [name, value] of Object.entries(edit.props)) {- const attribute = attributeOf(element, name);- if (attribute && !isWritable(attribute)) {- skipped.push(`${edit.source} (${name})`);- continue;- }-- if (attribute) splices.replace(attribute, attributeText(name, value));- else splices.addAttribute(element, attributeText(name, value));- wrote = true;- }-- // Named only once something was written: failing to write to an element- // is no reason to touch the file.- if (!wrote || idOf(element)) continue;- const id = nextId();- splices.addAttribute(element, `${ID_ATTR}="${id}"`);- ids[edit.source] = formatSource(file, id);- }-- if (splices.empty) continue;- await save(context, file, splices.apply(parsed.source));- }-- return { skipped, ...(Object.keys(ids).length ? { ids } : {}) };-}diff --git a/package-lock.json b/package-lock.jsonindex 8cdf5409..3aa93217 100644--- a/package-lock.json+++ b/package-lock.json@@ -53,6 +53,7 @@"babel-preset-solid": "^1.9.12","esbuild": "^0.28.1","sqids": "^0.3.0",+ "ts-morph": "^28.0.0","update-electron-app": "^3.0.0"},"devDependencies": {@@ -3930,6 +3931,53 @@"typescript": ">=5.7.2"}},+ "node_modules/@ts-morph/common": {+ "version": "0.29.0",+ "resolved": "https://registry.npmjs.org/@ts-morph/common/-/common-0.29.0.tgz",+ "integrity": "sha512-35oUmphHbJvQ/+UTwFNme/t2p3FoKiGJ5auTjjpNTop2dyREspirjMy82PLSC1pnDJ8ah1GU98hwpVt64YXQsg==",+ "license": "MIT",+ "dependencies": {+ "minimatch": "^10.0.1",+ "path-browserify": "^1.0.1",+ "tinyglobby": "^0.2.14"+ }+ },+ "node_modules/@ts-morph/common/node_modules/balanced-match": {+ "version": "4.0.4",+ "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz",+ "integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==",+ "license": "MIT",+ "engines": {+ "node": "18 || 20 || >=22"+ }+ },+ "node_modules/@ts-morph/common/node_modules/brace-expansion": {+ "version": "5.0.9",+ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.9.tgz",+ "integrity": "sha512-ScQ4IuvIEF1TMlP7Zt+vjJ//9zlPb2SDcxWxM3bk8s6t6GGdJ7KO1dCcTidOPJKePW30LE/2cT7wCyPho9/Wxg==",+ "license": "MIT",+ "dependencies": {+ "balanced-match": "^4.0.2"+ },+ "engines": {+ "node": "20 || >=22"+ }+ },+ "node_modules/@ts-morph/common/node_modules/minimatch": {+ "version": "10.2.6",+ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.6.tgz",+ "integrity": "sha512-vpLQEs+VLCr1nU0BXS07maYoFwlDAH0gngQuuttxIwutDFEMHq2blX+8vpgxDdK3J1PwjCJiep77OitTZ4Ll1A==",+ "license": "BlueOak-1.0.0",+ "dependencies": {+ "brace-expansion": "^5.0.8"+ },+ "engines": {+ "node": "18 || 20 || >=22"+ },+ "funding": {+ "url": "https://github.com/sponsors/isaacs"+ }+ },"node_modules/@types/appdmg": {"version": "0.5.5","resolved": "https://registry.npmjs.org/@types/appdmg/-/appdmg-0.5.5.tgz",@@ -5640,6 +5688,12 @@"solid-js": "^1.8.15"}},+ "node_modules/code-block-writer": {+ "version": "13.0.3",+ "resolved": "https://registry.npmjs.org/code-block-writer/-/code-block-writer-13.0.3.tgz",+ "integrity": "sha512-Oofo0pq3IKnsFtuHqSF7TqBfr71aeyZDVJ0HpmqB7FBM2qEigL0iPONSCZSO9pE9dZTAxANe5XHG9Uy0YMv8cg==",+ "license": "MIT"+ },"node_modules/color-convert": {"version": "2.0.1","resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",@@ -9770,6 +9824,12 @@"node": ">=14.14"}},+ "node_modules/path-browserify": {+ "version": "1.0.1",+ "resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",+ "integrity": "sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==",+ "license": "MIT"+ },"node_modules/path-exists": {"version": "4.0.0","resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",@@ -11272,7 +11332,6 @@"version": "0.2.17","resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz","integrity": "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==",- "dev": true,"license": "MIT","dependencies": {"fdir": "^6.5.0",@@ -11289,7 +11348,6 @@"version": "6.5.0","resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz","integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==",- "dev": true,"license": "MIT","engines": {"node": ">=12.0.0"@@ -11307,7 +11365,6 @@"version": "4.0.5","resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.5.tgz","integrity": "sha512-RvwwcruNjI1ncT5xRakeyS9Lf8lcItv34KD+aif+VH9kduAyfYBipGh12274xtenIPZ119/R9BdTBa8gAwSh0A==",- "dev": true,"license": "MIT","engines": {"node": ">=12"@@ -11407,6 +11464,16 @@"typescript": ">=4.8.4"}},+ "node_modules/ts-morph": {+ "version": "28.0.0",+ "resolved": "https://registry.npmjs.org/ts-morph/-/ts-morph-28.0.0.tgz",+ "integrity": "sha512-Wp3tnZ2bzwxyTZMtgWVzXDfm7lB1Drz+y9DmmYH/L702PQhPyVrp3pkou3yIz4qjS14GY9kcpmLiOOMvl8oG1g==",+ "license": "MIT",+ "dependencies": {+ "@ts-morph/common": "~0.29.0",+ "code-block-writer": "^13.0.3"+ }+ },"node_modules/tslib": {"version": "2.8.1","resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",