How can one locate exclusively unused JSDoc type definitions?

For instance:

/**
 * A boolean value.
 * @typedef {boolean} BoolType
 *
 * Another dummy type definition.
 * @typedef {(number|string)} SomeType
 */

/**
 * Set the boolean flag.
 * @param {BoolType} flag - The bool flag value.
 */
function setFlag(flag) {
}

In this code snippet, we have defined BoolType for boolean values, but the SomeType type is not used anywhere in our project. I am interested in identifying and removing such unused type definitions systematically. Our project is based on Node.js with TypeScript integration.

Below is our configuration file named tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "jsx": "react",
    "allowJs": true,
    "checkJs": true,
    "target": "ESNext",
    "noEmit": true,
    "moduleResolution": "node",
    "isolatedModules": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "strictNullChecks": true,
  },
}

Answer №1

While I may not be well-versed in AST tools, which are typically used for locating certain information, I can provide an example to help illustrate a concept:

Imagine you have a JavaScript file named ./module.mjs. Now, consider another module called ./find-typedefs.mjs:

import {promises as fs} from 'fs';

function getTypedefNames (sourceText) {
  const regex = /@typedef\s+{[^}]+}\s+[A-Za-z]+/g;
  return [...new Set((sourceText.match(regex) ?? [])
    .map(str => str.split(/\s+/).slice(-1)[0]))];
}

async function main () {
  const [filePath] = process.argv.slice(2)
  const text = await fs.readFile(filePath, {encoding: 'utf8'});
  const names = getTypedefNames(text);
  console.log(names.join('\n'));
}

main();

Executing this file will produce the following output:

$ node find-typedefs.mjs module.mjs
NumberLike
StringLike

By utilizing the getTypedefNames function and potentially building upon it with additional abstractions, you could automate the process of searching through your project files to generate a list of names that can be further examined or counted within each file. If you're using an editor such as VS Code, you can even use regular expressions (similar to the one in the function) directly within the Find menu.

Hopefully, this method will help streamline the task of manually scanning through code.

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

Issue with importing a file using JQuery causing the click event to not function properly

I have a file named navigation.html located in a directory called IMPORTS <!-- Navigation --> <div class="navbar-inner"> <div class="container"> <button type="button" class="btn btn-navbar" data-toggle="collapse" data-ta ...

Issues with appending new rows using JavaScript

I'm facing an issue with adding rows to the table using this code and I can't seem to find a solution. function add() { document.getElementById("popup").style.display = "block"; document.getElementById("add").addEventListener("click", func ...

Steps for importing jQuery typings into TypeScript:1. First, install the jQuery

I've searched for similar questions, but haven't found one that matches my issue. Can someone help me figure out what to do next? In my Visual Studio project, I used package.json to download jquery typings into the node_modules folder: { "ver ...

Issues with jQuery AJAX, occasionally experiencing repeated requests

Currently, I am in the final stages of revamping our JavaScript system by transitioning from Prototype to jQuery. We have numerous AJAX requests that are triggered when specific events occur on certain elements. One instance of this is when a new event is ...

Leveraging the power of a JavaScript framework within the script tag

Hello there! I'm just starting out with vue.js and exploring different JavaScript frameworks. Recently, I came across an interesting example based on the documentation. import modal from 'vue-semantic-modal' export default { components ...

Is it better to store CSS and JavaScript in separate files or within the main HTML document

While the best practice is to place JavaScript and CSS code in separate files (such as .js and .css), many popular websites like Amazon, Facebook, etc. often include a large portion of their JavaScript and CSS code directly within the main HTML page. Whic ...

store user settings in local storage

After writing some code with a link that toggles text visibility upon click, I now want to incorporate saving this state in web storage so that it persists upon page reload. As a beginner in JavaScript and HTML, this task has proven challenging for me. Th ...

What is the procedure for transferring the inputted data from an HTML file to its corresponding TS file and subsequently to a different component file?

I have created two components, a login and a home-page. I am attempting to capture user input from the login template, pass it to the login component, and then display it on the home-page template using the home-page component. What is the best approach to ...

What is causing my tooltips to flicker in Firefox but not in Chrome?

When I tried viewing the provided fiddle in Firefox (version 5.0.1 on a Mac), I noticed that when hovering over a day in the calendar and placing the mouse pointer inside the tooltip, the tooltip would flash on and off. Interestingly, this issue did not oc ...

What is the best way to ensure all keys of a certain type are mandatory, while still allowing for the possibility of

I am looking to create a mapping of key/value pairs for a specific type in the following way: required_key: string | undefined transformed to required_key: string | undefined (remains the same) required_key: string transformed to required_key: string (rem ...

Google Maps is experiencing difficulties maintaining its longitude and latitude coordinates within the Bootstrap tabbed user interface

I implemented ACF's Google Map to display a map on my webpage. I followed the instructions closely and made some minor modifications to the map js for styling purposes. One key change I had to make was in this section to ensure the map loads correctly ...

Display a separate component within a primary component upon clicking a button

Looking to display data from a placeholder module upon component click. As a beginner with React, my attempts have been unsuccessful so far. I have a component that lists some information for each element in the module as a list, and I would like to be ab ...

Managing various situations using observables

Currently facing a coding dilemma: I have an observable that I need to subscribe to and certain requirements must be met: 1 - The registerUser function should only execute after processing the callback data. 2 - If registerTask returns data, I receive an ...

The page you are trying to access does not support the HTTP request method 'GET'. Please try a different method to access the page

I have been attempting to incorporate AJAX for asynchronous communication with the server. However, I keep encountering the following error message. Any insights on how to resolve this issue? org.springframework.web.servlet.PageNotFound.handleHttpReques ...

What are the potential drawbacks of combining the useState hook with Context API in React.js?

Within my code, I establish a context and a provider in the following manner. Utilizing useState() within the provider enables me to manage state while also implementing functions passed as an object to easily destructure elements needed in child component ...

Buttons and JavaScript: A Comprehensive Compilation

I'm facing an issue with my HTML buttons and JavaScript. When a button is clicked, an AJAX call should be triggered to display data. However, JavaScript seems unable to detect the button. Below is the HTML code: <ul> <li><input ...

Hover over an element to change its text using jQuery JS

Whenever I hover over the entire DIV kontakt-block, I want the text in the span TEXT-DISPLAY to be altered. It's easy for me to do this when there is only one kontakt-block. However, I face a challenge when it comes to dealing with classes. In each sp ...

Hide the MaterialTopTabNavigator from view

Is there a way to hide the react native MaterialTopTabNavigator on the screen while still maintaining the swiping functionality between screens? I want the user to be able to swipe between screens but not see the tab navigator itself. const Tab = creat ...

Prevent typing in text box when drawer is activated by pressing a button

update 1 : After removing unnecessary files, I need assistance with https://codesandbox.io/s/0pk0z5prqn I am attempting to disable a textbox. When clicking the advanced sports search button, a drawer opens where I want to display a textbox. The toggleDra ...

Utilize JavaScript to deliver a message to a mobile device

I'm a beginner in using JavaScript and I would like to send a message to a mobile device using JavaScript. In PHP, this task can be accomplished with the following code: $number = 'your-number'; $url = "http://pointsms.in/API/sms.php?user ...