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.
Files changed (53) hide show
  1. package/Cargo.lock +1 -1
  2. package/Cargo.toml +1 -1
  3. package/README.md +8 -8
  4. package/package.json +2 -2
  5. package/src/boundaries.rs +1 -9
  6. package/src/cli/analyze.rs +33 -3
  7. package/src/cli/error.rs +16 -0
  8. package/src/cli/list_run.rs +3 -1
  9. package/src/cli/scope_args.rs +14 -3
  10. package/src/config.rs +12 -8
  11. package/src/dart_parser.rs +100 -9
  12. package/src/dependencies/analyze.rs +4 -3
  13. package/src/dependencies/discovery.rs +10 -2
  14. package/src/dependencies/flutter_usage.rs +4 -0
  15. package/src/dependencies/tests.rs +5 -5
  16. package/src/dependencies/usage.rs +0 -9
  17. package/src/dependencies.rs +12 -7
  18. package/src/dupes/declarations.rs +1 -2
  19. package/src/dupes.rs +1 -10
  20. package/src/explain/misc.rs +1 -1
  21. package/src/explain.rs +1 -1
  22. package/src/extract/members.rs +24 -15
  23. package/src/extract/tests.rs +89 -0
  24. package/src/extract.rs +51 -15
  25. package/src/graph.rs +9 -0
  26. package/src/lib.rs +3 -2
  27. package/src/output/duplication_findings.rs +1 -2
  28. package/src/output/human.rs +7 -2
  29. package/src/output/human_details.rs +6 -0
  30. package/src/output/next_steps.rs +111 -0
  31. package/src/output/route_cycle/aliases.rs +1 -2
  32. package/src/output/route_cycle/wrappers.rs +1 -2
  33. package/src/output/types.rs +2 -0
  34. package/src/output.rs +27 -5
  35. package/src/outside/loader.rs +133 -0
  36. package/src/outside/loader_tests.rs +136 -0
  37. package/src/outside/resolver.rs +484 -0
  38. package/src/outside/tests.rs +242 -0
  39. package/src/outside.rs +186 -0
  40. package/src/package_map.rs +30 -1
  41. package/src/report_schema.rs +1 -0
  42. package/src/routes.rs +2 -0
  43. package/src/scan.rs +24 -3
  44. package/src/security/detect/process_arguments.rs +106 -0
  45. package/src/security/detect.rs +98 -40
  46. package/src/symbols/extensions.rs +42 -27
  47. package/src/symbols/path_filters.rs +10 -1
  48. package/src/symbols/tests.rs +15 -0
  49. package/src/symbols.rs +489 -67
  50. package/src/trace.rs +6 -5
  51. package/src/widgets/reads.rs +4 -8
  52. package/src/widgets/resolution.rs +63 -10
  53. package/src/workspace_scope.rs +1 -10
@@ -3,6 +3,7 @@ use std::path::Path;
3
3
 
4
4
  use tree_sitter::Node;
5
5
 
6
+ use self::process_arguments::{list_is_proven_fixed, name_is_reassigned};
6
7
  use super::catalogue::MatcherCatalogue;
7
8
  use super::{
8
9
  DetectedSecurityCandidate, DetectionResult, LibraryImportContext, SecurityBlindSpot,
@@ -12,6 +13,7 @@ use crate::Location;
12
13
  use crate::dart_parser::parse_dart_source_lossy;
13
14
  use crate::generated::{is_flutterfire_options_path, is_generated_dart_path};
14
15
 
16
+ mod process_arguments;
15
17
  mod weak_randomness;
16
18
 
17
19
  pub(super) fn detect_in_source(
@@ -676,6 +678,7 @@ fn detect_process_call(
676
678
  let Ok(function) = function_node.utf8_text(source.as_bytes()) else {
677
679
  return;
678
680
  };
681
+ let function = function.rsplit("=>").next().map_or(function, str::trim);
679
682
  let index = function_node.start_byte();
680
683
  let pattern = matcher
681
684
  .callees
@@ -741,19 +744,17 @@ fn detect_process_call(
741
744
  return;
742
745
  }
743
746
  let risky = if package_process {
744
- !parsed_run_in_shell_is_disabled(root, source, index)
745
- || process_manager_invokes_command_shell(root, source, index, arguments[0])
746
- || !fixed_argument_list(root, source, index, arguments[0])
747
+ process_manager_call_is_risky(root, source, index, arguments[0])
747
748
  } else {
748
- let fixed_executable =
749
- resolved_fixed_literal_value(root, source, index, arguments[0]).is_some();
750
- let fixed_arguments = fixed_argument_list(root, source, index, arguments[1]);
751
- let fixed_dart_runtime = pattern == "Process.start"
752
- && resolved_dart_runtime_start(Some((root, source)), index, call, inherited_imports);
753
- !parsed_run_in_shell_is_disabled(root, source, index)
754
- || call_invokes_command_shell(root, source, index, call)
755
- || (!fixed_executable && !fixed_dart_runtime)
756
- || !fixed_arguments
749
+ process_call_is_risky(
750
+ root,
751
+ source,
752
+ index,
753
+ call,
754
+ pattern,
755
+ &arguments,
756
+ inherited_imports,
757
+ )
757
758
  };
758
759
  if risky {
759
760
  result.candidates.push(detected(
@@ -768,6 +769,37 @@ fn detect_process_call(
768
769
  }
769
770
  }
770
771
 
772
+ fn process_manager_call_is_risky(
773
+ root: Node<'_>,
774
+ source: &str,
775
+ index: usize,
776
+ command: &str,
777
+ ) -> bool {
778
+ !parsed_run_in_shell_is_disabled(root, source, index)
779
+ || process_manager_invokes_command_shell(root, source, index, command)
780
+ || !fixed_argument_list(root, source, index, command)
781
+ }
782
+
783
+ fn process_call_is_risky(
784
+ root: Node<'_>,
785
+ source: &str,
786
+ index: usize,
787
+ call: &str,
788
+ pattern: &str,
789
+ arguments: &[&str],
790
+ inherited_imports: Option<&LibraryImportContext>,
791
+ ) -> bool {
792
+ let fixed_executable =
793
+ resolved_fixed_literal_value(root, source, index, arguments[0]).is_some();
794
+ let fixed_dart_runtime = pattern == "Process.start"
795
+ && resolved_dart_runtime_start(Some((root, source)), index, call, inherited_imports);
796
+ let fixed_arguments = fixed_argument_list(root, source, index, arguments[1]);
797
+ !parsed_run_in_shell_is_disabled(root, source, index)
798
+ || call_invokes_command_shell(root, source, index, call)
799
+ || (!fixed_executable && !fixed_dart_runtime)
800
+ || !fixed_arguments
801
+ }
802
+
771
803
  fn flattened_process_arguments<'source>(
772
804
  node: Node<'_>,
773
805
  source: &'source str,
@@ -1096,6 +1128,9 @@ fn string_literals(source: &str) -> Vec<StringLiteral> {
1096
1128
  fn read_string(source: &str, start: usize) -> Option<(String, usize, usize)> {
1097
1129
  let bytes = source.as_bytes();
1098
1130
  let quote = *bytes.get(start)?;
1131
+ if !matches!(quote, b'\'' | b'"') {
1132
+ return None;
1133
+ }
1099
1134
  if bytes.get(start..start + 3) == Some([quote, quote, quote].as_slice()) {
1100
1135
  let mut index = start + 3;
1101
1136
  let value_start = index;
@@ -1883,6 +1918,9 @@ fn resolved_list_arguments_inner(
1883
1918
  return None;
1884
1919
  }
1885
1920
  let binding = lexical_binding_value(root, source, index, value)?;
1921
+ if !list_is_proven_fixed(root, source, index, value) {
1922
+ return None;
1923
+ }
1886
1924
  resolved_list_arguments_inner(root, source, index, binding, visited)
1887
1925
  }
1888
1926
 
@@ -1890,6 +1928,23 @@ fn fixed_string_literal(value: &str) -> bool {
1890
1928
  fixed_literal_value(value).is_some()
1891
1929
  }
1892
1930
 
1931
+ fn settled_binding<'source>(
1932
+ root: Node<'_>,
1933
+ source: &str,
1934
+ index: usize,
1935
+ name: &str,
1936
+ binding: LexicalBinding<'source>,
1937
+ local: bool,
1938
+ ) -> Option<&'source str> {
1939
+ match binding {
1940
+ LexicalBinding::Value(value) => Some(value),
1941
+ LexicalBinding::Mutable(value) => {
1942
+ (local && !name_is_reassigned(root, source, index, name)).then_some(value)
1943
+ }
1944
+ LexicalBinding::Unknown => None,
1945
+ }
1946
+ }
1947
+
1893
1948
  fn lexical_binding_value<'source>(
1894
1949
  root: Node<'_>,
1895
1950
  source: &'source str,
@@ -1901,10 +1956,7 @@ fn lexical_binding_value<'source>(
1901
1956
  if parent.kind() == "class_body"
1902
1957
  && let Some(binding) = class_binding(parent, child, name, source)
1903
1958
  {
1904
- return match binding {
1905
- LexicalBinding::Value(value) => Some(value),
1906
- LexicalBinding::Unknown => None,
1907
- };
1959
+ return settled_binding(root, source, index, name, binding, false);
1908
1960
  }
1909
1961
  if process_scope_parameters_bind_name(parent, name, source)
1910
1962
  || process_scope_header_binds_name(parent, child, name, source)
@@ -1912,7 +1964,14 @@ fn lexical_binding_value<'source>(
1912
1964
  return None;
1913
1965
  }
1914
1966
  if parent.parent().is_none() {
1915
- return top_level_binding(parent, name, source);
1967
+ return settled_binding(
1968
+ root,
1969
+ source,
1970
+ index,
1971
+ name,
1972
+ top_level_binding(parent, name, source)?,
1973
+ false,
1974
+ );
1916
1975
  }
1917
1976
  let mut cursor = parent.walk();
1918
1977
  let mut siblings = parent
@@ -1921,10 +1980,14 @@ fn lexical_binding_value<'source>(
1921
1980
  .collect::<Vec<_>>();
1922
1981
  while let Some(sibling) = siblings.pop() {
1923
1982
  if let Some(binding) = direct_binding(sibling, name, source, false) {
1924
- return match binding {
1925
- LexicalBinding::Value(value) => Some(value),
1926
- LexicalBinding::Unknown => None,
1927
- };
1983
+ return settled_binding(
1984
+ root,
1985
+ source,
1986
+ index,
1987
+ name,
1988
+ binding,
1989
+ parent.kind() != "class_body",
1990
+ );
1928
1991
  }
1929
1992
  }
1930
1993
  child = parent;
@@ -2039,7 +2102,7 @@ fn top_level_binding<'source>(
2039
2102
  root: Node<'_>,
2040
2103
  name: &str,
2041
2104
  source: &'source str,
2042
- ) -> Option<&'source str> {
2105
+ ) -> Option<LexicalBinding<'source>> {
2043
2106
  let mut cursor = root.walk();
2044
2107
  let mut binding = None;
2045
2108
  for child in root.named_children(&mut cursor) {
@@ -2051,10 +2114,7 @@ fn top_level_binding<'source>(
2051
2114
  }
2052
2115
  binding = Some(candidate);
2053
2116
  }
2054
- match binding {
2055
- Some(LexicalBinding::Value(value)) => Some(value),
2056
- Some(LexicalBinding::Unknown) | None => None,
2057
- }
2117
+ binding
2058
2118
  }
