Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to get the inheritance chain of an already obtained class? #218

Open
liaodalin19903 opened this issue Aug 16, 2024 · 0 comments

Comments

@liaodalin19903
Copy link

I can use node-tree-sitter to get a class node:

const Parser = require("tree-sitter")
const JavaScript = require("tree-sitter-javascript");



const { Query } = Parser 

//#region 【JavaScript】

const parser = new Parser()
parser.setLanguage(JavaScript)


// 1.  .matches 

const query = new Query(
  JavaScript,
  `
    (class_declaration name: (identifier) @class-name)
  `
);


const tree = parser.parse(`

class AnimalBase {
  readonly skeleton: number
  readonly blood: 'red' | 'blue' | 'transparent'
}

abstract class Animal extends AnimalBase {

  readonly age: number = 0
  abstract shout (): void  

}

class Cat extends Animal {
  shout() {
      console.log('mew mew')
  }
}

class Dog extends Animal {
  shout() {
      console.log('bark bark')
  }
}

  `);
const matches = query.matches(tree.rootNode);


// I can get a special class node
console.log(matches[3].captures[0].node.text)  // Dog 


//#endregion

but how to get the inheritance chain of the special class([Dog, Animal, AnimalBase])?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant