tree-sitter / tree-sitter/node-tree-sitter

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

Open
#218 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
876
Forks
172
PR merge metrics
No merged PRs in 30d

Description

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])?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Query, matches result, and captured class node shown in the issue. Determine whether the node-tree-sitter API already exposes enough information to traverse the demonstrated class declarations; done would be a documented, supported way to obtain the [Dog, Animal, AnimalBase] chain.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.