use std::sync::LazyLock;
use regex::Regex;
pub const NODE_KINDS: &[&str] = &[];
static TS_JS_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(JSON\.(parse|stringify)\(|structuredClone\()")
.expect("serialization TS/JS regex is valid")
});
static PYTHON_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(json|pickle)\.(loads|dumps|load|dump)\(")
.expect("serialization Python regex is valid")
});
static GO_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^json\.(Marshal|Unmarshal|MarshalIndent|NewEncoder|NewDecoder)\(")
.expect("serialization Go 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),
"go" => GO_RE.is_match(text),
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn json_parse_matches_ts() {
assert!(matches_callee("JSON.parse(s)", "typescript"));
}
#[test]
fn json_stringify_matches_js() {
assert!(matches_callee("JSON.stringify(obj)", "javascript"));
}
#[test]
fn structured_clone_matches_ts() {
assert!(matches_callee("structuredClone(data)", "typescript"));
}
#[test]
fn json_parse_matches_js() {
assert!(matches_callee("JSON.parse(text)", "javascript"));
}
#[test]
fn json_loads_matches_python() {
assert!(matches_callee("json.loads(s)", "python"));
}
#[test]
fn json_dumps_matches_python() {
assert!(matches_callee("json.dumps(o)", "python"));
}
#[test]
fn json_load_matches_python() {
assert!(matches_callee("json.load(f)", "python"));
}
#[test]
fn json_dump_matches_python() {
assert!(matches_callee("json.dump(o, f)", "python"));
}
#[test]
fn pickle_loads_matches_python() {
assert!(matches_callee("pickle.loads(b)", "python"));
}
#[test]
fn pickle_dumps_matches_python() {
assert!(matches_callee("pickle.dumps(o)", "python"));
}
#[test]
fn json_marshal_matches_go() {
assert!(matches_callee("json.Marshal(v)", "go"));
}
#[test]
fn json_unmarshal_matches_go() {
assert!(matches_callee("json.Unmarshal(b, &v)", "go"));
}
#[test]
fn json_marshal_indent_matches_go() {
assert!(matches_callee("json.MarshalIndent(v, \"\", \" \")", "go"));
}
#[test]
fn json_new_encoder_matches_go() {
assert!(matches_callee("json.NewEncoder(w)", "go"));
}
#[test]
fn json_new_decoder_matches_go() {
assert!(matches_callee("json.NewDecoder(r)", "go"));
}
#[test]
fn schema_parse_does_not_match_ts() {
assert!(!matches_callee("schema.parse(x)", "typescript"));
}
#[test]
fn json_other_method_does_not_match_ts() {
assert!(!matches_callee("JSON.error(x)", "typescript"));
}
#[test]
fn len_does_not_match_python() {
assert!(!matches_callee("len(x)", "python"));
}
#[test]
fn requests_does_not_match_python() {
assert!(!matches_callee("requests.get(url)", "python"));
}
#[test]
fn json_other_does_not_match_go() {
assert!(!matches_callee("json.Something(v)", "go"));
}
#[test]
fn fmt_println_does_not_match_go() {
assert!(!matches_callee("fmt.Println(x)", "go"));
}
#[test]
fn rust_returns_false() {
assert!(!matches_callee("serde_json::to_string(&v)", "rust"));
}
#[test]
fn node_kinds_is_empty() {
#[allow(clippy::const_is_empty)]
let empty = NODE_KINDS.is_empty();
assert!(empty);
}
}