reference-query 0.57.0

Reference Query — find the code you're looking for.
Documentation
//! Go, Python, TypeScript and JavaScript plugins, end to end: index a fixture
//! file and assert the ordering — the named definition wins, with the right
//! kind and qualification.

use std::fs;

use crate::search::{self, Context};
use crate::tests::support::{indexed, top};

const WIDGET_GO: &str = include_str!("fixtures/go/widget.go");
const ACCOUNT_PY: &str = include_str!("fixtures/python/account.py");
const WIDGET_TS: &str = include_str!("fixtures/typescript/widget.ts");
const ACCOUNT_JSX: &str = include_str!("fixtures/javascript/account.jsx");

#[test]
fn go_definitions_rank_and_classify() {
    let (store, dir) = indexed("go", "widget.go", WIDGET_GO);

    let widget = top(&store, "Widget");
    assert_eq!(widget.name, "Widget");
    assert_eq!(widget.kind, "struct");
    assert_eq!(top(&store, "Renderer").kind, "trait");

    let resize = top(&store, "Resize");
    assert_eq!(resize.kind, "method");
    assert_eq!(resize.parent.as_deref(), Some("Widget"));

    assert_eq!(top(&store, "BuildWidget").kind, "function");

    // a named func type outranks a function whose name only starts with it
    let render = top(&store, "RenderFunc");
    assert_eq!(
        (render.name.as_str(), render.kind.as_str()),
        ("RenderFunc", "type")
    );

    // the const outranks a function whose name only starts with it
    let max = top(&store, "MaxRetries");
    assert_eq!(
        (max.name.as_str(), max.kind.as_str()),
        ("MaxRetries", "constant")
    );
    assert_eq!(top(&store, "ColorBlue").kind, "constant");

    fs::remove_dir_all(&dir).ok();
}

#[test]
fn python_definitions_rank_and_classify() {
    let (store, dir) = indexed("py", "account.py", ACCOUNT_PY);

    let account = top(&store, "Account");
    assert_eq!(account.name, "Account");
    assert_eq!(account.kind, "class");

    let deposit = top(&store, "deposit");
    assert_eq!(deposit.kind, "method");
    assert_eq!(deposit.parent.as_deref(), Some("Account"));

    assert_eq!(top(&store, "build_account").kind, "function");

    // an enum.Enum subclass is an enum of variants, found through its scope
    let status = top(&store, "AccountStatus");
    assert_eq!(
        (status.name.as_str(), status.kind.as_str()),
        ("AccountStatus", "enum")
    );
    let open = top(&store, "AccountStatus.OPEN");
    assert_eq!(
        (open.name.as_str(), open.kind.as_str()),
        ("OPEN", "variant")
    );

    // a closure ranks below the module-level function it shares a name with,
    // and says why
    let audits = search::search(&store, "_audit", None, None, &Context::default(), 10)
        .unwrap()
        .hits;
    let parents: Vec<_> = audits.iter().map(|h| h.parent.as_deref()).collect();
    assert_eq!(parents, [None, Some("build_account")]);
    assert!(audits[1].features.iter().any(|f| f.name == "local"));

    // the constant outranks a function whose name only starts with it
    let max = top(&store, "MAX_RETRIES");
    assert_eq!(
        (max.name.as_str(), max.kind.as_str()),
        ("MAX_RETRIES", "constant")
    );
    let default = top(&store, "DEFAULT_BALANCE");
    assert_eq!(default.kind, "constant");
    assert_eq!(default.parent.as_deref(), Some("Account"));

    fs::remove_dir_all(&dir).ok();
}

#[test]
fn typescript_definitions_rank_and_classify() {
    let (store, dir) = indexed("ts", "widget.ts", WIDGET_TS);

    let widget = top(&store, "Widget");
    assert_eq!(widget.name, "Widget");
    assert_eq!(widget.kind, "class");
    assert_eq!(widget.language, "typescript");

    assert_eq!(top(&store, "Renderer").kind, "trait");
    assert_eq!(top(&store, "WidgetSize").kind, "type");
    assert_eq!(top(&store, "WidgetColor").kind, "enum");

    // an enum member is found by its own name and scoped by its enum
    let green = top(&store, "WidgetColor.Green");
    assert_eq!(
        (green.name.as_str(), green.kind.as_str()),
        ("Green", "variant")
    );
    assert_eq!(green.parent.as_deref(), Some("WidgetColor"));

    let resize = top(&store, "resize");
    assert_eq!(resize.kind, "method");
    assert_eq!(resize.parent.as_deref(), Some("Widget"));

    assert_eq!(top(&store, "buildWidget").kind, "function");
    // an arrow assigned to a const is a function like any other
    assert_eq!(top(&store, "defaultWidget").kind, "function");

    // the constant outranks a function whose name only starts with it
    let max = top(&store, "MAX_RETRIES");
    assert_eq!(
        (max.name.as_str(), max.kind.as_str()),
        ("MAX_RETRIES", "constant")
    );
    let width = top(&store, "DEFAULT_WIDTH");
    assert_eq!(width.kind, "constant");
    assert_eq!(width.parent.as_deref(), Some("Widget"));

    fs::remove_dir_all(&dir).ok();
}

#[test]
fn javascript_definitions_rank_and_classify() {
    let (store, dir) = indexed("jsx", "account.jsx", ACCOUNT_JSX);

    let account = top(&store, "Account");
    assert_eq!(account.name, "Account");
    assert_eq!(account.kind, "class");
    assert_eq!(account.language, "javascript");

    let deposit = top(&store, "deposit");
    assert_eq!(deposit.kind, "method");
    assert_eq!(deposit.parent.as_deref(), Some("Account"));

    assert_eq!(top(&store, "buildAccount").kind, "function");
    // a JSX-returning component in a `.jsx` file still parses
    assert_eq!(top(&store, "AccountBadge").kind, "function");
    // so is a component wrapped in `memo(…)`, over a longer name it prefixes
    let row = top(&store, "AccountRow");
    assert_eq!(
        (row.name.as_str(), row.kind.as_str()),
        ("AccountRow", "function")
    );

    // a camelCase const is a constant too, and outranks a longer function
    let default = top(&store, "defaultAccount");
    assert_eq!(
        (default.name.as_str(), default.kind.as_str()),
        ("defaultAccount", "constant")
    );

    fs::remove_dir_all(&dir).ok();
}