use std::path::{Component, Path, PathBuf};
use vvv_core::{
Address, Layout, ModulePath, Name, Package, PackageId, PathHead, PathSyntax, Project,
ResolveError,
};
pub(crate) const SYNTAX: PathSyntax = PathSyntax::Posix;
const EXTENSIONS: &[&str] = &["ts", "tsx", "mts", "cts", "js", "jsx", "mjs", "cjs", "d.ts"];
const TRY_EXTENSIONS: &[&str] = &["ts", "tsx", "d.ts", "js", "jsx", "mts", "cts"];
const INDEX: &[&str] = &["index.ts", "index.tsx", "index.js"];
#[derive(Debug, Clone, Copy, Default)]
pub struct TsLayout;
impl TsLayout {
const PACKAGE: &str = "";
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
}
pub(crate) fn relative(dir: &Path, target: &Path) -> ModulePath {
let dir: Vec<_> = dir.components().collect();
let target: Vec<_> = target.components().collect();
let common = dir.iter().zip(&target).take_while(|(a, b)| a == b).count();
let ups = dir.len() - common;
let head = if ups == 0 {
PathHead::Here
} else {
PathHead::Up(ups as u8)
};
ModulePath::new(
SYNTAX,
head,
target[common..]
.iter()
.map(|c| Name::new(c.as_os_str().to_string_lossy())),
)
}
pub(crate) fn known_extension(name: &str) -> Option<&'static str> {
EXTENSIONS
.iter()
.copied()
.filter(|ext| name.ends_with(&format!(".{ext}")))
.max_by_key(|ext| ext.len())
}
fn address_of(path: &Path) -> Address {
Address::new(
PackageId::new(Self::PACKAGE),
path.components()
.map(|c| c.as_os_str().to_string_lossy().into_owned()),
)
}
pub(crate) fn path_of(address: &Address) -> PathBuf {
address.path().iter().map(Name::as_str).collect()
}
}
impl Layout for TsLayout {
fn manifests(&self) -> &'static [&'static str] {
&[]
}
fn package(&self, _: &Path, _: &str) -> Option<Package> {
None
}
fn address(&self, _: &Project, path: &Path) -> Result<Address, ResolveError> {
let name = path.file_name().map(|n| n.to_string_lossy());
let is_dir = path.extension().is_none();
match name.as_deref().and_then(Self::known_extension) {
Some(_) => Ok(Self::address_of(path)),
None if is_dir => Ok(Self::address_of(path)),
None => Err(ResolveError::NotAddressable(path.into())),
}
}
fn resolve(&self, project: &Project, file: &Path, import: &ModulePath) -> Option<Address> {
let ups = match import.head {
PathHead::Here => 0,
PathHead::Up(n) => n,
PathHead::Named | PathHead::Package | PathHead::SelfType | PathHead::Root => {
return None;
}
};
let mut base = file.parent()?.to_path_buf();
for _ in 0..ups {
base.push("..");
}
base.extend(import.segments.iter().map(Name::as_str));
let base = Self::normalize(&base);
let name = base.file_name()?.to_string_lossy().into_owned();
let mut candidates: Vec<PathBuf> = Vec::new();
if let Some(ext) = Self::known_extension(&name) {
candidates.push(base.clone());
if let Some(stem) = name.strip_suffix(&format!(".{ext}")) {
let swapped = match ext {
"js" => Some("ts"),
"jsx" => Some("tsx"),
"mjs" => Some("mts"),
"cjs" => Some("cts"),
_ => None,
};
if let Some(swapped) = swapped {
candidates.push(base.with_file_name(format!("{stem}.{swapped}")));
}
}
} else {
candidates.extend(
TRY_EXTENSIONS
.iter()
.map(|ext| base.with_file_name(format!("{name}.{ext}"))),
);
candidates.extend(INDEX.iter().map(|index| base.join(index)));
}
candidates
.into_iter()
.find(|c| project.files.contains(c))
.map(|c| Self::address_of(&c))
}
fn candidates(&self, _: &Project, address: &Address) -> Vec<PathBuf> {
vec![Self::path_of(address)]
}
}