dart-decimate 0.0.38 → 0.0.40

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/src/graph.rs CHANGED
@@ -394,23 +394,23 @@ fn add_file_dependencies(
394
394
  return;
395
395
  };
396
396
 
397
- if let Some(library) = &file.library {
398
- if let Some(augment_uri) = &library.augment_uri {
399
- add_dependency(
400
- root,
401
- packages,
402
- known_paths,
403
- nodes_by_path,
404
- graph,
405
- unresolved,
406
- from_index,
407
- &from_path,
408
- augment_uri,
409
- DependencyKind::Augment,
410
- library.location,
411
- DependencyVisibility::default(),
412
- );
413
- }
397
+ if let Some(library) = &file.library
398
+ && let Some(augment_uri) = &library.augment_uri
399
+ {
400
+ add_dependency(
401
+ root,
402
+ packages,
403
+ known_paths,
404
+ nodes_by_path,
405
+ graph,
406
+ unresolved,
407
+ from_index,
408
+ &from_path,
409
+ augment_uri,
410
+ DependencyKind::Augment,
411
+ library.location,
412
+ DependencyVisibility::default(),
413
+ );
414
414
  }
415
415
 
416
416
  let imports = selected_imports(&file.imports, &options.conditional_environment);
@@ -578,14 +578,13 @@ fn percent_decode_uri_path(value: &str) -> String {
578
578
  let mut index = 0;
579
579
 
580
580
  while index < bytes.len() {
581
- if bytes[index] == b'%' {
582
- if let (Some(high), Some(low)) = (bytes.get(index + 1), bytes.get(index + 2)) {
583
- if let Some(decoded_byte) = decode_hex_pair(*high, *low) {
584
- decoded.push(decoded_byte);
585
- index += 3;
586
- continue;
587
- }
588
- }
581
+ if bytes[index] == b'%'
582
+ && let (Some(high), Some(low)) = (bytes.get(index + 1), bytes.get(index + 2))
583
+ && let Some(decoded_byte) = decode_hex_pair(*high, *low)
584
+ {
585
+ decoded.push(decoded_byte);
586
+ index += 3;
587
+ continue;
589
588
  }
590
589
 
591
590
  decoded.push(bytes[index]);
@@ -535,11 +535,11 @@ fn decode_runtime_path(raw_path: &str) -> String {
535
535
  if byte == b'%' {
536
536
  let high = bytes.next();
537
537
  let low = bytes.next();
538
- if let (Some(high), Some(low)) = (high, low) {
539
- if let Some(decoded) = decode_hex_pair(high, low) {
540
- output.push(char::from(decoded));
541
- continue;
542
- }
538
+ if let (Some(high), Some(low)) = (high, low)
539
+ && let Some(decoded) = decode_hex_pair(high, low)
540
+ {
541
+ output.push(char::from(decoded));
542
+ continue;
543
543
  }
544
544
  output.push('%');
545
545
  if let Some(high) = high {
@@ -72,9 +72,10 @@ pub(super) fn next_steps(root: &Path, results: &AnalysisResults) -> Vec<NextStep
72
72
  });
73
73
  }
74
74
 
75
- if let Some(health) = &results.health {
76
- if let Some(complexity) = health.complexity.first() {
77
- steps.push(NextStep {
75
+ if let Some(health) = &results.health
76
+ && let Some(complexity) = health.complexity.first()
77
+ {
78
+ steps.push(NextStep {
78
79
  id: "complexity-breakdown".to_owned(),
79
80
  command: format!(
80
81
  "dart-decimate health --format json --complexity-breakdown --top 1 --max-cyclomatic {} --max-cognitive {}",
@@ -85,7 +86,6 @@ pub(super) fn next_steps(root: &Path, results: &AnalysisResults) -> Vec<NextStep
85
86
  display_path(root, &complexity.path)
86
87
  ),
87
88
  });
88
- }
89
89
  }
90
90
 
91
91
  if results.security.as_ref().is_some_and(|report| {
@@ -131,16 +131,16 @@ fn result_properties(
131
131
  properties.insert("safeToDelete".to_owned(), json!(finding.safe_to_delete));
132
132
  properties.insert("files".to_owned(), json!(&finding.files));
133
133
  properties.insert("actions".to_owned(), json!(&finding.actions));
134
- if let Some(fingerprint) = &finding.fingerprint {
135
- if let Some(security) = security_by_fingerprint.get(fingerprint) {
136
- properties.insert("cwe".to_owned(), security["cwe"].clone());
137
- properties.insert("effect".to_owned(), security["effect"].clone());
138
- if !security["reachability"].is_null() {
139
- properties.insert(
140
- "securityReachability".to_owned(),
141
- security["reachability"].clone(),
142
- );
143
- }
134
+ if let Some(fingerprint) = &finding.fingerprint
135
+ && let Some(security) = security_by_fingerprint.get(fingerprint)
136
+ {
137
+ properties.insert("cwe".to_owned(), security["cwe"].clone());
138
+ properties.insert("effect".to_owned(), security["effect"].clone());
139
+ if !security["reachability"].is_null() {
140
+ properties.insert(
141
+ "securityReachability".to_owned(),
142
+ security["reachability"].clone(),
143
+ );
144
144
  }
145
145
  }
146
146
  Value::Object(properties)
@@ -287,18 +287,18 @@ struct ParsedSuppression {
287
287
  fn parse_suppression(line: &str) -> Option<ParsedSuppression> {
288
288
  let comment = line.trim_start().strip_prefix("//")?.trim_start();
289
289
  for directive in ["dart-decimate-ignore-next-line"] {
290
- if let Some(rest) = comment.strip_prefix(directive) {
291
- if rest.chars().next().is_none_or(char::is_whitespace) {
292
- let (rules, reason) = split_reason(rest.trim_start());
293
- return Some(ParsedSuppression {
294
- rules: rules
295
- .split(|character: char| character == ',' || character.is_whitespace())
296
- .filter(|rule| !rule.is_empty())
297
- .map(str::to_owned)
298
- .collect(),
299
- has_reason: reason.is_some_and(|value| !value.trim().is_empty()),
300
- });
301
- }
290
+ if let Some(rest) = comment.strip_prefix(directive)
291
+ && rest.chars().next().is_none_or(char::is_whitespace)
292
+ {
293
+ let (rules, reason) = split_reason(rest.trim_start());
294
+ return Some(ParsedSuppression {
295
+ rules: rules
296
+ .split(|character: char| character == ',' || character.is_whitespace())
297
+ .filter(|rule| !rule.is_empty())
298
+ .map(str::to_owned)
299
+ .collect(),
300
+ has_reason: reason.is_some_and(|value| !value.trim().is_empty()),
301
+ });
302
302
  }
303
303
  }
304
304
  None
@@ -68,30 +68,29 @@ impl PackageMap {
68
68
  let roots = self.by_name.get(package)?;
69
69
  let owner_package = self.owner_package(from_path);
70
70
  let owner_root = owner_package.map(|package| package.root.as_path());
71
- if let Some(owner_package) = owner_package {
72
- if owner_package.name == package {
73
- return Some(package_resolution(owner_package, path));
74
- }
71
+ if let Some(owner_package) = owner_package
72
+ && owner_package.name == package
73
+ {
74
+ return Some(package_resolution(owner_package, path));
75
75
  }
76
76
  let configured_root = roots.iter().find(|root| root.from_package_config);
77
- if owner_package.is_some_and(|package| package.from_package_config) {
78
- if let Some(root) = configured_root {
79
- return Some(package_resolution(root, path));
80
- }
77
+ if owner_package.is_some_and(|package| package.from_package_config)
78
+ && let Some(root) = configured_root
79
+ {
80
+ return Some(package_resolution(root, path));
81
81
  }
82
- if let Some(owner_root) = owner_root {
83
- if let Some(root) = self
82
+ if let Some(owner_root) = owner_root
83
+ && let Some(root) = self
84
84
  .by_owner_dependency
85
85
  .get(owner_root)
86
86
  .and_then(|dependencies| dependencies.get(package))
87
- {
88
- return Some(package_resolution(root, path));
89
- }
87
+ {
88
+ return Some(package_resolution(root, path));
90
89
  }
91
- if let Some(owner_package) = owner_package {
92
- if owner_package.from_package_config || self.uses_package_config {
93
- return None;
94
- }
90
+ if let Some(owner_package) = owner_package
91
+ && (owner_package.from_package_config || self.uses_package_config)
92
+ {
93
+ return None;
95
94
  }
96
95
  if let Some(root) = configured_root {
97
96
  return Some(package_resolution(root, path));
@@ -223,10 +222,9 @@ impl PackageMap {
223
222
  self.discover_nested_pubspecs(&path, visited)?;
224
223
  } else if file_type.is_file()
225
224
  && path.file_name().is_some_and(|name| name == "pubspec.yaml")
225
+ && let Some(package_root) = path.parent()
226
226
  {
227
- if let Some(package_root) = path.parent() {
228
- self.discover_optional_pubspec(package_root, visited)?;
229
- }
227
+ self.discover_optional_pubspec(package_root, visited)?;
230
228
  }
231
229
  }
232
230
 
@@ -417,14 +417,12 @@ fn initialized_binding_name(root: Node<'_>, source: &str, index: usize) -> Optio
417
417
  ) && node
418
418
  .child_by_field_name("value")
419
419
  .is_some_and(|value| value.start_byte() <= index && value.end_byte() >= index)
420
- {
421
- if let Some(binding) = node
420
+ && let Some(binding) = node
422
421
  .child_by_field_name("name")
423
422
  .and_then(|name| name.utf8_text(source.as_bytes()).ok())
424
423
  .map(str::to_owned)
425
- {
426
- return Some(binding);
427
- }
424
+ {
425
+ return Some(binding);
428
426
  }
429
427
  let Some(parent) = node.parent() else {
430
428
  break;
@@ -1076,16 +1076,17 @@ fn string_literals(source: &str) -> Vec<StringLiteral> {
1076
1076
  let mut index = 0;
1077
1077
  let bytes = source.as_bytes();
1078
1078
  while index < bytes.len() {
1079
- if matches!(bytes[index], b'\'' | b'"') && !is_comment_match(source, index) {
1080
- if let Some((value, value_index, end)) = read_string(source, index) {
1081
- literals.push(StringLiteral {
1082
- index,
1083
- value_index,
1084
- value,
1085
- });
1086
- index = end;
1087
- continue;
1088
- }
1079
+ if matches!(bytes[index], b'\'' | b'"')
1080
+ && !is_comment_match(source, index)
1081
+ && let Some((value, value_index, end)) = read_string(source, index)
1082
+ {
1083
+ literals.push(StringLiteral {
1084
+ index,
1085
+ value_index,
1086
+ value,
1087
+ });
1088
+ index = end;
1089
+ continue;
1089
1090
  }
1090
1091
  index += 1;
1091
1092
  }
@@ -1897,13 +1898,13 @@ fn lexical_binding_value<'source>(
1897
1898
  ) -> Option<&'source str> {
1898
1899
  let mut child = root.descendant_for_byte_range(index, index.saturating_add(1))?;
1899
1900
  while let Some(parent) = child.parent() {
1900
- if parent.kind() == "class_body" {
1901
- if let Some(binding) = class_binding(parent, child, name, source) {
1902
- return match binding {
1903
- LexicalBinding::Value(value) => Some(value),
1904
- LexicalBinding::Unknown => None,
1905
- };
1906
- }
1901
+ if parent.kind() == "class_body"
1902
+ && let Some(binding) = class_binding(parent, child, name, source)
1903
+ {
1904
+ return match binding {
1905
+ LexicalBinding::Value(value) => Some(value),
1906
+ LexicalBinding::Unknown => None,
1907
+ };
1907
1908
  }
1908
1909
  if process_scope_parameters_bind_name(parent, name, source)
1909
1910
  || process_scope_header_binds_name(parent, child, name, source)
@@ -1908,6 +1908,8 @@ fn unquote_dart_string(text: &str) -> Option<String> {
1908
1908
 
1909
1909
  fn framework_import_uri(uri: &str) -> bool {
1910
1910
  uri.starts_with("package:flutter/")
1911
+ || uri.starts_with("package:material_ui/")
1912
+ || uri.starts_with("package:cupertino_ui/")
1911
1913
  || uri.starts_with("package:flutter_riverpod/")
1912
1914
  || uri.starts_with("package:hooks_riverpod/")
1913
1915
  }
@@ -1346,8 +1346,13 @@ String prefixedShadowHelper(PrefixedInheritedShadowWidget widget) {
1346
1346
  #[test]
1347
1347
  fn object_pattern_helpers_accept_prefixed_flutter_framework_superclasses()
1348
1348
  -> Result<(), Box<dyn std::error::Error>> {
1349
- let source = r"
1350
- import 'package:flutter/widgets.dart' as f;
1349
+ for package in [
1350
+ "flutter/widgets.dart",
1351
+ "material_ui/material_ui.dart",
1352
+ "cupertino_ui/cupertino_ui.dart",
1353
+ ] {
1354
+ let source = r"
1355
+ import 'package:PACKAGE' as f;
1351
1356
 
1352
1357
  class PrefixedFrameworkHelperWidget extends f.StatelessWidget {
1353
1358
  const PrefixedFrameworkHelperWidget({super.key, required this.used, required this.unused});
@@ -1360,10 +1365,16 @@ String prefixedFrameworkHelper(PrefixedFrameworkHelperWidget widget) {
1360
1365
  final PrefixedFrameworkHelperWidget(:used) = widget;
1361
1366
  return used;
1362
1367
  }
1363
- ";
1364
- let targets = unused_param_targets(parse_findings(source)?.unused_params);
1368
+ "
1369
+ .replace("PACKAGE", package);
1370
+ let targets = unused_param_targets(parse_findings(&source)?.unused_params);
1365
1371
 
1366
- assert_eq!(targets, vec!["PrefixedFrameworkHelperWidget.unused"]);
1372
+ assert_eq!(
1373
+ targets,
1374
+ vec!["PrefixedFrameworkHelperWidget.unused"],
1375
+ "{package}"
1376
+ );
1377
+ }
1367
1378
  Ok(())
1368
1379
  }
1369
1380
 
@@ -2010,6 +2021,18 @@ fn flags_top_level_helpers_in_screen_files() -> Result<(), Box<dyn std::error::E
2010
2021
  Ok(())
2011
2022
  }
2012
2023
 
2024
+ #[test]
2025
+ fn standalone_flutter_ui_packages_mark_ui_helpers() -> Result<(), Box<dyn std::error::Error>> {
2026
+ for package in ["material_ui", "cupertino_ui"] {
2027
+ let source = format!(
2028
+ "import 'package:{package}/{package}.dart';\nWidget header() => const SizedBox();\n"
2029
+ );
2030
+ let helpers = parse_findings_at("lib/helpers.dart", &source)?.top_level_functions;
2031
+ assert_eq!(helpers.len(), 1, "{package}");
2032
+ }
2033
+ Ok(())
2034
+ }
2035
+
2013
2036
  #[test]
2014
2037
  fn does_not_flag_methods_local_functions_or_namespaces() -> Result<(), Box<dyn std::error::Error>> {
2015
2038
  let source = r"
@@ -96,6 +96,8 @@ fn imports_flutter_ui(root: Node<'_>, source: &str) -> bool {
96
96
  .filter_map(|child| child.utf8_text(source.as_bytes()).ok())
97
97
  .any(|text| {
98
98
  text.contains("package:flutter/")
99
+ || text.contains("package:material_ui/")
100
+ || text.contains("package:cupertino_ui/")
99
101
  || text.contains("package:flutter_hooks/")
100
102
  || text.contains("package:flutter_riverpod/")
101
103
  || text.contains("package:hooks_riverpod/")
@@ -136,15 +136,15 @@ fn object_constructor_names(root: Node<'_>, source: &str) -> Vec<String> {
136
136
  let mut constructors = Vec::new();
137
137
  visit_named(root, &mut |node| {
138
138
  if is_object_constructor(node) && !has_ancestor_kind(node, &["annotation"]) {
139
- if let Some(constructor) = constructor_type_name(node, source) {
140
- if !constructor_name_candidates(&constructor).is_empty() {
141
- constructors.push(constructor);
142
- }
143
- }
144
- } else if is_arrow_body_constructor_identifier(node, source) {
145
- if let Ok(constructor) = node.utf8_text(source.as_bytes()) {
146
- constructors.push(constructor.to_owned());
139
+ if let Some(constructor) = constructor_type_name(node, source)
140
+ && !constructor_name_candidates(&constructor).is_empty()
141
+ {
142
+ constructors.push(constructor);
147
143
  }
144
+ } else if is_arrow_body_constructor_identifier(node, source)
145
+ && let Ok(constructor) = node.utf8_text(source.as_bytes())
146
+ {
147
+ constructors.push(constructor.to_owned());
148
148
  }
149
149
  });
150
150
  constructors