Skip to main content

getAstTreeInFile

codebolt.codeparsers.getAstTreeInFile(file: string, className: string): object
Generates an Abstract Syntax Tree (AST) for a given file.

Parameters

NameTypeDescription
filestringThe file path to generate an AST for.
classNamestring(Optional) The name of the class to focus the AST generation on. If not provided, returns the full file AST.

Examples

The getAstTreeInFile function generates Abstract Syntax Trees for various programming languages. The output structure varies by language but consistently includes type, text, startPosition, endPosition, and children properties.

JavaScript Class AST

const astResult = await codebolt.codeparsers.getAstTreeInFile('path/to/file.js', 'Calculator');

Output:

{
"type": "class_declaration",
"text": "class Calculator {\n add(a, b) {\n return a + b;\n }\n \n multiply(a, b) {\n return a * b;\n }\n}",
"startPosition": { "row": 5, "column": 16 },
"endPosition": { "row": 13, "column": 17 },
"children": [
{
"type": "class",
"text": "class",
"startPosition": { "row": 5, "column": 16 },
"endPosition": { "row": 5, "column": 21 },
"children": []
},
{
"type": "identifier",
"text": "Calculator",
"startPosition": { "row": 5, "column": 22 },
"endPosition": { "row": 5, "column": 32 },
"children": []
},
{
"type": "class_body",
"text": "{\n add(a, b) {\n return a + b;\n }\n \n multiply(a, b) {\n return a * b;\n }\n}",
"startPosition": { "row": 5, "column": 33 },
"endPosition": { "row": 13, "column": 17 },
"children": [
// Method declarations...
]
}
]
}

JavaScript Full File AST

const fullAstResult = await codebolt.codeparsers.getAstTreeInFile('path/to/file.js');

Output:

{
"type": "program",
"text": "function greet(name) {\n return \"Hello, \" + name + \"!\";\n}\n\nclass Calculator {\n add(a, b) {\n return a + b;\n }\n \n multiply(a, b) {\n return a * b;\n }\n}",
"startPosition": { "row": 1, "column": 16 },
"endPosition": { "row": 14, "column": 8 },
"children": [
{
"type": "function_declaration",
"text": "function greet(name) {\n return \"Hello, \" + name + \"!\";\n}",
"startPosition": { "row": 1, "column": 16 },
"endPosition": { "row": 3, "column": 17 },
"children": []
},
{
"type": "class_declaration",
"text": "class Calculator {\n add(a, b) {\n return a + b;\n }\n \n multiply(a, b) {\n return a * b;\n }\n}",
"startPosition": { "row": 5, "column": 16 },
"endPosition": { "row": 13, "column": 17 },
"children": []
}
]
}

Usage Notes

  • File Parameter: Provide the full path to the source code file
  • ClassName Parameter: Optional. When specified, returns the AST for that specific class. When omitted, returns the complete file AST
  • Language Support: Supports JavaScript, TypeScript, Python, and other languages with tree-sitter parsers
  • Return Structure: All AST nodes include:
    • type: The node type (varies by language)
    • text: The source code text for this node
    • startPosition: Object with row and column properties
    • endPosition: Object with row and column properties
    • children: Array of child AST nodes

Error Handling

The function handles various error cases gracefully:

  • Non-existent files
  • Unsupported file types
  • Invalid class names
  • Malformed source code