Skip to content

javascript

TREE_SITTER_QUERY = '(function_declaration name: (identifier) @function.name) @function.definition(variable_declarator name: (identifier) @function.name value: (function_expression)) @function.definition(variable_declarator name: (identifier) @function.name value: (arrow_function)) @function.definition(class_declaration name: (identifier) @class.name body: (class_body (method_definition name: (property_identifier) @function.name) @function.definition))(variable_declarator name: (identifier) @class.name value: (class body: (class_body (method_definition name: (property_identifier) @function.name) @function.definition)))' module-attribute

Tree-sitter query for extracting function names and definitions.

JavaScriptExtractor

Bases: TreeSitterExtractor

Source code extractor for extracting JavaScript functions.

Source code in src/codablellm/languages/javascript.py
class JavaScriptExtractor(TreeSitterExtractor):
    """
    Source code extractor for extracting JavaScript functions.
    """

    def __init__(self) -> None:
        super().__init__("JavaScript", TREE_SITTER_QUERY)

    def get_extractable_files(self, path: PathLike) -> Set[Path]:
        return rglob_file_extensions(path, [".js", ".cjs", ".mjs"])

    @requires_extra(
        "javascript", "JavaScript source code extraction", "tree_sitter_javascript"
    )
    def get_language(self) -> Language:
        return Language(tsjs.language())  # type: ignore

    def is_installed(self) -> bool:
        return tsjs is not None