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

Feature Request: LanguageServiceHost plugin #29706

Open
5 tasks done
kitsonk opened this issue Feb 2, 2019 · 5 comments
Open
5 tasks done

Feature Request: LanguageServiceHost plugin #29706

kitsonk opened this issue Feb 2, 2019 · 5 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@kitsonk
Copy link
Contributor

kitsonk commented Feb 2, 2019

Search Terms

language service host compiler plugin

Refs #16607

Suggestion

Currently, TypeScript only supports proxying the language service. It would be good to have an officially supported way of being able to proxy the LanguageServiceHost as well.

Use Cases

Being able to provide a plugin that changes how the language service host behaves is often a requirement for use cases where the default behaviour of TypeScript in Visual Studio Code does not mirror the compile time behaviour when using tools that implement a language service host with TypeScript.

For example in Deno, we utilise full path names for modules, including TypeScript ones. The standard language service host disallows this (because it assumes that the module name won't end in .ts at runtime).

There are likely to be other use cases where the behaviour of the language service host differs when using other forms of TypeScript other than tsc and Visual Studio Code, where it would be useful to allow plugins to mimic that behaviour, in particular in Visual Studio Code, so that the editing experience can be more rich.

Examples

I currently have a plugin which just monkey patches the LanguageServiceHost to inject similar logic for module resolution to what is done in Deno. This is because I cannot return a proxy, like I could with the LanaguageService.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@afinch7
Copy link

afinch7 commented Feb 3, 2019

I second this proposal.
I've been working on dynamic module resolution for fly.rs. Similar to deno's http/https module loading, but I want to go a little further than just http/https. It's not difficult to do this on the server side with the heavily deno inspired code fly.rs uses, but I want integration in the editor as well. I've looked at the api for typescript language service plugins, and I think it might be possible to "replace" the LanguageServiceHost with your own. Here is an example, but something just feels wrong about doing it this way.

export function init(modules: { typescript: typeof ts_module }) {
    const ts = modules.typescript;
  
    function create(info: ts.server.PluginCreateInfo) {
        // Set up decorator
        const proxy: ts.LanguageService = Object.create(null);
        for (let k of Object.keys(info.languageService) as Array<
            keyof ts.LanguageService
        >) {
            info.project.projectService.logger.info(
                "I'm getting set up now! Check the log for this message."
            );
            const x = info.languageService[k];
            proxy[k] = (...args: Array<{}>) => x.apply(info.languageService, args);
        }

        info.languageServiceHost.resolveModuleNames = (moduleNames: string[], containingFile: string, reusedNames?: string[], redirectedReference?: ts_module.ResolvedProjectReference): ts_module.ResolvedModule[] => {
            const oldFunction = info.languageServiceHost.resolveModuleNames; // Hold onto old function handle for future use.
            // Do some custom module resolution.
        }

        return proxy;
    }
  
    return { create };
}

I'm also not even really sure if this would work anyways, but I figured it might be a solution until official support comes.

Edit: also support for async LanguageServiceHost functions would be nice.
Edit again: Somehow completely missed your example until now, nice work. I'm working on a PR for this feature, but I think it might require some heavy refactoring of the language service system.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 5, 2019
@fis-cz
Copy link

fis-cz commented Jan 27, 2020

Hello, is this still under discussion? Its almost year old. This feature will be handy as described above and as monkey patching does not work anymore it is not possible to do a custom module resolution.

@eddiecooro
Copy link

Hey there.
I'm trying to do something similar and faced the same issue...
@fis-cz Do you have any idea why monkey patching has stopped working??? couldn't find any change related to that.

@fis-cz
Copy link

fis-cz commented Jan 22, 2021 via email

@jindong-zhannng
Copy link

A note for those who come after, I learned one workable workaround here: https://github.com/mrmckeb/typescript-plugin-css-modules/blob/main/src/index.ts. In short, you can create a completely new language service instance with a customized host then return it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants