infiniloom_engine/index/
patterns.rs1use once_cell::sync::Lazy;
7use regex::Regex;
8
9pub static PYTHON_IMPORT: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s*import\s+(\S+)").unwrap());
11
12pub static PYTHON_FROM_IMPORT: Lazy<Regex> =
14 Lazy::new(|| Regex::new(r"^\s*from\s+(\S+)\s+import").unwrap());
15
16pub static JS_IMPORT: Lazy<Regex> =
18 Lazy::new(|| Regex::new(r#"import\s+.*\s+from\s+['"]([^'"]+)['"]"#).unwrap());
19
20pub static JS_IMPORT_MULTILINE: Lazy<Regex> =
22 Lazy::new(|| Regex::new(r#"(?s)import\s+.*?\s+from\s+['"]([^'"]+)['"]"#).unwrap());
23
24pub static JS_REQUIRE: Lazy<Regex> =
26 Lazy::new(|| Regex::new(r#"require\s*\(\s*['"]([^'"]+)['"]\s*\)"#).unwrap());
27
28pub static RUST_USE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s*use\s+([^;]+);").unwrap());
30
31pub static GO_IMPORT: Lazy<Regex> =
33 Lazy::new(|| Regex::new(r#"import\s+(?:\(\s*)?(?:[\w.]+\s+)?["']([^"']+)["']"#).unwrap());
34
35pub static JAVA_IMPORT: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s*import\s+([\w.]+);").unwrap());
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn test_python_import() {
44 assert!(PYTHON_IMPORT.is_match("import os"));
45 assert!(PYTHON_IMPORT.is_match("import os.path"));
46 assert!(PYTHON_IMPORT.is_match(" import json"));
47 }
48
49 #[test]
50 fn test_python_from_import() {
51 assert!(PYTHON_FROM_IMPORT.is_match("from os import path"));
52 assert!(PYTHON_FROM_IMPORT.is_match("from typing import List"));
53 }
54
55 #[test]
56 fn test_js_import() {
57 assert!(JS_IMPORT.is_match("import { foo } from 'module'"));
58 assert!(JS_IMPORT.is_match("import foo from \"module\""));
59 }
60
61 #[test]
62 fn test_js_require() {
63 assert!(JS_REQUIRE.is_match("require('module')"));
64 assert!(JS_REQUIRE.is_match("const x = require(\"module\")"));
65 }
66
67 #[test]
68 fn test_rust_use() {
69 assert!(RUST_USE.is_match("use std::collections::HashMap;"));
70 assert!(RUST_USE.is_match(" use crate::module;"));
71 }
72
73 #[test]
74 fn test_go_import() {
75 assert!(GO_IMPORT.is_match("import \"fmt\""));
76 assert!(GO_IMPORT.is_match("import f \"fmt\""));
77 }
78
79 #[test]
80 fn test_java_import() {
81 assert!(JAVA_IMPORT.is_match("import java.util.List;"));
82 assert!(JAVA_IMPORT.is_match("import com.example.MyClass;"));
83 }
84}