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

[question] How to query out all the properties and methods from a class?

Open
#223 2 comments 1 reaction 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

How to query out all the properties and methods from a class?

I tried to match a class for getting all the properties and methods:

const Parser = require("tree-sitter")
const TypeScript = require("tree-sitter-typescript").typescript

const { Query } = Parser 

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

/*
const query = new Query(
  TypeScript,
  `
    (class_declaration 
      name: (identifier) @class-name
      (class_heritage (extends_clause value: (identifier)))
      )
  `
);
*/

//(class_declaration name: (identifier) @class-name)
const query = new Query(
  TypeScript,
  `
    (class_declaration 
      name: (type_identifier) @class-name
      body: (class_body
        (method_definition
          name: (property_identifier) @the-method-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')
  }
  shout2() {
      console.log('bark bark')
  }
}
    `);

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

//console.log(matches[1].captures[0].node.text)  // Dog 

console.log(matches[1].captures)

but I only get one class and one method:

[
  {
    name: 'class-name',
    node: TypeIdentifierNode {
      type: type_identifier,
      startPosition: {row: 19, column: 6},
      endPosition: {row: 19, column: 9},
      childCount: 0,
    }
  },
  {
    name: 'the-method-name',
    node: PropertyIdentifierNode {
      type: property_identifier,
      startPosition: {row: 20, column: 2},
      endPosition: {row: 20, column: 7},
      childCount: 0,
    }
  }
]

How to get the class Dog's all properties and methods?

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

Review the query construction and query.matches(tree.rootNode) example in the issue, then consult the Tree-sitter query API and TypeScript grammar nodes. Done means documenting how to retrieve every property and method for Dog, including whether inherited members are included.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, typescript
Domain
tooling
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.