Skip to content

Dialects

When string-level mutation isn’t enough (“add an import to this module”, “set a prop on this JSX element”), dialects provide structured, AST-aware operations for one file type. A dialect never edits text by search-and-replace: it parses the file into an AST, mutates the tree, and prints the result back. Each file type gets the AST library that fits it best. The TypeScript and React dialects (@pbuilder/sdk/typescript and @pbuilder/sdk/react) are both built on TypeScript’s own AST (surfaced through ts-morph), because TS and TSX share a grammar. JSON and YAML files have dialects of their own: see JSON dialect and YAML dialect, and CSS files use the CSS dialect. The React dialect, the NestJS dialect and the Angular dialect add framework ops on top of the TypeScript ones. .cs files use the C# dialect, which the ASP.NET dialect extends, and .NET project files use the MSBuild dialect.

A dialect’s entry point is its own find(path): it opens a coalescing, awaitable handle. Every op you chain mutates the same live AST, and the handle serializes to exactly one replaceContent-style directive when it flushes.

import * as ts from "@pbuilder/sdk/typescript";
await ts.find("src/index.ts")
.addImport("readFileSync", "node:fs")
.addFunction("hi", "(): void {}", { export: true });

A read (.read() on the handle, or any read on any path) drains the open directive first; a chain with one mid-chain read produces exactly two cumulative directives, and no edit is lost.

The handle is a thenable, the one async departure from the SDK’s otherwise synchronous verbs. await is not required for correctness: the run boundary drains every outstanding handle before the run flushes, so a forgotten await still commits its edit, and a chain that throws still surfaces its error contained at the run boundary instead of as an unhandled rejection. await the chain yourself when you need to sequence a read after your own write, or to observe a failure locally in your own try/catch.

From @pbuilder/sdk 0.25.0, a dialect handle has a read-only path, as a commons handle does: the workspace-relative path its ops act on. ts.find("src/index.ts").path is "src/index.ts", and each handle from a dialect’s glob carries the path that glob listed. Inside .inspect() the AST keeps the file name the dialect parses under, so read the path from the handle. See A handle’s path.

Most ops write and return the handle, so they chain. A dialect can also declare queries, which read a value. A query ends the chain: await it and it resolves to the value. It sees every op chained before it on the same handle, writes nothing, and the ops before it still commit. A query that would change the file rejects and fails the run. The CSS dialect’s getDeclaration is a query.

Named ops cover the common edits. When the change you need is not in that vocabulary, use .modify(fn), which every dialect handle has. Pick the tool by the edit:

You need to… Use
Add, remove or sort imports, add a top-level declaration, append to an array, add a class property, edit one JSX element’s props, children, wrapper or tag, add a hook call or a prop to a React component The dialect’s named ops
Make any other structural change to a .ts, .tsx or .jsx file .modify(fn) on the dialect handle
Register a provider, controller, module import or export in a NestJS @Module The NestJS dialect
Register a component, provider or route, or add a signal or inject() member in Angular The Angular dialect
Edit a JSON, YAML or CSS file The JSON, YAML or CSS dialect
Add a using directive, base type, member or constructor parameter to a .cs file The C# dialect
Register a service in an ASP.NET Core dependency injection container The ASP.NET dialect
Add a project reference to a .csproj, .props, .targets or .slnx file The MSBuild dialect
Edit a file type no dialect covers, such as Markdown find from @pbuilder/sdk/commons

The callback receives only the AST: a ts-morph SourceFile for the TypeScript and React dialects, the same live instance the named ops mutate. There is no second argument. Named ops and .modify() calls chained on one handle still flush as a single write, in chain order.

Custom edits usually need more than the methods on the AST: enum constants such as SyntaxKind, type guards such as Node.isStringLiteral, and type names for your own annotations. Each dialect exports its complete AST library as astLibrary, from the same entry point as find:

