Back

Technologies:

javascriptjavascript
node.jsnode.js
avatar
Tolerim
12 days ago

Is it not functioning properly to retrieve all classes using ts-morph?

After cloning the opensource project from GitHub, I am attempting to extract all the classes from it. I imported the Project module from "ts-morph", created a project instance, and added source files using the project.addSourceFilesAtPaths() method. Using this method, I attempted to retrieve all the classes by calling the getClasses() method. After mapping the resulting list of classes to a dictionary of base and child class names, I attempted to log the dictionary to the console. However, this resulted in {}, indicating an empty object. I verified that my code was correct by changing the path to just "xflow-core//*.ts", which successfully printed out all the classes. I am unsure why the previous paths, "/Users/mark/githubrepo/XFlow/packages/xflow-core/src//*.ts" and "/Users/mark/githubrepo/XFlow/packages/xflow-core/**/*.ts[x]", did not work. I am certain that there is a class called MenuRegistry under the path "/Users/mark/github_repo/XFlow/packages/xflow-core/src/menu/menu-registry.ts". Here's an example of my code:

import { Project } from "ts-morph";

const project = new Project();
project.addSourceFilesAtPaths("xflow-core/**/*.ts");

// get all the classes
const classes = project.getSourceFiles().flatMap(sf => sf.getClasses());

// get the inheritance relations
const classInherits = classes.reduce((result, clazz) => {
  const superClass = clazz.getBaseClass();
  if (superClass) {
    const superClassName = superClass.getName();
    result[clazz.getName()] = superClassName;
  }
  return result;
}, {});

console.log(classInherits);
Answers(2)
avatar
Vogi
11 days ago
Verified Answer
It seems that you are trying to extract all the classes from an open-source project cloned from GitHub using the ts-morph module. You have added source files using the project.addSourceFilesAtPaths() method and attempted to retrieve all the classes by calling the getClasses() method. However, when you mapped the resulting list of classes to a dictionary of base and child class names and attempted to log the dictionary to the console, it resulted in {}, indicating an empty object. You verified that your code was correct by changing the path to just "xflow-core//*.ts", which successfully printed out all the classes. However, you are unsure why the previous paths, "/Users/mark/github_repo/XFlow/packages/xflow-core/src//*.ts" and "/Users/mark/github_repo/XFlow/packages/xflow-core/**/*.ts[x]", did not work. It is possible that there is a problem with your path syntax. The ** syntax is used to match any number of directories and subdirectories, while * is used to match any number of characters in a filename. The [x] syntax is used to match files with an extension of "x". It is possible that there are no files with an extension of "ts[x]" in the directories you specified. You mentioned that there is a class called MenuRegistry under the path "/Users/mark/github_repo/XFlow/packages/xflow-core/src/menu/menu-registry.ts". If you want to extract this class specifically, you can use the following code:
import { Project } from "ts-morph";

const project = new Project();
const sourceFile = project.addSourceFileAtPath("/Users/mark/github_repo/XFlow/packages/xflow-core/src/menu/menu-registry.ts");
const menuRegistryClass = sourceFile.getClass("MenuRegistry");

console.log(menuRegistryClass);
This code creates a new project instance and adds a source file at the specified path using the addSourceFileAtPath() method. It then retrieves the MenuRegistry class using the getClass() method and logs it to the console. I hope this helps! Let me know if you have any other questions.
avatar
Tolerim
12 days ago
The reason why the paths "/Users/mark/github_repo/XFlow/packages/xflow-core/src//*.ts" and "/Users/mark/github_repo/XFlow/packages/xflow-core//*.ts[x]" do not work may be due to the fact that they are not valid file paths. In the first path, the /**/*.ts at the end of the path may not be required as the *.ts should already include all .ts files in any subdirectory of the xflow-core/src directory. However, in the second path "/Users/mark/github_repo/XFlow/packages/xflow-core//*.ts[x]", the /*.ts[x] may not be a valid file pattern. It seems to be a combination of the file extensions .ts and .tsx which should be separate file patterns with a | separator in between. To extract all the classes from the project, you can try using the following modified code with separate file patterns for .ts and .tsx files:
import { Project } from "ts-morph";

const project = new Project();
project.addSourceFilesAtPaths("/Users/mark/github_repo/XFlow/packages/xflow-core/src/**/*.ts", "/Users/mark/github_repo/XFlow/packages/xflow-core/src/**/*.tsx");

// get all the classes
const classes = project.getSourceFiles().flatMap(sf => sf.getClasses());

// get the inherit relations
const classInherits = classes.reduce((result, clazz) => {
  const superClass = clazz.getBaseClass();
  if (superClass) {
    const superClassName = superClass.getName();
    result[clazz.getName()] = superClassName;
  }
  return result;
}, {});

console.log(classInherits);
Make sure to add the correct file patterns for the .ts and .tsx files within the addSourceFilesAtPaths() method.
;