How can I parse, modify, and regenerate the AST of a TypeScript file (like jscodeshift)?
Does ts-simple-ast
fit your needs?
import { Project } from "ts-simple-ast";
const project = new Project();
// ...lots of code here that manipulates, copies, moves, and deletes files...
const sourceFile = project.getSourceFile("Models/Person.ts");
const importDeclaration = sourceFile.addImportDeclaration({
defaultImport: "MyClass",
moduleSpecifier: "./file"
});
// when you're all done, call this and it will save everything to the file system
project.save();
https://github.com/dsherret/ts-simple-ast
https://dsherret.github.io/ts-simple-ast/
https://dsherret.github.io/ts-simple-ast/setup/ast-viewers
https://dsherret.github.io/ts-simple-ast/manipulation/
It looks like jscodeshift
supports TypeScript (ts
, and tsx
) via --parser
option since v0.6.0 (https://github.com/facebook/jscodeshift/releases/tag/v0.6.0).