1use crate::{
4 compiler::{cfg, diagnostics::CompileError},
5 options::{CompileOptions, TargetOs},
6};
7use miette::SourceSpan;
8use oxc_allocator::Allocator;
9use oxc_ast::ast::{
10 BindingPattern, Declaration, ImportDeclarationSpecifier, ModuleExportName, Statement,
11};
12use oxc_parser::Parser;
13use oxc_span::{GetSpan, SourceType};
14use std::{
15 collections::{HashMap, HashSet},
16 fs,
17 path::{Component, Path, PathBuf},
18};
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21pub enum BuiltinModule {
22 Fs,
23 Process,
24 Unix,
25 Windows,
26 Linux,
27 Macos,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub enum ImportTarget {
32 Local(PathBuf),
33 Builtin(BuiltinModule),
34}
35
36#[derive(Debug, Clone)]
37pub struct ImportBinding {
38 pub imported: String,
39 pub local: String,
40 pub target: ImportTarget,
41 pub span: SourceSpan,
42 pub type_only: bool,
45}
46
47#[derive(Debug, Clone)]
48pub struct SourceUnit {
49 pub path: PathBuf,
50 pub source: String,
52 pub original: String,
54 pub imports: Vec<ImportBinding>,
55 pub exports: Vec<String>,
56}
57
58#[derive(Debug)]
59struct PendingBinding {
60 imported: String,
61 local: String,
62 span: SourceSpan,
63 type_only: bool,
64}
65
66#[derive(Debug)]
67struct PendingImport {
68 target: ImportTarget,
69 specifier: String,
70 span: SourceSpan,
71 bindings: Vec<PendingBinding>,
72}
73
74#[derive(Default)]
75struct Loader {
76 units: Vec<SourceUnit>,
77 exports: HashMap<PathBuf, HashSet<String>>,
78 visited: HashSet<PathBuf>,
79 stack: Vec<PathBuf>,
80}
81
82pub fn load(path: &Path, options: &CompileOptions) -> Result<Vec<SourceUnit>, CompileError> {
85 let path = fs::canonicalize(path).map_err(|error| {
86 CompileError::plain(format!(
87 "cannot open source file `{}`: {error}",
88 path.display()
89 ))
90 })?;
91 let source = fs::read_to_string(&path).map_err(|error| {
92 CompileError::plain(format!(
93 "cannot read source file `{}`: {error}",
94 path.display()
95 ))
96 })?;
97 load_source(&path, &source, options)
98}
99
100pub fn load_source(
103 name: &Path,
104 source: &str,
105 options: &CompileOptions,
106) -> Result<Vec<SourceUnit>, CompileError> {
107 let path = if name.is_absolute() {
108 normalized(name)
109 } else {
110 normalized(&std::env::current_dir().unwrap_or_default().join(name))
111 };
112 let mut loader = Loader::default();
113 loader.visit(path, source.to_owned(), options, None)?;
114 Ok(loader.units)
115}
116
117impl Loader {
118 fn visit(
119 &mut self,
120 path: PathBuf,
121 original: String,
122 options: &CompileOptions,
123 via: Option<(&Path, &str, usize, usize)>,
124 ) -> Result<(), CompileError> {
125 if self.visited.contains(&path) {
126 return Ok(());
127 }
128 if let Some(cycle_start) = self.stack.iter().position(|entry| entry == &path) {
129 let chain = self.stack[cycle_start..]
130 .iter()
131 .chain(std::iter::once(&path))
132 .map(|entry| entry.display().to_string())
133 .collect::<Vec<_>>()
134 .join(" -> ");
135 if let Some((importer_path, importer_source, start, len)) = via {
136 return Err(CompileError::new(
137 importer_path.display().to_string(),
138 importer_source,
139 start,
140 len,
141 format!("module cycle detected: {chain}"),
142 ));
143 }
144 return Err(CompileError::plain(format!(
145 "module cycle detected: {chain}"
146 )));
147 }
148
149 let source = cfg::preprocess(&path, &original, options)?;
150 let (pending_imports, exports) = module_metadata(&path, &source, options)?;
151 self.stack.push(path.clone());
152
153 for import in &pending_imports {
156 if let ImportTarget::Local(target) = &import.target {
157 let imported_source = fs::read_to_string(target).map_err(|error| {
158 CompileError::new(
159 path.display().to_string(),
160 &original,
161 import.span.offset(),
162 import.span.len(),
163 format!(
164 "cannot read imported module `{}`: {error}",
165 target.display()
166 ),
167 )
168 })?;
169 self.visit(
170 target.clone(),
171 imported_source,
172 options,
173 Some((&path, &original, import.span.offset(), import.span.len())),
174 )?;
175 }
176 }
177
178 let mut imports = Vec::new();
179 for import in &pending_imports {
180 match &import.target {
181 ImportTarget::Builtin(module) => {
182 for binding in &import.bindings {
183 if !builtin_exports(*module).contains(&binding.imported.as_str()) {
184 return Err(CompileError::new(
185 path.display().to_string(),
186 &original,
187 binding.span.offset(),
188 binding.span.len(),
189 format!(
190 "`{}` is not exported by `{}`",
191 binding.imported, import.specifier
192 ),
193 ));
194 }
195 imports.push(ImportBinding {
196 imported: binding.imported.clone(),
197 local: binding.local.clone(),
198 target: import.target.clone(),
199 span: binding.span,
200 type_only: binding.type_only,
201 });
202 }
203 }
204 ImportTarget::Local(target) => {
205 let target_exports = self.exports.get(target).cloned().unwrap_or_default();
206 for binding in &import.bindings {
207 if !target_exports.contains(&binding.imported) {
208 return Err(CompileError::new(
209 path.display().to_string(),
210 &original,
211 binding.span.offset(),
212 binding.span.len(),
213 format!(
214 "`{}` is not exported by `{}`",
215 binding.imported,
216 target.display()
217 ),
218 ));
219 }
220 imports.push(ImportBinding {
221 imported: binding.imported.clone(),
222 local: binding.local.clone(),
223 target: import.target.clone(),
224 span: binding.span,
225 type_only: binding.type_only,
226 });
227 }
228 }
229 }
230 }
231
232 self.stack.pop();
233 self.visited.insert(path.clone());
234 self.exports
235 .insert(path.clone(), exports.iter().cloned().collect());
236 self.units.push(SourceUnit {
237 path,
238 source,
239 original,
240 imports,
241 exports,
242 });
243 Ok(())
244 }
245}
246
247fn module_metadata(
248 path: &Path,
249 source: &str,
250 options: &CompileOptions,
251) -> Result<(Vec<PendingImport>, Vec<String>), CompileError> {
252 let allocator = Allocator::default();
255 let parsed = Parser::new(&allocator, source, SourceType::ts()).parse();
256 let mut imports = Vec::new();
257 let mut exports = Vec::new();
258 let mut exported = HashSet::new();
259 let mut declared = HashSet::new();
260 let mut named_exports = Vec::new();
261
262 for statement in &parsed.program.body {
263 match statement {
264 Statement::ImportDeclaration(declaration) => {
265 let import = parse_import(path, source, declaration, options)?;
266 declared.extend(import.bindings.iter().map(|binding| binding.local.clone()));
267 imports.push(import);
268 }
269 Statement::ExportDeclaration(export) => {
270 collect_declaration_names(&export.declaration, &mut declared);
271 collect_declaration_exports(&export.declaration, &mut exports);
272 }
273 Statement::ExportNamedDeclaration(export) => {
274 for specifier in &export.specifiers {
275 let local = export_name(&specifier.local);
276 let name = export_name(&specifier.exported);
277 if local != name {
278 return Err(CompileError::new(
279 path.display().to_string(),
280 source,
281 specifier.span.start as usize,
282 (specifier.span.end - specifier.span.start) as usize,
283 "export aliases are not supported yet",
284 ));
285 }
286 let span: SourceSpan = (
287 specifier.span.start as usize,
288 (specifier.span.end - specifier.span.start) as usize,
289 )
290 .into();
291 named_exports.push((name, span));
292 }
293 }
294 Statement::ExportFromDeclaration(export) => {
295 return Err(CompileError::new(
296 path.display().to_string(),
297 source,
298 export.span.start as usize,
299 (export.span.end - export.span.start) as usize,
300 "re-export declarations are not supported yet",
301 ));
302 }
303 Statement::ExportAllDeclaration(export) => {
304 return Err(CompileError::new(
305 path.display().to_string(),
306 source,
307 export.span.start as usize,
308 (export.span.end - export.span.start) as usize,
309 "star exports are not supported yet",
310 ));
311 }
312 Statement::ExportDefaultDeclaration(export) => {
313 return Err(CompileError::new(
314 path.display().to_string(),
315 source,
316 export.span.start as usize,
317 (export.span.end - export.span.start) as usize,
318 "default exports are not supported yet",
319 ));
320 }
321 Statement::VariableDeclaration(declaration) => {
322 collect_variable_names(declaration, &mut declared);
323 }
324 Statement::FunctionDeclaration(function) => {
325 if let Some(id) = &function.id {
326 declared.insert(id.name.as_str().to_owned());
327 }
328 }
329 Statement::ClassDeclaration(class) => {
330 if let Some(id) = &class.id {
331 declared.insert(id.name.as_str().to_owned());
332 }
333 }
334 Statement::TSTypeAliasDeclaration(alias) => {
335 declared.insert(alias.id.name.as_str().to_owned());
336 }
337 Statement::TSInterfaceDeclaration(interface) => {
338 declared.insert(interface.id.name.as_str().to_owned());
339 }
340 Statement::TSEnumDeclaration(enumeration) => {
341 declared.insert(enumeration.id.name.as_str().to_owned());
342 }
343 _ => {}
344 }
345 }
346 for (name, span) in named_exports {
347 if !declared.contains(&name) {
348 return Err(CompileError::new(
349 path.display().to_string(),
350 source,
351 span.offset(),
352 span.len(),
353 format!("cannot export `{name}` because this module does not declare or import it"),
354 ));
355 }
356 exports.push(name);
357 }
358 for name in &exports {
359 if !exported.insert(name.clone()) {
360 return Err(CompileError::new(
361 path.display().to_string(),
362 source,
363 0,
364 0,
365 format!("duplicate export `{name}`"),
366 ));
367 }
368 }
369 Ok((imports, exports))
370}
371
372fn parse_import(
373 path: &Path,
374 source: &str,
375 declaration: &oxc_ast::ast::ImportDeclaration<'_>,
376 options: &CompileOptions,
377) -> Result<PendingImport, CompileError> {
378 let specifier = declaration.source.value.as_str().to_owned();
379 let span: SourceSpan = (
380 declaration.span.start as usize,
381 (declaration.span.end - declaration.span.start) as usize,
382 )
383 .into();
384 let Some(specifiers) = declaration.specifiers.as_ref() else {
385 return Err(CompileError::new(
386 path.display().to_string(),
387 source,
388 span.offset(),
389 span.len(),
390 "side-effect imports are not supported; import named symbols instead",
391 ));
392 };
393
394 let mut bindings = Vec::new();
395 for specifier_node in specifiers {
396 let ImportDeclarationSpecifier::ImportSpecifier(specifier_node) = specifier_node else {
397 return Err(CompileError::new(
398 path.display().to_string(),
399 source,
400 specifier_node.span().start as usize,
401 (specifier_node.span().end - specifier_node.span().start) as usize,
402 "default and namespace imports are not supported; use named imports",
403 ));
404 };
405 let imported = match &specifier_node.imported {
406 ModuleExportName::IdentifierName(name) => name.name.as_str().to_owned(),
407 ModuleExportName::IdentifierReference(name) => name.name.as_str().to_owned(),
408 ModuleExportName::StringLiteral(_) => {
409 return Err(CompileError::new(
410 path.display().to_string(),
411 source,
412 specifier_node.span.start as usize,
413 (specifier_node.span.end - specifier_node.span.start) as usize,
414 "string-named imports are not supported",
415 ));
416 }
417 };
418 let local = specifier_node.local.name.as_str().to_owned();
419 if imported != local {
420 return Err(CompileError::new(
421 path.display().to_string(),
422 source,
423 specifier_node.span.start as usize,
424 (specifier_node.span.end - specifier_node.span.start) as usize,
425 "import aliases are not supported yet; imported and local names must match",
426 ));
427 }
428 let type_only = declaration.import_kind == oxc_ast::ast::ImportOrExportKind::Type
429 || specifier_node.import_kind == oxc_ast::ast::ImportOrExportKind::Type;
430 let specifier_span: SourceSpan = (
431 specifier_node.span.start as usize,
432 (specifier_node.span.end - specifier_node.span.start) as usize,
433 )
434 .into();
435 bindings.push(PendingBinding {
436 imported,
437 local,
438 span: specifier_span,
439 type_only,
440 });
441 }
442 let target = resolve_target(
445 path,
446 source,
447 &specifier,
448 declaration.span.start as usize,
449 (declaration.span.end - declaration.span.start) as usize,
450 options,
451 )?;
452 Ok(PendingImport {
453 target,
454 specifier,
455 span,
456 bindings,
457 })
458}
459
460fn resolve_target(
461 importer: &Path,
462 source: &str,
463 specifier: &str,
464 start: usize,
465 len: usize,
466 options: &CompileOptions,
467) -> Result<ImportTarget, CompileError> {
468 if let Some(module) = builtin_module(specifier) {
469 if !builtin_compatible(module, options.os) {
470 return Err(CompileError::new(
471 importer.display().to_string(),
472 source,
473 start,
474 len,
475 format!(
476 "module `{specifier}` is unavailable for target OS `{}`",
477 os_name(options.os)
478 ),
479 ));
480 }
481 return Ok(ImportTarget::Builtin(module));
482 }
483 if specifier.starts_with("tsh:") {
484 return Err(CompileError::new(
485 importer.display().to_string(),
486 source,
487 start,
488 len,
489 format!("unknown built-in module `{specifier}`"),
490 ));
491 }
492 if !(specifier.starts_with("./") || specifier.starts_with("../")) {
493 return Err(CompileError::new(
494 importer.display().to_string(),
495 source,
496 start,
497 len,
498 format!(
499 "module `{specifier}` is not supported; imports must be relative `.tsh`/`.ts` files or `tsh:` built-ins"
500 ),
501 ));
502 }
503 let requested = Path::new(specifier);
504 if requested
505 .extension()
506 .is_some_and(|extension| extension != "tsh" && extension != "ts")
507 {
508 return Err(CompileError::new(
509 importer.display().to_string(),
510 source,
511 start,
512 len,
513 format!("local module `{specifier}` must use `.tsh` or `.ts` extension"),
514 ));
515 }
516 let base = importer.parent().unwrap_or_else(|| Path::new("."));
517 let joined = base.join(requested);
518 let candidates = if requested.extension().is_some() {
519 vec![joined]
520 } else {
521 vec![
522 joined.with_extension("tsh"),
523 joined.with_extension("ts"),
524 joined.join("index.tsh"),
525 joined.join("index.ts"),
526 ]
527 };
528 for candidate in candidates {
529 if candidate.is_file() {
530 return fs::canonicalize(&candidate)
531 .map(ImportTarget::Local)
532 .map_err(|error| {
533 CompileError::new(
534 importer.display().to_string(),
535 source,
536 start,
537 len,
538 format!("cannot resolve module `{specifier}`: {error}"),
539 )
540 });
541 }
542 }
543 Err(CompileError::new(
544 importer.display().to_string(),
545 source,
546 start,
547 len,
548 format!("cannot resolve local module `{specifier}` (tried `.tsh` and `.ts`)"),
549 ))
550}
551
552fn builtin_module(specifier: &str) -> Option<BuiltinModule> {
553 Some(match specifier {
554 "tsh:fs" => BuiltinModule::Fs,
555 "tsh:process" => BuiltinModule::Process,
556 "tsh:unix" => BuiltinModule::Unix,
557 "tsh:windows" => BuiltinModule::Windows,
558 "tsh:linux" => BuiltinModule::Linux,
559 "tsh:macos" => BuiltinModule::Macos,
560 _ => return None,
561 })
562}
563
564fn builtin_compatible(module: BuiltinModule, os: TargetOs) -> bool {
565 match module {
566 BuiltinModule::Fs | BuiltinModule::Process => true,
567 BuiltinModule::Unix => os.is_unix(),
568 BuiltinModule::Windows => os == TargetOs::Windows,
569 BuiltinModule::Linux => os == TargetOs::Linux,
570 BuiltinModule::Macos => os == TargetOs::Macos,
571 }
572}
573
574fn builtin_exports(module: BuiltinModule) -> &'static [&'static str] {
575 match module {
576 BuiltinModule::Fs => &["echo", "mkdir", "rm", "cp", "mv", "cd"],
577 BuiltinModule::Process => &["run", "env", "exit"],
578 BuiltinModule::Unix => &["chmod"],
579 BuiltinModule::Windows | BuiltinModule::Linux | BuiltinModule::Macos => &[],
580 }
581}
582
583fn collect_declaration_exports(declaration: &Declaration<'_>, exports: &mut Vec<String>) {
584 match declaration {
585 Declaration::FunctionDeclaration(function) => {
586 if let Some(id) = &function.id {
587 exports.push(id.name.as_str().to_owned());
588 }
589 }
590 Declaration::ClassDeclaration(class) => {
591 if let Some(id) = &class.id {
592 exports.push(id.name.as_str().to_owned());
593 }
594 }
595 Declaration::TSTypeAliasDeclaration(alias) => {
596 exports.push(alias.id.name.as_str().to_owned())
597 }
598 Declaration::TSInterfaceDeclaration(interface) => {
599 exports.push(interface.id.name.as_str().to_owned())
600 }
601 Declaration::TSEnumDeclaration(enumeration) => {
602 exports.push(enumeration.id.name.as_str().to_owned())
603 }
604 Declaration::VariableDeclaration(variable) => {
605 for name in variable_names(variable) {
606 exports.push(name);
607 }
608 }
609 _ => {}
610 }
611}
612
613fn collect_declaration_names(declaration: &Declaration<'_>, names: &mut HashSet<String>) {
614 match declaration {
615 Declaration::VariableDeclaration(variable) => collect_variable_names(variable, names),
616 Declaration::FunctionDeclaration(function) => {
617 if let Some(id) = &function.id {
618 names.insert(id.name.as_str().to_owned());
619 }
620 }
621 Declaration::ClassDeclaration(class) => {
622 if let Some(id) = &class.id {
623 names.insert(id.name.as_str().to_owned());
624 }
625 }
626 Declaration::TSTypeAliasDeclaration(alias) => {
627 names.insert(alias.id.name.as_str().to_owned());
628 }
629 Declaration::TSInterfaceDeclaration(interface) => {
630 names.insert(interface.id.name.as_str().to_owned());
631 }
632 Declaration::TSEnumDeclaration(enumeration) => {
633 names.insert(enumeration.id.name.as_str().to_owned());
634 }
635 _ => {}
636 }
637}
638
639fn collect_variable_names(
640 variable: &oxc_ast::ast::VariableDeclaration<'_>,
641 names: &mut HashSet<String>,
642) {
643 names.extend(variable_names(variable));
644}
645
646fn variable_names(variable: &oxc_ast::ast::VariableDeclaration<'_>) -> Vec<String> {
647 variable
648 .declarations
649 .iter()
650 .filter_map(|declarator| {
651 if let BindingPattern::BindingIdentifier(identifier) = &declarator.id {
652 Some(identifier.name.as_str().to_owned())
653 } else {
654 None
655 }
656 })
657 .collect()
658}
659
660fn export_name(name: &ModuleExportName<'_>) -> String {
661 match name {
662 ModuleExportName::IdentifierName(identifier) => identifier.name.as_str().to_owned(),
663 ModuleExportName::IdentifierReference(identifier) => identifier.name.as_str().to_owned(),
664 ModuleExportName::StringLiteral(literal) => literal.value.as_str().to_owned(),
665 }
666}
667
668fn os_name(os: TargetOs) -> &'static str {
669 match os {
670 TargetOs::Windows => "windows",
671 TargetOs::Linux => "linux",
672 TargetOs::Macos => "macos",
673 TargetOs::Freebsd => "freebsd",
674 }
675}
676
677fn normalized(path: &Path) -> PathBuf {
678 let mut result = PathBuf::new();
679 for component in path.components() {
680 match component {
681 Component::CurDir => {}
682 Component::ParentDir => {
683 result.pop();
684 }
685 other => result.push(other.as_os_str()),
686 }
687 }
688 result
689}
690
691#[cfg(test)]
692mod tests {
693 use super::*;
694 use crate::options::{Comments, Target};
695 use std::fs;
696
697 fn options(os: TargetOs) -> CompileOptions {
698 CompileOptions {
699 target: Target::Bash,
700 os,
701 comments: Comments::All,
702 }
703 }
704
705 #[test]
706 fn local_dependencies_are_bundled_before_importers_and_exports_are_checked() {
707 let dir = tempfile::tempdir().unwrap();
708 let lib = dir.path().join("lib.tsh");
709 fs::write(
710 &lib,
711 "export function add(a: number, b: number): number { return a + b; }",
712 )
713 .unwrap();
714 let main = dir.path().join("main.tsh");
715 let source = "import { add } from './lib';\nconst result = add(1, 2);\n";
716 let units = load_source(&main, source, &options(TargetOs::Linux)).unwrap();
717 assert_eq!(units.len(), 2);
718 assert!(units[0].path.ends_with("lib.tsh"));
719 assert_eq!(units[1].imports[0].imported, "add");
720
721 let missing = "import { nope } from './lib';\n";
722 assert!(load_source(&main, missing, &options(TargetOs::Linux)).is_err());
723 }
724
725 #[test]
726 fn rejects_os_incompatible_empty_imports_and_unknown_builtin_exports() {
727 let path = Path::new("main.tsh");
728 assert!(
729 load_source(
730 path,
731 "import {} from 'tsh:windows';",
732 &options(TargetOs::Linux)
733 )
734 .is_err()
735 );
736 assert!(
737 load_source(
738 path,
739 "import { chmod } from 'tsh:unix';",
740 &options(TargetOs::Windows)
741 )
742 .is_err()
743 );
744 assert!(
745 load_source(
746 path,
747 "import { readFile } from 'tsh:fs';",
748 &options(TargetOs::Linux)
749 )
750 .is_err()
751 );
752 }
753
754 #[test]
755 fn inactive_cfg_import_does_not_resolve_a_missing_file() {
756 let source =
757 "// @cfg(windows)\nimport { missing } from './does-not-exist';\nconst available = 1;\n";
758 let units = load_source(Path::new("main.tsh"), source, &options(TargetOs::Linux)).unwrap();
759 assert_eq!(units.len(), 1);
760 assert!(units[0].source.contains("const available = 1;"));
761 assert!(units[0].imports.is_empty());
762 }
763
764 #[test]
765 fn named_export_must_refer_to_a_local_declaration_or_import() {
766 let path = Path::new("main.tsh");
767 assert!(
768 load_source(path, "export { missing };", &options(TargetOs::Linux))
769 .unwrap_err()
770 .to_string()
771 .contains("does not declare or import")
772 );
773 let units = load_source(
774 path,
775 "const value = 1; export { value };",
776 &options(TargetOs::Linux),
777 )
778 .unwrap();
779 assert_eq!(units[0].exports, vec!["value"]);
780 }
781
782 #[test]
783 fn rejects_aliases_packages_and_cycles() {
784 let dir = tempfile::tempdir().unwrap();
785 let a = dir.path().join("a.tsh");
786 let b = dir.path().join("b.ts");
787 fs::write(&b, "import { a } from './a'; export function b() {}\n").unwrap();
788 fs::write(&a, "import { b } from './b'; export function a() {}\n").unwrap();
789 assert!(
790 load(&a, &options(TargetOs::Linux))
791 .unwrap_err()
792 .to_string()
793 .contains("cycle")
794 );
795 assert!(
796 load_source(
797 Path::new("main.tsh"),
798 "import { x as y } from './x';",
799 &options(TargetOs::Linux)
800 )
801 .unwrap_err()
802 .to_string()
803 .contains("aliases")
804 );
805 assert!(
806 load_source(
807 Path::new("main.tsh"),
808 "import { x } from 'npm-package';",
809 &options(TargetOs::Linux)
810 )
811 .unwrap_err()
812 .to_string()
813 .contains("relative")
814 );
815 }
816}