Giter Club home page Giter Club logo

tsserver's Introduction

Tsserver wrapper

A lite wrapper around tsserver

tsserver

Total Downloads Latest Release License

Install

yarn add tsserver-lite

Example

For example, your project structure:

├── src
│  └── test.ts
└──── xxx
// src/test.ts
export const number = 123;
export const string = 'abc';

How to use:

// index.mjs
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';

import { client } from 'tsserver-lite';

const updateTSFile = async (info) => {
  const fileInfo = Object.assign(
    {
      changedFiles: [],
      closedFiles: [],
      openFiles: [],
    },
    info,
  );

  await client.execute('updateOpen', fileInfo);
};

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const filePath = path.resolve(__dirname, './test.ts');

const main = async () => {
  client.startService();

  const openFile = {
    file: filePath,
    fileContent: fs.readFileSync(filePath, 'utf-8'),
    projectRootPath: path.resolve(__dirname, '../'),
    scriptKindName: 'ts',
  };

  try {
    await updateTSFile({
      openFiles: [openFile]
    });

    const result = await client.execute(
      'quickinfo',
      {
        file: filePath,
        line: 1,
        offset: 15,
      },
    );
    if (result.type === 'response') {
      console.log('result---', result);
    }
  } catch (err) {
    console.log(`tsserver error: ${err.message}`);
  }

  client.closeService();
};

main().catch(err => {
  console.error(err);
  process.exit(1);
});

Try run script:

node index.mjs

You will see these logs:

result: {
  seq: 0,
  type: 'response',
  command: 'quickinfo',
  request_seq: 3,
  success: true,
  body: {
    kind: 'const',
    kindModifiers: 'export',
    start: { line: 1, offset: 14 },
    end: { line: 1, offset: 20 },
    displayString: 'const number: 123',
    documentation: '',
    tags: []
  }
}

tsserver's People

Contributors

shiyangzhaoa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.