-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to emit TypeScript from syntax tree #10786
Comments
Depending on what you're doing, https://github.com/RyanCavanaugh/dts-dom may be a useful alternative |
@ctaggart hey there, you seem to have a pretty good grasp on the whole emitting functionality. I was just curious if you could tell me what exactly I need to watch out for in order to be able to read a (background: I would love to be able to build the TypeScript version of my Babel plugin https://github.com/stephanos/babel-plugin-immutable-record) |
@stephanos, it has been possible to do everything you are asking by using the compiler api for a while, except emit the .ts file. If it is indeed possible now, I don't know how to do it. I too would like it to be possible, which is why I opened this issue. I recommend adding a 👍 above if you would like to see support added. |
@ctaggart thanks for your reply :) I'm now focusing on emitting |
Would you accept a pull request on this? I'd propose a new function |
I played a bit with it, and I created PR #11561. |
We really need something like babel-types & babel-generator Sometimes, we need to generate code by specs like json-schema, swagger. There is a cool tool TSTypeInfo to do this. However, Hope |
Now that TypeScript 2.1 has been released where ya'll are now rewriting the emit pipeline to use tree transforms, is this something that may be enabled soon. I brought up this issue at both @DanielRosenwasser's presentation and a Q&A with @ahejlsberg at the Microsoft MVP conference in November. thanks, |
Hey @ctaggart! #11561 was the most recent PR regarding this, but we want to do some cleanup on the API. See @mhegazy's comment at #11561 (comment). TL;DR: pretty-printing a synthesized tree is planned for the 2.2 timeframe. |
Yes, and it'll be in the nightlies in about an hour or two! 🎉 Caveat: Forgive me for my last comment, as it's now slated for TypeScript 2.3. There is still some stuff you can still use in 2.2 - having spoken with @rbuckton earlier tonight, if I'm not mistaken, the factory (#13825) and printer (#13761) will be in 2.2... which I think you should actually be all you need! So let us know how that works out. 😄 |
Great progress. I love TypeScript and community! |
Based on the comments it seems like generating TypeScript code with TypeScript 2.2 is to some extent possible? If so, could you please share some tips on how to do that? |
@alexarg As far as I know it will be in TypeScript 2.3 and is available from the nightly builds. Generally you can use the printer to generate TypeScript code from a SourceFile, a bundle of source files or a single Node. This is a very basic example on how to transform TypeScript code and generate TypeScript again after that. I've typed everything, just that it's easier to follow what's going on. const source: string = `function logString(param: string): void {
console.log(param);
}`;
const sourceFile: ts.SourceFile = ts.createSourceFile(
'test.ts', source, ts.ScriptTarget.ES2015, true, ts.ScriptKind.TS
);
// Options may be passed to transform
const result: ts.TransformationResult<ts.SourceFile> = ts.transform(
sourceFile, [ yourTransformer ]
);
const transformedSourceFile: ts.SourceFile = result.transformed[0];
// Options may be passed to createPrinter
const printer: ts.Printer = ts.createPrinter();
const generated: string = printer.printFile(transformedSourceFile);
result.dispose(); Probably you may also want to check #13940 where you can find some more examples and explanations. |
I used this technique to make a transformer module: |
This is fixed now. Before we mark this closed, which version shipped with Creating and Printing a TypeScript AST working? |
Now that #5595 has landed, it has me excited that it may soon be possible to emit TypeScript. Is it now possible with the 2.1 nightly? If not, please consider this a feature request. It would open the door for a lot of cool code generation.
Related issues:
The text was updated successfully, but these errors were encountered: