Skip to main content

getAstTreeInFile

codebolt.codeparsers.getAstTreeInFile(file: string, className: string): object

Generates an Abstract Syntax Tree (AST) for a given file.

Parameters​

  • file (string): The file path to generate an AST for.
  • className (string): (Optional) The name of the class to focus the AST generation on. If not provided, returns the full file AST.

Returns​

  • object: Returns an AST object with type, text, startPosition, endPosition, and children properties.

Response Structure​

The method returns a Promise that resolves to an ASTNode object with the following properties:

  • type (string): The node type (varies by programming language, e.g., "class_declaration", "function_declaration", "program").
  • text (string): The source code text for this node.
  • startPosition (object): Object containing the starting position with:
    • row (number): The starting row number
    • column (number): The starting column number
  • endPosition (object): Object containing the ending position with:
    • row (number): The ending row number
    • column (number): The ending column number
  • children (array): Array of child ASTNode objects representing nested code structures.

Error Response: If the file doesn't exist, is not supported, or the specified class is not found, the method returns an object with:

  • error (string): Description of the error that occurred.

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