dart-decimate 0.0.76 → 0.0.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +1 -1
- package/Cargo.toml +1 -1
- package/README.md +8 -8
- package/package.json +2 -2
- package/src/boundaries.rs +1 -9
- package/src/cli/analyze.rs +33 -3
- package/src/cli/error.rs +16 -0
- package/src/cli/list_run.rs +3 -1
- package/src/cli/scope_args.rs +14 -3
- package/src/config.rs +12 -8
- package/src/dart_parser.rs +100 -9
- package/src/dependencies/analyze.rs +4 -3
- package/src/dependencies/discovery.rs +10 -2
- package/src/dependencies/flutter_usage.rs +4 -0
- package/src/dependencies/tests.rs +5 -5
- package/src/dependencies/usage.rs +0 -9
- package/src/dependencies.rs +12 -7
- package/src/dupes/declarations.rs +1 -2
- package/src/dupes.rs +1 -10
- package/src/explain/misc.rs +1 -1
- package/src/explain.rs +1 -1
- package/src/extract/members.rs +24 -15
- package/src/extract/tests.rs +89 -0
- package/src/extract.rs +51 -15
- package/src/graph.rs +9 -0
- package/src/lib.rs +3 -2
- package/src/output/duplication_findings.rs +1 -2
- package/src/output/human.rs +7 -2
- package/src/output/human_details.rs +6 -0
- package/src/output/next_steps.rs +111 -0
- package/src/output/route_cycle/aliases.rs +1 -2
- package/src/output/route_cycle/wrappers.rs +1 -2
- package/src/output/types.rs +2 -0
- package/src/output.rs +27 -5
- package/src/outside/loader.rs +133 -0
- package/src/outside/loader_tests.rs +136 -0
- package/src/outside/resolver.rs +484 -0
- package/src/outside/tests.rs +242 -0
- package/src/outside.rs +186 -0
- package/src/package_map.rs +30 -1
- package/src/report_schema.rs +1 -0
- package/src/routes.rs +2 -0
- package/src/scan.rs +24 -3
- package/src/security/detect/process_arguments.rs +106 -0
- package/src/security/detect.rs +98 -40
- package/src/symbols/extensions.rs +42 -27
- package/src/symbols/path_filters.rs +10 -1
- package/src/symbols/tests.rs +15 -0
- package/src/symbols.rs +489 -67
- package/src/trace.rs +6 -5
- package/src/widgets/reads.rs +4 -8
- package/src/widgets/resolution.rs +63 -10
- package/src/workspace_scope.rs +1 -10
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
use std::fs;
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use tempfile::TempDir;
|
|
5
|
+
|
|
6
|
+
use super::*;
|
|
7
|
+
|
|
8
|
+
const EMBEDDER: &str =
|
|
9
|
+
"embedded_libs:\n \"dart:core\": \"core/core.dart\"\n \"dart:ui\": \"ui/ui.dart\"\n";
|
|
10
|
+
const LIBRARIES: &str = "{\"vm_common\":{\"libraries\":{\"core\":{\"uri\":\"core/core.dart\"},\"async\":{\"uri\":\"async/async.dart\"}}},\"vm\":{\"libraries\":{\"io\":{\"uri\":\"io/io.dart\"}}}}";
|
|
11
|
+
|
|
12
|
+
struct Layout {
|
|
13
|
+
project: TempDir,
|
|
14
|
+
toolchain: TempDir,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
fn write(root: &Path, path: &str, source: &str) -> Result<(), std::io::Error> {
|
|
18
|
+
let path = root.join(path);
|
|
19
|
+
if let Some(parent) = path.parent() {
|
|
20
|
+
fs::create_dir_all(parent)?;
|
|
21
|
+
}
|
|
22
|
+
fs::write(path, source)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
fn config(toolchain: &Path, extra: &str, generator: &str) -> String {
|
|
26
|
+
format!(
|
|
27
|
+
"{{\"configVersion\":2,\"generatorVersion\":\"{generator}\",\"flutterRoot\":\"file://{root}\",\"packages\":[{extra}{{\"name\":\"app\",\"rootUri\":\"../\",\"packageUri\":\"lib/\"}}]}}",
|
|
28
|
+
root = toolchain.join("flutter").display(),
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn sky_engine(toolchain: &Path) -> String {
|
|
33
|
+
format!(
|
|
34
|
+
"{{\"name\":\"sky_engine\",\"rootUri\":\"file://{}\",\"packageUri\":\"lib/\"}},",
|
|
35
|
+
toolchain.join("flutter/bin/cache/pkg/sky_engine").display()
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
fn layout(with_sky_engine: bool, sdk_version: &str) -> Result<Layout, Box<dyn std::error::Error>> {
|
|
40
|
+
let project = tempfile::tempdir()?;
|
|
41
|
+
let toolchain = tempfile::tempdir()?;
|
|
42
|
+
let flutter = toolchain.path().join("flutter");
|
|
43
|
+
write(&flutter, "bin/cache/dart-sdk/version", sdk_version)?;
|
|
44
|
+
write(&flutter, "bin/cache/dart-sdk/lib/libraries.json", LIBRARIES)?;
|
|
45
|
+
write(
|
|
46
|
+
&flutter,
|
|
47
|
+
"bin/cache/pkg/sky_engine/lib/_embedder.yaml",
|
|
48
|
+
EMBEDDER,
|
|
49
|
+
)?;
|
|
50
|
+
write(project.path(), "pubspec.yaml", "name: app\n")?;
|
|
51
|
+
write(project.path(), "lib/main.dart", "void main() {}\n")?;
|
|
52
|
+
let extra = if with_sky_engine {
|
|
53
|
+
sky_engine(toolchain.path())
|
|
54
|
+
} else {
|
|
55
|
+
String::new()
|
|
56
|
+
};
|
|
57
|
+
write(
|
|
58
|
+
project.path(),
|
|
59
|
+
".dart_tool/package_config.json",
|
|
60
|
+
&config(toolchain.path(), &extra, "3.14.0"),
|
|
61
|
+
)?;
|
|
62
|
+
Ok(Layout { project, toolchain })
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fn library(sources: &OutsideSources, name: &str) -> Option<PathBuf> {
|
|
66
|
+
match sources.resolve(
|
|
67
|
+
&PathBuf::from("/project/lib/main.dart"),
|
|
68
|
+
&format!("dart:{name}"),
|
|
69
|
+
false,
|
|
70
|
+
) {
|
|
71
|
+
Target::Outside(path) => Some(path),
|
|
72
|
+
Target::Local | Target::Unknown => None,
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#[test]
|
|
77
|
+
fn flutter_application_reads_dart_libraries_from_the_embedder_map()
|
|
78
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
79
|
+
let layout = layout(true, "3.14.0\n")?;
|
|
80
|
+
|
|
81
|
+
let sources = OutsideSources::discover_with_path(layout.project.path(), None)
|
|
82
|
+
.ok_or("no outside sources")?;
|
|
83
|
+
|
|
84
|
+
let sky = layout
|
|
85
|
+
.toolchain
|
|
86
|
+
.path()
|
|
87
|
+
.join("flutter/bin/cache/pkg/sky_engine/lib");
|
|
88
|
+
assert_eq!(library(&sources, "ui"), Some(sky.join("ui/ui.dart")));
|
|
89
|
+
assert_eq!(library(&sources, "core"), Some(sky.join("core/core.dart")));
|
|
90
|
+
assert_eq!(library(&sources, "io"), None);
|
|
91
|
+
|
|
92
|
+
Ok(())
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#[test]
|
|
96
|
+
fn dart_project_reads_dart_libraries_from_the_flutter_root_sdk()
|
|
97
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
98
|
+
let layout = layout(false, "3.14.0")?;
|
|
99
|
+
|
|
100
|
+
let sources = OutsideSources::discover_with_path(layout.project.path(), None)
|
|
101
|
+
.ok_or("no outside sources")?;
|
|
102
|
+
|
|
103
|
+
let lib = layout
|
|
104
|
+
.toolchain
|
|
105
|
+
.path()
|
|
106
|
+
.join("flutter/bin/cache/dart-sdk/lib");
|
|
107
|
+
assert_eq!(library(&sources, "io"), Some(lib.join("io/io.dart")));
|
|
108
|
+
assert_eq!(
|
|
109
|
+
library(&sources, "async"),
|
|
110
|
+
Some(lib.join("async/async.dart"))
|
|
111
|
+
);
|
|
112
|
+
assert_eq!(library(&sources, "ui"), None);
|
|
113
|
+
|
|
114
|
+
Ok(())
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
#[test]
|
|
118
|
+
fn dart_project_without_flutter_root_reads_the_sdk_on_path_when_the_version_matches()
|
|
119
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
120
|
+
let cases = [("3.14.0", true), ("3.13.0", false)];
|
|
121
|
+
for (version, found) in cases {
|
|
122
|
+
let layout = layout(false, "3.14.0")?;
|
|
123
|
+
let sdk = layout.toolchain.path().join("path-sdk");
|
|
124
|
+
write(&sdk, "version", version)?;
|
|
125
|
+
write(&sdk, "lib/libraries.json", LIBRARIES)?;
|
|
126
|
+
write(&sdk, "bin/dart", "")?;
|
|
127
|
+
write(
|
|
128
|
+
layout.project.path(),
|
|
129
|
+
".dart_tool/package_config.json",
|
|
130
|
+
"{\"configVersion\":2,\"generatorVersion\":\"3.14.0\",\"packages\":[{\"name\":\"app\",\"rootUri\":\"../\",\"packageUri\":\"lib/\"}]}",
|
|
131
|
+
)?;
|
|
132
|
+
let path_var = std::env::join_paths([sdk.join("bin")])?;
|
|
133
|
+
|
|
134
|
+
let sources =
|
|
135
|
+
OutsideSources::discover_with_path(layout.project.path(), Some(path_var.as_os_str()));
|
|
136
|
+
|
|
137
|
+
assert_eq!(sources.is_some(), found, "{version}");
|
|
138
|
+
if let Some(sources) = sources {
|
|
139
|
+
assert_eq!(
|
|
140
|
+
library(&sources, "io"),
|
|
141
|
+
Some(fs::canonicalize(&sdk)?.join("lib/io/io.dart"))
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
Ok(())
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#[test]
|
|
150
|
+
fn no_package_config_or_other_sdk_version_means_no_outside_reading()
|
|
151
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
152
|
+
let mismatched = layout(true, "3.13.0")?;
|
|
153
|
+
let missing = layout(true, "3.14.0")?;
|
|
154
|
+
fs::remove_file(
|
|
155
|
+
missing
|
|
156
|
+
.project
|
|
157
|
+
.path()
|
|
158
|
+
.join(".dart_tool/package_config.json"),
|
|
159
|
+
)?;
|
|
160
|
+
|
|
161
|
+
assert!(OutsideSources::discover_with_path(mismatched.project.path(), None).is_none());
|
|
162
|
+
assert!(OutsideSources::discover_with_path(missing.project.path(), None).is_none());
|
|
163
|
+
|
|
164
|
+
Ok(())
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
#[test]
|
|
168
|
+
fn package_uris_resolve_outside_the_project_only() -> Result<(), Box<dyn std::error::Error>> {
|
|
169
|
+
let layout = layout(true, "3.14.0")?;
|
|
170
|
+
let from = layout.project.path().join("lib/main.dart");
|
|
171
|
+
let sources = OutsideSources::discover_with_path(layout.project.path(), None)
|
|
172
|
+
.ok_or("no outside sources")?
|
|
173
|
+
.with_scanned(Rc::new(BTreeSet::from([layout
|
|
174
|
+
.project
|
|
175
|
+
.path()
|
|
176
|
+
.join("lib/other.dart")])));
|
|
177
|
+
let sky = layout
|
|
178
|
+
.toolchain
|
|
179
|
+
.path()
|
|
180
|
+
.join("flutter/bin/cache/pkg/sky_engine/lib");
|
|
181
|
+
|
|
182
|
+
assert_eq!(
|
|
183
|
+
sources.resolve(&from, "package:sky_engine/ui/ui.dart", false),
|
|
184
|
+
Target::Outside(sky.join("ui/ui.dart"))
|
|
185
|
+
);
|
|
186
|
+
assert_eq!(
|
|
187
|
+
sources.resolve(&from, "package:app/other.dart", false),
|
|
188
|
+
Target::Local
|
|
189
|
+
);
|
|
190
|
+
assert_eq!(sources.resolve(&from, "other.dart", false), Target::Local);
|
|
191
|
+
assert_eq!(
|
|
192
|
+
sources.resolve(&from, "package:app/ignored.dart", false),
|
|
193
|
+
Target::Outside(layout.project.path().join("lib/ignored.dart"))
|
|
194
|
+
);
|
|
195
|
+
assert_eq!(
|
|
196
|
+
sources.resolve(&from, "package:missing/x.dart", false),
|
|
197
|
+
Target::Unknown
|
|
198
|
+
);
|
|
199
|
+
assert_eq!(sources.resolve(&from, "dart:html", false), Target::Unknown);
|
|
200
|
+
assert_eq!(
|
|
201
|
+
sources.resolve(&sky.join("ui/ui.dart"), "painting.dart", true),
|
|
202
|
+
Target::Outside(sky.join("ui/painting.dart"))
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
Ok(())
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
#[test]
|
|
209
|
+
fn readers_that_share_a_loader_parse_each_outside_file_once()
|
|
210
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
211
|
+
let layout = layout(true, "3.14.0")?;
|
|
212
|
+
let ui = layout
|
|
213
|
+
.toolchain
|
|
214
|
+
.path()
|
|
215
|
+
.join("flutter/bin/cache/pkg/sky_engine/lib/ui/ui.dart");
|
|
216
|
+
write(
|
|
217
|
+
layout.toolchain.path(),
|
|
218
|
+
"flutter/bin/cache/pkg/sky_engine/lib/ui/ui.dart",
|
|
219
|
+
"class Window {}\n",
|
|
220
|
+
)?;
|
|
221
|
+
let parses = crate::dart_parser::track_parse_count(&ui);
|
|
222
|
+
let loader = SharedLoader::default();
|
|
223
|
+
let imports = [crate::DartImport {
|
|
224
|
+
uri: "dart:ui".to_owned(),
|
|
225
|
+
condition: None,
|
|
226
|
+
prefix: None,
|
|
227
|
+
deferred: false,
|
|
228
|
+
combinators: Vec::new(),
|
|
229
|
+
location: crate::Location { line: 1, column: 0 },
|
|
230
|
+
}];
|
|
231
|
+
let library = layout.project.path().join("lib/main.dart");
|
|
232
|
+
|
|
233
|
+
for _ in 0..3 {
|
|
234
|
+
let mut types =
|
|
235
|
+
OutsideTypes::with_loader(layout.project.path(), loader.clone(), &Rc::default());
|
|
236
|
+
assert!(types.link(&library, &imports, "Window", false).is_some());
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
assert_eq!(parses.count(), 1);
|
|
240
|
+
|
|
241
|
+
Ok(())
|
|
242
|
+
}
|
package/src/outside.rs
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
use std::cell::{Cell, RefCell};
|
|
2
|
+
use std::collections::{BTreeMap, BTreeSet};
|
|
3
|
+
use std::ffi::OsStr;
|
|
4
|
+
use std::fs;
|
|
5
|
+
use std::path::{Path, PathBuf};
|
|
6
|
+
use std::rc::Rc;
|
|
7
|
+
|
|
8
|
+
use crate::graph::{normalize_path, resolve_local_uri};
|
|
9
|
+
use crate::package_map::{PackageMap, parse_yaml};
|
|
10
|
+
|
|
11
|
+
mod loader;
|
|
12
|
+
mod resolver;
|
|
13
|
+
pub(crate) use loader::SharedLoader;
|
|
14
|
+
pub(crate) use resolver::OutsideTypes;
|
|
15
|
+
#[cfg(test)]
|
|
16
|
+
mod loader_tests;
|
|
17
|
+
#[cfg(test)]
|
|
18
|
+
mod tests;
|
|
19
|
+
|
|
20
|
+
#[derive(Debug, PartialEq, Eq)]
|
|
21
|
+
pub(crate) enum Target {
|
|
22
|
+
Outside(PathBuf),
|
|
23
|
+
Local,
|
|
24
|
+
Unknown,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
pub(crate) struct OutsideSources {
|
|
28
|
+
root: PathBuf,
|
|
29
|
+
packages: PackageMap,
|
|
30
|
+
dart_libraries: BTreeMap<String, PathBuf>,
|
|
31
|
+
sdk_lib: PathBuf,
|
|
32
|
+
scanned: Rc<BTreeSet<PathBuf>>,
|
|
33
|
+
missing: RefCell<BTreeSet<PathBuf>>,
|
|
34
|
+
gaps: Cell<usize>,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
impl OutsideSources {
|
|
38
|
+
pub(crate) fn with_scanned(mut self, scanned: Rc<BTreeSet<PathBuf>>) -> Self {
|
|
39
|
+
self.scanned = scanned;
|
|
40
|
+
self
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
pub(crate) fn discover(root: &Path) -> Option<Self> {
|
|
44
|
+
Self::discover_with_path(root, std::env::var_os("PATH").as_deref())
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn discover_with_path(root: &Path, path_var: Option<&OsStr>) -> Option<Self> {
|
|
48
|
+
let packages = PackageMap::discover(root).ok()?;
|
|
49
|
+
let toolchain = packages.toolchain()?;
|
|
50
|
+
let flutter_sdk = toolchain
|
|
51
|
+
.flutter_root
|
|
52
|
+
.as_ref()
|
|
53
|
+
.map(|flutter| flutter.join("bin/cache/dart-sdk"));
|
|
54
|
+
let version = toolchain.generator_version.as_deref();
|
|
55
|
+
let (dart_libraries, sdk_lib) =
|
|
56
|
+
if let Some(sky_engine) = packages.package_path("sky_engine") {
|
|
57
|
+
if flutter_sdk
|
|
58
|
+
.as_ref()
|
|
59
|
+
.is_some_and(|sdk| !sdk_matches(sdk, version))
|
|
60
|
+
{
|
|
61
|
+
return None;
|
|
62
|
+
}
|
|
63
|
+
(embedder_libraries(sky_engine)?, sky_engine.to_path_buf())
|
|
64
|
+
} else {
|
|
65
|
+
let sdk = flutter_sdk.or_else(|| sdk_on_path(path_var))?;
|
|
66
|
+
if !sdk_matches(&sdk, version) {
|
|
67
|
+
return None;
|
|
68
|
+
}
|
|
69
|
+
(sdk_libraries(&sdk)?, sdk.join("lib"))
|
|
70
|
+
};
|
|
71
|
+
Some(Self {
|
|
72
|
+
root: root.to_path_buf(),
|
|
73
|
+
packages,
|
|
74
|
+
dart_libraries,
|
|
75
|
+
sdk_lib,
|
|
76
|
+
scanned: Rc::default(),
|
|
77
|
+
missing: RefCell::default(),
|
|
78
|
+
gaps: Cell::new(0),
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
fn in_local_package(&self, path: &Path) -> bool {
|
|
83
|
+
path.starts_with(&self.root)
|
|
84
|
+
|| self
|
|
85
|
+
.packages
|
|
86
|
+
.local_roots()
|
|
87
|
+
.iter()
|
|
88
|
+
.any(|root| path.starts_with(root))
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
pub(crate) fn gaps(&self) -> usize {
|
|
92
|
+
self.gaps.get()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
pub(crate) fn note_gap(&self, path: Option<&Path>) {
|
|
96
|
+
if path.is_none_or(|path| !self.missing.borrow().contains(path)) {
|
|
97
|
+
self.gaps.set(self.gaps.get() + 1);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
pub(crate) fn is_sdk_source(&self, path: &Path) -> bool {
|
|
102
|
+
path.starts_with(&self.sdk_lib)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
pub(crate) fn resolve(&self, from: &Path, specifier: &str, from_outside: bool) -> Target {
|
|
106
|
+
if let Some(library) = specifier.strip_prefix("dart:") {
|
|
107
|
+
let name = library.split('/').next().unwrap_or(library);
|
|
108
|
+
return self
|
|
109
|
+
.dart_libraries
|
|
110
|
+
.get(name)
|
|
111
|
+
.map_or(Target::Unknown, |path| Target::Outside(path.clone()));
|
|
112
|
+
}
|
|
113
|
+
let anchor = self.root.join("pubspec.yaml");
|
|
114
|
+
let resolved = if from_outside || !specifier.starts_with("package:") {
|
|
115
|
+
resolve_local_uri(&self.root, &self.packages, from, specifier)
|
|
116
|
+
} else {
|
|
117
|
+
resolve_local_uri(&self.root, &self.packages, &anchor, specifier)
|
|
118
|
+
.or_else(|| resolve_local_uri(&self.root, &self.packages, from, specifier))
|
|
119
|
+
};
|
|
120
|
+
match resolved {
|
|
121
|
+
None => Target::Unknown,
|
|
122
|
+
Some(target)
|
|
123
|
+
if target.local && !from_outside && self.scanned.contains(&target.path) =>
|
|
124
|
+
{
|
|
125
|
+
Target::Local
|
|
126
|
+
}
|
|
127
|
+
Some(target) => {
|
|
128
|
+
if target.local && !target.path.is_file() && self.in_local_package(&target.path) {
|
|
129
|
+
self.missing.borrow_mut().insert(target.path.clone());
|
|
130
|
+
}
|
|
131
|
+
Target::Outside(target.path)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
fn sdk_matches(sdk: &Path, generator_version: Option<&str>) -> bool {
|
|
138
|
+
generator_version.is_some_and(|expected| {
|
|
139
|
+
fs::read_to_string(sdk.join("version")).is_ok_and(|version| version.trim() == expected)
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
fn sdk_on_path(path_var: Option<&OsStr>) -> Option<PathBuf> {
|
|
144
|
+
std::env::split_paths(path_var?)
|
|
145
|
+
.map(|directory| directory.join("dart"))
|
|
146
|
+
.find_map(|binary| {
|
|
147
|
+
let binary = fs::canonicalize(binary).ok()?;
|
|
148
|
+
binary
|
|
149
|
+
.parent()?
|
|
150
|
+
.parent()
|
|
151
|
+
.filter(|sdk| sdk.join("lib/libraries.json").is_file())
|
|
152
|
+
.map(Path::to_path_buf)
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
fn embedder_libraries(sky_engine: &Path) -> Option<BTreeMap<String, PathBuf>> {
|
|
157
|
+
let source = fs::read_to_string(sky_engine.join("_embedder.yaml")).ok()?;
|
|
158
|
+
let document = parse_yaml(&source).ok()?;
|
|
159
|
+
let libraries = document.as_mapping()?.get("embedded_libs")?.as_mapping()?;
|
|
160
|
+
Some(
|
|
161
|
+
libraries
|
|
162
|
+
.iter()
|
|
163
|
+
.filter_map(|(name, path)| {
|
|
164
|
+
let name = name.strip_prefix("dart:")?;
|
|
165
|
+
Some((
|
|
166
|
+
name.to_owned(),
|
|
167
|
+
normalize_path(&sky_engine.join(path.as_str()?)),
|
|
168
|
+
))
|
|
169
|
+
})
|
|
170
|
+
.collect(),
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
fn sdk_libraries(sdk: &Path) -> Option<BTreeMap<String, PathBuf>> {
|
|
175
|
+
let source = fs::read_to_string(sdk.join("lib/libraries.json")).ok()?;
|
|
176
|
+
let document = serde_json::from_str::<serde_json::Value>(&source).ok()?;
|
|
177
|
+
let mut libraries = BTreeMap::new();
|
|
178
|
+
for target in ["vm_common", "vm"] {
|
|
179
|
+
let entries = document.get(target)?.get("libraries")?.as_object()?;
|
|
180
|
+
for (name, entry) in entries {
|
|
181
|
+
let uri = entry.get("uri")?.as_str()?;
|
|
182
|
+
libraries.insert(name.clone(), normalize_path(&sdk.join("lib").join(uri)));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
Some(libraries)
|
|
186
|
+
}
|
package/src/package_map.rs
CHANGED
|
@@ -13,6 +13,13 @@ pub(crate) struct PackageMap {
|
|
|
13
13
|
by_name: BTreeMap<String, Vec<PackageRoot>>,
|
|
14
14
|
by_owner_dependency: BTreeMap<PathBuf, BTreeMap<String, PackageRoot>>,
|
|
15
15
|
uses_package_config: bool,
|
|
16
|
+
toolchain: Option<Toolchain>,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
20
|
+
pub(crate) struct Toolchain {
|
|
21
|
+
pub(crate) generator_version: Option<String>,
|
|
22
|
+
pub(crate) flutter_root: Option<PathBuf>,
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
@@ -47,6 +54,18 @@ impl PackageMap {
|
|
|
47
54
|
Ok(packages)
|
|
48
55
|
}
|
|
49
56
|
|
|
57
|
+
pub(crate) fn toolchain(&self) -> Option<&Toolchain> {
|
|
58
|
+
self.toolchain.as_ref()
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
pub(crate) fn package_path(&self, name: &str) -> Option<&Path> {
|
|
62
|
+
self.by_name
|
|
63
|
+
.get(name)?
|
|
64
|
+
.iter()
|
|
65
|
+
.find(|root| root.from_package_config)
|
|
66
|
+
.map(|root| root.package_path.as_path())
|
|
67
|
+
}
|
|
68
|
+
|
|
50
69
|
pub(crate) fn names(&self) -> Vec<&str> {
|
|
51
70
|
self.by_name.keys().map(String::as_str).collect()
|
|
52
71
|
}
|
|
@@ -148,6 +167,13 @@ impl PackageMap {
|
|
|
148
167
|
by_name,
|
|
149
168
|
by_owner_dependency: BTreeMap::new(),
|
|
150
169
|
uses_package_config: true,
|
|
170
|
+
toolchain: Some(Toolchain {
|
|
171
|
+
generator_version: config.generator_version.clone(),
|
|
172
|
+
flutter_root: config
|
|
173
|
+
.flutter_root
|
|
174
|
+
.as_deref()
|
|
175
|
+
.and_then(|uri| resolve_config_uri(config_dir, uri)),
|
|
176
|
+
}),
|
|
151
177
|
}
|
|
152
178
|
}
|
|
153
179
|
|
|
@@ -274,9 +300,12 @@ fn is_optional_pubspec_error(error: &GraphError) -> bool {
|
|
|
274
300
|
}
|
|
275
301
|
|
|
276
302
|
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
|
303
|
+
#[serde(rename_all = "camelCase")]
|
|
277
304
|
struct PackageConfig {
|
|
278
305
|
#[serde(default)]
|
|
279
306
|
packages: Vec<PackageConfigPackage>,
|
|
307
|
+
generator_version: Option<String>,
|
|
308
|
+
flutter_root: Option<String>,
|
|
280
309
|
}
|
|
281
310
|
|
|
282
311
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
@@ -307,7 +336,7 @@ impl PackageConfigFile {
|
|
|
307
336
|
}
|
|
308
337
|
}
|
|
309
338
|
|
|
310
|
-
fn find_package_config(root: &Path) -> Option<PathBuf> {
|
|
339
|
+
pub(crate) fn find_package_config(root: &Path) -> Option<PathBuf> {
|
|
311
340
|
root.ancestors()
|
|
312
341
|
.map(|dir| dir.join(".dart_tool/package_config.json"))
|
|
313
342
|
.find(|path| path.is_file())
|
package/src/report_schema.rs
CHANGED
package/src/routes.rs
CHANGED
package/src/scan.rs
CHANGED
|
@@ -26,12 +26,16 @@ pub struct ScannedProject {
|
|
|
26
26
|
pub files: Vec<DartFile>,
|
|
27
27
|
/// Directed graph built from the extracted files.
|
|
28
28
|
pub graph: ModuleGraph,
|
|
29
|
+
/// Glob patterns that excluded Dart files and pubspecs from discovery.
|
|
30
|
+
pub ignore_patterns: Vec<String>,
|
|
31
|
+
/// The folder the user scanned when the package that holds it sits above it.
|
|
32
|
+
pub scope: Option<PathBuf>,
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
/// Options controlling Dart file discovery.
|
|
32
36
|
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
|
33
37
|
pub struct ScanOptions {
|
|
34
|
-
/// Glob patterns excluded from Dart file discovery.
|
|
38
|
+
/// Glob patterns excluded from Dart file and pubspec discovery.
|
|
35
39
|
pub ignore_patterns: Vec<String>,
|
|
36
40
|
/// Dart conditional URI environment values. Empty means include every branch.
|
|
37
41
|
pub conditional_environment: BTreeMap<String, String>,
|
|
@@ -116,7 +120,8 @@ pub fn scan_project_with_options(
|
|
|
116
120
|
root: impl AsRef<Path>,
|
|
117
121
|
options: &ScanOptions,
|
|
118
122
|
) -> Result<ScannedProject, ScanError> {
|
|
119
|
-
let
|
|
123
|
+
let scanned = normalize_scan_root(root.as_ref())?;
|
|
124
|
+
let (root, scope) = package_above(&scanned);
|
|
120
125
|
let ignore_matcher = IgnoreMatcher::new(&options.ignore_patterns)?;
|
|
121
126
|
let package_map = PackageMap::discover(&root)?;
|
|
122
127
|
let mut scan_roots = BTreeSet::new();
|
|
@@ -161,9 +166,25 @@ pub fn scan_project_with_options(
|
|
|
161
166
|
scan_roots: scan_roots.into_iter().collect(),
|
|
162
167
|
files,
|
|
163
168
|
graph,
|
|
169
|
+
ignore_patterns: options.ignore_patterns.clone(),
|
|
170
|
+
scope,
|
|
164
171
|
})
|
|
165
172
|
}
|
|
166
173
|
|
|
174
|
+
fn package_above(scanned: &Path) -> (PathBuf, Option<PathBuf>) {
|
|
175
|
+
if scanned.join("pubspec.yaml").is_file() {
|
|
176
|
+
return (scanned.to_path_buf(), None);
|
|
177
|
+
}
|
|
178
|
+
scanned
|
|
179
|
+
.ancestors()
|
|
180
|
+
.skip(1)
|
|
181
|
+
.find(|folder| folder.join("pubspec.yaml").is_file())
|
|
182
|
+
.map_or_else(
|
|
183
|
+
|| (scanned.to_path_buf(), None),
|
|
184
|
+
|package| (package.to_path_buf(), Some(scanned.to_path_buf())),
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
|
|
167
188
|
fn ignored_by_parent_gitignore(root: &Path, scan_root: &Path) -> bool {
|
|
168
189
|
let Some(repository_root) = root
|
|
169
190
|
.ancestors()
|
|
@@ -311,7 +332,7 @@ pub(crate) struct IgnoreMatcher {
|
|
|
311
332
|
}
|
|
312
333
|
|
|
313
334
|
impl IgnoreMatcher {
|
|
314
|
-
fn new(patterns: &[String]) -> Result<Self, ScanError> {
|
|
335
|
+
pub(crate) fn new(patterns: &[String]) -> Result<Self, ScanError> {
|
|
315
336
|
let patterns = patterns
|
|
316
337
|
.iter()
|
|
317
338
|
.map(|pattern| {
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
use tree_sitter::Node;
|
|
2
|
+
|
|
3
|
+
pub(super) fn name_is_reassigned(root: Node<'_>, source: &str, index: usize, name: &str) -> bool {
|
|
4
|
+
function_body_of(root, index).is_none_or(|body| body_use_count(body, source, name) != 2)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
fn body_use_count(body: Node<'_>, source: &str, name: &str) -> usize {
|
|
8
|
+
let text = &source[body.start_byte()..body.end_byte()];
|
|
9
|
+
let bytes = text.as_bytes();
|
|
10
|
+
let word = |byte: u8| byte.is_ascii_alphanumeric() || byte == b'_';
|
|
11
|
+
text.match_indices(name)
|
|
12
|
+
.filter(|(start, _)| {
|
|
13
|
+
let after = bytes.get(start + name.len()).copied();
|
|
14
|
+
let before = text[..*start].trim_end();
|
|
15
|
+
!start
|
|
16
|
+
.checked_sub(1)
|
|
17
|
+
.is_some_and(|position| word(bytes[position]))
|
|
18
|
+
&& !after.is_some_and(|byte| word(byte) || byte == b'$')
|
|
19
|
+
&& (!before.ends_with('.') || before.ends_with("..."))
|
|
20
|
+
})
|
|
21
|
+
.count()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fn function_body_of(root: Node<'_>, index: usize) -> Option<Node<'_>> {
|
|
25
|
+
let mut node = root.descendant_for_byte_range(index, index.saturating_add(1))?;
|
|
26
|
+
let mut body = None;
|
|
27
|
+
while let Some(parent) = node.parent() {
|
|
28
|
+
match parent.kind() {
|
|
29
|
+
"class_body" => break,
|
|
30
|
+
"function_body" | "function_expression_body" => body = Some(parent),
|
|
31
|
+
_ => {}
|
|
32
|
+
}
|
|
33
|
+
node = parent;
|
|
34
|
+
}
|
|
35
|
+
body
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
pub(super) fn list_is_proven_fixed(root: Node<'_>, source: &str, index: usize, name: &str) -> bool {
|
|
39
|
+
if name_is_const(root, source, name) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
let Some(body) = function_body_of(root, index) else {
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
body_use_count(body, source, name) == 2 && !scope_assigns_name(body, source, name)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
fn name_is_const(root: Node<'_>, source: &str, name: &str) -> bool {
|
|
49
|
+
let mut declarations = 0;
|
|
50
|
+
let mut constant = false;
|
|
51
|
+
let mut stack = vec![root];
|
|
52
|
+
while let Some(node) = stack.pop() {
|
|
53
|
+
if matches!(
|
|
54
|
+
node.kind(),
|
|
55
|
+
"initialized_identifier"
|
|
56
|
+
| "initialized_variable_definition"
|
|
57
|
+
| "static_final_declaration"
|
|
58
|
+
) && node
|
|
59
|
+
.child_by_field_name("name")
|
|
60
|
+
.and_then(|binding| binding.utf8_text(source.as_bytes()).ok())
|
|
61
|
+
== Some(name)
|
|
62
|
+
{
|
|
63
|
+
declarations += 1;
|
|
64
|
+
constant = node.parent().is_some_and(|parent| {
|
|
65
|
+
let mut cursor = parent.walk();
|
|
66
|
+
let direct = parent
|
|
67
|
+
.children(&mut cursor)
|
|
68
|
+
.any(|child| child.kind() == "const");
|
|
69
|
+
direct
|
|
70
|
+
|| parent.parent().is_some_and(|outer| {
|
|
71
|
+
let mut cursor = outer.walk();
|
|
72
|
+
outer
|
|
73
|
+
.children(&mut cursor)
|
|
74
|
+
.any(|child| child.kind() == "const")
|
|
75
|
+
})
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
let mut cursor = node.walk();
|
|
79
|
+
stack.extend(node.named_children(&mut cursor));
|
|
80
|
+
}
|
|
81
|
+
declarations == 1 && constant
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn scope_assigns_name(scope: Node<'_>, source: &str, name: &str) -> bool {
|
|
85
|
+
let mut stack = vec![scope];
|
|
86
|
+
while let Some(node) = stack.pop() {
|
|
87
|
+
if node.kind() == "assignment_expression"
|
|
88
|
+
&& node
|
|
89
|
+
.child_by_field_name("left")
|
|
90
|
+
.and_then(|left| left.utf8_text(source.as_bytes()).ok())
|
|
91
|
+
.is_some_and(|left| assigns_name(left.trim(), name))
|
|
92
|
+
{
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
let mut cursor = node.walk();
|
|
96
|
+
stack.extend(node.named_children(&mut cursor));
|
|
97
|
+
}
|
|
98
|
+
false
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
fn assigns_name(left: &str, name: &str) -> bool {
|
|
102
|
+
left == name
|
|
103
|
+
|| left
|
|
104
|
+
.strip_prefix(name)
|
|
105
|
+
.is_some_and(|rest| rest.starts_with('['))
|
|
106
|
+
}
|