Overview of ts-morph
latestts-morph is a library that wraps the TypeScript compiler API to simplify the setup, navigation, and manipulation of the TypeScript Abstract Syntax Tree (AST).repository·latest·Indexed 27 days ago
https://github.com/dsherret/ts-morphA high-level wrapper around the TypeScript Compiler API designed to make programmatic code navigation and manipulation of TypeScript and JavaScript code easier and more intuitive. It provides features such as in-memory change tracking, helper methods for modifying files, and the ability to fall back to underlying compiler API objects.
ts-morph is a library that wraps the TypeScript compiler API to simplify the setup, navigation, and manipulation of the TypeScript Abstract Syntax Tree (AST).ts-morph is a wrapper around the TypeScript Compiler API. It provides an easier way to programmatically navigate and manipulate TypeScript and JavaScript code.
Key features include:
.compilerNode or .compilerObject).project.save() to write to the file system.@ts-morph/bootstrap package, which is designed to simplify the initial setup process.In ts-morph, nodes are categorized as either 'wrapped' or 'unwrapped'.
Node objects but lack the specialized helper methods provided by more specific node types. They are essentially the base Node type without the extended API.If you encounter a node that lacks the expected helper methods for navigation or manipulation, it is likely an unwrapped node. The library is being incrementally updated to wrap more nodes over time.
To use ts-morph, follow these general steps:
Project.project.save().When adding files via tsconfig.json, ts-morph automatically analyzes and includes dependent source files. You can skip this analysis by setting skipFileDependencyResolution: true in the Project constructor.
If you are adding files using other methods (like globs or paths) and want to ensure all dependencies are included, call project.resolveSourceFileDependencies() after all files have been added.
const project = new Project();
// add everything to the project
project.addSourceFilesFromTsConfig("dir1/tsconfig.json");
project.addSourceFilesFromTsConfig("dir2/tsconfig.json");
project.addSourceFilesAtPaths("dir3/**/*{.d.ts,.ts}");
// resolve and add the dependent source files to the project
project.resolveSourceFileDependencies();Instead of formatting an entire file, you can target specific nodes (such as a single statement within a method) and call formatText() on them to selectively format only that portion of the code.
project.getSourceFileOrThrow("file.ts")
.getClassOrThrow("MyClass")
.getInstanceMethodOrThrow("myMethod")
.getStatements()[0]
.formatText();Source files created via structure, writer function, or string exist only in memory and are not automatically saved to disk. To persist them, call await sourceFile.save() or sourceFile.saveSync().
// save it to the disk if you wish:
await sourceFile.save(); // or saveSync();Use the format task to apply code formatting across the project.
deno task formatTo start using ts-morph, import and instantiate the Project class. This serves as the main entry point for managing TypeScript source files and the compiler context.
import { Project } from "ts-morph";
const project = new Project();When using an in-memory file system, standard types may resolve to any because the default script target is ES5. To resolve types correctly (e.g., Set<string>), you must explicitly specify the lib compiler option or a higher target in the Project configuration.
import { Project, ts } from "ts-morph";
// Option 1: Specify specific lib files
const projectLib = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.es2015.d.ts"],
},
});
// Option 2: Specify a target that implicitly loads required libs
const projectTarget = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
target: ts.ScriptTarget.ES2015,
},
});
// Option 3: Include all lib files
const projectFull = new Project({
useInMemoryFileSystem: true,
compilerOptions: {
lib: ["lib.esnext.full.d.ts"],
},
});When initializing a new Project object, you can define manipulationSettings to control how code transformations (like renaming or adding imports) are applied. You can provide a full or partial configuration object.
Available settings include:
indentationText: Controls indentation style (IndentationText.TwoSpaces, IndentationText.FourSpaces, IndentationText.EightSpaces, or IndentationText.Tab).newLineKind: Controls line endings (NewLineKind.LineFeed or NewLineKind.CarriageReturnLineFeed).quoteKind: Controls quote usage (QuoteKind.Single or QuoteKind.Double).usePrefixAndSuffixTextForRename: Boolean determining whether to change shorthand property assignments to property assignments and add aliases to import/export specifiers.useTrailingCommas: Boolean determining whether to use trailing commas in multi-line scenarios.import { IndentationText, NewLineKind, Project, QuoteKind } from "ts-morph";
const project = new Project({
manipulationSettings: {
indentationText: IndentationText.FourSpaces,
newLineKind: NewLineKind.LineFeed,
quoteKind: QuoteKind.Double,
usePrefixAndSuffixTextForRename: false,
useTrailingCommas: false,
},
});