dart-decimate 0.0.19 → 0.0.21

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 CHANGED
@@ -102,7 +102,7 @@ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
102
102
 
103
103
  [[package]]
104
104
  name = "dart-decimate"
105
- version = "0.0.19"
105
+ version = "0.0.21"
106
106
  dependencies = [
107
107
  "clap",
108
108
  "glob",
package/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "dart-decimate"
3
- version = "0.0.19"
3
+ version = "0.0.21"
4
4
  edition = "2024"
5
5
  rust-version = "1.85"
6
6
  description = "Rust-native codebase intelligence for Dart and Flutter module graphs."
package/README.md CHANGED
@@ -91,7 +91,7 @@ Add this to `package.json` if you want a short project command:
91
91
  "dart-decimate": "dart-decimate json ."
92
92
  },
93
93
  "devDependencies": {
94
- "dart-decimate": "^0.0.19"
94
+ "dart-decimate": "^0.0.21"
95
95
  }
96
96
  }
97
97
  ```
@@ -262,6 +262,9 @@ Clone windows must meet both line and token thresholds; sparse duplicated blocks
262
262
  can span more than `--min-lines`, and `line_count` reports the actual match.
263
263
  Declaration-only abstract contracts are filtered, and copied local Pub package
264
264
  mirrors are canonicalized before same-mirror matches are ignored.
265
+ Pair-only class clones joined by an explicit `toEntity()` or `toDomain()` mapper
266
+ are treated as intentional boundaries by default. Set
267
+ `dupes.ignore_mapper_pairs = false` to report those pairs.
265
268
 
266
269
  Useful commands:
267
270
 
@@ -611,6 +614,7 @@ targets = true
611
614
  mode = "semantic"
612
615
  min_tokens = 80
613
616
  threshold = 5
617
+ ignore_mapper_pairs = true
614
618
 
615
619
  [flags]
616
620
  allow = ["SKIP_PERMISSION_PROMPT"]
@@ -675,7 +679,7 @@ This repository forbids `unsafe_code`.
675
679
 
676
680
  ## Release Flow
677
681
 
678
- Current version: `0.0.19`.
682
+ Current version: `0.0.21`.
679
683
 
680
684
  After the first public release, changes should go through pull requests. Every
681
685
  PR to `main` must bump both `Cargo.toml` and `package.json` above the base
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dart-decimate",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "description": "Rust-native codebase intelligence for Dart and Flutter module graphs.",
5
5
  "license": "MIT",
6
6
  "bin": {
package/src/config.rs CHANGED
@@ -116,6 +116,9 @@ pub struct DuplicateConfig {
116
116
  /// Ignore import/export/part/augment directives.
117
117
  #[serde(default, alias = "ignoreImports")]
118
118
  pub ignore_imports: Option<bool>,
119
+ /// Ignore pair-only clones joined by an explicit entity/domain mapper.
120
+ #[serde(default, alias = "ignoreMapperPairs")]
121
+ pub ignore_mapper_pairs: Option<bool>,
119
122
  }
120
123
 
121
124
  /// Feature flag analyzer config defaults.
@@ -331,6 +334,9 @@ impl DuplicateConfig {
331
334
  if let Some(ignore_imports) = self.ignore_imports {
332
335
  options.ignore_imports = ignore_imports;
333
336
  }
337
+ if let Some(ignore_mapper_pairs) = self.ignore_mapper_pairs {
338
+ options.ignore_mapper_pairs = ignore_mapper_pairs;
339
+ }
334
340
  }
335
341
  }
336
342
 
@@ -581,7 +587,9 @@ fn dupes_schema() -> Value {
581
587
  "skip_local": { "type": "boolean" },
582
588
  "skipLocal": { "type": "boolean" },
583
589
  "ignore_imports": { "type": "boolean" },
584
- "ignoreImports": { "type": "boolean" }
590
+ "ignoreImports": { "type": "boolean" },
591
+ "ignore_mapper_pairs": { "type": "boolean" },
592
+ "ignoreMapperPairs": { "type": "boolean" }
585
593
  }
586
594
  })
587
595
  }
@@ -25,18 +25,20 @@ impl MappingBoundaryFilter {
25
25
  let model_path = normalize_against(&project.root, &model_file.path);
26
26
  for member in model_file.members.iter().filter(|member| {
27
27
  member.kind == MemberKind::Method
28
- && member.name == "toEntity"
29
- && member.owner.ends_with("Model")
28
+ && matches!(member.name.as_str(), "toEntity" | "toDomain")
30
29
  }) {
31
30
  let Some(model) = model_file.declarations.iter().find(|declaration| {
32
31
  declaration.kind == DeclarationKind::Class && declaration.name == member.owner
33
32
  }) else {
34
33
  continue;
35
34
  };
36
- let Some(prefix) = model.name.strip_suffix("Model") else {
35
+ let Some(entity_name) = member
36
+ .return_type
37
+ .as_deref()
38
+ .and_then(mapper_target_class_name)
39
+ else {
37
40
  continue;
38
41
  };
39
- let entity_name = format!("{prefix}Entity");
40
42
 
41
43
  for dependency in dependencies.iter().filter(|dependency| {
42
44
  dependency.kind == DependencyKind::Import && dependency.from_path == model_path
@@ -94,6 +96,32 @@ impl MappingBoundaryFilter {
94
96
  }
95
97
  }
96
98
 
99
+ fn mapper_target_class_name(return_type: &str) -> Option<&str> {
100
+ let qualified_name = return_type.strip_suffix('?').unwrap_or(return_type);
101
+ let mut segments = qualified_name.split('.');
102
+ let first = segments.next()?;
103
+ let second = segments.next();
104
+ if segments.next().is_some() {
105
+ return None;
106
+ }
107
+ let name = match second {
108
+ Some(name) if is_identifier(first) => name,
109
+ Some(_) => return None,
110
+ None => first,
111
+ };
112
+ let first = name.chars().next()?;
113
+ (first.is_ascii_uppercase() && is_identifier(name)).then_some(name)
114
+ }
115
+
116
+ fn is_identifier(name: &str) -> bool {
117
+ let mut characters = name.chars();
118
+ let Some(first) = characters.next() else {
119
+ return false;
120
+ };
121
+ (first.is_ascii_alphabetic() || first == '_')
122
+ && characters.all(|character| character.is_ascii_alphanumeric() || character == '_')
123
+ }
124
+
97
125
  fn instance_inside(instance: &CodeCloneInstance, path: &PathBuf, range: SourceRange) -> bool {
98
126
  instance.path == *path
99
127
  && instance.start_line >= range.start_line
@@ -321,6 +321,7 @@ fn options(mode: DuplicateMode, min_lines: usize, min_tokens: usize) -> Duplicat
321
321
  min_occurrences: 2,
322
322
  skip_local: false,
323
323
  ignore_imports: true,
324
+ ignore_mapper_pairs: true,
324
325
  top: None,
325
326
  threshold: None,
326
327
  }
package/src/dupes.rs CHANGED
@@ -65,6 +65,9 @@ pub struct DuplicateOptions {
65
65
  pub skip_local: bool,
66
66
  /// Ignore import/export/part/augment directives.
67
67
  pub ignore_imports: bool,
68
+ /// Ignore pair-only clones joined by an explicit entity/domain mapper.
69
+ #[serde(default = "default_ignore_mapper_pairs")]
70
+ pub ignore_mapper_pairs: bool,
68
71
  /// Limit output to the N largest clone groups.
69
72
  pub top: Option<usize>,
70
73
  /// Fail when duplicated lines exceed this percentage.
@@ -81,12 +84,17 @@ impl Default for DuplicateOptions {
81
84
  min_occurrences: 2,
82
85
  skip_local: false,
83
86
  ignore_imports: true,
87
+ ignore_mapper_pairs: true,
84
88
  top: None,
85
89
  threshold: None,
86
90
  }
87
91
  }
88
92
  }
89
93
 
94
+ const fn default_ignore_mapper_pairs() -> bool {
95
+ true
96
+ }
97
+
90
98
  /// Duplicate percentage threshold represented as percentage basis points.
91
99
  #[derive(Debug, Clone, Copy, PartialEq, Eq)]
92
100
  pub struct DuplicationThreshold(u32);
@@ -389,8 +397,10 @@ pub fn detect_duplicates(
389
397
  clone_groups.retain(|group| group_satisfies_occurrence_options(group, options));
390
398
  let mut declaration_filter = DeclarationCloneFilter::new();
391
399
  clone_groups.retain(|group| !declaration_filter.is_declaration_only_clone(group));
392
- let mapping_filter = MappingBoundaryFilter::new(project);
393
- clone_groups.retain(|group| !mapping_filter.is_mapping_boundary_clone(group));
400
+ if options.ignore_mapper_pairs {
401
+ let mapping_filter = MappingBoundaryFilter::new(project);
402
+ clone_groups.retain(|group| !mapping_filter.is_mapping_boundary_clone(group));
403
+ }
394
404
  sort_clone_groups(&mut clone_groups);
395
405
  clone_groups = collapse_overlapping_groups(clone_groups);
396
406
  clone_groups.retain(|group| !copied_packages.is_copied_package_clone(group));
@@ -2,7 +2,8 @@ use tree_sitter::Node;
2
2
 
3
3
  use super::{
4
4
  MemberDeclaration, MemberKind, collect_direct_identifier_children, collect_named_fields,
5
- field_text, find_first_named_descendant, find_first_named_descendant_in, first_identifier_text,
5
+ declared_return_type, field_text, find_first_named_descendant, find_first_named_descendant_in,
6
+ first_identifier_text,
6
7
  };
7
8
 
8
9
  pub(super) fn push_class_like_members(
@@ -80,6 +81,7 @@ fn push_bodyless_member(
80
81
  owner: owner.to_owned(),
81
82
  kind: MemberKind::Field,
82
83
  name,
84
+ return_type: None,
83
85
  location: node.start_position().into(),
84
86
  }));
85
87
  }
@@ -132,10 +134,14 @@ fn push_signature_member(
132
134
  .map(|name| member_name_without_owner(&name, owner)),
133
135
  };
134
136
  if let Some(name) = name {
137
+ let return_type = (kind == MemberKind::Method)
138
+ .then(|| declared_return_type(signature, source))
139
+ .flatten();
135
140
  members.push(MemberDeclaration {
136
141
  owner: owner.to_owned(),
137
142
  kind,
138
143
  name,
144
+ return_type,
139
145
  location: node.start_position().into(),
140
146
  });
141
147
  }
@@ -183,6 +189,7 @@ fn push_member(
183
189
  owner: owner.to_owned(),
184
190
  kind,
185
191
  name,
192
+ return_type: None,
186
193
  location: node.start_position().into(),
187
194
  });
188
195
  }
package/src/extract.rs CHANGED
@@ -27,6 +27,14 @@ pub use signatures::SignatureReference;
27
27
  use signatures::extract_signature_references;
28
28
  use strings::unquote_dart_string;
29
29
 
30
+ pub(crate) fn declared_return_type(signature: Node<'_>, source: &str) -> Option<String> {
31
+ let name = signature.child_by_field_name("name")?;
32
+ let before_name = source
33
+ .get(signature.start_byte()..name.start_byte())?
34
+ .trim();
35
+ (!before_name.is_empty()).then(|| before_name.to_owned())
36
+ }
37
+
30
38
  /// A 1-based line and 0-based byte column in a Dart source file.
31
39
  #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
32
40
  pub struct Location {
@@ -214,6 +222,9 @@ pub struct MemberDeclaration {
214
222
  pub kind: MemberKind,
215
223
  /// Declared member name.
216
224
  pub name: String,
225
+ /// Declared method return type, when the member is a method.
226
+ #[serde(default, skip_serializing_if = "Option::is_none")]
227
+ pub return_type: Option<String>,
217
228
  /// Location of the member declaration node.
218
229
  pub location: Location,
219
230
  }
@@ -101,7 +101,13 @@ fn detect_hardcoded_secrets(
101
101
  {
102
102
  continue;
103
103
  }
104
- if secret_name && literal.value.len() >= 12 || secret_value {
104
+ if ((secret_binding_name
105
+ || (secret_name
106
+ && (literal_has_concrete_token_like_segment(&literal.value)
107
+ || literal_has_embedded_url_credentials(&literal.value))))
108
+ && literal.value.len() >= 12)
109
+ || secret_value
110
+ {
105
111
  candidates.push(detected(
106
112
  path,
107
113
  source,
@@ -141,6 +141,65 @@ class Copy {
141
141
  Ok(())
142
142
  }
143
143
 
144
+ #[test]
145
+ fn skips_low_entropy_labels_with_secret_like_words() -> Result<(), Box<dyn std::error::Error>> {
146
+ let fixture = tempfile::tempdir()?;
147
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
148
+ write(
149
+ &fixture,
150
+ "lib/main.dart",
151
+ "void log(String message) => print(message);
152
+
153
+ void main() {
154
+ log('requestAuthorization result');
155
+ log('synchronizeRemoteCatalog');
156
+ }
157
+ ",
158
+ )?;
159
+
160
+ let project = scan_project(fixture.path())?;
161
+ let report = analyze_security(&project, &SecurityOptions::default(), None)?;
162
+
163
+ assert!(report.candidates.is_empty());
164
+ assert_eq!(report.total_occurrences, 0);
165
+
166
+ Ok(())
167
+ }
168
+
169
+ #[test]
170
+ fn reports_secret_bindings_and_shaped_literals() -> Result<(), Box<dyn std::error::Error>> {
171
+ let fixture = tempfile::tempdir()?;
172
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
173
+ write(
174
+ &fixture,
175
+ "lib/main.dart",
176
+ "const clientSecret = 'ordinary dictionary words';
177
+
178
+ void authenticate({required String accessToken}) {}
179
+ void log(String message) => print(message);
180
+
181
+ void main() {
182
+ authenticate(accessToken: 'another dictionary phrase');
183
+ log('ghp_0123456789abcdef0123456789abcdef');
184
+ print(clientSecret);
185
+ }
186
+ ",
187
+ )?;
188
+
189
+ let project = scan_project(fixture.path())?;
190
+ let report = analyze_security(&project, &SecurityOptions::default(), None)?;
191
+
192
+ assert_eq!(report.total_occurrences, 3);
193
+ assert_eq!(report.candidates.len(), 1);
194
+ assert_eq!(
195
+ report.candidates[0].rule_id,
196
+ "dart-decimate/security-hardcoded-secret"
197
+ );
198
+ assert_eq!(report.candidates[0].occurrences.len(), 3);
199
+
200
+ Ok(())
201
+ }
202
+
144
203
  #[test]
145
204
  fn reports_operational_copy_with_concrete_token_segments() -> Result<(), Box<dyn std::error::Error>>
146
205
  {
@@ -2,7 +2,7 @@ use std::path::Path;
2
2
 
3
3
  use tree_sitter::Node;
4
4
 
5
- use crate::WidgetTopLevelFunction;
5
+ use crate::{WidgetTopLevelFunction, extract::declared_return_type};
6
6
 
7
7
  pub(super) fn top_level_widget_functions(
8
8
  path: &Path,
@@ -41,7 +41,7 @@ fn function_candidate(path: &Path, node: Node<'_>, source: &str) -> Option<Widge
41
41
  }
42
42
 
43
43
  let signature_text = signature.utf8_text(source.as_bytes()).ok()?;
44
- let return_type = declared_return_type(signature, name_node, source);
44
+ let return_type = declared_return_type(signature, source);
45
45
  let returns_widget = return_type.as_deref().is_some_and(return_type_has_ui_type);
46
46
  let build_helper =
47
47
  is_build_helper_name(&name) && (signature_text.contains("BuildContext") || returns_widget);
@@ -57,13 +57,6 @@ fn function_candidate(path: &Path, node: Node<'_>, source: &str) -> Option<Widge
57
57
  })
58
58
  }
59
59
 
60
- fn declared_return_type(signature: Node<'_>, name: Node<'_>, source: &str) -> Option<String> {
61
- let before_name = source
62
- .get(signature.start_byte()..name.start_byte())?
63
- .trim();
64
- (!before_name.is_empty()).then(|| before_name.to_owned())
65
- }
66
-
67
60
  fn return_type_has_ui_type(return_type: &str) -> bool {
68
61
  return_type
69
62
  .split(|character: char| !(character.is_ascii_alphanumeric() || character == '_'))