dart-decimate 0.0.13 → 0.0.15
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 +75 -1
- package/Cargo.toml +2 -1
- package/README.md +22 -27
- package/npm/scripts/postinstall.js +33 -12
- package/npm/scripts/test-npx-local.js +7 -2
- package/npm/scripts/test-npx-mcp-local.js +3 -1
- package/npm/scripts/test-npx-prebuilt.js +33 -13
- package/npm/scripts/test-postinstall-prebuilt.js +33 -13
- package/package.json +9 -3
- package/src/changed_scope.rs +4 -7
- package/src/cli/audit_run.rs +1 -3
- package/src/cli/decision_surface_run.rs +1 -3
- package/src/cli/entry_points.rs +75 -13
- package/src/dart_parser.rs +1 -3
- package/src/dependencies/analyze.rs +94 -11
- package/src/dependencies/codegen.rs +26 -0
- package/src/dependencies.rs +8 -1
- package/src/dependency_scripts.rs +42 -8
- package/src/extract.rs +6 -0
- package/src/git_command.rs +58 -0
- package/src/graph.rs +5 -0
- package/src/lib.rs +1 -0
- package/src/manifest.rs +1 -1
- package/src/output/route_cycle/navigation.rs +150 -22
- package/src/output/route_cycle.rs +4 -1
- package/src/scan/tests.rs +133 -0
- package/src/scan.rs +122 -47
- package/src/security/detect.rs +1275 -10
- package/src/security_gate.rs +3 -5
- package/src/trace.rs +22 -13
- package/src/widgets/inheritance.rs +250 -0
- package/src/widgets/params.rs +81 -1
- package/src/widgets/patterns.rs +32 -25
- package/src/widgets/reads.rs +61 -11
- package/src/widgets/resolution.rs +281 -0
- package/src/widgets/unrendered.rs +111 -47
- package/src/widgets.rs +69 -11
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
use tree_sitter::Node;
|
|
2
2
|
|
|
3
3
|
use super::{
|
|
4
|
-
balanced_enclosed_text, class_extends_state, direct_constructor_call_text,
|
|
5
|
-
direct_static_member_call_text, is_identifier_character, simple_type_name,
|
|
4
|
+
argument_list, balanced_enclosed_text, class_extends_state, direct_constructor_call_text,
|
|
5
|
+
direct_named_child, direct_static_member_call_text, is_identifier_character, simple_type_name,
|
|
6
6
|
strip_constructor_keyword_prefix, strip_whitespace, unwrap_parenthesized_text,
|
|
7
7
|
};
|
|
8
8
|
|
|
@@ -42,10 +42,8 @@ pub(super) fn route_extension_navigation_has_context_argument(
|
|
|
42
42
|
) -> bool {
|
|
43
43
|
first_positional_argument(arguments)
|
|
44
44
|
.and_then(|argument| argument.utf8_text(source.as_bytes()).ok())
|
|
45
|
-
.and_then(
|
|
46
|
-
.is_some_and(|
|
|
47
|
-
target_resolves_to_navigation_type(root, site, &target, &[BUILD_CONTEXT], source)
|
|
48
|
-
})
|
|
45
|
+
.and_then(|target| navigation_expression_resolution(root, site, target, source))
|
|
46
|
+
.is_some_and(|resolution| resolution.matches(&[BUILD_CONTEXT]))
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
pub(super) fn navigation_receiver_accepts_route_location(
|
|
@@ -58,9 +56,8 @@ pub(super) fn navigation_receiver_accepts_route_location(
|
|
|
58
56
|
if go_router_receiver_expression(root, site, receiver, source) {
|
|
59
57
|
return true;
|
|
60
58
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})
|
|
59
|
+
navigation_expression_resolution(root, site, receiver, source)
|
|
60
|
+
.is_some_and(|resolution| resolution.matches(&[BUILD_CONTEXT, GO_ROUTER]))
|
|
64
61
|
}
|
|
65
62
|
|
|
66
63
|
fn go_router_receiver_expression(
|
|
@@ -325,19 +322,76 @@ fn navigation_target_name(text: &str) -> Option<NavigationTargetName> {
|
|
|
325
322
|
})
|
|
326
323
|
}
|
|
327
324
|
|
|
328
|
-
fn
|
|
325
|
+
fn navigation_expression_resolution(
|
|
329
326
|
root: Node<'_>,
|
|
330
327
|
site: Node<'_>,
|
|
331
|
-
|
|
332
|
-
|
|
328
|
+
expression: &str,
|
|
329
|
+
source: &str,
|
|
330
|
+
) -> Option<NameResolution> {
|
|
331
|
+
let compact = strip_whitespace(expression.trim());
|
|
332
|
+
let unwrapped = unwrap_parenthesized_text(&compact).unwrap_or(&compact);
|
|
333
|
+
let expression = unwrapped.trim_end_matches(['?', '!']);
|
|
334
|
+
if navigator_context_expression(root, site, expression, source) {
|
|
335
|
+
return Some(NameResolution::Type(BUILD_CONTEXT.to_owned()));
|
|
336
|
+
}
|
|
337
|
+
if let Some(target) = navigation_target_name(expression) {
|
|
338
|
+
return if target.member_only {
|
|
339
|
+
class_member_resolution_at(root, site, &target.name, source)
|
|
340
|
+
} else {
|
|
341
|
+
visible_name_resolution_at(root, site, &target.name, source)
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
let (receiver, member) = expression.rsplit_once('.')?;
|
|
345
|
+
if !is_simple_identifier(receiver) || !is_simple_identifier(member) {
|
|
346
|
+
return None;
|
|
347
|
+
}
|
|
348
|
+
let NameResolution::Type(owner_type) =
|
|
349
|
+
visible_name_resolution_at(root, site, receiver, source)?
|
|
350
|
+
else {
|
|
351
|
+
return Some(NameResolution::Shadowed);
|
|
352
|
+
};
|
|
353
|
+
local_type_member_resolution(root, &owner_type, member, source)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
fn navigator_context_expression(
|
|
357
|
+
root: Node<'_>,
|
|
358
|
+
site: Node<'_>,
|
|
359
|
+
expression: &str,
|
|
333
360
|
source: &str,
|
|
334
361
|
) -> bool {
|
|
335
|
-
let
|
|
336
|
-
|
|
337
|
-
} else {
|
|
338
|
-
visible_name_resolution_at(root, site, &target.name, source)
|
|
362
|
+
let Some(factory) = expression.strip_suffix(".context") else {
|
|
363
|
+
return false;
|
|
339
364
|
};
|
|
340
|
-
|
|
365
|
+
if !direct_static_member_call_text(factory, "Navigator", "of")
|
|
366
|
+
|| term_identifier_shadowed_at(root, site, "Navigator", source)
|
|
367
|
+
|| !framework_build_context_import(root, None, source)
|
|
368
|
+
{
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
let mut context_argument_resolves = false;
|
|
372
|
+
visit_named(site, &mut |node| {
|
|
373
|
+
if context_argument_resolves
|
|
374
|
+
|| !matches!(
|
|
375
|
+
node.kind(),
|
|
376
|
+
"call_expression" | "function_expression_invocation"
|
|
377
|
+
)
|
|
378
|
+
{
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
let Some(arguments) = argument_list(node) else {
|
|
382
|
+
return;
|
|
383
|
+
};
|
|
384
|
+
let Some(prefix) = source.get(node.start_byte()..arguments.start_byte()) else {
|
|
385
|
+
return;
|
|
386
|
+
};
|
|
387
|
+
if strip_whitespace(prefix) == "Navigator.of" {
|
|
388
|
+
context_argument_resolves = first_positional_argument(arguments)
|
|
389
|
+
.and_then(|argument| argument.utf8_text(source.as_bytes()).ok())
|
|
390
|
+
.and_then(|argument| navigation_expression_resolution(root, node, argument, source))
|
|
391
|
+
.is_some_and(|resolution| resolution.matches(&[BUILD_CONTEXT]));
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
context_argument_resolves
|
|
341
395
|
}
|
|
342
396
|
|
|
343
397
|
fn visible_name_resolution_at(
|
|
@@ -505,6 +559,9 @@ fn declaration_type_resolution(
|
|
|
505
559
|
return formal_parameter_type(root, node, name, source)
|
|
506
560
|
.map_or(NameResolution::Shadowed, NameResolution::Type);
|
|
507
561
|
}
|
|
562
|
+
if let Some(resolution) = object_pattern_member_resolution(root, node, name, source) {
|
|
563
|
+
return resolution;
|
|
564
|
+
}
|
|
508
565
|
if let Some(resolution) = declared_type_for_name(root, node, name, source) {
|
|
509
566
|
return resolution;
|
|
510
567
|
}
|
|
@@ -611,9 +668,8 @@ fn initializer_type_for_name(
|
|
|
611
668
|
}
|
|
612
669
|
|
|
613
670
|
fn initializer_type(root: Node<'_>, node: Node<'_>, source: &str) -> Option<String> {
|
|
614
|
-
let
|
|
615
|
-
let
|
|
616
|
-
let expression = right.trim().trim_end_matches([',', ';']).trim();
|
|
671
|
+
let value = node.child_by_field_name("value")?;
|
|
672
|
+
let expression = value.utf8_text(source.as_bytes()).ok()?.trim();
|
|
617
673
|
let unwrapped = unwrap_parenthesized_text(expression).unwrap_or(expression);
|
|
618
674
|
let without_postfix = unwrapped.trim_end_matches(['?', '!']).trim();
|
|
619
675
|
let unwrapped = unwrap_parenthesized_text(without_postfix).unwrap_or(without_postfix);
|
|
@@ -627,7 +683,60 @@ fn initializer_type(root: Node<'_>, node: Node<'_>, source: &str) -> Option<Stri
|
|
|
627
683
|
return Some(target_type.to_owned());
|
|
628
684
|
}
|
|
629
685
|
}
|
|
630
|
-
|
|
686
|
+
match navigation_expression_resolution(root, node, expression, source)? {
|
|
687
|
+
NameResolution::Type(type_name) => Some(type_name),
|
|
688
|
+
NameResolution::Shadowed => None,
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
fn object_pattern_member_resolution(
|
|
693
|
+
root: Node<'_>,
|
|
694
|
+
node: Node<'_>,
|
|
695
|
+
name: &str,
|
|
696
|
+
source: &str,
|
|
697
|
+
) -> Option<NameResolution> {
|
|
698
|
+
let mut owner_type = None;
|
|
699
|
+
visit_named(node, &mut |candidate| {
|
|
700
|
+
if owner_type.is_some()
|
|
701
|
+
|| candidate.kind() != "object_pattern"
|
|
702
|
+
|| !declaration_binds_name(candidate, name, source)
|
|
703
|
+
{
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
let Some(text) = candidate.utf8_text(source.as_bytes()).ok() else {
|
|
707
|
+
return;
|
|
708
|
+
};
|
|
709
|
+
let Some((raw_type, _)) = text.split_once('(') else {
|
|
710
|
+
return;
|
|
711
|
+
};
|
|
712
|
+
let type_name = simple_type_name(raw_type.trim());
|
|
713
|
+
if is_simple_identifier(&type_name) {
|
|
714
|
+
owner_type = Some(type_name);
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
local_type_member_resolution(root, &owner_type?, name, source)
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
fn local_type_member_resolution(
|
|
721
|
+
root: Node<'_>,
|
|
722
|
+
owner_type: &str,
|
|
723
|
+
member_name: &str,
|
|
724
|
+
source: &str,
|
|
725
|
+
) -> Option<NameResolution> {
|
|
726
|
+
let mut resolution = None;
|
|
727
|
+
visit_named(root, &mut |candidate| {
|
|
728
|
+
if resolution.is_some()
|
|
729
|
+
|| candidate.kind() != "class_declaration"
|
|
730
|
+
|| field_text(candidate, "name", source).as_deref() != Some(owner_type)
|
|
731
|
+
{
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
let Some(body) = candidate.child_by_field_name("body") else {
|
|
735
|
+
return;
|
|
736
|
+
};
|
|
737
|
+
resolution = class_member_resolution(root, body, member_name, source);
|
|
738
|
+
});
|
|
739
|
+
resolution
|
|
631
740
|
}
|
|
632
741
|
|
|
633
742
|
fn scoped_header_binding_exists(
|
|
@@ -756,7 +865,9 @@ fn is_body_statement_child(kind: &str) -> bool {
|
|
|
756
865
|
}
|
|
757
866
|
|
|
758
867
|
fn declaration_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
759
|
-
if binding_name(node, source).as_deref() == Some(name)
|
|
868
|
+
if binding_name(node, source).as_deref() == Some(name)
|
|
869
|
+
|| object_pattern_shorthand_binds_name(node, name, source)
|
|
870
|
+
{
|
|
760
871
|
return true;
|
|
761
872
|
}
|
|
762
873
|
if is_callable_node(node.kind()) {
|
|
@@ -767,6 +878,23 @@ fn declaration_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
|
767
878
|
.any(|child| declaration_binds_name(child, name, source))
|
|
768
879
|
}
|
|
769
880
|
|
|
881
|
+
fn object_pattern_shorthand_binds_name(node: Node<'_>, name: &str, source: &str) -> bool {
|
|
882
|
+
if node.kind() != "object_pattern" {
|
|
883
|
+
return false;
|
|
884
|
+
}
|
|
885
|
+
let Some(text) = node.utf8_text(source.as_bytes()).ok() else {
|
|
886
|
+
return false;
|
|
887
|
+
};
|
|
888
|
+
let compact = strip_whitespace(text);
|
|
889
|
+
let marker = format!(":{name}");
|
|
890
|
+
compact.match_indices(&marker).any(|(start, _)| {
|
|
891
|
+
compact
|
|
892
|
+
.get(start + marker.len()..)
|
|
893
|
+
.and_then(|rest| rest.chars().next())
|
|
894
|
+
.is_none_or(|character| !is_identifier_character(character))
|
|
895
|
+
})
|
|
896
|
+
}
|
|
897
|
+
|
|
770
898
|
fn binding_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
771
899
|
if !matches!(
|
|
772
900
|
node.kind(),
|
|
@@ -181,7 +181,10 @@ fn raw_receiver_before_navigation<'source>(
|
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
fn constructor_receiver_text(receiver: &str) -> String {
|
|
184
|
-
let trimmed = receiver
|
|
184
|
+
let trimmed = receiver
|
|
185
|
+
.rsplit_once("=>")
|
|
186
|
+
.map_or(receiver, |(_, expression)| expression)
|
|
187
|
+
.trim();
|
|
185
188
|
let unwrapped = unwrap_parenthesized_text(trimmed).unwrap_or(trimmed);
|
|
186
189
|
let without_keyword = strip_constructor_keyword_prefix(unwrapped).unwrap_or(unwrapped);
|
|
187
190
|
strip_whitespace(without_keyword)
|
package/src/scan/tests.rs
CHANGED
|
@@ -221,6 +221,139 @@ fn scans_pub_workspace_member_files() -> Result<(), Box<dyn std::error::Error>>
|
|
|
221
221
|
Ok(())
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
+
#[test]
|
|
225
|
+
fn repository_scan_stops_parent_gitignore_discovery_at_git_root()
|
|
226
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
227
|
+
let fixture = tempfile::tempdir()?;
|
|
228
|
+
write(&fixture, ".gitignore", "*.dart\n")?;
|
|
229
|
+
write(&fixture, "repo/.git/HEAD", "ref: refs/heads/main\n")?;
|
|
230
|
+
write(&fixture, "repo/.gitignore", "ignored.dart\n")?;
|
|
231
|
+
write(&fixture, "repo/pubspec.yaml", "name: app\n")?;
|
|
232
|
+
write(&fixture, "repo/lib/main.dart", "void main() {}\n")?;
|
|
233
|
+
write(&fixture, "repo/lib/ignored.dart", "class Ignored {}\n")?;
|
|
234
|
+
|
|
235
|
+
let project = scan_project(fixture.path().join("repo"))?;
|
|
236
|
+
|
|
237
|
+
assert_eq!(project.files.len(), 1);
|
|
238
|
+
assert_eq!(
|
|
239
|
+
project.files[0].path,
|
|
240
|
+
fixture.path().join("repo/lib/main.dart")
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
Ok(())
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
#[test]
|
|
247
|
+
fn non_repository_scan_uses_root_gitignore_without_parent_rules()
|
|
248
|
+
-> Result<(), Box<dyn std::error::Error>> {
|
|
249
|
+
let fixture = tempfile::tempdir()?;
|
|
250
|
+
write(&fixture, ".gitignore", "*.dart\n")?;
|
|
251
|
+
write(&fixture, "app/.gitignore", "ignored.dart\n")?;
|
|
252
|
+
write(&fixture, "app/pubspec.yaml", "name: app\n")?;
|
|
253
|
+
write(&fixture, "app/lib/main.dart", "void main() {}\n")?;
|
|
254
|
+
write(&fixture, "app/lib/ignored.dart", "class Ignored {}\n")?;
|
|
255
|
+
|
|
256
|
+
let project = scan_project(fixture.path().join("app"))?;
|
|
257
|
+
|
|
258
|
+
assert_eq!(project.files.len(), 1);
|
|
259
|
+
assert_eq!(
|
|
260
|
+
project.files[0].path,
|
|
261
|
+
fixture.path().join("app/lib/main.dart")
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
Ok(())
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
#[test]
|
|
268
|
+
fn ignored_external_package_root_is_not_scanned() -> Result<(), Box<dyn std::error::Error>> {
|
|
269
|
+
let fixture = tempfile::tempdir()?;
|
|
270
|
+
write(&fixture, ".git/HEAD", "ref: refs/heads/main\n")?;
|
|
271
|
+
write(&fixture, ".gitignore", "shared/\n")?;
|
|
272
|
+
write(
|
|
273
|
+
&fixture,
|
|
274
|
+
"app/pubspec.yaml",
|
|
275
|
+
"name: app\ndependencies:\n shared:\n path: ../shared\n",
|
|
276
|
+
)?;
|
|
277
|
+
write(&fixture, "app/lib/main.dart", "void main() {}\n")?;
|
|
278
|
+
write(&fixture, "shared/pubspec.yaml", "name: shared\n")?;
|
|
279
|
+
write(
|
|
280
|
+
&fixture,
|
|
281
|
+
"shared/lib/generated.dart",
|
|
282
|
+
"class Generated {}\n",
|
|
283
|
+
)?;
|
|
284
|
+
|
|
285
|
+
let project = scan_project(fixture.path().join("app"))?;
|
|
286
|
+
|
|
287
|
+
assert_eq!(project.files.len(), 1);
|
|
288
|
+
assert_eq!(
|
|
289
|
+
project.files[0].path,
|
|
290
|
+
fixture.path().join("app/lib/main.dart")
|
|
291
|
+
);
|
|
292
|
+
Ok(())
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
#[test]
|
|
296
|
+
fn tracked_dart_files_override_gitignore_rules() -> Result<(), Box<dyn std::error::Error>> {
|
|
297
|
+
let fixture = tempfile::tempdir()?;
|
|
298
|
+
let status = crate::git_command::for_repository(fixture.path())
|
|
299
|
+
.args(["init", "--quiet"])
|
|
300
|
+
.status()?;
|
|
301
|
+
assert!(status.success());
|
|
302
|
+
write(&fixture, ".gitignore", "*.g.dart\n")?;
|
|
303
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
304
|
+
write(&fixture, "lib/main.dart", "void main() {}\n")?;
|
|
305
|
+
write(&fixture, "lib/tracked.g.dart", "class Tracked {}\n")?;
|
|
306
|
+
write(&fixture, "lib/untracked.g.dart", "class Untracked {}\n")?;
|
|
307
|
+
let status = crate::git_command::for_repository(fixture.path())
|
|
308
|
+
.args([
|
|
309
|
+
"add",
|
|
310
|
+
"--force",
|
|
311
|
+
".gitignore",
|
|
312
|
+
"pubspec.yaml",
|
|
313
|
+
"lib/main.dart",
|
|
314
|
+
"lib/tracked.g.dart",
|
|
315
|
+
])
|
|
316
|
+
.status()?;
|
|
317
|
+
assert!(status.success());
|
|
318
|
+
|
|
319
|
+
let project = scan_project(fixture.path())?;
|
|
320
|
+
let paths = project
|
|
321
|
+
.files
|
|
322
|
+
.iter()
|
|
323
|
+
.map(|file| file.path.strip_prefix(fixture.path()))
|
|
324
|
+
.collect::<Result<Vec<_>, _>>()?;
|
|
325
|
+
|
|
326
|
+
assert!(paths.contains(&Path::new("lib/tracked.g.dart")));
|
|
327
|
+
assert!(!paths.contains(&Path::new("lib/untracked.g.dart")));
|
|
328
|
+
Ok(())
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
#[test]
|
|
332
|
+
fn existing_ignored_dependency_targets_are_not_unresolved() -> Result<(), Box<dyn std::error::Error>>
|
|
333
|
+
{
|
|
334
|
+
let fixture = tempfile::tempdir()?;
|
|
335
|
+
write(&fixture, ".gitignore", "*.gen.dart\n")?;
|
|
336
|
+
write(&fixture, "pubspec.yaml", "name: app\n")?;
|
|
337
|
+
write(
|
|
338
|
+
&fixture,
|
|
339
|
+
"lib/main.dart",
|
|
340
|
+
"import 'assets.gen.dart';\nvoid main() {}\n",
|
|
341
|
+
)?;
|
|
342
|
+
write(
|
|
343
|
+
&fixture,
|
|
344
|
+
"lib/assets.gen.dart",
|
|
345
|
+
"class GeneratedAssets {}\n",
|
|
346
|
+
)?;
|
|
347
|
+
|
|
348
|
+
let project = scan_project(fixture.path())?;
|
|
349
|
+
|
|
350
|
+
assert_eq!(project.files.len(), 1);
|
|
351
|
+
assert_eq!(project.graph.node_count(), 1);
|
|
352
|
+
assert_eq!(project.graph.edge_count(), 0);
|
|
353
|
+
assert!(project.graph.unresolved().is_empty());
|
|
354
|
+
Ok(())
|
|
355
|
+
}
|
|
356
|
+
|
|
224
357
|
#[test]
|
|
225
358
|
fn scans_all_conditional_import_targets() -> Result<(), Box<dyn std::error::Error>> {
|
|
226
359
|
let fixture = tempfile::tempdir()?;
|
package/src/scan.rs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
use std::collections::{BTreeMap, BTreeSet};
|
|
2
|
-
use std::fs;
|
|
3
2
|
use std::path::{Path, PathBuf};
|
|
4
3
|
|
|
5
4
|
use glob::Pattern;
|
|
5
|
+
use ignore::WalkBuilder;
|
|
6
|
+
use ignore::gitignore::GitignoreBuilder;
|
|
6
7
|
use rayon::prelude::*;
|
|
7
8
|
use serde::{Deserialize, Serialize};
|
|
8
9
|
use thiserror::Error;
|
|
@@ -11,7 +12,7 @@ use crate::graph::normalize_path;
|
|
|
11
12
|
use crate::package_map::PackageMap;
|
|
12
13
|
use crate::{
|
|
13
14
|
DartFile, ExtractError, GraphError, GraphOptions, ModuleGraph, build_module_graph_with_options,
|
|
14
|
-
extract_dart_file,
|
|
15
|
+
extract_dart_file, git_command,
|
|
15
16
|
};
|
|
16
17
|
|
|
17
18
|
/// Parsed Dart files plus their resolved module graph.
|
|
@@ -45,29 +46,13 @@ pub enum ScanError {
|
|
|
45
46
|
/// Underlying IO error.
|
|
46
47
|
source: std::io::Error,
|
|
47
48
|
},
|
|
48
|
-
/// A
|
|
49
|
-
#[error("failed to
|
|
50
|
-
|
|
49
|
+
/// A scan root could not be traversed.
|
|
50
|
+
#[error("failed to walk directory {path}: {source}")]
|
|
51
|
+
Walk {
|
|
51
52
|
/// Directory path.
|
|
52
53
|
path: PathBuf,
|
|
53
|
-
/// Underlying
|
|
54
|
-
source:
|
|
55
|
-
},
|
|
56
|
-
/// A directory entry could not be read.
|
|
57
|
-
#[error("failed to read directory entry under {path}: {source}")]
|
|
58
|
-
ReadDirEntry {
|
|
59
|
-
/// Directory being scanned.
|
|
60
|
-
path: PathBuf,
|
|
61
|
-
/// Underlying IO error.
|
|
62
|
-
source: std::io::Error,
|
|
63
|
-
},
|
|
64
|
-
/// A file type could not be read.
|
|
65
|
-
#[error("failed to read file type for {path}: {source}")]
|
|
66
|
-
FileType {
|
|
67
|
-
/// Entry path.
|
|
68
|
-
path: PathBuf,
|
|
69
|
-
/// Underlying IO error.
|
|
70
|
-
source: std::io::Error,
|
|
54
|
+
/// Underlying ignore-aware walk error.
|
|
55
|
+
source: ignore::Error,
|
|
71
56
|
},
|
|
72
57
|
/// A pubspec could not be read while expanding local packages.
|
|
73
58
|
#[error("failed to read pubspec {path}: {source}")]
|
|
@@ -150,7 +135,19 @@ pub fn scan_project_with_options(
|
|
|
150
135
|
|
|
151
136
|
let mut paths = BTreeSet::new();
|
|
152
137
|
for scan_root in &scan_roots {
|
|
138
|
+
if scan_roots
|
|
139
|
+
.iter()
|
|
140
|
+
.any(|other| other != scan_root && scan_root.starts_with(other))
|
|
141
|
+
{
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
let tracked = tracked_dart_files(&root, scan_root, &ignore_matcher);
|
|
145
|
+
if scan_root != &root && tracked.is_empty() && ignored_by_parent_gitignore(&root, scan_root)
|
|
146
|
+
{
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
153
149
|
discover_dart_files(&root, scan_root, &ignore_matcher, &mut paths)?;
|
|
150
|
+
paths.extend(tracked);
|
|
154
151
|
}
|
|
155
152
|
let paths = paths.into_iter().collect::<Vec<_>>();
|
|
156
153
|
|
|
@@ -160,13 +157,14 @@ pub fn scan_project_with_options(
|
|
|
160
157
|
.collect::<Result<Vec<_>, _>>()?;
|
|
161
158
|
files.sort_by(|left, right| left.path.cmp(&right.path));
|
|
162
159
|
|
|
163
|
-
let graph = build_module_graph_with_options(
|
|
160
|
+
let mut graph = build_module_graph_with_options(
|
|
164
161
|
&root,
|
|
165
162
|
&files,
|
|
166
163
|
&GraphOptions {
|
|
167
164
|
conditional_environment: options.conditional_environment.clone(),
|
|
168
165
|
},
|
|
169
166
|
)?;
|
|
167
|
+
graph.suppress_existing_unresolved_targets();
|
|
170
168
|
|
|
171
169
|
Ok(ScannedProject {
|
|
172
170
|
root,
|
|
@@ -176,6 +174,39 @@ pub fn scan_project_with_options(
|
|
|
176
174
|
})
|
|
177
175
|
}
|
|
178
176
|
|
|
177
|
+
fn ignored_by_parent_gitignore(root: &Path, scan_root: &Path) -> bool {
|
|
178
|
+
let Some(repository_root) = root
|
|
179
|
+
.ancestors()
|
|
180
|
+
.find(|ancestor| ancestor.join(".git").exists() || ancestor.join(".jj").exists())
|
|
181
|
+
else {
|
|
182
|
+
return false;
|
|
183
|
+
};
|
|
184
|
+
if !scan_root.starts_with(repository_root) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
let Some(parent) = scan_root.parent() else {
|
|
188
|
+
return false;
|
|
189
|
+
};
|
|
190
|
+
let mut directories = parent
|
|
191
|
+
.ancestors()
|
|
192
|
+
.take_while(|ancestor| ancestor.starts_with(repository_root))
|
|
193
|
+
.collect::<Vec<_>>();
|
|
194
|
+
directories.reverse();
|
|
195
|
+
|
|
196
|
+
let mut builder = GitignoreBuilder::new(repository_root);
|
|
197
|
+
for directory in directories {
|
|
198
|
+
let path = directory.join(".gitignore");
|
|
199
|
+
if path.is_file() && builder.add(path).is_some() {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
builder.build().ok().is_some_and(|gitignore| {
|
|
204
|
+
gitignore
|
|
205
|
+
.matched_path_or_any_parents(scan_root, true)
|
|
206
|
+
.is_ignore()
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
179
210
|
fn normalize_scan_root(root: &Path) -> Result<PathBuf, ScanError> {
|
|
180
211
|
if root.is_absolute() {
|
|
181
212
|
return Ok(normalize_path(root));
|
|
@@ -191,41 +222,85 @@ fn discover_dart_files(
|
|
|
191
222
|
ignore_matcher: &IgnoreMatcher,
|
|
192
223
|
paths: &mut BTreeSet<PathBuf>,
|
|
193
224
|
) -> Result<(), ScanError> {
|
|
194
|
-
let
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
225
|
+
let filter_root = root.to_path_buf();
|
|
226
|
+
let filter_matcher = ignore_matcher.clone();
|
|
227
|
+
let has_repository_ancestor = dir
|
|
228
|
+
.ancestors()
|
|
229
|
+
.any(|ancestor| ancestor.join(".git").exists() || ancestor.join(".jj").exists());
|
|
230
|
+
let mut builder = WalkBuilder::new(dir);
|
|
231
|
+
builder
|
|
232
|
+
.standard_filters(false)
|
|
233
|
+
.git_ignore(true)
|
|
234
|
+
.parents(has_repository_ancestor)
|
|
235
|
+
.require_git(has_repository_ancestor)
|
|
236
|
+
.filter_entry(move |entry| {
|
|
237
|
+
entry.depth() == 0
|
|
238
|
+
|| !(filter_matcher.matches(&filter_root, entry.path())
|
|
239
|
+
|| entry
|
|
240
|
+
.file_type()
|
|
241
|
+
.is_some_and(|file_type| file_type.is_dir())
|
|
242
|
+
&& should_skip_dir(entry.path()))
|
|
243
|
+
});
|
|
198
244
|
|
|
199
|
-
for
|
|
200
|
-
let entry =
|
|
245
|
+
for result in builder.build() {
|
|
246
|
+
let entry = result.map_err(|source| ScanError::Walk {
|
|
201
247
|
path: dir.to_path_buf(),
|
|
202
248
|
source,
|
|
203
249
|
})?;
|
|
204
250
|
let path = entry.path();
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if should_skip_dir(&path) {
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
if ignore_matcher.matches(root, &path) {
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
discover_dart_files(root, &path, ignore_matcher, paths)?;
|
|
218
|
-
} else if file_type.is_file()
|
|
219
|
-
&& path.extension().is_some_and(|ext| ext == "dart")
|
|
220
|
-
&& !ignore_matcher.matches(root, &path)
|
|
251
|
+
if entry
|
|
252
|
+
.file_type()
|
|
253
|
+
.is_some_and(|file_type| file_type.is_file())
|
|
254
|
+
&& path
|
|
255
|
+
.extension()
|
|
256
|
+
.is_some_and(|extension| extension == "dart")
|
|
221
257
|
{
|
|
222
|
-
paths.insert(normalize_path(
|
|
258
|
+
paths.insert(normalize_path(path));
|
|
223
259
|
}
|
|
224
260
|
}
|
|
225
261
|
|
|
226
262
|
Ok(())
|
|
227
263
|
}
|
|
228
264
|
|
|
265
|
+
fn tracked_dart_files(
|
|
266
|
+
root: &Path,
|
|
267
|
+
scan_root: &Path,
|
|
268
|
+
ignore_matcher: &IgnoreMatcher,
|
|
269
|
+
) -> BTreeSet<PathBuf> {
|
|
270
|
+
let Ok(output) = git_command::for_repository(scan_root)
|
|
271
|
+
.args(["ls-files", "--cached", "-z"])
|
|
272
|
+
.output()
|
|
273
|
+
else {
|
|
274
|
+
return BTreeSet::new();
|
|
275
|
+
};
|
|
276
|
+
if !output.status.success() {
|
|
277
|
+
return BTreeSet::new();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
output
|
|
281
|
+
.stdout
|
|
282
|
+
.split(|byte| *byte == 0)
|
|
283
|
+
.filter_map(|path| std::str::from_utf8(path).ok())
|
|
284
|
+
.map(|path| normalize_path(&scan_root.join(path)))
|
|
285
|
+
.filter(|path| {
|
|
286
|
+
path.starts_with(scan_root)
|
|
287
|
+
&& path.is_file()
|
|
288
|
+
&& path
|
|
289
|
+
.extension()
|
|
290
|
+
.is_some_and(|extension| extension == "dart")
|
|
291
|
+
&& !ignore_matcher.matches(root, path)
|
|
292
|
+
&& !has_skipped_directory(scan_root, path)
|
|
293
|
+
})
|
|
294
|
+
.collect()
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
fn has_skipped_directory(root: &Path, path: &Path) -> bool {
|
|
298
|
+
path.strip_prefix(root)
|
|
299
|
+
.ok()
|
|
300
|
+
.and_then(Path::parent)
|
|
301
|
+
.is_some_and(|parent| parent.ancestors().any(should_skip_dir))
|
|
302
|
+
}
|
|
303
|
+
|
|
229
304
|
fn should_skip_dir(path: &Path) -> bool {
|
|
230
305
|
matches!(
|
|
231
306
|
path.file_name().and_then(|name| name.to_str()),
|