npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@manuth/typescript-languageservice-tester

v5.0.2

Published

Provides components for testing the typescript language-server and language-services.

Downloads

137

Readme

TypeScriptLanguageServiceTester

Provides components for testing the typescript language-server and language-services.

status-badge

Installing the package

This package can be added to your dependencies by invoking:

npm i -D @manuth/typescript-languageservice-tester

LanguageServiceTester

The LanguageServiceTester allows you to analyze code for ensuring all expected diagnostics are being delivered.

A LanguageServiceTester can be initialized by passing a directory to run the tsserver in:

import { LanguageServiceTester } from "@manuth/typescript-languageservice-tester";

let tester = new LanguageServiceTester("/tmp/test-workspace");

Installing dependencies

If typescript is not installed in your workspace, you can install it using the LanguageServiceTester.Install method:

await tester.Install();

Live-Updating Plugins

You can perform a live-update of plugin-configurations by invoking the LanguageServiceTester.ConfigurePlugin by passing the plugin-name and the configuration to apply at runtime:

await tester.ConfigurePlugin("typescript-eslint-language-service", { watchDirs: "." });

Analyzing Diagnostics

The LanguageServiceTester.AnalyzeCode-method allows you to analyze a code and getting all diagnostics for the specified code.

You can optionally pass a script-kind (such as TS, JSX etc.) and a file-name to be used. Otherwise the code is assumed to be TS and a generic file-name is used.

import { writeFile } from "fs-extra";

await writeFile(
    tester.MakePath("tsconfig.json"),
    {
        compilerOptions: {
            plugins: [
                {
                    name: "typescript-eslint-language-service"
                }
            ]
        }
    });

let diagnosticResponse = await AnalyzeCode("let x;;;");

Getting Code-Fixes

You can then either get all code-fixes for the analyzed code or code-fixes for a specific diagnostic:

await diagnosticResponse.GetCodeFixes();
await diagnosticResponse.Diagnostics[0].GetCodeFixes();

Running and controlling a TSServer

You can run an instance of typescript/lib/tsserver by creating a new instance of the TSServer-class. The TSServer class allows you to communicate with the tsserver by sending requests and awaiting answers or events:

let tsServer = new TSServer("/tmp/test-workspace");

Sending Requests

The TSServer.Send-method allows you to send requests to the tsserver easily:

import { server } from "typescript/lib/tsserverlibrary";

await tsServer.Send(
    {
        type: "request",
        command: server.protocol.CommandTypes.ReloadProjects
    },
    false);

The second argument allows you to specify whether a response is expected.

Some requests such as the SemanticDiagnosticsSyncRequest return a response. In that case you can pass true as the second argument which will cause the Send-method to resolve with the Response to the request.

import { join } from "upath";
import { server } from "typescript/lib/tsserverlibrary";

let response = await tsServer.Send<server.protocol.SemanticDiagnosticsSyncRequest>(
    {
        type: "request",
        command: server.protocol.CommandTypes.SemanticDiagnosticsSync,
        arguments: {
            file: "/tmp/test-workspace/example.js",
            includeLinePosition: false
        }
    });

console.log(response.body); // Logs the diagnostics which have been found

Waiting for Events

The WaitEvent method allows you to wait for an event to be emitted:

await tsServer.WaitEvent("typingsInstallerPid");

Disposing the TSServer

By disposing the TSServer, the underlying tsserver-process is closed by sending an ExitRequest and closing the stdin-stream.

await tsServer.Dispose();

Working with Workspaces

In some cases you might want to analyze code from a different workspace-folder to see how typescript is acting if files from a foreign project are being opened.

You can test the behaviour by creating temporary workspaces. Workspaces provide the same features like the LanguageServiceTester.

let workspace = await tester.CreateTemporaryWorkspace();
await workspace.Install();
await workspace.AnalyzeCode("let x;;;", "JSX");