def search()

in bugbug/code_search/searchfox_api.py [0:0]


def search(commit_hash, symbol_name):
    r = utils.get_session("searchfox").get(
        f"https://searchfox.org/mozilla-central/search?q=id:{symbol_name}",
        headers={
            "User-Agent": utils.get_user_agent(),
        },
    )
    r.raise_for_status()

    results = r.text
    results = results.split("var results = ", 1)[1]
    results = results.split(";\n", 1)[0]

    # A workaround to fix: https://github.com/mozilla/bugbug/issues/4448
    results = results.replace(r"<\s", r"<\\s")
    results = results.replace(r"<\!", r"<\\!")

    results = json.loads(results)

    symbol_word_re = re.compile(rf"\b{symbol_name}\b")

    definitions = []
    for type_ in ["normal", "thirdparty", "test"]:
        if type_ not in results:
            continue

        for sub_type, values in results[type_].items():
            if sub_type.startswith("Definitions") and sub_type.endswith(
                f"{symbol_name})"
            ):
                for value in values:
                    line = value["lines"][0]["line"]

                    if symbol_name not in line:
                        continue

                    symbol_word_match = symbol_word_re.search(line)
                    symbol_word_position = (
                        symbol_word_match.start()
                        if symbol_word_match is not None
                        else None
                    )

                    # Filter out Rust files where the line containing the string doesn't also contain "fn FUNCTION_NAME" or "|" as this
                    # means it probably isn't a function definition.
                    if any(
                        value["path"].endswith(ext)
                        for ext in SOURCE_CODE_TYPES_TO_EXT["Rust"]
                    ):
                        if "fn {symbol_name}" in line or (
                            "|" in line
                            and symbol_word_position is not None
                            and symbol_word_position < line.index("|")
                        ):
                            definitions.append(value)

                    # Filter out JS files where the line containing the string doesn't also contain "function", "FUNCTION_NAME(", or "=>" as this
                    # means it probably isn't a function definition.
                    elif any(
                        value["path"].endswith(ext)
                        for ext in SOURCE_CODE_TYPES_TO_EXT["Javascript"]
                    ):
                        if (
                            f"{symbol_name}(" in line
                            or f"function {symbol_name}" in line
                            or (
                                "function" in line
                                and symbol_word_position is not None
                                and symbol_word_position < line.index("function")
                            )
                            or (
                                "=>" in line
                                and symbol_word_position is not None
                                and symbol_word_position < line.index("=>")
                            )
                        ):
                            definitions.append(value)

                    # Filter out C/C++ files where the line containing the string doesn't also contain "FUNCTION_NAME(" or "->" as this
                    # means it probably isn't a function definition.
                    elif any(
                        value["path"].endswith(ext)
                        for ext in SOURCE_CODE_TYPES_TO_EXT["C/C++"]
                        + SOURCE_CODE_TYPES_TO_EXT["Objective-C/C++"]
                    ):
                        if f"{symbol_name}(" in line or (
                            "->" in line
                            and symbol_word_position is not None
                            and symbol_word_position < line.index("->")
                        ):
                            definitions.append(value)

                    # Filter out Python files where the line containing the string doesn't also contain "def FUNCTION_NAME(" or "lambda" as this
                    # means it probably isn't a function definition.
                    elif any(
                        value["path"].endswith(ext)
                        for ext in SOURCE_CODE_TYPES_TO_EXT["Python"]
                    ):
                        if f"def {symbol_name}(" in line or (
                            "lambda" in line
                            and symbol_word_position is not None
                            and symbol_word_position < line.index("lambda")
                        ):
                            definitions.append(value)

                    else:
                        definitions.append(value)

    paths = list(set(definition["path"] for definition in definitions))

    return sum((get_functions(commit_hash, path, symbol_name) for path in paths), [])