0

I'm using the built-in parser to generate the AST from source code:

const ts = require('typescript')
//...
const ast = ts.createSourceFile(filename, fs.readFileSync(filename).toString(), ts.ScriptTarget.ES6, true)

Given an arbitrary node in the tree, how do I generate the typescript from the node?

1 Answer 1

1

an arbitrary node in the tree, how do I generate the typescript from the node

You cannot in the current version of the compiler. The only node that supports generation is SourceFile.

There is talk to make the emitter more split (transformer based like the Babel emitter).

Sign up to request clarification or add additional context in comments.

3 Comments

So given a node, is there any way to get back the corresponding source code? I notice that there are pos and end properties.
There is a getTextOfNode function in the ts namespace
ts.getTextOfNode is exactly what i was looking for. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.