Fetch function names from a file using tree-sitter-javascript.

huangapple go评论85阅读模式
英文:

Fetch function names from a file using tree-sitter-javascript

问题

Using the tree-sitter-javascript库,我正在尝试从JavaScript代码(以字符串格式)中提取所有函数名称,并将其存储在functionNames数组中。

  1. const Parser = require('tree-sitter');
  2. const JavaScript = require('tree-sitter-javascript');
  3. const parser = new Parser();
  4. parser.setLanguage(JavaScript);
  5. const sourceCode = `function foo() {
  6. console.log('hello world');
  7. }
  8. function bar() {
  9. console.log('bye world');
  10. }`;
  11. const tree = parser.parse(sourceCode);
  12. const functionNames = [];
  13. tree.walk((node) => {
  14. console.log("Node: ", node);
  15. if (node.type === 'function_declaration') {
  16. functionNames.push(node.child(1).text);
  17. }
  18. })
  19. console.log(functionNames);
  20. // 期望输出 ['foo', 'bar']
  21. // 实际输出 []

然而,tree.walk()的回调根本不执行。
日志中什么都不打印,只是一个空数组[]。

这段代码有什么问题?
我在官方文档(https://github.com/tree-sitter/tree-sitter-javascript)中找不到太多关于此的文档。
是否有其他方法可以实现这个目标?提前谢谢。

英文:

Using tree-sitter-javascript library, I'm trying to fetch all function names from a Javascript code (in string format) and storing it in a functionNames array.

  1. const Parser = require('tree-sitter');
  2. const JavaScript = require('tree-sitter-javascript');
  3. const parser = new Parser();
  4. parser.setLanguage(JavaScript);
  5. const sourceCode = `function foo() {
  6. console.log('hello world');
  7. }
  8. function bar() {
  9. console.log('bye world');
  10. }`;
  11. const tree = parser.parse(sourceCode);
  12. const functionNames = [];
  13. tree.walk(()=>{
  14. console.log("Node: ", node);
  15. if (node.type === 'function_declaration') {
  16. functionNames.push(node.child(1).text);
  17. }
  18. })
  19. console.log(functionNames);
  20. // Expected output ['foo', 'bar']
  21. // Actual output []

However, the callback to tree.walk() is not executing at all.
Nothing gets printed in the log. just an empty array []

What is wrong with this code ?
I couldn't find much documentation on this in the official docs (https://github.com/tree-sitter/tree-sitter-javascript)

Is there an alternate way to do this ? Thanks in advance

答案1

得分: 2

parser.parse已经给你解析好的结果。该对象上没有.walk()函数。要获取所有函数名称,你可以递归遍历rootNode

  1. const tree = parser.parse(sourceCode);
  2. const rootNode = tree.rootNode;
  3. const functionNames = [];
  4. function findFunctionNames(node) {
  5. if (node.type === 'function_declaration') {
  6. for (let child of node.namedChildren) {
  7. if (child.type === 'identifier') {
  8. functionNames.push(child.text);
  9. }
  10. }
  11. }
  12. for (let child of node.namedChildren) {
  13. findFunctionNames(child);
  14. }
  15. }
  16. findFunctionNames(rootNode);
  17. console.log(functionNames);
  18. // -> [ 'foo', 'bar' ]
英文:

That's because parser.parse already gives you the parsed result. There is no .walk() function on that object. To get all the function names you can traverse the rootNode recursively:

  1. const tree = parser.parse(sourceCode);
  2. const rootNode = tree.rootNode;
  3. const functionNames = [];
  4. function findFunctionNames(node) {
  5. if (node.type === 'function_declaration') {
  6. for (let child of node.namedChildren) {
  7. if (child.type === 'identifier') {
  8. functionNames.push(child.text);
  9. }
  10. }
  11. }
  12. for (let child of node.namedChildren) {
  13. findFunctionNames(child);
  14. }
  15. }
  16. findFunctionNames(rootNode);
  17. console.log(functionNames);
  18. // -> [ 'foo', 'bar' ]

huangapple
  • 本文由 发表于 2023年5月22日 18:19:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定