-
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
Expose public API for transformation. #13940
Conversation
src/compiler/comments.ts
Outdated
performance.mark("postEmitNodeWithSynthesizedComments"); | ||
} | ||
|
||
debugger; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove.
//CC @alexeagle, @chuckjaz, @mprobst, @rkirov and @evmar would like to get your feedback on this change. this should allow tools like https://github.com/angular/tsickle to inject a transom either before or after the other TS transforms are applied. |
Thanks for thinking of me! I intend to look at this but I have had a lot of other work recently. If you can't wait I also don't think it'd be too harmful to land this and let us poke at it in a release. I'm not sure you have a mechanism for clearly tagging "experimental" API though. |
Unordered observations from my interaction with the API. Apologies if some stem from my misuse or misunderstanding of the API:
|
@rkirov While I still have some work to do on the API documentation, here are a few pointers based on your bullet points:
|
Thanks @rbuckton, I created mini implementations of all the transformations we want to do in tsickle and they seem to work great. The final two questions:
and one observation, TS internal API are still not strictNullChecks compatible, I had to use a lot of |
There is an exported function ts.setEmitFlags(node, EmitFlags.NoLeadingComments);
There is an exported function // range is a TextRange, either an existing node or some other `{ pos, end }` value.
ts.setSourceMapRange(node, range); |
When you say "before all transformations have been done", do you mean before custom transformations are executed? I would like to include new source files in one of my custom transformers. |
Transforms, all of them, happen after the checking phase has happened. you can not add new files at this point. |
@mhegazy Did this ever get documented? |
We still have that on our todo list. you can find a lot of samples though in https://github.com/Microsoft/TypeScript/tree/master/src/compiler/transformers |
@EisenbergEffect right :) @mhegazy i had written lot of babel-compiler plugins for javascript and postcss but now im coding for a while (because of new company) with typescript and im missing these documentation for typescript. @mhegazy when will you guys start with the documentation? We really need documentation like these:
postcss: |
hello @mhegazy ,
any plan for this? |
API is still extremely difficult to figure out. Would appreciate a bit of documentation on this |
@mhegazy any feedback will be good! |
Sorry have not had chance to get back to adding the docs yet. #14419 tracks doing so. there are a multiple examples for what a transformation looks like in https://github.com/Microsoft/TypeScript/tree/master/src/compiler/transformers |
@mhegazy what i meant was: is Microsoft planning something like this: We really need documentation like these:
postcss: |
@SerkanSipahi We are not exposing these transforms on the commandline compiler (like babel does) at the time being, this proposal is tracked by #16607. |
Here there are three working examples of using transformation APII among other things: https://typescript-api-playground.glitch.me/. BTW you can modify the code and run it again, have intellisense, etc. ...like a TypeScript Compiler API Playground. hope it helps. |
@cancerberoSgx thank you. |
This PR contains the following changes:
TransformationContext
is now public:getEmitResolver
andgetEmitHost
are still marked/*@internal*/
.TransformationResult
is now public.Transformer
is now public.visitNode()
,visitNodes()
, andvisitEachChild()
are now public.visitEachChild()
does not recursively visitTypeNode
nodes, as they have so far not been necessary for emit transformations.visitLexicalEnvironment()
,visitParameterList()
, andvisitFunctionBody()
helpers are now public.customTransformers
parameter toProgram.emit()
, to run custom transformations before and after the main transformation pipeline.getCustomTransformers()
method toLanguageServiceHost
to allow implementers to supply custom transformations to be run before and after the main transformation pipeline.transform()
function in services that can be used to perform arbitrary transformations on source files.EmitResolver
andEmitHost
, they are not run as part oftransform()
, as it does not have access to these. If you require this use case consider usingProgram.emit()
with thecustomTransformers
parameter, or create aLanguageService
from a customLanguageServiceHost
that implements thegetCustomTransformers()
method.Fixes #13764
Fixes #13763