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
use crate::map::{ExportsField, Field, ImportsField, PathTreeNode};
use crate::{AliasMap, Error, RResult, Resolver};
use indexmap::IndexMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
#[derive(Clone, Debug)]
pub enum SideEffects {
Bool(bool),
Array(Vec<String>),
}
#[derive(Debug)]
pub struct PkgJSON {
pub name: Option<String>,
pub version: Option<String>,
pub alias_fields: IndexMap<String, AliasMap>,
pub exports_field_tree: Option<PathTreeNode>,
pub imports_field_tree: Option<PathTreeNode>,
pub side_effects: Option<SideEffects>,
pub raw: serde_json::Value,
}
#[derive(Debug)]
pub struct PkgInfo {
pub json: Arc<PkgJSON>,
pub dir_path: PathBuf,
}
impl PkgJSON {
pub(crate) fn parse(content: &str, file_path: &Path) -> RResult<Self> {
let json: serde_json::Value =
tracing::debug_span!("serde_json_from_str").in_scope(|| {
serde_json::from_str(content)
.map_err(|error| Error::UnexpectedJson((file_path.to_path_buf(), error)))
})?;
let mut alias_fields = IndexMap::new();
if let Some(value) = json.get("browser") {
if let Some(map) = value.as_object() {
for (key, value) in map {
if let Some(b) = value.as_bool() {
assert!(!b);
alias_fields.insert(key.to_string(), AliasMap::Ignored);
} else if let Some(s) = value.as_str() {
alias_fields.insert(key.to_string(), AliasMap::Target(s.to_string()));
}
}
}
}
let exports_field_tree = if let Some(value) = json.get("exports") {
let tree = ExportsField::build_field_path_tree(value)?;
Some(tree)
} else {
None
};
let imports_field_tree = if let Some(value) = json.get("imports") {
let tree = ImportsField::build_field_path_tree(value)?;
Some(tree)
} else {
None
};
let name = json
.get("name")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
let side_effects: Option<SideEffects> =
json.get("sideEffects").map_or(Ok(None), |value| {
if let Some(b) = value.as_bool() {
Ok(Some(SideEffects::Bool(b)))
} else if let Some(vec) = value.as_array() {
let mut ans = vec![];
for value in vec {
if let Some(str) = value.as_str() {
ans.push(str.to_string());
} else {
return Err(Error::UnexpectedValue(format!(
"sideEffects in {} had unexpected value {}",
file_path.display(),
value
)));
}
}
Ok(Some(SideEffects::Array(ans)))
} else {
Err(Error::UnexpectedValue(format!(
"sideEffects in {} had unexpected value {}",
file_path.display(),
value
)))
}
})?;
let version = json
.get("version")
.and_then(|value| value.as_str())
.map(|str| str.to_string());
Ok(Self {
name,
version,
alias_fields,
exports_field_tree,
imports_field_tree,
side_effects,
raw: json,
})
}
}
impl Resolver {
pub fn load_side_effects(
&self,
path: &Path,
) -> RResult<Option<(PathBuf, Option<SideEffects>)>> {
let entry = self.load_entry(path)?;
let ans = entry.pkg_info.as_ref().map(|pkg_info| {
(
pkg_info.dir_path.join(&self.options.description_file),
pkg_info.json.side_effects.clone(),
)
});
Ok(ans)
}
}