2059
2119
 
2060
2120
  fn class_binding<'source>(
@@ -2249,8 +2309,10 @@ fn root_node(mut node: Node<'_>) -> Node<'_> {
2249
2309
  node
2250
2310
  }
2251
2311
 
2312
+ #[derive(Clone, Copy)]
2252
2313
  enum LexicalBinding<'source> {
2253
2314
  Value(&'source str),
2315
+ Mutable(&'source str),
2254
2316
  Unknown,
2255
2317
  }
2256
2318
 
@@ -2288,26 +2350,22 @@ fn direct_binding<'source>(
2288
2350
  if binding_name != name {
2289
2351
  return None;
2290
2352
  }
2291
- if !immutable {
2292
- return Some(LexicalBinding::Unknown);
2293
- }
2294
- return Some(
2295
- node.child_by_field_name("value")
2296
- .and_then(|value| value.utf8_text(source.as_bytes()).ok())
2297
- .map(str::trim)
2298
- .map_or(LexicalBinding::Unknown, LexicalBinding::Value),
2299
- );
2353
+ let value = node
2354
+ .child_by_field_name("value")
2355
+ .and_then(|value| value.utf8_text(source.as_bytes()).ok())
2356
+ .map(str::trim);
2357
+ return Some(match (value, immutable) {
2358
+ (Some(value), true) => LexicalBinding::Value(value),
2359
+ (Some(value), false) => LexicalBinding::Mutable(value),
2360
+ (None, _) => LexicalBinding::Unknown,
2361
+ });
2300
2362
  }
2301
2363
  if node.kind() == "assignment_expression" {
2302
2364
  let left = node
2303
2365
  .child_by_field_name("left")?
2304
2366
  .utf8_text(source.as_bytes())
2305
2367
  .ok()?;
2306
- let operator = node
2307
- .child_by_field_name("operator")?
2308
- .utf8_text(source.as_bytes())
2309
- .ok()?;
2310
- if left.trim() == name && operator == "=" {
2368
+ if left.trim() == name {
2311
2369
  return Some(LexicalBinding::Unknown);
2312
2370
  }
2313
2371
  return None;
@@ -14,6 +14,23 @@ pub(super) fn extend_implicit_extension_references(
14
14
  project: &ScannedProject,
15
15
  ) {
16
16
  let dependencies = project.graph.dependencies();
17
+ let mut imports_by_source = BTreeMap::<&PathBuf, Vec<&crate::ResolvedDependency>>::new();
18
+ for dependency in dependencies
19
+ .iter()
20
+ .filter(|dependency| dependency.kind == DependencyKind::Import)
21
+ {
22
+ imports_by_source
23
+ .entry(&dependency.from_path)
24
+ .or_default()
25
+ .push(dependency);
26
+ }
27
+ let mut member_names = BTreeMap::<(&PathBuf, &String), Vec<&String>>::new();
28
+ for member in members {
29
+ member_names
30
+ .entry((&member.path, &member.member.owner))
31
+ .or_default()
32
+ .push(&member.member.name);
33
+ }
17
34
  for declaration in declarations
18
35
  .iter()
19
36
  .filter(|declaration| declaration.declaration.kind == DeclarationKind::Extension)
@@ -27,13 +44,11 @@ pub(super) fn extend_implicit_extension_references(
27
44
  .map(|exported| exported.entry_path.clone())
28
45
  .chain(std::iter::once(declaration.path.clone()))
29
46
  .collect::<BTreeSet<_>>();
30
- let implicit_references = members
31
- .iter()
32
- .filter(|member| {
33
- member.path == declaration.path
34
- && member.member.owner == declaration.declaration.name
35
- })
36
- .filter_map(|member| references_by_name.get(&member.member.name))
47
+ let implicit_references = member_names
48
+ .get(&(&declaration.path, &declaration.declaration.name))
49
+ .into_iter()
50
+ .flatten()
51
+ .filter_map(|name| references_by_name.get(*name))
37
52
  .flatten()
38
53
  .filter(|reference| {
39
54
  extension_visible_from(
@@ -41,7 +56,7 @@ pub(super) fn extend_implicit_extension_references(
41
56
  declaration,
42
57
  &entry_paths,
43
58
  library_by_path,
44
- &dependencies,
59
+ &imports_by_source,
45
60
  )
46
61
  })
47
62
  .cloned()
@@ -62,7 +77,7 @@ fn extension_visible_from(
62
77
  declaration: &IndexedDeclaration,
63
78
  entry_paths: &BTreeSet<PathBuf>,
64
79
  library_by_path: &BTreeMap<PathBuf, PathBuf>,
65
- dependencies: &[crate::ResolvedDependency],
80
+ imports_by_source: &BTreeMap<&PathBuf, Vec<&crate::ResolvedDependency>>,
66
81
  ) -> bool {
67
82
  let reference_library = library_by_path
68
83
  .get(&reference.path)
@@ -74,22 +89,22 @@ fn extension_visible_from(
74
89
  return true;
75
90
  }
76
91
 
77
- dependencies.iter().any(|dependency| {
78
- dependency.kind == DependencyKind::Import
79
- && dependency.from_path == reference.path
80
- && entry_paths.contains(&dependency.to_path)
81
- && dependency.visibility.prefix.is_none()
82
- && dependency
83
- .visibility
84
- .combinators
85
- .iter()
86
- .all(|combinator| match combinator.kind {
87
- DartCombinatorKind::Show => {
88
- combinator.names.contains(&declaration.declaration.name)
89
- }
90
- DartCombinatorKind::Hide => {
91
- !combinator.names.contains(&declaration.declaration.name)
92
- }
93
- })
94
- })
92
+ imports_by_source
93
+ .get(&reference.path)
94
+ .is_some_and(|imports| {
95
+ imports.iter().any(|dependency| {
96
+ entry_paths.contains(&dependency.to_path)
97
+ && dependency.visibility.prefix.is_none()
98
+ && dependency.visibility.combinators.iter().all(|combinator| {
99
+ match combinator.kind {
100
+ DartCombinatorKind::Show => {
101
+ combinator.names.contains(&declaration.declaration.name)
102
+ }
103
+ DartCombinatorKind::Hide => {
104
+ !combinator.names.contains(&declaration.declaration.name)
105
+ }
106
+ }
107
+ })
108
+ })
109
+ })
95
110
  }
@@ -4,7 +4,7 @@ pub(super) fn is_private(name: &str) -> bool {
4
4
  name.starts_with('_')
5
5
  }
6
6
 
7
- pub(super) fn is_library_source(root: &Path, path: &Path) -> bool {
7
+ pub(crate) fn is_library_source(root: &Path, path: &Path) -> bool {
8
8
  path.strip_prefix(root).is_ok_and(|relative| {
9
9
  relative
10
10
  .components()
@@ -13,6 +13,15 @@ pub(super) fn is_library_source(root: &Path, path: &Path) -> bool {
13
13
  })
14
14
  }
15
15
 
16
+ pub(super) fn is_bin_source(root: &Path, path: &Path) -> bool {
17
+ path.strip_prefix(root).is_ok_and(|relative| {
18
+ relative
19
+ .components()
20
+ .next()
21
+ .is_some_and(|component| component.as_os_str() == "bin")
22
+ })
23
+ }
24
+
16
25
  pub(super) fn is_public_library_entry(root: &Path, path: &Path) -> bool {
17
26
  path.strip_prefix(root).is_ok_and(|relative| {
18
27
  let mut components = relative.components();
@@ -725,3 +725,18 @@ fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Err
725
725
  }
726
726
  fs::write(path, source)
727
727
  }
728
+
729
+ #[test]
730
+ fn package_folder_text_uses_forward_slashes() {
731
+ let package = Path::new("/r").join("apps").join("pure");
732
+ assert_eq!(
733
+ super::package_folder(Path::new("/r"), &package),
734
+ "apps/pure"
735
+ );
736
+ if std::path::MAIN_SEPARATOR == '/' {
737
+ assert_eq!(
738
+ super::package_folder(Path::new("/r"), Path::new("/r/a\\b")),
739
+ "a\\b"
740
+ );
741
+ }
742
+ }