import { find, astLibrary } from "@pbuilder/sdk/typescript";
astLibrary.SyntaxKind.ArrayLiteralExpression; // runtime constant
astLibrary.Node.isStringLiteral(node); // runtime helper
type Routes = astLibrary.ArrayLiteralExpression; // type-only access

Use the astLibrary of the dialect whose handle you are editing: @pbuilder/sdk/typescript for .ts, @pbuilder/sdk/react for .tsx and .jsx. You do not need to add ts-morph to your own dependencies to work with the AST. Each dialect owns its library and version; which library backs each dialect is listed in AST libraries.

Worked examples of .modify() live with the dialect they use: register a route (TypeScript) and add a <Route> (React).

  • Operate only on the ast the callback hands you, with the astLibrary of the same dialect. If your project also installs ts-morph directly, that copy is a separate realm: a Node or SourceFile created from it is not interchangeable with the callback’s AST, even at the same version. Do not pass objects across that boundary, and do not assume AST objects from different dialects are interchangeable.
  • .modify() runs with full process privilege and is not a sandbox. Your own callbacks are your own trust; treat third-party dialects or op-packs built on it accordingly.
  • Strings you insert are code. Text passed to addElement, setBodyText and similar methods is inserted verbatim, so sanitize anything derived from user input.

Dialects exist for .ts, .tsx, .jsx, .json, .jsonc, .yaml, .yml, .css, .cs, .csproj, .props, .targets and .slnx today. Any other file (a Markdown index, an .scss stylesheet, an .env file) is still editable with find from @pbuilder/sdk/commons: it returns a handle for any path, with read() plus the text-level verbs (replaceContent, rename, move, copy, remove). There is no AST: read the content, transform it with whatever parser fits the format, and write the result back.

The example below uses a JSON file because the round trip is short. In a real factory, edit JSON with the JSON dialect, which keeps the file’s formatting and comments.

config/features.json (before)
{
"enabled": ["search"]
}
factory.ts
import { find, replaceContent } from "@pbuilder/sdk/commons";
export default async (input: { flag: string }) => {
const content = await find("config/features.json").read();
if (content === undefined) {
throw new Error("config/features.json not found");
}
const config = JSON.parse(content) as { enabled: string[] };
if (config.enabled.includes(input.flag)) return;
config.enabled.push(input.flag);
replaceContent("config/features.json", `${JSON.stringify(config, null, 2)}\n`);
};
config/features.json (after, with --flag=billing)
{
"enabled": [
"search",
"billing"
]
}

Two differences from a dialect edit:

  • You own the formatting. Re-serializing rewrites the whole file. Here, JSON.stringify reflows the array. Use a format-preserving parser when layout matters.
  • Handle the three read states. read() resolves undefined for a missing file and "" for an empty one; branch with strict comparisons, as described in Reading files.
  • JSON dialect and YAML dialect: named ops for JSON, JSONC and YAML files that keep comments and formatting.
  • CSS dialect: set and remove declarations in a .css file, such as design tokens in a Tailwind @theme block.
  • NestJS dialect: register classes in a NestJS @Module.
  • Angular dialect: register components, providers and routes, and add signal and inject() members.
  • C# dialect and ASP.NET dialect: add using directives, base types, members, constructor parameters and service registrations to .cs files.
  • MSBuild dialect: add project references to .csproj, .props, .targets and .slnx files.
  • AST libraries: which library and version back each dialect, and how astLibrary exposes them.
  • Publish and use a dialect package: ship a dialect as an npm package and declare it in project-builder.json with a version range. For a dialect that lives in one workspace, see Write a local dialect.
  • Building a dialect of your own is a contributor-level surface. Every dialect must export its complete library as astLibrary, and its conformance fixture must pass the actual module and a library exercise. See the SDK’s dialect authoring guide.

Support for more file types such as HTML (each backed by its own parser/AST library), and framework-aware dialects for Vue and Svelte, are planned. Until then, edit those files with find from @pbuilder/sdk/commons.