-
Notifications
You must be signed in to change notification settings - Fork 41
/
specifiers.rs
221 lines (201 loc) · 6.55 KB
/
specifiers.rs
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
// Copyright 2018-2024 the Deno authors. MIT license.
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use anyhow::Result;
use deno_ast::ModuleSpecifier;
use deno_graph::Module;
use deno_graph::Resolution;
use deno_semver::npm::NpmPackageReqReference;
use crate::declaration_file_resolution::resolve_declaration_file_mappings;
use crate::declaration_file_resolution::DeclarationFileResolution;
use crate::graph::ModuleGraph;
use crate::loader::LoaderSpecifiers;
use crate::PackageMappedSpecifier;
#[derive(Debug)]
pub struct Specifiers {
pub local: Vec<ModuleSpecifier>,
pub remote: Vec<ModuleSpecifier>,
pub types: BTreeMap<ModuleSpecifier, DeclarationFileResolution>,
pub test_modules: HashSet<ModuleSpecifier>,
pub main: EnvironmentSpecifiers,
pub test: EnvironmentSpecifiers,
}
impl Specifiers {
pub fn has_mapped(&self, specifier: &ModuleSpecifier) -> bool {
self.main.mapped.contains_key(specifier)
|| self.test.mapped.contains_key(specifier)
}
}
#[derive(Debug)]
pub struct EnvironmentSpecifiers {
pub mapped: BTreeMap<ModuleSpecifier, PackageMappedSpecifier>,
}
pub fn get_specifiers<'a>(
entry_points: &[ModuleSpecifier],
mut specifiers: LoaderSpecifiers,
module_graph: &ModuleGraph,
modules: impl Iterator<Item = &'a Module>,
) -> Result<Specifiers> {
let mut local_specifiers = Vec::new();
let mut remote_specifiers = Vec::new();
let mut modules: BTreeMap<&ModuleSpecifier, &Module> =
modules.map(|m| (m.specifier(), m)).collect();
let mut found_module_specifiers = Vec::new();
let mut found_mapped_specifiers = BTreeMap::new();
// search for all the non-test modules
for entry_point in entry_points.iter() {
let module = module_graph.get(entry_point);
let mut pending = vec![module.specifier()];
while !pending.is_empty() {
if let Some(module) = pending
.pop()
.and_then(|s| modules.remove(&module_graph.resolve(s)))
{
if let Some(mapped_entry) =
specifiers.mapped_packages.remove(module.specifier())
{
found_mapped_specifiers
.insert(module.specifier().clone(), mapped_entry);
} else if let Ok(npm_specifier) =
deno_semver::npm::NpmPackageReqReference::from_specifier(
module.specifier(),
)
{
found_mapped_specifiers.insert(
module.specifier().clone(),
PackageMappedSpecifier::from_npm_specifier(&npm_specifier),
);
} else {
found_module_specifiers.push(module.specifier().clone());
if let Some(module) = module.js() {
for dep in module.dependencies.values() {
if let Some(specifier) = dep.get_code() {
pending.push(specifier);
}
if let Some(specifier) = dep.get_type() {
pending.push(specifier);
}
}
if let Some(deno_graph::TypesDependency {
dependency: Resolution::Ok(resolved),
..
}) = &module.maybe_types_dependency
{
pending.push(&resolved.specifier);
}
}
}
}
}
}
// clear out all the mapped modules
for specifier in specifiers.mapped_packages.keys() {
modules.remove(specifier);
}
// at this point, the remaining modules are the test modules
let test_modules = modules;
let all_modules = test_modules
.values()
.copied()
.chain(found_module_specifiers.iter().map(|s| module_graph.get(s)))
.collect::<Vec<_>>();
for module in all_modules.iter() {
match module {
Module::Js(_) | Module::Json(_) => {
match module.specifier().scheme().to_lowercase().as_str() {
"file" => local_specifiers.push(module.specifier().clone()),
"http" | "https" => {
remote_specifiers.push(module.specifier().clone())
}
_ => {
anyhow::bail!("Unhandled scheme on url: {}", module.specifier());
}
}
}
Module::Npm(_) | Module::Node(_) => {
// ignore
}
Module::External(module) => {
let specifier = &module.specifier;
if let Ok(npm_specifier) =
NpmPackageReqReference::from_specifier(specifier)
{
if !found_mapped_specifiers.contains_key(specifier) {
specifiers.mapped_packages.insert(
specifier.clone(),
PackageMappedSpecifier::from_npm_specifier(&npm_specifier),
);
}
}
}
}
}
let types = resolve_declaration_file_mappings(
module_graph,
&all_modules,
&found_mapped_specifiers,
)?;
let mut declaration_specifiers = HashSet::new();
for value in types.values() {
declaration_specifiers.insert(&value.selected.specifier);
for dep in value.ignored.iter() {
declaration_specifiers.insert(&dep.specifier);
}
}
ensure_package_mapped_specifiers_valid(
&found_mapped_specifiers,
&specifiers.mapped_packages,
)?;
Ok(Specifiers {
local: local_specifiers
.into_iter()
.filter(|l| !declaration_specifiers.contains(&l))
.collect(),
remote: remote_specifiers
.into_iter()
.filter(|l| !declaration_specifiers.contains(&l))
.collect(),
types,
test_modules: test_modules
.values()
.map(|k| k.specifier().clone())
.collect(),
main: EnvironmentSpecifiers {
mapped: found_mapped_specifiers,
},
test: EnvironmentSpecifiers {
mapped: specifiers.mapped_packages,
},
})
}
fn ensure_package_mapped_specifiers_valid(
mapped_specifiers: &BTreeMap<ModuleSpecifier, PackageMappedSpecifier>,
test_mapped_specifiers: &BTreeMap<ModuleSpecifier, PackageMappedSpecifier>,
) -> Result<()> {
let mut specifier_for_name: HashMap<
String,
(ModuleSpecifier, PackageMappedSpecifier),
> = HashMap::new();
for (from_specifier, mapped_specifier) in mapped_specifiers
.iter()
.chain(test_mapped_specifiers.iter())
{
if let Some(specifier) = specifier_for_name.get(&mapped_specifier.name) {
if specifier.1.version != mapped_specifier.version {
anyhow::bail!("Specifier {} with version {} did not match specifier {} with version {}.",
specifier.0,
specifier.1.version.as_deref().unwrap_or("<unknown>"),
from_specifier,
mapped_specifier.version.as_deref().unwrap_or("<unknown>"),
);
}
} else {
specifier_for_name.insert(
mapped_specifier.name.to_string(),
(from_specifier.clone(), mapped_specifier.clone()),
);
}
}
Ok(())
}