Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions src/code_type_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,22 @@ impl CodeTypeDetector {
}
}

fn extract_imports(&self, content: &str, _language: &str) -> Vec<String> {
fn extract_imports(&self, content: &str, language: &str) -> Vec<String> {
let lang = language.to_lowercase();
if !matches!(lang.as_str(), "javascript" | "typescript" | "tsx" | "jsx" | "js" | "ts") {
return Vec::new();
}

let mut imports = Vec::new();

// Extract require() calls
// Extract require() calls and ES import statements (including multiline and side-effect imports).
// The import ... from pattern is constrained to not match across statement boundaries (semicolons).
let require_patterns = vec![
regex::Regex::new(r#"require\(\s*['"]([^'"]+)['"]\s*\)"#).unwrap(),
regex::Regex::new(r#"import\s+.*from\s+['"]([^'"]+)['"]"#).unwrap(),
regex::Regex::new(r#"(?s)import\s+(?:\{[^};]*\}|[^;]+?)\s+from\s*['"]([^'"]+)['"]"#)
.unwrap(),
regex::Regex::new(r#"import\s*\(\s*['"]([^'"]+)['"]\s*\)"#).unwrap(),
regex::Regex::new(r#"import\s+['"]([^'"]+)['"]"#).unwrap(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a Go import such as github.com/reactivex/rxgo can match the react signature and override the Backend fallback; should we limit this extraction to applicable languages?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juangaitanv Thanks! Restricted extract_imports in src/code_type_detector.rs to JavaScript / TypeScript languages (javascript, typescript, tsx, jsx, js, ts), ensuring non-JS languages like Go or Python do not extract false JS framework signatures and cleanly retain their backend classification. Added regression test non_js_imports_do_not_override_backend_default.

];

for pattern in require_patterns {
Expand Down Expand Up @@ -513,4 +521,46 @@ mod tests {

assert_eq!(detector.detect_from_ast(&tree, source), CodeType::Backend);
}

#[test]
fn detect_code_type_handles_multiline_imports() {
let detector = CodeTypeDetector::new();
let code = r#"
import {
Injectable,
NestMiddleware,
} from '@nestjs/common';

@Injectable()
export class AuthMiddleware {}
"#;
assert_eq!(detector.detect_code_type("auth.ts", code, "typescript"), CodeType::Backend);
}

#[test]
fn detect_code_type_handles_side_effect_imports() {
let detector = CodeTypeDetector::new();
let code = r#"
import 'express';
export const app = {};
"#;
assert_eq!(detector.detect_code_type("server.ts", code, "typescript"), CodeType::Backend);
}

#[test]
fn import_regex_does_not_capture_across_statement_boundaries() {
let detector = CodeTypeDetector::new();
// Semicolon ends the import statement; subsequent `from` in unrelated code must not form a match with `import`
let code = "import foo;\nlet x = calculate(from, 'react');\n";
let imports = detector.extract_imports(code, "javascript");
assert!(!imports.contains(&"react".to_string()));
}

#[test]
fn non_js_imports_do_not_override_backend_default() {
let detector = CodeTypeDetector::new();
// A Go file importing rxgo should not match JS frontend react signature
let code = "package main\n\nimport \"github.com/reactivex/rxgo\"\n";
assert_eq!(detector.detect_code_type("main.go", code, "go"), CodeType::Backend);
}
}
Loading