use tree_sitter::Node;
use sephera_core::types::ImportStatement;
use sephera_core::plugins::{line_of, node_text, string_value};
pub fn extract_from_node(
source: &[u8],
node: &Node<'_>,
) -> Option<Vec<ImportStatement>> {
let line = line_of(node)?;
match node.kind() {
"import_statement" | "export_statement" => {
let module = node.child_by_field_name("source")?;
let path = string_value(source, &module);
(!path.is_empty()).then(|| vec![ImportStatement::new(path, line)])
}
"call_expression" => {
let callee = node.child_by_field_name("function")?;
let name = node_text(source, &callee);
if name != "require" && name != "import" {
return None;
}
let arguments = node.child_by_field_name("arguments")?;
let first =
arguments.named_children(&mut arguments.walk()).next()?;
if first.kind() != "string" {
return None;
}
let path = string_value(source, &first);
(!path.is_empty()).then(|| vec![ImportStatement::new(path, line)])
}
_ => None,
}
}
#[cfg(test)]
mod tests {
use sephera_compression::{SupportedLanguage, with_parser};
use sephera_core::plugins::imports_found_by;
fn paths(source: &[u8], language: SupportedLanguage) -> Vec<String> {
with_parser(language, source.len(), |parser| {
Ok(imports_found_by(
source,
parser,
&crate::JavaScriptPlugin { language },
))
})
.expect("a parser exists")
.into_iter()
.map(|s| s.raw_path)
.collect()
}
#[test]
fn a_binding_named_from_is_not_mistaken_for_the_module_specifier() {
let source = b"import { from as origin } from './b';\n";
assert_eq!(
paths(source, SupportedLanguage::JavaScript),
vec!["./b".to_owned()]
);
}
#[test]
fn an_instantiated_require_names_only_its_module() {
let source = b"var hash = require('pbkdf2-password')()\n";
assert_eq!(
paths(source, SupportedLanguage::JavaScript),
vec!["pbkdf2-password".to_owned()]
);
}
#[test]
fn every_module_form_reports_its_module() {
let source = b"import x from './a';\n\
import { a, b } from './c';\n\
import * as ns from './d';\n\
import './side';\n\
export { z } from './e';\n\
const q = require('./f');\n";
for expected in ["./a", "./c", "./d", "./side", "./e", "./f"] {
assert!(
paths(source, SupportedLanguage::JavaScript)
.iter()
.any(|path| path == expected),
"{expected} missing from the extracted paths"
);
}
}
#[test]
fn a_dynamic_import_names_its_module() {
let source = b"const m = import('./lazy');\n";
assert_eq!(
paths(source, SupportedLanguage::JavaScript),
vec!["./lazy".to_owned()]
);
assert_eq!(
paths(source, SupportedLanguage::TypeScript),
vec!["./lazy".to_owned()],
"the grammar is the only difference between the two"
);
}
#[test]
fn a_dynamic_import_with_a_template_string_is_not_a_path() {
assert_eq!(
paths(
b"const m = import(`./pages/${name}`);\n",
SupportedLanguage::JavaScript
),
Vec::<String>::new()
);
}
#[test]
fn a_call_that_is_not_require_is_not_an_import() {
assert_eq!(
paths(b"callee('./x')\n", SupportedLanguage::JavaScript),
Vec::<String>::new()
);
}
#[test]
fn typescript_and_javascript_agree_on_the_forms_they_share() {
let source = b"import x from './a';\nconst q = require('./f');\n";
assert_eq!(
paths(source, SupportedLanguage::TypeScript),
paths(source, SupportedLanguage::JavaScript)
);
}
}