use std::sync::LazyLock;
use regex::Regex;
pub const NODE_KINDS: &[&str] = &["go_statement", "select_statement"];
static TS_JS_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^Promise\.(all|allSettled|race|any)\(").expect("concurrency TS/JS regex is valid")
});
static PYTHON_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^asyncio\.(gather|create_task|wait|as_completed|run)\(")
.expect("concurrency Python regex is valid")
});
pub fn matches_callee(text: &str, language: &str) -> bool {
match language {
"typescript" | "javascript" => TS_JS_RE.is_match(text),
"python" => PYTHON_RE.is_match(text),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn promise_all_matches_ts() {
assert!(matches_callee("Promise.all([a, b])", "typescript"));
}
#[test]
fn promise_all_settled_matches_js() {
assert!(matches_callee("Promise.allSettled([p1, p2])", "javascript"));
}
#[test]
fn promise_race_matches_js() {
assert!(matches_callee("Promise.race([a, b])", "javascript"));
}
#[test]
fn promise_any_matches_ts() {
assert!(matches_callee("Promise.any([a, b])", "typescript"));
}
#[test]
fn asyncio_gather_matches_python() {
assert!(matches_callee("asyncio.gather(*tasks)", "python"));
}
#[test]
fn asyncio_create_task_matches_python() {
assert!(matches_callee("asyncio.create_task(coro())", "python"));
}
#[test]
fn asyncio_wait_matches_python() {
assert!(matches_callee("asyncio.wait(tasks)", "python"));
}
#[test]
fn asyncio_as_completed_matches_python() {
assert!(matches_callee("asyncio.as_completed(tasks)", "python"));
}
#[test]
fn asyncio_run_matches_python() {
assert!(matches_callee("asyncio.run(main())", "python"));
}
#[test]
fn promise_then_does_not_match_ts() {
assert!(!matches_callee("promise.then(r)", "typescript"));
}
#[test]
fn promise_resolve_does_not_match_js() {
assert!(!matches_callee("Promise.resolve(x)", "javascript"));
}
#[test]
fn asyncio_sleep_does_not_match_python() {
assert!(!matches_callee("asyncio.sleep(1)", "python"));
}
#[test]
fn go_returns_false() {
assert!(!matches_callee("go worker(ch)", "go"));
}
#[test]
fn rust_returns_false() {
assert!(!matches_callee("tokio::spawn(async {})", "rust"));
}
#[test]
fn node_kinds_contains_go_statement() {
assert!(NODE_KINDS.contains(&"go_statement"));
}
#[test]
fn node_kinds_contains_select_statement() {
assert!(NODE_KINDS.contains(&"select_statement"));
}
}