-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
How do you do a straight TS emit? #1651
Comments
What I meant is that you shouldn't use our trees as a direct target, you should simply find a way to transform FunScript code to TypeScript, or emit straight to JavaScript.
I'm not sure what you're trying to achieve. It depends what your input is. If your input is TypeScript, then yes, we have a formatter.
Yes, but you'll need an actual TypeScript source file as input. As part of our services API, we provide the following functions:
I think what you might be looking for |
For this issue, I'm just interested in emitting TypeScript code using Node.js & TypeScript. function formatDocument(sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[]; It looks like you can pass in a |
Why do you need that function? |
Is there any easy way to apply the https://github.com/ctaggart/TsAst/blob/format/app.ts#L67-L76 var sf = services.getSourceFile("file1.ts");
textChanges.forEach(tc => {
var tcr: ts.TextChangeRange = {
span: tc.span,
newLength: tc.newText.length,
}
var b = sf.update(tc.newText, tcr);
console.log('b.text: ' + b.text);
});
console.log('sf.text: ' + sf.text); |
The most simple way is just to apply the changes in reverse: function formatCode(orig: string, changes: TextChange[]): string {
var result = orig;
for (var i = changes.length - 1; i >= 0; i--) {
var change = changes[i];
var head = result.slice(0, change.span.start);
var tail = result.slice(change.span.start + change.span.length)
result = head + change.newText + tail;
}
return result;
} Let me know if you have any more questions. PS: I thought about it a bit more and I can see why you probably wanted to use |
Thanks! Formatting works now. My original goal was to be able to create the nodes from scratch and have a function that writes out the TypeScript source to a string. I reviewed src/compiler/emitter.fs and src/services/formatting/formatting.ts yesterday and witnessed what was mentioned:
Indeed, there are a few calls to |
Cleaned up for the formatting code and put the example here. :) |
thanks @ctaggart for sharing. i have left a few comments. i am also working on cleaning up the host API so it can be easier to use. your feedback would be highly appreciated! |
@mhegazy I made the corrections that you recommended. I also have a new blog post that shows how to call the |
Hey, very cool! We appreciate you working on this and sharing it with us. Completely unrelated, I realize you could write TypeScript code in your Edge app and use the compiler at runtime to generate the appropriate JavaScript. Huh. |
Thank you guys! I really appreciate the move to GitHub. It is fun watching how quickly things progress on this project. The Compiler API is a great addition. My immediate need is to be able to generate TypeScript code for web clients when there is a defined web API contract (not necessary .NET Web API). I've created such clients in the past using Yes, there are several very cool possibilities when combining F# + Edge.js + TypeScript.
/cc @tjanczuk @alfonsogarciacaro Hopefully this thread won't get too off track. :) The goal of this thread being how to emit or pretty print |
@ctaggart would you feel comfortable with us adapting your pretty printer for use on Using the Compiler API? Of course, we'd give attribution and link to your blog post as the original source. |
Sure, feel free to adapt stuff from my blog. I don't have anything on pretty printing yet. My last blog was about using the Compiler API to do formatting: |
I want to be able to programmatically create a TS file and emit it.
@DanielRosenwasser mentioned last week:
How do do a
straight TS emit
?In the Using the Compiler API it says:
That is cool that you can emit a
.d.ts
definition. Is there any way to emit a.ts
file? Is there anything that can "pretty print" a TS AST?The text was updated successfully, but these errors were encountered: