Skip to main content

getFunctionsinClass

codebolt.codeparsers.getFunctionsinClass(file: string, className: string): Array<FunctionInfo>
Retrieves all functions/methods within a specified class in a given file. Supports JavaScript, TypeScript, and Python files.

Parameters

NameTypeDescription
filestringThe file path containing the class to parse.
classNamestringThe name of the class to parse for functions/methods.

Description

The getFunctionsinClass function parses a source code file and extracts information about all functions/methods defined within a specified class. This function supports multiple programming languages including JavaScript, TypeScript, and Python.

Usage

const result = await codebolt.codeparsers.getFunctionsinClass(filePath, className);

Examples

// For a JavaScript file containing:
// class Calculator {
// add(a, b) {
// return a + b;
// }
// multiply(a, b) {
// return a * b;
// }
// }

const jsResult = await codebolt.codeparsers.getFunctionsinClass('test.js', 'Calculator');
console.log(jsResult);
// Output:
// [
// {
// name: 'add',
// class: 'Calculator',
// location: 'C:\\path\\to\\file\\test.js'
// },
// {
// name: 'multiply',
// class: 'Calculator',
// location: 'C:\\path\\to\\file\\test.js'
// }
// ]

Response Format

The function returns an array of objects, where each object represents a function/method found in the specified class:

[
{
name: 'functionName',
class: 'ClassName',
location: 'C:\\path\\to\\file\\filename.ext'
}
]

Response Properties

  • name (string): The name of the function/method
  • class (string): The name of the class containing the function
  • location (string): The absolute file path where the function is defined

Supported File Types

  • JavaScript (.js)
  • TypeScript (.ts)
  • Python (.py)

Notes

  • Constructor methods (like __init__ in Python) are typically excluded from the results
  • Private methods and public methods are both included in the results
  • The function analyzes only the specified class, not the entire file

Error Handling

If the file doesn't exist, the class is not found, or the file type is not supported, the function will return an appropriate error response. Always handle potential errors when using this function.