Skip to main content

blast_radius/parse/
facts.rs

1use std::collections::{BTreeMap, BTreeSet};
2use std::path::{Path, PathBuf};
3
4#[derive(Debug, Clone)]
5pub struct ModuleFacts {
6    pub file: PathBuf,
7    pub exports: Vec<ExportFact>,
8    pub imports: Vec<ImportFact>,
9    pub reexports: Vec<ReexportFact>,
10    pub used_locals: BTreeSet<String>,
11    pub jsx_locals: BTreeSet<String>,
12    pub namespace_member_usage: BTreeMap<String, BTreeSet<String>>,
13    pub jsx_namespace_member_usage: BTreeMap<String, BTreeSet<String>>,
14    pub warnings: Vec<String>,
15}
16
17impl ModuleFacts {
18    pub(super) fn empty(file: &Path) -> Self {
19        Self {
20            file: file.to_path_buf(),
21            exports: Vec::new(),
22            imports: Vec::new(),
23            reexports: Vec::new(),
24            used_locals: BTreeSet::new(),
25            jsx_locals: BTreeSet::new(),
26            namespace_member_usage: BTreeMap::new(),
27            jsx_namespace_member_usage: BTreeMap::new(),
28            warnings: Vec::new(),
29        }
30    }
31}
32
33#[derive(Debug, Clone)]
34pub struct ExportFact {
35    pub exported: String,
36    pub local: Option<String>,
37    pub kind: ExportKind,
38}
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum ExportKind {
42    Local,
43    Default,
44    Reexport,
45    CommonJs,
46}
47
48#[derive(Debug, Clone)]
49pub struct ImportFact {
50    pub source: String,
51    pub local: String,
52    pub imported: ImportTarget,
53    pub kind: ImportKind,
54    pub type_only: bool,
55}
56
57#[derive(Debug, Clone, PartialEq, Eq)]
58pub enum ImportTarget {
59    Name(String),
60    Default,
61    Namespace,
62    /// A specifier-less import (`import './setup'`, bare `require('./setup')`)
63    /// run only for its side effects: the importer depends on the whole file.
64    SideEffect,
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum ImportKind {
69    Esm,
70    CommonJs,
71    /// A dynamic `import("...")` call, e.g. a lazy route or code-split component.
72    Dynamic,
73}
74
75#[derive(Debug, Clone)]
76pub struct ReexportFact {
77    pub source: String,
78    pub imported: ReexportTarget,
79    pub exported: String,
80    pub is_ambiguous: bool,
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub enum ReexportTarget {
85    Name(String),
86    Default,
87    Namespace,
88    All,
89}