1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! Which exports of which modules are used, following re-export chains.
use std::collections::{BTreeSet, HashMap, HashSet, VecDeque};
use skif_js_syntax::{ExportKind, ImportKind, ImportedKind};
use skif_workspace::PackageId;
use crate::graph::{ModuleGraph, ModuleId, Target};
/// How the members of one export (an enum's or a namespace's) are used.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MemberUse {
/// Members read by name.
pub members: BTreeSet<String>,
/// The export is used in a way that may touch any member.
pub all: bool,
}
impl MemberUse {
fn everything() -> Self {
Self {
members: BTreeSet::new(),
all: true,
}
}
}
/// How a name reached a module.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Via {
/// Imported by name, as the default export or through a use that may touch anything.
Named,
/// Read as a member of a namespace (`ns.member`, `export * as ns` followed by `ns.member`).
Namespace,
}
enum Task {
Name(ModuleId, String, MemberUse, Via),
All(ModuleId),
}
/// The exports referenced from other modules, computed once for the whole graph.
#[derive(Debug, Clone, Default)]
pub struct ExportUsage {
used: HashMap<ModuleId, HashMap<String, MemberUse>>,
all: HashSet<ModuleId>,
/// Modules some export of which was reached by name or as a whole.
reached_by_name: HashSet<ModuleId>,
/// Modules some export of which was reached as a namespace member.
reached_by_namespace: HashSet<ModuleId>,
}
/// The re-exports of one module, resolved to the modules they come from.
#[derive(Default)]
struct Reexports {
/// `export { source as name } from`, by exported `name`.
named: HashMap<String, Vec<(ModuleId, String)>>,
/// `export * as name from`, by `name`.
namespaces: HashMap<String, Vec<ModuleId>>,
/// `export * from`.
stars: Vec<ModuleId>,
/// Every target with the name it is re-exported under: `Some((name, source))` for a named re-export.
all: Vec<(ModuleId, Option<(String, String)>)>,
/// Names the module defines or re-exports itself, excluding what `export *` forwards.
defined: HashSet<String>,
}
/// Re-export lookups for every module, computed once so that propagating a name touches only what can answer it.
struct ReexportIndex {
modules: Vec<Reexports>,
/// Per module, every name it exposes: its own and those forwarded by `export *`, transitively.
exposed: Vec<HashSet<String>>,
}
impl ReexportIndex {
fn build(graph: &ModuleGraph) -> Self {
let modules: Vec<Reexports> = graph
.modules()
.iter()
.map(|module| {
let mut sources: HashMap<&str, ModuleId> = HashMap::new();
for resolved in &module.imports {
let Some(import) = module.info.imports.get(resolved.import) else {
continue;
};
let Target::Module(id) = &resolved.target else {
continue;
};
// `export { x }` of an imported binding names the source of the `import`, not of an
// `export … from`, so any import of the specifier locates the module; an `export … from`
// wins when there are several.
match import.kind {
ImportKind::ExportFrom => {
sources.insert(import.specifier.as_str(), *id);
}
ImportKind::ImportMetaGlob | ImportKind::SideEffect => {}
_ => {
sources.entry(import.specifier.as_str()).or_insert(*id);
}
}
}
let mut reexports = Reexports::default();
for export in &module.info.exports {
if !(export.kind == ExportKind::StarReExport && export.name == "*") {
reexports.defined.insert(export.name.clone());
}
let Some(target) = export.from.as_deref().and_then(|from| sources.get(from)).copied() else {
continue;
};
match export.kind {
ExportKind::ReExport => {
let source = export.imported_name.clone().unwrap_or_else(|| export.name.clone());
reexports
.named
.entry(export.name.clone())
.or_default()
.push((target, source.clone()));
reexports.all.push((target, Some((export.name.clone(), source))));
}
ExportKind::StarReExport if export.name == "*" => {
reexports.stars.push(target);
reexports.all.push((target, None));
}
ExportKind::StarReExport => {
reexports
.namespaces
.entry(export.name.clone())
.or_default()
.push(target);
reexports.all.push((target, None));
}
_ => {}
}
}
reexports
})
.collect();
let mut exposed: Vec<HashSet<String>> = modules.iter().map(|m| m.defined.clone()).collect();
loop {
let mut changed = false;
for (index, reexports) in modules.iter().enumerate() {
for target in &reexports.stars {
let additions: Vec<String> = exposed[target.index()]
.iter()
.filter(|name| name.as_str() != "default" && !exposed[index].contains(*name))
.cloned()
.collect();
if !additions.is_empty() {
changed = true;
exposed[index].extend(additions);
}
}
}
if !changed {
break;
}
}
Self { modules, exposed }
}
}
impl ExportUsage {
/// Propagates every import of the graph to the exports it uses.
///
/// Exports of entry files count as used unless `include_entry_exports` is set.
#[must_use]
pub fn compute(graph: &ModuleGraph, include_entry_exports: bool) -> Self {
Self::compute_by(graph, |_| include_entry_exports)
}
/// Like [`compute`](Self::compute), with `include_entry_exports` decided per module: it is called with the owner of
/// each entry module.
#[must_use]
pub fn compute_by(graph: &ModuleGraph, include_entry_exports: impl Fn(PackageId) -> bool) -> Self {
let mut usage = Self::default();
let index = ReexportIndex::build(graph);
let mut queue = VecDeque::new();
for module in graph.modules() {
if module.is_entry && !include_entry_exports(module.owner) {
queue.push_back(Task::All(module.id));
}
for resolved in &module.imports {
let Some(import) = module.info.imports.get(resolved.import) else {
continue;
};
if matches!(import.kind, ImportKind::ExportFrom | ImportKind::SideEffect) {
continue;
}
let target = match &resolved.target {
Target::Module(id) => *id,
Target::Modules(ids) => {
queue.extend(ids.iter().map(|id| Task::All(*id)));
continue;
}
_ => continue,
};
if import.names.is_empty() {
queue.push_back(Task::All(target));
continue;
}
for name in &import.names {
let access = MemberUse {
members: name.accessed_members.iter().cloned().collect(),
all: name.escapes,
};
match &name.kind {
ImportedKind::Default => {
queue.push_back(Task::Name(target, "default".to_owned(), access, Via::Named));
}
ImportedKind::Named(export) => {
queue.push_back(Task::Name(target, export.clone(), access, Via::Named));
}
ImportedKind::Namespace => {
if name.escapes || name.local.is_none() {
queue.push_back(Task::All(target));
} else {
queue.extend(name.accessed_members.iter().map(|member| {
Task::Name(target, member.clone(), MemberUse::everything(), Via::Namespace)
}));
}
}
}
}
}
}
while let Some(task) = queue.pop_front() {
match task {
Task::All(id) => usage.mark_all(&index, id, &mut queue),
Task::Name(id, name, access, via) => usage.mark_name(&index, id, &name, &access, via, &mut queue),
}
}
usage
}
fn mark_all(&mut self, index: &ReexportIndex, id: ModuleId, queue: &mut VecDeque<Task>) {
if !self.all.insert(id) {
return;
}
for (target, named) in &index.modules[id.index()].all {
match named {
Some((_, source)) => {
queue.push_back(Task::Name(*target, source.clone(), MemberUse::everything(), Via::Named));
}
None => queue.push_back(Task::All(*target)),
}
}
}
fn mark_name(
&mut self,
index: &ReexportIndex,
id: ModuleId,
name: &str,
access: &MemberUse,
via: Via,
queue: &mut VecDeque<Task>,
) {
match via {
Via::Named => self.reached_by_name.insert(id),
Via::Namespace => self.reached_by_namespace.insert(id),
};
if self.all.contains(&id) {
return;
}
let slot = self.used.entry(id).or_default();
let fresh = !slot.contains_key(name);
let entry = slot.entry(name.to_owned()).or_default();
let before = (entry.members.len(), entry.all);
entry.members.extend(access.members.iter().cloned());
entry.all |= access.all;
if !fresh && (entry.members.len(), entry.all) == before {
return;
}
let reexports = &index.modules[id.index()];
for (target, source) in reexports.named.get(name).into_iter().flatten() {
queue.push_back(Task::Name(*target, source.clone(), access.clone(), via));
}
// `export * as name from` (or `import * as name; export { name }`): only the members read through `name`
// are used, unless the namespace is used as a whole.
for target in reexports.namespaces.get(name).into_iter().flatten() {
if access.all || access.members.is_empty() {
queue.push_back(Task::All(*target));
} else {
queue.extend(
access
.members
.iter()
.map(|member| Task::Name(*target, member.clone(), MemberUse::everything(), Via::Namespace)),
);
}
}
if !reexports.defined.contains(name) && name != "default" {
for target in &reexports.stars {
if index.exposed[target.index()].contains(name) {
queue.push_back(Task::Name(*target, name.to_owned(), access.clone(), via));
}
}
}
}
/// Whether another module uses export `name` of `module`.
#[must_use]
pub fn is_used(&self, module: ModuleId, name: &str) -> bool {
self.all.contains(&module) || self.used.get(&module).is_some_and(|names| names.contains_key(name))
}
/// Whether every use of `module` reads members of a namespace: it is imported with `import * as ns` or re-exported
/// with `export * as ns` and only `ns.member` is accessed, never `{ member }` by name and never the whole namespace.
///
/// The unused exports of such a module are `unused-namespace-exports` and `unused-namespace-types`, not
/// `unused-exports` and `unused-types`.
#[must_use]
pub fn is_namespace_only(&self, module: ModuleId) -> bool {
self.reached_by_namespace.contains(&module)
&& !self.reached_by_name.contains(&module)
&& !self.all.contains(&module)
}
/// Whether member `member` of export `name` of `module` is used, or the export is used as a whole.
#[must_use]
pub fn is_member_used(&self, module: ModuleId, name: &str, member: &str) -> bool {
if self.all.contains(&module) {
return true;
}
self.used
.get(&module)
.and_then(|names| names.get(name))
.is_some_and(|usage| usage.all || usage.members.contains(member))
}
}