cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json
1.53k stars 56 forks source link

tsserver Support. #107

Closed Griffork closed 2 years ago

Griffork commented 3 years ago

Hello! I'm looking at making a custom visual studio SDK (following this post). I thought it would be easy because ttypescript defines a .tsc and a .tsserver, however upon inspection I realised that the tsserver is actually unmodified.

Would it be possible to get tsserver to be modified to use the transforms? I know nothing about what this entails, but I'm guessing it should be similar to the (already existing) -watch support?

Griffork commented 3 years ago

Oh fantastic! Thank you!

TheMrZZ commented 2 years ago

Oh fantastic! Thank you!

@Griffork I need to know, does it work? ttsserver still looks unmodified, and I can't see who you answered to.

nonara commented 2 years ago

I deleted my reply, which mentioned that I added support to ts-patch. Unfortunately, it turns out tsserver has issues if modified the same way. On closer inspection, it also actually doesn't make sense to approach it the same way, if targeting the language server is your goal.

Are you trying to make transformation affect the language server or is there another reason you want to patch tsserver?

Update

We have a PR finished that adds support for tsserver + tsserverlibrary

TheMrZZ commented 2 years ago

I am trying to make transformations affect the language server. Basically, I want to create an operator overloading proof of concept, but this cannot work if there isn't proper IDE support. I don't want to write a full custom language server if I can just change the normal TS one.

nonara commented 2 years ago

Got it! The issue is that the LS works differently. It doesn't really support transformations, and it would actually not be a great idea to add the support, as the LS's job is to work with the code, as it exists.

That said, you can still probably accomplish what you're looking to do. In the future, I hope to make this a little easier, but right now what you can do is create a separate Language Server plugin. From there, you can cause it to drop the errors that would result. That would probably be sufficient for a proof of concept. You can bundle both the LS plugin and the transformers in the same package, but they will need separate entries in plugins. You could also have them in the same file, as long as you use the import property for the transformer entry.

A more thorough solution for operator overloading would likely require modification to the compiler, which is also something I hope to make easier in the next major iteration. Hopefully that helps, in the mean time. Good luck with it!

TheMrZZ commented 2 years ago

If I'm not mistaken, creating a Language Server plugin means I have to create my own extension, right? Or does the TS LS have a native capacity to load specific plugins?

And if you have a link to the documentation on creating said plugins, I'd love to have it!

Anyway, thanks for your hard work and your quick answers.

nonara commented 2 years ago

If I'm not mistaken, creating a Language Server plugin means I have to create my own extension, right?

Typescript natively supports what it calls "plugins". Specifically, the support is for language service plugins. That is actually what the tsconfig plugins property is for! We just extend its use slightly in ts-patch and ttypescript to support transformers as well.

Writing a plugin is similar to writing a transformer. It just requires a little background knowledge on how to interact with the language service API. A good place to start is googling "writing a language service plugin for typescript". Microsoft has some official documentation and examples which are helpful, and there are also a handful of useful blogs.

When you're all finished, you simply have your library require two entries in tsconfig's plugins. One for your language service and one for the transformer.

Anyway, thanks for your hard work and your quick answers.

Happy to help! Would love to see the final result if it ends up being public.

WalterWoshid commented 1 year ago

I tried to create a Language Service Plugin to extend the IDE's type checking and error capabilities, but soon found out that it's not possible to change internal methods. Now I found this beautiful library, but it doesn't support transformers in the tsserver, which is a bummer...

nonara commented 1 year ago

@WalterWoshid For that, you would use a Language Service plugin rather than a transformer. Good luck!

WalterWoshid commented 1 year ago

@nonara Sadly the language plugin doesn't hook into the type checking, so both tools are not an option.

nonara commented 1 year ago

@WalterWoshid Not sure what you're trying to do, but if you are trying to add or remove Diagnostics in the language service, you can accomplish that with a LS plugin.

If that isn't it, could you describe your use case in some more detail?

WalterWoshid commented 1 year ago

@nonara Here you go: https://github.com/microsoft/TypeScript/issues/51493#issue-1446044201

nonara commented 1 year ago

Ah, interesting! Unofficially, ts-patch supports tsserver, but I believe there are crashes. I recently learned that one user still uses it in an API fashion.

What may work, if you have the time and motivation, is to look at tsserver with ts-patch and see if you can work out why it crashes. If you can figure it out, I'd be happy to add support.

We've had a few requests, but there's rarely an actual use case of anyone who needs to use tsserver.

WalterWoshid commented 1 year ago

Thanks for the answer! I'll look over it. Seems easier than extending the typescript source code.

WalterWoshid commented 1 year ago

@nonara Would you mind sending me the link to that user or some kind of guidance in the right direction?

I am using Typescript 4.7, ttypescript 1.5, ts-patch 2.0, patched all the files and it is working for ttsc and tsc, but whenever I start the server with JetBrains Webstorm or VSCode it just runs the default server (changed the TypeScript SDK). No errors or crashes and the transformer plugin is not being executed. Is there something I'm missing?

nonara commented 1 year ago

@WalterWoshid ttypescript isn't needed if you use ts-patch. I've dealt with Webstorm to some degree. First, you need to be sure you configure it to use the specific version in your packages node_modules.

I don't remember which library it uses, but I think it's tsserverlibrary. You'll need to find out which and manually make tsp patch the library.

IIRC webstorm performs a bit of its own modification while loading the library. I was able to get it to load after some trial and error, but I believe it did still crash.

Debugging was difficult also. You might have better luck debugging it with vscode. You could probably use the vscode source code to debug and step through the while process while it loads the typechecker, which would help you see what is going on.

WalterWoshid commented 1 year ago

@nonara Oh, I see. Thanks for the information!

Small tip for debugging: Easy-Attach You're gonna love it, if you ever need to debug something where you can modify the source code, but can't control what application started it for debugging :)