I'm trying to generate some typescript code using the compiler api. Unfortunately official (or any for that matter) documentation is sparse, and I am now completely stuck on a very simple task:
I want to generate a simple function call like foo(). That's all, a simple global function call.
After a long search I have come this far:
import ts from 'typescript'
ts.createCall(
ts.createPropertyAccess(
*some expression*,
'foo'
),
undefined, //generics
[], //parameters
);
As far as I understand it I need to pass an expression to createPropertyAccess to be the 'owner' of the accessed property (like foo in foo.bar). However, in this case there is no 'owner' because the function resides in the global scope.
Is there a way to generate such a function call? I tried using ts.createOmittedExpression, but that generated the following code:
().bar();
In this scenario you can just use ts.createIdentifier. There's no property access expression in foo().
const statement = ts.createExpressionStatement(
ts.createCall(ts.createIdentifier("foo"), undefined, undefined)
);
By the way, check out my site ts-ast-viewer. It will now show the compiler API code that will create the code in the editor.
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