Extracting the variable value from a TypeScript file using a VS Code extension

Currently, I'm developing a VS Code extension that focuses on parsing OpenAPI specifications. These specifications are represented as a variable with a predetermined name within a TypeScript file.

Within my VS Code extension, I retrieve the document as a string:

const doc = vscode.window.activeTextEditor?.document;

I am trying to figure out the best way to extract the value of the variable from this string. I considered using an Abstract Syntax Tree (AST), but perhaps there is a more straightforward solution available.

Answer №1

Unfortunately, VS Code does not come equipped with an AST or a similar API that relates to the document you have open. This means that if you require something like an AST for parsing a document, you will need to handle it within your extension.

However, on the bright side, VS Code offers the Go to Symbol command, which depends on each language extension to reveal the symbols within a document. Specifically, the variables symbol is accessible for the TypeScript language. Therefore, if you are looking to identify the variables within a document and their locations, you can utilize the

vscode.executeDocumentSymbolProvider
command.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering the quantity of items with a specific value in Angular 8

I'm attempting to determine the number of objects with a status value of 'Served', which should yield 2. I'm unsure about the method I should use to achieve this. Any suggestions on which method would be best? {full_name: 'Jenny&a ...

Zoom in and out on a WebGL canvas created using Three.js using the mouse scroll wheel

Currently delving into the world of WebGL (Three.js) to explore the possibilities of rendering 3D scenes within an Angular app. One challenge I'm facing is finding a way to make the mousewheel events zoom in and out of the canvas specifically, rather ...

String defines the type

I came across the following code snippet in some external sources that I intend to incorporate into my project: const INIT: 'jsonforms/INIT' = 'jsonforms/INIT' Can someone explain what it means to define a type with a string like INIT ...

Importing a member into a nested name may be achievable

Although I've been utilizing rxjs with Typescript by importing it as follows: import * as rx from 'rxjs/Rx'; var stream: rx.Subject<boolean> = new rx.Subject<boolean>(); While this approach works, I am now interested in implem ...

Having difficulty resolving all parameters for the component: (?, [object Object]) in the Jasmine component Unit Test

While defining a UT for a component with an extended class using i8nService and ChangeDetectionRef, I encountered an error preventing me from instantiating it: Failed: Can't resolve all parameters for BrandingMultiselectComponent: (?, [object Object] ...

What is the process for configuring ng e2e to run on a specific port?

Ever since moving to Angular 6, I've noticed that ng e2e is no longer able to run on an available port like it used to. In the past, when we were using Angular 4, the app service would be on port 4200 and running ng e2e would automatically select a fr ...

What's the deal with TypeScript tsconfig allowing .json imports, but not allowing them in built .js files?

By including the line "resolveJsonModule": true in my project's .tsconfig file, I have successfully implemented direct importing of data from .json files. The project functions properly, even when using nodemon. However, upon building the project and ...

Enhance object position by utilizing Dragula and Angular 5 through dropping action

I have a bag divided into different sections, each containing items with a specific location value. For example, the "Misc" section has items with "location: misc," the "Armor" section has items with "location: armor," and so on. While I can sort the item ...

Executing a function from within a promise in JavaScript

Understanding the Code In this code snippet, there is a function named placeDecode that takes an HTML input element as a parameter. Inside this function, there is a promise used to convert the input value into a formatted address. The function placeDecod ...

Store the filereader output in a variable for future reference

I am struggling to find a simple answer, even though my code is simple. I attempted the following approach, but every time I try to console.log my testResult, it always returns null. How can I correctly save data from a file? public getFile( sourceFile ...

When applying custom style rules to HTML and body tags using React JSS, the @global directive does not take effect

I've been trying to add custom style rules to all elements, html, and body tags using React JSS in a TypeScript library. However, when inspecting the component in Storybook, the '@global' rules are not present. Below is the relevant code sn ...

Last online activity on a website

I need to record the exact time when a user last interacted with a website - whether they clicked the mouse, typed on the keyboard, or moved the cursor. By capturing this timestamp, I can calculate the remaining duration until the Web Token expires. ...

Creating a data structure that filters out specific classes when returning an object

Consider the following scenario: class MyClass {} class MyOtherClass { something!: number; } type HasClasses = { foo: MyClass; bar: string; doo: MyClass; coo: {x: string;}; boo: MyOtherClass; }; type RemovedClasses = RemoveClassTypes& ...

Container filled with AGM Maps

Currently, I am working with angular2 and the Google Maps component from . The issue I am trying to address is the need for the map to fill my container div. For example: <div id="container" style="height:100%; width: 100%"> <agm-map></agm- ...

What is the best way to transfer an object from the view to the controller in AngularJS and ASP.net MVC

How to pass an object with a specific amount of data from the View to the Controller using ASP.net MVC and AngularJS VIEW var Person = {}; Person.IdPerson = 69425; Person.Year = new Date().getFullYear(); $http.post('/API/Update_Person', { para ...

Is there a way to transfer the node_module folder to a different drive using npm without duplicating the API multiple times?

I have come across a problem in my workspace where Node projects are utilizing various version dependencies, resulting in duplicate copies of the same node module. This has made it extremely difficult to locate my files and is causing excessive storage c ...

Issue with Angular 7 ngZone causing undefined error

I've been struggling to display a 3D object using three.js. Every time I attempt to start the animation loop (within ngAfterViewInit), I keep encountering the same error: TypeError: Cannot read property 'ngZone' of undefined In an effort t ...

Custom type checker that validates whether all properties of a generic object are not null or undefined

In an attempt to create a user-defined type guard function for a specific use-case, I am faced with a challenge: There are over 100 TypeScript functions, each requiring an options object. These functions utilize only certain properties from the object wh ...

Enabling the use of an extensive interface when passing a function as a parameter in React with Typescript

While working with React, I am facing a challenge with passing multiple functions as props and encountering an issue when using extended interfaces as parameter types for a function in a "standard" component. I have tried typing the component itself with g ...

Refresh an array prior to subscribing in Angular 2

When attempting to apply multiple filters in a quick session, I am encountering an issue where the previous data persists in the array alongside the new data. How can I effectively remove the previous data? component.ts ngOnInit() { this.vehicleAttribu ...