Is there any way to access existing visual studio type data or vscode extension outputs?

Viewed 26

I'm currently trying to write my own VSCode extension. The purpose of this extension is to compare types of HTML Custom Attributes and javascript code expressions. Now, it's completely possible to get the type of HTML attributes, there are NPM packages and as long as they're documented via JSDoc or have proper types using typescript - no problem.

but now look at the following example:

/** @param {MyPage} page */
export function renderSomePage(page)
{
 return html`
 <my-component .my-attribute="${page.SomeProperty}"></my-attribute>
 `
}

the question now is, how do I get the type of the expression ${page.SomeProperty}? This is the main question i want to solve with this question.

I have some ideas.

  1. visual studio does already know what type this expression is. Because when i hover over the parts of this expression, then i get a hover showing me the type. So ideally i want to receive some kind of Dictionary of tokens mapped to types that VSCode must have somewhere
  2. there's always the typescript/javascript language support plugin in VSCode that probably does all that work for the IDE. I'd need to get exactly those compiling/parsing results to get to the type of this expression

of course there are also corner cases, where it's not that simple. I also need to be able to evaluate expressions like:

  • (args) => result
  • identfiier.function()
  • primitive types (string, number, boolean)
  • arrays of complex types
  • anonymous objects
  • arrays of anonymous objects
1 Answers

You didn't mention anything about an LSP, which tells me that that is what your missing.

Bellow is in reference to idea #1 in the question:

The type information that you see when hovering is generated by the "TS LSP", which stands for...

"TypeScript Language Server Provider"


What you want to do is use a feature called "Request Forwarding" to access the information already present, which is provided by the VSCode LSP. I believe that the node Language server provides features for TypeScript, and always runs in the background. This is because VSCode runs in a Node RTE, and it has built-in features that are written in TypeScript. If you were writing a language like Go (or Go) per-say, then you would need to call to that specific language server.

You can use the "VS Code Language API", which makes requests to language servers. The API is written to work with language server that adheres wholey to the "Language Server Protocol". to access the "Programmatic LSP Features".

VSCode has a few examples that demonstrate how to use the LSP.

Really, you have two options, you can...

  1. ...parse TypeScript yourself, write grammars for the embedded mark-up, pull out all the type info, and write the type checking software you intend to write, or you can...

  2. ...use the already existing LSP to access the info, which includes requests for type info, and then write the type-checking software.

Hopefully that points you in the write direction.


Related