use crate::TsSynError;
#[derive(Debug, Clone, Copy)]
pub struct ImportConfig {
pub name: &'static str,
pub alias: &'static str,
pub module: &'static str,
pub is_type: bool,
}
impl ImportConfig {
pub const fn value(name: &'static str, alias: &'static str, module: &'static str) -> Self {
Self {
name,
alias,
module,
is_type: false,
}
}
pub const fn type_only(name: &'static str, alias: &'static str, module: &'static str) -> Self {
Self {
name,
alias,
module,
is_type: true,
}
}
}
pub struct TsStream {
source: String,
file_name: String,
pub ctx: Option<crate::abi::MacroContextIR>,
pub runtime_patches: Vec<crate::abi::Patch>,
pub insert_pos: crate::abi::InsertPos,
pub cross_module_suffixes: Vec<String>,
pub cross_module_type_suffixes: Vec<String>,
diagnostics: Vec<crate::abi::Diagnostic>,
}
impl TsStream {
pub fn parse_stmt<'a>(
&'a self,
allocator: &'a oxc::allocator::Allocator,
) -> Result<oxc::ast::ast::Statement<'a>, TsSynError> {
let source_type = oxc::span::SourceType::ts()
.with_typescript(true)
.with_jsx(self.file_name.ends_with(".tsx"));
let ret = oxc::parser::Parser::new(allocator, &self.source, source_type).parse();
if !ret.diagnostics.is_empty() {
return Err(TsSynError::Parse(format!(
"parse errors: {:?}",
ret.diagnostics
)));
}
ret.program
.body
.into_iter()
.next()
.ok_or_else(|| TsSynError::Parse("No statement found".to_string()))
}
}
impl TsStream {
pub fn new(source: &str, file_name: &str) -> Result<Self, TsSynError> {
Ok(TsStream {
source: source.to_string(),
file_name: file_name.to_string(),
ctx: None,
runtime_patches: vec![],
insert_pos: crate::abi::InsertPos::default(),
cross_module_suffixes: vec![],
cross_module_type_suffixes: vec![],
diagnostics: vec![],
})
}
pub fn from_string(source: String) -> Self {
TsStream {
source,
file_name: "macro_output.ts".to_string(),
ctx: None,
runtime_patches: vec![],
insert_pos: crate::abi::InsertPos::default(),
cross_module_suffixes: vec![],
cross_module_type_suffixes: vec![],
diagnostics: vec![],
}
}
pub fn with_insert_pos(source: String, insert_pos: crate::abi::InsertPos) -> Self {
TsStream {
source,
file_name: "macro_output.ts".to_string(),
ctx: None,
runtime_patches: vec![],
insert_pos,
cross_module_suffixes: vec![],
cross_module_type_suffixes: vec![],
diagnostics: vec![],
}
}
pub fn source(&self) -> &str {
&self.source
}
pub fn take_source(&mut self) -> String {
std::mem::take(&mut self.source)
}
pub fn splice_source_into(&mut self, out: &mut String) {
let line_start = out.rfind('\n').map_or(0, |newline| newline + 1);
let current = out.get(line_start..).unwrap_or_default();
let indent = current
.get(..current.len() - current.trim_start_matches(' ').len())
.unwrap_or_default()
.to_string();
let source = self.take_source();
for (index, line) in source.split('\n').enumerate() {
if index > 0 {
out.push('\n');
if line.trim().is_empty() {
continue;
}
out.push_str(&indent);
}
out.push_str(line);
}
}
pub fn add_diagnostic(&mut self, diagnostic: crate::abi::Diagnostic) {
self.diagnostics.push(diagnostic);
}
pub fn add_diagnostics(
&mut self,
diagnostics: impl IntoIterator<Item = crate::abi::Diagnostic>,
) {
self.diagnostics.extend(diagnostics);
}
pub fn diagnostics(&self) -> &[crate::abi::Diagnostic] {
&self.diagnostics
}
pub fn with_context(
source: &str,
file_name: &str,
ctx: crate::abi::MacroContextIR,
) -> Result<Self, TsSynError> {
Ok(TsStream {
source: source.to_string(),
file_name: file_name.to_string(),
ctx: Some(ctx),
runtime_patches: vec![],
insert_pos: crate::abi::InsertPos::default(),
cross_module_suffixes: vec![],
cross_module_type_suffixes: vec![],
diagnostics: vec![],
})
}
pub fn context(&self) -> Option<&crate::abi::MacroContextIR> {
self.ctx.as_ref()
}
pub fn into_result(self) -> crate::abi::MacroResult {
let imports = crate::import_registry::with_registry_mut(|r| r.take_generated_imports());
crate::abi::MacroResult {
runtime_patches: self.runtime_patches,
type_patches: vec![],
diagnostics: self.diagnostics,
tokens: Some(self.source),
insert_pos: self.insert_pos,
debug: None,
cross_module_suffixes: self.cross_module_suffixes,
cross_module_type_suffixes: self.cross_module_type_suffixes,
imports,
registry_reads: None,
}
}
pub fn add_import(&mut self, specifier: &str, module: &str) {
let (original, local) = parse_import_specifier(specifier);
crate::import_registry::with_registry_mut(|r| {
r.request_import(&local, original.as_deref(), module, false);
});
}
pub fn add_type_import(&mut self, specifier: &str, module: &str) {
let (original, local) = parse_import_specifier(specifier);
crate::import_registry::with_registry_mut(|r| {
r.request_import(&local, original.as_deref(), module, true);
});
}
pub fn module_specifier_for(&self, type_name: &str) -> Option<String> {
if let Some(ctx) = self.ctx.as_ref() {
return ctx.import_specifier_for(type_name);
}
crate::context_registry::with_context(|ctx| {
ctx.and_then(|c| c.import_specifier_for(type_name))
})
}
pub fn add_import_for(&mut self, local_name: &str, type_name: &str) -> bool {
let Some(module) = self.module_specifier_for(type_name) else {
return false;
};
self.add_import(local_name, &module);
true
}
pub fn add_type_import_for(&mut self, local_name: &str, type_name: &str) -> bool {
let Some(module) = self.module_specifier_for(type_name) else {
return false;
};
self.add_type_import(local_name, &module);
true
}
pub fn add_helpers_for(&mut self, type_name: &str, helpers: &[(&str, bool)]) -> bool {
let Some(module) = self.module_specifier_for(type_name) else {
return false;
};
for (name, is_type) in helpers {
if *is_type {
self.add_type_import(name, &module);
} else {
self.add_import(name, &module);
}
}
true
}
pub fn add_aliased_import(&mut self, name: &str, module: &str) {
let alias = format!("__mf_{name}");
crate::import_registry::with_registry_mut(|r| {
r.request_import(&alias, Some(name), module, false);
});
}
pub fn add_aliased_type_import(&mut self, name: &str, module: &str) {
let alias = format!("__mf_{name}");
crate::import_registry::with_registry_mut(|r| {
r.request_import(&alias, Some(name), module, true);
});
}
pub fn add_import_as(&mut self, name: &str, alias: &str, module: &str) {
crate::import_registry::with_registry_mut(|r| {
r.request_import(alias, Some(name), module, false);
});
}
pub fn add_type_import_as(&mut self, name: &str, alias: &str, module: &str) {
crate::import_registry::with_registry_mut(|r| {
r.request_import(alias, Some(name), module, true);
});
}
pub fn add_imports(&mut self, imports: &[ImportConfig]) {
crate::import_registry::with_registry_mut(|r| {
for import in imports {
r.request_import(
import.alias,
Some(import.name),
import.module,
import.is_type,
);
}
});
}
pub fn add_cross_module_suffix(&mut self, suffix: &str) {
self.cross_module_suffixes.push(suffix.to_string());
}
pub fn add_cross_module_type_suffix(&mut self, suffix: &str) {
self.cross_module_type_suffixes.push(suffix.to_string());
}
pub fn merge(mut self, other: Self) -> Self {
if !self.source.is_empty() && !other.source.is_empty() {
let left_ends_ws = self
.source
.chars()
.last()
.map(|c| c.is_whitespace())
.unwrap_or(false);
let right_starts_ws = other
.source
.chars()
.next()
.map(|c| c.is_whitespace())
.unwrap_or(false);
if !(left_ends_ws || right_starts_ws) {
self.source.push('\n');
}
}
self.source.push_str(&other.source);
self.runtime_patches.extend(other.runtime_patches);
self.cross_module_suffixes
.extend(other.cross_module_suffixes);
self.cross_module_type_suffixes
.extend(other.cross_module_type_suffixes);
self.diagnostics.extend(other.diagnostics);
self
}
pub fn merge_all(streams: impl IntoIterator<Item = Self>) -> Self {
let mut iter = streams.into_iter();
match iter.next() {
Some(first) => iter.fold(first, |acc, stream| acc.merge(stream)),
None => Self::from_string(String::new()),
}
}
}
pub trait ParseTs: Sized {
fn parse(input: &mut TsStream) -> Result<Self, TsSynError>;
}
fn parse_import_specifier(specifier: &str) -> (Option<String>, String) {
if let Some(idx) = specifier.find(" as ") {
let original = specifier[..idx].trim().to_string();
let local = specifier[idx + 4..].trim().to_string();
(Some(original), local)
} else {
(None, specifier.trim().to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_cross_module_suffix() {
let mut stream = TsStream::from_string("export function foo() {}".to_string());
assert!(stream.cross_module_suffixes.is_empty());
stream.add_cross_module_suffix("GetFields");
assert_eq!(stream.cross_module_suffixes, vec!["GetFields"]);
stream.add_cross_module_suffix("CustomSuffix");
assert_eq!(
stream.cross_module_suffixes,
vec!["GetFields", "CustomSuffix"]
);
}
#[test]
fn test_cross_module_suffixes_propagate_to_result() {
let mut stream = TsStream::from_string("export function foo() {}".to_string());
stream.add_cross_module_suffix("GetFields");
stream.add_cross_module_suffix("OtherSuffix");
let result = stream.into_result();
assert_eq!(
result.cross_module_suffixes,
vec!["GetFields", "OtherSuffix"]
);
}
#[test]
fn test_merge_combines_cross_module_suffixes() {
let mut a = TsStream::from_string("export function a() {}".to_string());
a.add_cross_module_suffix("GetFields");
let mut b = TsStream::from_string("export function b() {}".to_string());
b.add_cross_module_suffix("CustomSuffix");
let merged = a.merge(b);
assert_eq!(
merged.cross_module_suffixes,
vec!["GetFields", "CustomSuffix"]
);
}
#[test]
fn test_empty_cross_module_suffixes_by_default() {
let stream = TsStream::from_string("const x = 1;".to_string());
assert!(stream.cross_module_suffixes.is_empty());
let result = stream.into_result();
assert!(result.cross_module_suffixes.is_empty());
}
}
#[cfg(test)]
mod import_for_tests {
use super::*;
use crate::abi::ir::context::{MacroContextIR, MacroKind, TargetIR};
use crate::abi::ir::interface::InterfaceIR;
use crate::abi::ir::type_registry::{TypeDefinitionIR, TypeRegistry, TypeRegistryEntry};
use crate::import_registry::{ImportRegistry, take_registry};
use crate::{SpanIR, context_registry};
fn empty_interface(name: &str) -> InterfaceIR {
InterfaceIR {
name: name.to_string(),
span: SpanIR::new(0, 0),
body_span: SpanIR::new(0, 0),
type_params: vec![],
heritage: vec![],
fields: vec![],
methods: vec![],
decorators: vec![],
}
}
fn make_ctx_with_registry(file_name: &str, type_name: &str, type_path: &str) -> MacroContextIR {
let mut registry = TypeRegistry::new();
registry.types.insert(
type_name.to_string(),
TypeRegistryEntry {
name: type_name.to_string(),
file_path: type_path.to_string(),
is_exported: true,
definition: TypeDefinitionIR::Interface(empty_interface(type_name)),
file_imports: vec![],
},
);
MacroContextIR {
abi_version: 1,
macro_kind: MacroKind::Derive,
macro_name: "Test".to_string(),
module_path: "@test".to_string(),
decorator_span: SpanIR::new(0, 0),
macro_name_span: None,
target_span: SpanIR::new(0, 0),
file_name: file_name.to_string(),
target: TargetIR::Interface(empty_interface("Probe")),
target_source: String::new(),
import_registry: ImportRegistry::new(),
config: None,
type_registry: registry,
resolved_fields: None,
expansion_id: 0,
}
}
fn reset_thread_locals() {
context_registry::clear_context();
let _ = take_registry();
crate::import_registry::install_registry(ImportRegistry::new());
}
#[test]
fn add_import_for_resolves_via_thread_local_context() {
reset_thread_locals();
let ctx = make_ctx_with_registry(
"/proj/src/order.svelte.ts",
"Customer",
"/proj/src/customer.svelte.ts",
);
context_registry::install_context(ctx);
let mut stream = TsStream::from_string(String::new());
let added = stream.add_import_for("customerDefaultValue", "Customer");
assert!(added);
let result = stream.into_result();
let customer = result
.imports
.iter()
.find(|i| i.local_name == "customerDefaultValue")
.expect("expected customerDefaultValue import to be queued");
assert_eq!(customer.source_module, "./customer.svelte");
assert!(!customer.is_type_only);
context_registry::clear_context();
}
#[test]
fn add_helpers_for_batches_correctly() {
reset_thread_locals();
let ctx = make_ctx_with_registry(
"/proj/src/order.svelte.ts",
"Customer",
"/proj/src/customer.svelte.ts",
);
context_registry::install_context(ctx);
let mut stream = TsStream::from_string(String::new());
let added = stream.add_helpers_for(
"Customer",
&[
("customerDefaultValue", false),
("CustomerErrors", true),
("customerHasShape", false),
],
);
assert!(added);
let result = stream.into_result();
let value = result
.imports
.iter()
.find(|i| i.local_name == "customerDefaultValue")
.expect("value import missing");
assert_eq!(value.source_module, "./customer.svelte");
assert!(!value.is_type_only);
let type_import = result
.imports
.iter()
.find(|i| i.local_name == "CustomerErrors")
.expect("type import missing");
assert_eq!(type_import.source_module, "./customer.svelte");
assert!(type_import.is_type_only);
let has_shape = result
.imports
.iter()
.find(|i| i.local_name == "customerHasShape")
.expect("has_shape import missing");
assert_eq!(has_shape.source_module, "./customer.svelte");
assert!(!has_shape.is_type_only);
context_registry::clear_context();
}
#[test]
fn add_import_for_returns_false_without_context() {
reset_thread_locals();
let mut stream = TsStream::from_string(String::new());
let added = stream.add_import_for("customerDefaultValue", "Customer");
assert!(!added);
let result = stream.into_result();
assert!(result.imports.is_empty());
}
#[test]
fn add_import_for_returns_false_for_co_located_type() {
reset_thread_locals();
let ctx = make_ctx_with_registry(
"/proj/src/customer.svelte.ts",
"Customer",
"/proj/src/customer.svelte.ts",
);
context_registry::install_context(ctx);
let mut stream = TsStream::from_string(String::new());
let added = stream.add_import_for("customerDefaultValue", "Customer");
assert!(!added);
let result = stream.into_result();
assert!(result.imports.is_empty());
context_registry::clear_context();
}
#[test]
fn add_helpers_for_emits_bare_variant_type_import() {
reset_thread_locals();
let ctx = make_ctx_with_registry(
"/proj/src/employee.svelte.ts",
"LinkedUser",
"/proj/src/linked-user.svelte.ts",
);
context_registry::install_context(ctx);
crate::import_registry::with_registry_mut(|r| {
r.install_source_imports(vec![crate::import_registry::SourceImportEntry {
local_name: "LinkedUser".to_string(),
source_module: "./linked-user.svelte".to_string(),
original_name: None,
is_type_only: true,
}]);
});
let mut stream = TsStream::from_string(String::new());
let added = stream.add_helpers_for(
"LinkedUser",
&[
("LinkedUserFieldControllers", true),
("LinkedUser", true),
("linkedUserGetControllers", false),
("linkedUserDefaultErrors", false),
("linkedUserDefaultTainted", false),
("linkedUserDefaultValue", false),
("linkedUserIs", false),
],
);
assert!(added, "add_helpers_for should have resolved LinkedUser");
let result = stream.into_result();
let field_controllers = result
.imports
.iter()
.find(|i| i.local_name == "LinkedUserFieldControllers")
.expect("LinkedUserFieldControllers type import missing");
assert_eq!(field_controllers.source_module, "./linked-user.svelte");
assert!(field_controllers.is_type_only);
let bare = result.imports.iter().find(|i| i.local_name == "LinkedUser");
assert!(
bare.is_none(),
"bare LinkedUser should be dedup'd against source_imports: \
callers must NOT pre-register variant types in source_imports \
if they also want a generated `import type {{ LinkedUser }}` line"
);
for name in [
"linkedUserGetControllers",
"linkedUserDefaultErrors",
"linkedUserDefaultTainted",
"linkedUserDefaultValue",
"linkedUserIs",
] {
let entry = result
.imports
.iter()
.find(|i| i.local_name == name)
.unwrap_or_else(|| panic!("{name} value import missing"));
assert_eq!(entry.source_module, "./linked-user.svelte");
assert!(!entry.is_type_only);
}
context_registry::clear_context();
}
#[test]
fn add_helpers_for_emits_bare_type_when_source_imports_clean() {
reset_thread_locals();
let ctx = make_ctx_with_registry(
"/proj/src/entry.svelte.ts",
"CustomerReferral",
"/proj/src/customer-referral.svelte.ts",
);
context_registry::install_context(ctx);
let mut stream = TsStream::from_string(String::new());
let added = stream.add_helpers_for(
"CustomerReferral",
&[
("CustomerReferralFieldControllers", true),
("CustomerReferral", true),
("customerReferralIs", false),
],
);
assert!(added);
let result = stream.into_result();
let bare = result
.imports
.iter()
.find(|i| i.local_name == "CustomerReferral")
.expect("bare CustomerReferral type import missing");
assert_eq!(bare.source_module, "./customer-referral.svelte");
assert!(bare.is_type_only);
context_registry::clear_context();
}
}