I want to get all classNames, I think as long as I match all the class node, I could get the classname,
so I tried below code:
const Parser = require("tree-sitter")
const JavaScript = require("tree-sitter-javascript")
const { Query } = Parser
const parser = new Parser()
parser.setLanguage(JavaScript)
const query = new Query(
JavaScript,
`
(class_declaration name: (identifier) @class-name)
`
);
const tree = parser.parse(`
class Person {}
const TestPerson = class {}
class Person2 {}
const TestPerson2 = class {}
`);
const matches = query.matches(tree.rootNode);
matches.forEach(item => {
console.log(item.captures[0])
})
When I execute it:
$ node query-test.js
{
name: 'class-name',
node: IdentifierNode {
type: identifier,
startPosition: {row: 2, column: 6},
endPosition: {row: 2, column: 12},
childCount: 0,
}
}
{
name: 'class-name',
node: IdentifierNode {
type: identifier,
startPosition: {row: 8, column: 6},
endPosition: {row: 8, column: 13},
childCount: 0,
}
}
there is no classname ['Person', 'Person2']
,
please tell me how to get the class names.