def get_calls(tree, blob): calls = [] traverse_type(tree.root_node, calls, 'call') def _traverse_calls(node, identifiers): if node.type == 'identifier': identifiers.append(node) if not node.children or node.type == 'argument_list': return for n in node.children: _traverse_calls(n, identifiers) results = [] for call in calls: identifiers = [] _traverse_calls(call, identifiers) if identifiers: identifier = identifiers[-1] argument_lists = [ n for n in call.children if n.type == 'argument_list' ] argument_list = '' if argument_lists: argument_list = match_from_span(argument_lists[-1], blob) results.append({ 'identifier': match_from_span(identifier, blob), 'argument_list': argument_list, 'start_point': identifier.start_point, 'end_point': identifier.end_point, }) return results
def get_function_metadata(function_node, blob: str) -> Dict[str, str]: metadata = { 'identifier': '', 'parameters': '', } declarators = [] traverse_type(function_node, declarators, '{}_declaration'.format(function_node.type.split('_')[0])) parameters = [] for n in declarators[0].children: if n.type == 'identifier': metadata['identifier'] = match_from_span(n, blob).strip('(') elif n.type == 'formal_parameter': parameters.append(match_from_span(n, blob)) metadata['parameters'] = ' '.join(parameters) return metadata
def get_declarations(declaration_node, blob: str, node_type: str) -> List[Dict[str, Any]]: declarations = [] for idx, child in enumerate(declaration_node.children): if child.type == 'name': declaration_name = match_from_span(child, blob) elif child.type == 'method_declaration': docstring = PhpParser.get_docstring(declaration_node, blob, idx) docstring_summary = get_docstring_summary(docstring) function_nodes = [] traverse_type(child, function_nodes, 'function_definition') if function_nodes: function_node = function_nodes[0] metadata = PhpParser.get_function_metadata( function_node, blob) if metadata[ 'identifier'] in PhpParser.BLACKLISTED_FUNCTION_NAMES: continue declarations.append({ 'type': node_type, 'identifier': '{}.{}'.format(declaration_name, metadata['identifier']), 'parameters': metadata['parameters'], 'function': match_from_span(child, blob), 'function_tokens': tokenize_code(child, blob), 'docstring': docstring, 'docstring_summary': docstring_summary, 'start_point': function_node.start_point, 'end_point': function_node.end_point }) return declarations
def get_context(tree, blob): def _get_import_from(import_from_statement, blob): context = {} mode = 'from' library = '' for n in import_from_statement.children: if n.type == 'from': mode = 'from' elif n.type == 'import': mode = 'import' elif n.type == 'dotted_name': if mode == 'from': library = match_from_span(n, blob).strip() elif mode == 'import': if library: context[match_from_span( n, blob).strip().strip(',')] = library return context def _get_import(import_statement, blob): context = [] for n in import_statement.children: if n.type == 'dotted_name': context.append(match_from_span(n, blob).strip()) if n.type == 'aliased_import': for a in n.children: if a.type == 'dotted_name': context.append(match_from_span(a, blob).strip()) return context import_from_statements = [] traverse_type(tree.root_node, import_from_statements, 'import_from_statement') import_statements = [] traverse_type(tree.root_node, import_statements, 'import_statement') context = [] context.extend( (_get_import_from(i, blob) for i in import_from_statements)) context.extend((_get_import(i, blob) for i in import_statements)) return context
def get_definition(tree, blob: str) -> List[Dict[str, Any]]: function_nodes = [] functions = [] traverse_type(tree.root_node, function_nodes, 'function') for function in function_nodes: if function.children is None or len(function.children) == 0: continue parent_node = node_parent(tree, function) functions.append( (parent_node.type, function, JavascriptParser.get_docstring(tree, function, blob))) definitions = [] for node_type, function_node, docstring in functions: metadata = JavascriptParser.get_function_metadata( function_node, blob) docstring_summary = get_docstring_summary(docstring) if metadata[ 'identifier'] in JavascriptParser.BLACKLISTED_FUNCTION_NAMES: continue definitions.append({ 'type': node_type, 'identifier': metadata['identifier'], 'parameters': metadata['parameters'], 'function': match_from_span(function_node, blob), 'function_tokens': tokenize_code(function_node, blob), 'docstring': docstring, 'docstring_summary': docstring_summary, 'start_point': function_node.start_point, 'end_point': function_node.end_point }) return definitions