use std::sync::LazyLock;
use regex::Regex;
pub const NODE_KINDS: &[&str] = &[];
static TS_JS_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"\.(map|filter|reduce|flatMap|forEach|find|findIndex|some|every|flat)\(")
.expect("collection_pipelines regex is valid")
});
pub fn matches_callee(text: &str, language: &str) -> bool {
match language {
"typescript" | "javascript" | "go" | "java" => TS_JS_RE.is_match(text),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn map_matches() {
assert!(matches_callee("xs.map(f)", "typescript"));
assert!(matches_callee("data.map(fn)", "javascript"));
}
#[test]
fn filter_matches() {
assert!(matches_callee("xs.filter(p)", "typescript"));
}
#[test]
fn reduce_matches() {
assert!(matches_callee("xs.reduce(g, 0)", "typescript"));
}
#[test]
fn flat_map_matches() {
assert!(matches_callee("xs.flatMap(fn)", "javascript"));
}
#[test]
fn for_each_matches() {
assert!(matches_callee("xs.forEach(cb)", "typescript"));
}
#[test]
fn find_matches() {
assert!(matches_callee("xs.find(p)", "typescript"));
}
#[test]
fn find_index_matches() {
assert!(matches_callee("xs.findIndex(p)", "javascript"));
}
#[test]
fn some_matches() {
assert!(matches_callee("xs.some(p)", "typescript"));
}
#[test]
fn every_matches() {
assert!(matches_callee("xs.every(p)", "typescript"));
}
#[test]
fn flat_matches() {
assert!(matches_callee("xs.flat()", "javascript"));
}
#[test]
fn chained_pipeline_matches() {
assert!(matches_callee("xs.filter(p).reduce(g, 0)", "typescript"));
}
#[test]
fn data_access_methods_do_not_match() {
assert!(!matches_callee("db.query(sql)", "typescript"));
assert!(!matches_callee("client.read(buf)", "typescript"));
assert!(!matches_callee("stream.write(data)", "typescript"));
assert!(!matches_callee("client.fetch(url)", "typescript"));
}
#[test]
fn async_pattern_methods_do_not_match() {
assert!(!matches_callee("promise.then(resolve)", "typescript"));
assert!(!matches_callee("p.catch(err => {})", "javascript"));
assert!(!matches_callee("p.finally(() => {})", "typescript"));
}
#[test]
fn math_max_does_not_match() {
assert!(!matches_callee("Math.max(a, b)", "typescript"));
}
#[test]
fn bare_function_call_does_not_match() {
assert!(!matches_callee("map(f)", "typescript"));
assert!(!matches_callee("filter(p)", "javascript"));
}
#[test]
fn other_languages_return_false() {
for lang in ["python", "rust"] {
assert!(
!matches_callee("xs.map(f)", lang),
"xs.map should not match for {lang}"
);
}
}
#[test]
fn node_kinds_is_empty() {
#[allow(clippy::const_is_empty)]
let empty = NODE_KINDS.is_empty();
assert!(empty);
}
}