#Responsible for extracting classes and functions/methods in js code


def extract_classes(node, source_code):
    classes = [] # Collect the function names

    if node.type == "class_declaration":
        name_node = node.child_by_field_name("name")

        if name_node:
            className = source_code[name_node.start_byte : name_node.end_byte].decode("utf-8")
            line_num = node.start_point[0] + 1
            classes.append({
                "name": className,
                "line" : line_num
            })

    for child in node.children:
        classes.extend(extract_classes(child, source_code))
    
    return classes

def extract_functions(node, source_code):
    functions = [] # Collect the function names

    if node.type == "method_definition":
        name_node = node.child_by_field_name("name")

        if name_node:
            functionName = source_code[name_node.start_byte : name_node.end_byte].decode("utf-8")
            line_num = node.start_point[0] + 1
            functions.append({
                "name": functionName,
                "line" : line_num
            })

    for child in node.children:
        functions.extend(extract_functions(child, source_code))
    
    return functions

