Is it possible to convert a string of TypeScript code snippet to AST node? In memory, without creating any a file.
Yes, use the ts.createSourceFile function:
import * as ts from "typescript";
const sourceFile = ts.createSourceFile(
"file.ts", // filePath
"function myFunction() {}", // fileText
ts.ScriptTarget.Latest, // scriptTarget
true // setParentNodes -- sets the `parent` property
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With