use std::sync::LazyLock;
use regex::Regex;
pub const NODE_KINDS: &[&str] = &[];
static TS_JS_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"^(app|router|fastify|server|srv)\.(get|post|put|delete|patch|head|options|all|use|route)\(",
)
.expect("http_routing TS/JS regex is valid")
});
static GO_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(
r"^(http|mux|r|e|router|engine|g|rg)\.(HandleFunc|Handle|GET|POST|PUT|DELETE|PATCH|Any|Group)\(",
)
.expect("http_routing Go regex is valid")
});
static PYTHON_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\.add_url_rule\(").expect("http_routing Python regex is valid"));
pub fn matches_callee(text: &str, language: &str) -> bool {
match language {
"typescript" | "javascript" => TS_JS_RE.is_match(text),
"go" => GO_RE.is_match(text),
"python" => PYTHON_RE.is_match(text),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn app_get_matches_ts() {
assert!(matches_callee("app.get('/users', handler)", "typescript"));
}
#[test]
fn router_post_matches_js() {
assert!(matches_callee("router.post('/user', cb)", "javascript"));
}
#[test]
fn fastify_route_matches() {
assert!(matches_callee(
"fastify.route({ method: 'GET' })",
"typescript"
));
}
#[test]
fn server_use_matches() {
assert!(matches_callee("server.use(middleware)", "typescript"));
}
#[test]
fn srv_all_matches() {
assert!(matches_callee("srv.all('*', h)", "javascript"));
}
#[test]
fn app_delete_matches() {
assert!(matches_callee("app.delete('/user/:id', h)", "typescript"));
}
#[test]
fn app_put_matches() {
assert!(matches_callee("app.put('/user', update)", "typescript"));
}
#[test]
fn app_patch_matches() {
assert!(matches_callee("app.patch('/user/:id', h)", "typescript"));
}
#[test]
fn app_head_matches() {
assert!(matches_callee("app.head('/ping', h)", "javascript"));
}
#[test]
fn app_options_matches() {
assert!(matches_callee("app.options('/api', h)", "typescript"));
}
#[test]
fn go_http_handle_func_matches() {
assert!(matches_callee("http.HandleFunc(\"/\", h)", "go"));
}
#[test]
fn go_gin_r_get_matches() {
assert!(matches_callee("r.GET(\"/users\", h)", "go"));
}
#[test]
fn go_echo_e_post_matches() {
assert!(matches_callee("e.POST(\"/user\", h)", "go"));
}
#[test]
fn go_mux_handle_matches() {
assert!(matches_callee("mux.Handle(\"/\", h)", "go"));
}
#[test]
fn go_engine_group_matches() {
assert!(matches_callee("engine.Group(\"/api\")", "go"));
}
#[test]
fn python_add_url_rule_matches() {
assert!(matches_callee(
"app.add_url_rule('/u', view_func=h)",
"python"
));
}
#[test]
fn axios_get_does_not_match() {
assert!(!matches_callee("axios.get(url)", "typescript"));
}
#[test]
fn client_get_does_not_match() {
assert!(!matches_callee("client.get(url)", "typescript"));
}
#[test]
fn cache_get_does_not_match() {
assert!(!matches_callee("cache.get(key)", "typescript"));
}
#[test]
fn fetch_does_not_match() {
assert!(!matches_callee("fetch(\"/api\")", "typescript"));
}
#[test]
fn go_db_query_does_not_match() {
assert!(!matches_callee("db.Query(sql)", "go"));
}
#[test]
fn python_requests_get_does_not_match() {
assert!(!matches_callee("requests.get(url)", "python"));
}
#[test]
fn rust_returns_false() {
assert!(!matches_callee("app.get('/u', h)", "rust"));
}
#[test]
fn java_returns_false() {
assert!(!matches_callee("app.get('/u', h)", "java"));
}
#[test]
fn node_kinds_is_empty() {
#[allow(clippy::const_is_empty)]
let empty = NODE_KINDS.is_empty();
assert!(empty);
}
}