use std::path::{Component, Path, PathBuf};
use crate::discover::FileId;
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Scan {
Opaque,
Specifiers(Vec<String>),
}
const SOURCE_EXTENSIONS: &[&str] = &[
"js", "jsx", "mjs", "cjs", "ts", "tsx", "mts", "cts", "vue", "svelte", "astro", "mdx",
];
const LOADER_NAMES: &[&str] = &[
"requireActual",
"requireMock",
"importActual",
"importMock",
"importOriginal",
"importScripts",
];
const OPAQUE_MARKERS: &[&str] = &["createRequire", "importmap"];
enum Step {
Specifier(String),
Opaque,
Nothing,
}
pub(super) fn scan(text: &str) -> Scan {
if OPAQUE_MARKERS.iter().any(|marker| text.contains(marker)) {
return Scan::Opaque;
}
let bytes = text.as_bytes();
let mut specifiers = Vec::new();
let mut index = 0;
while let Some((start, end)) = next_identifier(bytes, index) {
index = end;
match keyword_step(text, start, end) {
Step::Specifier(value) => specifiers.push(value),
Step::Opaque => return Scan::Opaque,
Step::Nothing => {}
}
}
Scan::Specifiers(specifiers)
}
const fn is_ident_byte(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'$'
}
fn next_identifier(bytes: &[u8], from: usize) -> Option<(usize, usize)> {
let mut start = from;
while start < bytes.len() {
let boundary = start == 0 || !is_ident_byte(bytes[start - 1]);
if boundary && is_ident_byte(bytes[start]) {
let mut end = start + 1;
while end < bytes.len() && is_ident_byte(bytes[end]) {
end += 1;
}
return Some((start, end));
}
start += 1;
}
None
}
fn keyword_step(text: &str, start: usize, end: usize) -> Step {
let bytes = text.as_bytes();
let word = &text[start..end];
let cursor = skip_trivia(bytes, end);
let next = bytes.get(cursor).copied();
match word {
"from" if start == 0 || bytes[start - 1] != b'.' => literal_step(text, cursor),
"import" => match next {
Some(b'(') => call_step(text, cursor),
Some(b'.') => import_meta_step(text, cursor),
_ => literal_step(text, cursor),
},
"require" => match next {
Some(b'(') => call_step(text, cursor),
Some(b'.' | b';' | b',' | b')' | b']' | b'}' | b'=') | None => Step::Opaque,
_ => Step::Nothing,
},
name if LOADER_NAMES.contains(&name) && next == Some(b'(') => call_step(text, cursor),
_ => Step::Nothing,
}
}
fn literal_step(text: &str, cursor: usize) -> Step {
match literal_at(text, cursor) {
Some((Literal::Plain(value), _)) => Step::Specifier(value),
Some((Literal::Computed, _)) => Step::Opaque,
None => Step::Nothing,
}
}
fn call_step(text: &str, cursor: usize) -> Step {
let bytes = text.as_bytes();
let argument = skip_trivia(bytes, cursor + 1);
let Some((Literal::Plain(value), after)) = literal_at(text, argument) else {
return Step::Opaque;
};
match bytes.get(skip_trivia(bytes, after)) {
Some(b')' | b',') => Step::Specifier(value),
_ => Step::Opaque,
}
}
fn import_meta_step(text: &str, cursor: usize) -> Step {
let bytes = text.as_bytes();
let Some((start, end)) = next_identifier(bytes, skip_trivia(bytes, cursor + 1)) else {
return Step::Nothing;
};
if &text[start..end] != "meta" {
return Step::Nothing;
}
let dot = skip_trivia(bytes, end);
if bytes.get(dot) != Some(&b'.') {
return Step::Nothing;
}
match next_identifier(bytes, skip_trivia(bytes, dot + 1)) {
Some((start, end)) if text[start..end].starts_with("glob") => Step::Opaque,
_ => Step::Nothing,
}
}
fn skip_trivia(bytes: &[u8], mut index: usize) -> usize {
loop {
while index < bytes.len() && bytes[index].is_ascii_whitespace() {
index += 1;
}
let rest = bytes.get(index..).unwrap_or_default();
if rest.starts_with(b"//") {
index += rest
.iter()
.position(|&byte| byte == b'\n')
.unwrap_or(rest.len());
} else if rest.starts_with(b"/*") {
index += rest
.windows(2)
.skip(2)
.position(|pair| pair == b"*/")
.map_or(rest.len(), |offset| offset + 4);
} else {
return index;
}
}
}
enum Literal {
Plain(String),
Computed,
}
fn literal_at(text: &str, index: usize) -> Option<(Literal, usize)> {
let quote = *text.as_bytes().get(index)?;
if !matches!(quote, b'"' | b'\'' | b'`') {
return None;
}
let body_start = index + 1;
let close = text[body_start..].find(char::from(quote))? + body_start;
let body = &text[body_start..close];
if quote == b'`' && body.contains("${") {
return Some((Literal::Computed, close + 1));
}
Some((Literal::Plain(body.to_string()), close + 1))
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Target {
External,
Path(PathBuf),
RootRelative(String),
Bare { specifier: String, package: String },
Unknown,
}
pub(super) fn target(from: &Path, specifier: &str) -> Target {
let specifier = specifier
.split(['?', '#'])
.next()
.filter(|head| !head.is_empty())
.unwrap_or(specifier);
if specifier.starts_with("file:") {
return Target::Unknown;
}
if specifier.contains("://") || specifier.starts_with("data:") {
return Target::External;
}
if specifier.starts_with('.') {
let base = from.parent().unwrap_or_else(|| Path::new(""));
return Target::Path(canonical_prefix(&normalize(&base.join(specifier))));
}
if let Some(rooted) = specifier.strip_prefix('/') {
return Target::RootRelative(rooted.to_string());
}
Target::Bare {
specifier: specifier.to_string(),
package: package_name(specifier).to_string(),
}
}
fn package_name(specifier: &str) -> &str {
let mut parts = specifier.splitn(3, '/');
let first = parts.next().unwrap_or("");
if first.starts_with('@')
&& let Some(second) = parts.next()
{
return &specifier[..first.len() + 1 + second.len()];
}
first
}
fn normalize(path: &Path) -> PathBuf {
let mut out = PathBuf::new();
for component in path.components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
out.pop();
}
other => out.push(other),
}
}
out
}
fn canonical_prefix(path: &Path) -> PathBuf {
let mut rest = Vec::new();
let mut current = path;
loop {
if let Ok(real) = dunce::canonicalize(current) {
return rest.iter().rev().fold(real, |acc, part| acc.join(part));
}
match (current.parent(), current.file_name()) {
(Some(parent), Some(name)) => {
rest.push(name.to_os_string());
current = parent;
}
_ => return path.to_path_buf(),
}
}
}
fn strip_source_extension(path: &str) -> &str {
path.rsplit_once('.')
.filter(|(stem, extension)| {
!stem.is_empty() && !extension.contains('/') && SOURCE_EXTENSIONS.contains(extension)
})
.map_or(path, |(stem, _)| stem)
}
fn slash_lower(path: &Path) -> String {
path.to_string_lossy().replace('\\', "/").to_lowercase()
}
pub(super) struct ModuleIndex {
entries: Vec<(FileId, String, String)>,
}
impl ModuleIndex {
pub(super) fn new<'p>(modules: impl Iterator<Item = (FileId, &'p Path)>) -> Self {
let entries = modules
.map(|(file, path)| {
let full = slash_lower(path);
let key = match full.rsplit_once('.') {
Some((stem, extension)) if !stem.is_empty() && !extension.contains('/') => {
stem.to_string()
}
_ => full.clone(),
};
(file, full, key)
})
.collect();
Self { entries }
}
pub(super) fn under_path(&self, path: &Path) -> Vec<FileId> {
let full = slash_lower(path);
let wanted = [full.as_str(), strip_source_extension(&full)];
self.select(|module, key| {
wanted.iter().any(|wanted| {
key == *wanted
|| module == *wanted
|| module
.strip_prefix(*wanted)
.is_some_and(|rest| rest.starts_with('/'))
})
})
}
pub(super) fn with_suffix(&self, suffix: &str) -> Vec<FileId> {
let full = format!("/{}", suffix.trim_start_matches('/').to_lowercase());
let wanted = [full.as_str(), strip_source_extension(&full)];
let below: Vec<String> = wanted.iter().map(|wanted| format!("{wanted}/")).collect();
self.select(|module, key| {
wanted
.iter()
.any(|wanted| key.ends_with(*wanted) || module.ends_with(*wanted))
|| below.iter().any(|below| module.contains(below.as_str()))
})
}
fn select(&self, keep: impl Fn(&str, &str) -> bool) -> Vec<FileId> {
self.entries
.iter()
.filter(|(_, module, key)| keep(module, key))
.map(|(file, _, _)| *file)
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn specifiers(text: &str) -> Vec<String> {
match scan(text) {
Scan::Specifiers(found) => found,
Scan::Opaque => panic!("unexpected opaque scan of {text:?}"),
}
}
#[test]
fn finds_static_dynamic_require_and_loader_specifiers() {
let text = "import a from './a';\nimport { b } from\n /* why */ \"../b\";\nexport * from './c';\nimport './d.css';\nconst e = require('./e').value;\nconst f = await import(`./f`);\nconst g = jest.requireActual('./g');\nconst h = await vi.importActual('./h', {});\n";
assert_eq!(
specifiers(text),
vec!["./a", "../b", "./c", "./d.css", "./e", "./f", "./g", "./h"]
);
}
#[test]
fn a_computed_module_name_is_opaque() {
for text in [
"const m = require(name);",
"const m = await import(name);",
"const m = await import(`./pages/${name}`);",
"const m = await import('./locale-' + lang);",
"const m = require('./icon-'.concat(n));",
"const all = import.meta.glob('./pages/*.ts');",
"const all = import.meta\n .glob('./pages/*.ts');",
"const ctx = require.context('./pages', true);",
"const load = require;\nload('./a');",
"const req = createRequire(import.meta.url);",
"const mod = await importOriginal();",
"<script type=\"importmap\">{}</script>",
] {
assert_eq!(scan(text), Scan::Opaque, "{text}");
}
}
#[test]
fn prose_and_other_identifiers_give_no_specifier() {
let text = "// the value comes from the server, this is important.\nconst chars = Array.from('abc');\nconst url = new URL('./w.js', import.meta.url);\nconst data = importData(source);\nif (requiresAuth(user)) {}\nconst important = 1;\n";
assert_eq!(specifiers(text), Vec::<String>::new());
}
#[test]
fn classifies_each_specifier_kind() {
let dir = tempfile::tempdir().expect("temporary project");
let root = dunce::canonicalize(dir.path()).expect("canonical project root");
let from = root.join("src").join("pages").join("home.ts");
let from = from.as_path();
assert_eq!(
target(from, "../lib/format.js"),
Target::Path(root.join("src").join("lib").join("format.js"))
);
assert_eq!(
target(from, "/src/main.tsx"),
Target::RootRelative("src/main.tsx".into())
);
assert_eq!(
target(from, "@acme/ui/button"),
Target::Bare {
specifier: "@acme/ui/button".into(),
package: "@acme/ui".into()
}
);
assert_eq!(
target(from, "https://cdn.example.com/x.js"),
Target::External
);
assert_eq!(target(from, "file:///p/src/a.ts"), Target::Unknown);
}
fn index(paths: &[&'static str]) -> ModuleIndex {
ModuleIndex::new(
paths
.iter()
.enumerate()
.map(|(id, path)| (FileId(u32::try_from(id).expect("id")), Path::new(*path))),
)
}
#[test]
fn a_path_matches_with_or_without_extension_in_any_case_and_below() {
let modules = index(&[
"/p/src/lib/format.ts",
"/p/src/lib/formatter.ts",
"/p/src/counter.svelte.ts",
"/p/src/user.service.ts",
"/p/src/Utils.ts",
"/p/src/lib/index.ts",
"/p/src/library.ts",
]);
let ids = |path: &str| modules.under_path(Path::new(path));
assert_eq!(ids("/p/src/lib/format.js"), vec![FileId(0)]);
assert_eq!(ids("/p/src/counter.svelte"), vec![FileId(2)]);
assert_eq!(ids("/p/src/user.service"), vec![FileId(3)]);
assert_eq!(ids("/p/src/utils"), vec![FileId(4)]);
assert_eq!(ids("/p/src/lib"), vec![FileId(0), FileId(1), FileId(5)]);
assert_eq!(modules.with_suffix("src/lib/format.ts"), vec![FileId(0)]);
assert_eq!(modules.with_suffix("events/bus"), Vec::<FileId>::new());
}
}