dart-decimate 0.0.1

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 (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,236 @@
1
+ use super::{DuplicateMode, DuplicateOptions};
2
+
3
+ #[derive(Debug, Clone, PartialEq, Eq)]
4
+ pub(super) struct NormalizedLine {
5
+ pub(super) line: usize,
6
+ pub(super) column: usize,
7
+ pub(super) text: String,
8
+ pub(super) token_count: usize,
9
+ }
10
+
11
+ pub(super) fn normalized_lines(source: &str, options: &DuplicateOptions) -> Vec<NormalizedLine> {
12
+ let mut lines = Vec::new();
13
+ let mut in_block_comment = false;
14
+
15
+ for (index, line) in source.lines().enumerate() {
16
+ let (tokens, column) = tokenize_line(line, options.mode, &mut in_block_comment);
17
+ if tokens.is_empty() {
18
+ continue;
19
+ }
20
+ if options.ignore_imports && is_module_directive(&tokens) {
21
+ continue;
22
+ }
23
+ lines.push(NormalizedLine {
24
+ line: index + 1,
25
+ column,
26
+ text: tokens.join(" "),
27
+ token_count: tokens.len(),
28
+ });
29
+ }
30
+
31
+ lines
32
+ }
33
+
34
+ fn tokenize_line(
35
+ line: &str,
36
+ mode: DuplicateMode,
37
+ in_block_comment: &mut bool,
38
+ ) -> (Vec<String>, usize) {
39
+ let chars = line.char_indices().collect::<Vec<_>>();
40
+ let mut tokens = Vec::new();
41
+ let mut first_column = None;
42
+ let mut index = 0;
43
+
44
+ while let Some((column, current)) = chars.get(index).copied() {
45
+ if *in_block_comment {
46
+ if current == '*' && next_char(&chars, index) == Some('/') {
47
+ *in_block_comment = false;
48
+ index += 2;
49
+ } else {
50
+ index += 1;
51
+ }
52
+ continue;
53
+ }
54
+ if current.is_whitespace() {
55
+ index += 1;
56
+ continue;
57
+ }
58
+ if current == '/' && next_char(&chars, index) == Some('/') {
59
+ break;
60
+ }
61
+ if current == '/' && next_char(&chars, index) == Some('*') {
62
+ *in_block_comment = true;
63
+ index += 2;
64
+ continue;
65
+ }
66
+
67
+ first_column.get_or_insert(column);
68
+ if is_identifier_start(current) {
69
+ let (identifier, next_index) = read_identifier(&chars, index, line);
70
+ tokens.push(normalize_identifier(&identifier, mode));
71
+ index = next_index;
72
+ } else if current.is_ascii_digit() {
73
+ let next_index = read_number(&chars, index);
74
+ tokens.push(if mode == DuplicateMode::Semantic {
75
+ "NUM".to_owned()
76
+ } else {
77
+ line[column..byte_end(&chars, next_index, line)].to_owned()
78
+ });
79
+ index = next_index;
80
+ } else if current == '\'' || current == '"' {
81
+ let next_index = read_string(&chars, index, current);
82
+ tokens.push(
83
+ if matches!(mode, DuplicateMode::Weak | DuplicateMode::Semantic) {
84
+ "STR".to_owned()
85
+ } else {
86
+ line[column..byte_end(&chars, next_index, line)].to_owned()
87
+ },
88
+ );
89
+ index = next_index;
90
+ } else {
91
+ tokens.push(current.to_string());
92
+ index += 1;
93
+ }
94
+ }
95
+
96
+ (tokens, first_column.unwrap_or(0))
97
+ }
98
+
99
+ fn next_char(chars: &[(usize, char)], index: usize) -> Option<char> {
100
+ chars.get(index + 1).map(|(_, value)| *value)
101
+ }
102
+
103
+ fn read_identifier(chars: &[(usize, char)], start: usize, line: &str) -> (String, usize) {
104
+ let mut end = start + 1;
105
+ while chars
106
+ .get(end)
107
+ .is_some_and(|(_, value)| is_identifier_continue(*value))
108
+ {
109
+ end += 1;
110
+ }
111
+ let start_byte = chars[start].0;
112
+ let end_byte = byte_end(chars, end, line);
113
+ (line[start_byte..end_byte].to_owned(), end)
114
+ }
115
+
116
+ fn read_number(chars: &[(usize, char)], start: usize) -> usize {
117
+ let mut end = start + 1;
118
+ while chars.get(end).is_some_and(|(_, value)| {
119
+ value.is_ascii_alphanumeric() || matches!(value, '.' | '_' | '+' | '-')
120
+ }) {
121
+ end += 1;
122
+ }
123
+ end
124
+ }
125
+
126
+ fn read_string(chars: &[(usize, char)], start: usize, quote: char) -> usize {
127
+ let mut end = start + 1;
128
+ let mut escaped = false;
129
+ while let Some((_, value)) = chars.get(end) {
130
+ if escaped {
131
+ escaped = false;
132
+ } else if *value == '\\' {
133
+ escaped = true;
134
+ } else if *value == quote {
135
+ return end + 1;
136
+ }
137
+ end += 1;
138
+ }
139
+ end
140
+ }
141
+
142
+ fn byte_end(chars: &[(usize, char)], end: usize, line: &str) -> usize {
143
+ chars.get(end).map_or(line.len(), |(byte, _)| *byte)
144
+ }
145
+
146
+ fn normalize_identifier(identifier: &str, mode: DuplicateMode) -> String {
147
+ if mode == DuplicateMode::Semantic && !is_keyword(identifier) {
148
+ "ID".to_owned()
149
+ } else {
150
+ identifier.to_owned()
151
+ }
152
+ }
153
+
154
+ fn is_identifier_start(value: char) -> bool {
155
+ value == '_' || value == '$' || value.is_alphabetic()
156
+ }
157
+
158
+ fn is_identifier_continue(value: char) -> bool {
159
+ is_identifier_start(value) || value.is_ascii_digit()
160
+ }
161
+
162
+ fn is_module_directive(tokens: &[String]) -> bool {
163
+ matches!(
164
+ tokens.first().map(String::as_str),
165
+ Some("import" | "export" | "part" | "library")
166
+ )
167
+ }
168
+
169
+ fn is_keyword(identifier: &str) -> bool {
170
+ matches!(
171
+ identifier,
172
+ "abstract"
173
+ | "as"
174
+ | "assert"
175
+ | "async"
176
+ | "await"
177
+ | "base"
178
+ | "break"
179
+ | "case"
180
+ | "catch"
181
+ | "class"
182
+ | "const"
183
+ | "continue"
184
+ | "covariant"
185
+ | "default"
186
+ | "deferred"
187
+ | "do"
188
+ | "else"
189
+ | "enum"
190
+ | "export"
191
+ | "extends"
192
+ | "extension"
193
+ | "external"
194
+ | "factory"
195
+ | "false"
196
+ | "final"
197
+ | "finally"
198
+ | "for"
199
+ | "Function"
200
+ | "get"
201
+ | "hide"
202
+ | "if"
203
+ | "implements"
204
+ | "import"
205
+ | "in"
206
+ | "interface"
207
+ | "is"
208
+ | "library"
209
+ | "mixin"
210
+ | "new"
211
+ | "null"
212
+ | "on"
213
+ | "operator"
214
+ | "part"
215
+ | "required"
216
+ | "return"
217
+ | "sealed"
218
+ | "set"
219
+ | "show"
220
+ | "static"
221
+ | "super"
222
+ | "switch"
223
+ | "sync"
224
+ | "this"
225
+ | "throw"
226
+ | "true"
227
+ | "try"
228
+ | "typedef"
229
+ | "var"
230
+ | "void"
231
+ | "when"
232
+ | "while"
233
+ | "with"
234
+ | "yield"
235
+ )
236
+ }
@@ -0,0 +1,119 @@
1
+ use std::fs;
2
+
3
+ use tempfile::TempDir;
4
+
5
+ use super::*;
6
+ use crate::scan_project;
7
+
8
+ #[test]
9
+ fn detects_exact_duplicate_dart_blocks() -> Result<(), Box<dyn std::error::Error>> {
10
+ let fixture = tempfile::tempdir()?;
11
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
12
+ let source = "void shared() {\n final items = [1, 2, 3];\n final active = items.where((item) => item > 1);\n print(active.length);\n}\n";
13
+ write(&fixture, "lib/a.dart", source)?;
14
+ write(&fixture, "lib/b.dart", source)?;
15
+ let project = scan_project(fixture.path())?;
16
+
17
+ let report = detect_duplicates(&project, &options(DuplicateMode::Strict, 5, 10))?;
18
+
19
+ assert_eq!(report.clone_groups.len(), 1);
20
+ let clone = &report.clone_groups[0];
21
+ assert!(clone.fingerprint.starts_with("dup:"));
22
+ assert_eq!(clone.instances.len(), 2);
23
+ assert_eq!(clone.instances[0].path, fixture.path().join("lib/a.dart"));
24
+ assert_eq!(clone.instances[0].start_line, 1);
25
+ assert_eq!(clone.instances[1].path, fixture.path().join("lib/b.dart"));
26
+ assert_eq!(report.stats.analyzed_lines, 10);
27
+ assert_eq!(report.stats.duplicated_lines, 10);
28
+ assert_eq!(report.stats.duplication_percentage_basis_points, 10000);
29
+
30
+ Ok(())
31
+ }
32
+
33
+ #[test]
34
+ fn semantic_mode_normalizes_identifiers_and_literals() -> Result<(), Box<dyn std::error::Error>> {
35
+ let fixture = tempfile::tempdir()?;
36
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
37
+ write(
38
+ &fixture,
39
+ "lib/a.dart",
40
+ "int totalActive(List<int> values) {\n var sum = 0;\n for (final value in values) {\n if (value > 10) {\n print('مرحبا 👋');\n sum += value;\n }\n }\n return sum;\n}\n",
41
+ )?;
42
+ write(
43
+ &fixture,
44
+ "lib/b.dart",
45
+ "int countReady(List<int> scores) {\n var acc = 999;\n for (final score in scores) {\n if (score > 42) {\n print('hello 😀');\n acc += score;\n }\n }\n return acc;\n}\n",
46
+ )?;
47
+ let project = scan_project(fixture.path())?;
48
+
49
+ let report = detect_duplicates(&project, &options(DuplicateMode::Semantic, 9, 20))?;
50
+
51
+ assert_eq!(report.clone_groups.len(), 1);
52
+ assert_eq!(report.clone_groups[0].instances.len(), 2);
53
+ assert_eq!(report.clone_groups[0].line_count, 9);
54
+
55
+ Ok(())
56
+ }
57
+
58
+ #[test]
59
+ fn filters_short_blocks_and_generated_files() -> Result<(), Box<dyn std::error::Error>> {
60
+ let fixture = tempfile::tempdir()?;
61
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
62
+ write(&fixture, "lib/a.dart", "String get label => 'OK';\n")?;
63
+ write(&fixture, "lib/b.dart", "String get title => 'OK';\n")?;
64
+ write(
65
+ &fixture,
66
+ "lib/generated.g.dart",
67
+ "String get title => 'OK';\nString get subtitle => 'OK';\n",
68
+ )?;
69
+ let project = scan_project(fixture.path())?;
70
+
71
+ let report = detect_duplicates(&project, &options(DuplicateMode::Semantic, 1, 24))?;
72
+
73
+ assert!(report.clone_groups.is_empty());
74
+
75
+ Ok(())
76
+ }
77
+
78
+ #[test]
79
+ fn trace_clone_matches_fingerprint_and_source_line() -> Result<(), Box<dyn std::error::Error>> {
80
+ let fixture = tempfile::tempdir()?;
81
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
82
+ let source = "void shared() {\n final items = [1, 2, 3];\n final active = items.where((item) => item > 1);\n print(active.length);\n}\n";
83
+ write(&fixture, "lib/a.dart", source)?;
84
+ write(&fixture, "lib/b.dart", source)?;
85
+ let project = scan_project(fixture.path())?;
86
+ let report = detect_duplicates(&project, &options(DuplicateMode::Strict, 5, 10))?;
87
+ let fingerprint = report.clone_groups[0].fingerprint.clone();
88
+
89
+ let by_fingerprint = trace_clone(&project, &report, &fingerprint);
90
+ let by_line = trace_clone(&project, &report, "lib/a.dart:3");
91
+
92
+ assert!(by_fingerprint.found);
93
+ assert_eq!(by_fingerprint.clone_groups[0].fingerprint, fingerprint);
94
+ assert!(by_line.found);
95
+ assert_eq!(by_line.clone_groups[0].instances[0].path, "lib/a.dart");
96
+
97
+ Ok(())
98
+ }
99
+
100
+ fn options(mode: DuplicateMode, min_lines: usize, min_tokens: usize) -> DuplicateOptions {
101
+ DuplicateOptions {
102
+ mode,
103
+ min_tokens,
104
+ min_lines,
105
+ min_occurrences: 2,
106
+ skip_local: false,
107
+ ignore_imports: true,
108
+ top: None,
109
+ threshold: None,
110
+ }
111
+ }
112
+
113
+ fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
114
+ let path = fixture.path().join(path);
115
+ if let Some(parent) = path.parent() {
116
+ fs::create_dir_all(parent)?;
117
+ }
118
+ fs::write(path, source)
119
+ }