Skip to content
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

Introduce programTransformers option #879

Merged
merged 5 commits into from Sep 15, 2019
Merged

Introduce programTransformers option #879

merged 5 commits into from Sep 15, 2019

Conversation

saitonakamura
Copy link

@saitonakamura saitonakamura commented Sep 3, 2019

Here's the story, I have a transformer that can generate a runtime validation for json based on a generic type parameter. For that I need to have an access to a type checker, but vanilla transformers can't do that. I came across a solution in ts-loader: instead of passing an object literal, you pass a function that accepts a Program and should return that object with transformers. So I implemented the same api in ts-node considering the backwards compatibility (it's a brand new separate option). While implementing I came across chicken-egg problem cause LanguageServiceHost accepts transformers function, but you can only get an access to program once you create a LanguageService itself, so i'm kinda using the variable that will be initialized later. It's working, though it can be fragile with different typescript versions.

I'm also not sure about the name of the option programTransformers. It doens't sound like a perfect one, but i'm yet to find out the better alternative. @blakeembrey maybe you have some ideas?

Fixes #792

Saito Nakamura added 2 commits September 3, 2019 23:35
e.g. it will allow for a transformer to have an access to type checker
@coveralls
Copy link

coveralls commented Sep 3, 2019

Coverage Status

Coverage decreased (-1.2%) to 84.146% when pulling 616c481 on saitonakamura:feat/program-transformers into 6295254 on TypeStrong:master.

@saitonakamura
Copy link
Author

@blakeembrey sorry for bugging, what are your thoughts on this?

@blakeembrey
Copy link
Member

@saitonakamura This looks great, but how about we just make it a union signature of Transformers | (program) => Transformers? That's also backward compatible.

@saitonakamura
Copy link
Author

@blakeembrey yeah, totally, I don't know why I haven't thought about it in the first place, too much C# I guess 😃 . Thank you, I've done it and it helped to simplify the code!

@blakeembrey blakeembrey merged commit 12ff53d into TypeStrong:master Sep 15, 2019
@saitonakamura
Copy link
Author

Thanks for merging and refactoring it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[request] getCustomTransformer(program: ts.Program) just like as ts-loader's
3 participants