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,232 @@
1
+ use std::fs;
2
+ use std::path::Path;
3
+
4
+ use serde::Deserialize;
5
+
6
+ use crate::graph::normalize_against;
7
+
8
+ use super::{PolicyError, PolicyPack, PolicyRule, PolicyRuleKind, PolicySeverity};
9
+
10
+ /// Load a policy pack from a JSON, JSONC, or TOML file.
11
+ ///
12
+ /// # Errors
13
+ ///
14
+ /// Returns [`PolicyError`] when the file is missing, unreadable, malformed, or
15
+ /// contains a rule without an id, family, or pattern list.
16
+ pub fn load_policy_pack(
17
+ root: impl AsRef<Path>,
18
+ path: impl AsRef<Path>,
19
+ ) -> Result<PolicyPack, PolicyError> {
20
+ let root = root.as_ref();
21
+ let path = normalize_against(root, path.as_ref());
22
+ if !path.is_file() {
23
+ return Err(PolicyError::NotFound { path });
24
+ }
25
+ let source = fs::read_to_string(&path).map_err(|source| PolicyError::ReadPack {
26
+ path: path.clone(),
27
+ source,
28
+ })?;
29
+ let raw = parse_raw_policy_pack(&path, &source)?;
30
+ raw.into_pack(&path)
31
+ }
32
+
33
+ #[derive(Debug, Clone, Default, Deserialize)]
34
+ #[serde(default, deny_unknown_fields)]
35
+ struct RawPolicyPack {
36
+ name: Option<String>,
37
+ rules: Vec<RawPolicyRule>,
38
+ }
39
+
40
+ #[derive(Debug, Clone, Default, Deserialize)]
41
+ #[serde(default, deny_unknown_fields)]
42
+ struct RawPolicyRule {
43
+ id: String,
44
+ #[serde(rename = "type", alias = "kind")]
45
+ kind: String,
46
+ message: Option<String>,
47
+ severity: Option<PolicySeverity>,
48
+ pattern: Option<String>,
49
+ patterns: Vec<String>,
50
+ effect: Option<String>,
51
+ effects: Vec<String>,
52
+ }
53
+
54
+ impl RawPolicyPack {
55
+ fn into_pack(self, path: &Path) -> Result<PolicyPack, PolicyError> {
56
+ let name = self.name.unwrap_or_else(|| {
57
+ path.file_stem()
58
+ .and_then(|stem| stem.to_str())
59
+ .unwrap_or("policy")
60
+ .to_owned()
61
+ });
62
+ let mut rules = Vec::new();
63
+ for raw in self.rules {
64
+ rules.push(raw.into_rule(path)?);
65
+ }
66
+ Ok(PolicyPack {
67
+ name,
68
+ path: Some(path.to_path_buf()),
69
+ rules,
70
+ })
71
+ }
72
+ }
73
+
74
+ impl RawPolicyRule {
75
+ fn into_rule(self, path: &Path) -> Result<PolicyRule, PolicyError> {
76
+ if self.id.is_empty() {
77
+ return invalid_pack(path, "policy rule id is required");
78
+ }
79
+ let kind = match self.kind.as_str() {
80
+ "banned-import" | "banned_import" => PolicyRuleKind::BannedImport {
81
+ patterns: non_empty_patterns(path, self.pattern, self.patterns)?,
82
+ },
83
+ "banned-call" | "banned_call" => PolicyRuleKind::BannedCall {
84
+ patterns: non_empty_patterns(path, self.pattern, self.patterns)?,
85
+ },
86
+ "banned-effect" | "banned_effect" => PolicyRuleKind::BannedEffect {
87
+ effects: non_empty_patterns(path, self.effect, self.effects)?,
88
+ },
89
+ _ => {
90
+ return invalid_pack(
91
+ path,
92
+ "policy rule type must be banned-import, banned-call, or banned-effect",
93
+ );
94
+ }
95
+ };
96
+ Ok(PolicyRule {
97
+ id: self.id,
98
+ message: self.message,
99
+ severity: self.severity,
100
+ kind,
101
+ })
102
+ }
103
+ }
104
+
105
+ fn non_empty_patterns(
106
+ path: &Path,
107
+ single: Option<String>,
108
+ mut many: Vec<String>,
109
+ ) -> Result<Vec<String>, PolicyError> {
110
+ if let Some(single) = single {
111
+ many.push(single);
112
+ }
113
+ many.retain(|pattern| !pattern.trim().is_empty());
114
+ if many.is_empty() {
115
+ return invalid_pack(path, "policy rule requires pattern(s)");
116
+ }
117
+ Ok(many)
118
+ }
119
+
120
+ fn invalid_pack<T>(path: &Path, message: impl Into<String>) -> Result<T, PolicyError> {
121
+ Err(PolicyError::InvalidPack {
122
+ path: path.to_path_buf(),
123
+ message: message.into(),
124
+ })
125
+ }
126
+
127
+ fn parse_raw_policy_pack(path: &Path, source: &str) -> Result<RawPolicyPack, PolicyError> {
128
+ match policy_pack_format(path, source) {
129
+ PolicyPackFormat::Json => {
130
+ serde_json::from_str(source).map_err(|source| PolicyError::ParseJson {
131
+ path: path.to_path_buf(),
132
+ source,
133
+ })
134
+ }
135
+ PolicyPackFormat::Jsonc => {
136
+ serde_json::from_str(&strip_json_comments(source)).map_err(|source| {
137
+ PolicyError::ParseJson {
138
+ path: path.to_path_buf(),
139
+ source,
140
+ }
141
+ })
142
+ }
143
+ PolicyPackFormat::Toml => toml::from_str(source).map_err(|source| PolicyError::ParseToml {
144
+ path: path.to_path_buf(),
145
+ source,
146
+ }),
147
+ }
148
+ }
149
+
150
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
151
+ enum PolicyPackFormat {
152
+ Json,
153
+ Jsonc,
154
+ Toml,
155
+ }
156
+
157
+ fn policy_pack_format(path: &Path, source: &str) -> PolicyPackFormat {
158
+ match path.extension().and_then(|extension| extension.to_str()) {
159
+ Some("json") => PolicyPackFormat::Json,
160
+ Some("jsonc") => PolicyPackFormat::Jsonc,
161
+ Some("toml") => PolicyPackFormat::Toml,
162
+ _ if source.trim_start().starts_with('{') => PolicyPackFormat::Jsonc,
163
+ _ => PolicyPackFormat::Toml,
164
+ }
165
+ }
166
+
167
+ fn strip_json_comments(source: &str) -> String {
168
+ let mut output = String::with_capacity(source.len());
169
+ let mut chars = source.chars().peekable();
170
+ let mut in_string = false;
171
+ let mut escaped = false;
172
+
173
+ while let Some(ch) = chars.next() {
174
+ if in_string {
175
+ output.push(ch);
176
+ if escaped {
177
+ escaped = false;
178
+ } else if ch == '\\' {
179
+ escaped = true;
180
+ } else if ch == '"' {
181
+ in_string = false;
182
+ }
183
+ continue;
184
+ }
185
+
186
+ match ch {
187
+ '"' => {
188
+ in_string = true;
189
+ output.push(ch);
190
+ }
191
+ '/' if chars.peek() == Some(&'/') => {
192
+ let _ = chars.next();
193
+ skip_line_comment(&mut chars, &mut output);
194
+ }
195
+ '/' if chars.peek() == Some(&'*') => {
196
+ let _ = chars.next();
197
+ skip_block_comment(&mut chars, &mut output);
198
+ }
199
+ _ => output.push(ch),
200
+ }
201
+ }
202
+
203
+ output
204
+ }
205
+
206
+ fn skip_line_comment<I>(chars: &mut std::iter::Peekable<I>, output: &mut String)
207
+ where
208
+ I: Iterator<Item = char>,
209
+ {
210
+ for ch in chars.by_ref() {
211
+ if ch == '\n' {
212
+ output.push('\n');
213
+ break;
214
+ }
215
+ }
216
+ }
217
+
218
+ fn skip_block_comment<I>(chars: &mut std::iter::Peekable<I>, output: &mut String)
219
+ where
220
+ I: Iterator<Item = char>,
221
+ {
222
+ let mut previous = '\0';
223
+ for ch in chars.by_ref() {
224
+ if ch == '\n' {
225
+ output.push('\n');
226
+ }
227
+ if previous == '*' && ch == '/' {
228
+ break;
229
+ }
230
+ previous = ch;
231
+ }
232
+ }
@@ -0,0 +1,134 @@
1
+ use std::fs;
2
+
3
+ use tempfile::TempDir;
4
+
5
+ use super::{
6
+ BoundaryCallRule, PolicyPack, PolicyRule, PolicyRuleKind, detect_boundary_call_violations,
7
+ detect_policy_violations, load_policy_pack,
8
+ };
9
+ use crate::scan_project;
10
+
11
+ #[test]
12
+ fn detects_boundary_forbidden_calls_in_matching_zone() -> Result<(), Box<dyn std::error::Error>> {
13
+ let fixture = fixture()?;
14
+ write(
15
+ &fixture,
16
+ "lib/ui/page.dart",
17
+ "import 'package:flutter/services.dart';\nvoid render() { SystemChrome.setPreferredOrientations([]); }\n",
18
+ )?;
19
+ write(
20
+ &fixture,
21
+ "lib/domain/model.dart",
22
+ "void model() { SystemChrome.setPreferredOrientations([]); }\n",
23
+ )?;
24
+ let project = scan_project(fixture.path())?;
25
+
26
+ let violations = detect_boundary_call_violations(
27
+ &project,
28
+ &[BoundaryCallRule::new(
29
+ "lib/ui",
30
+ vec!["SystemChrome.*".to_owned()],
31
+ )],
32
+ )?;
33
+
34
+ assert_eq!(violations.len(), 1);
35
+ assert_eq!(
36
+ violations[0]
37
+ .path
38
+ .strip_prefix(fixture.path())?
39
+ .to_string_lossy(),
40
+ "lib/ui/page.dart"
41
+ );
42
+ assert_eq!(
43
+ violations[0].callee,
44
+ "SystemChrome.setPreferredOrientations"
45
+ );
46
+ assert_eq!(violations[0].pattern, "SystemChrome.*");
47
+
48
+ Ok(())
49
+ }
50
+
51
+ #[test]
52
+ fn detects_policy_banned_imports_and_calls() -> Result<(), Box<dyn std::error::Error>> {
53
+ let fixture = fixture()?;
54
+ write(
55
+ &fixture,
56
+ "lib/main.dart",
57
+ "import 'dart:io';\nvoid main() { Process.runSync('sh', []); }\n",
58
+ )?;
59
+ let project = scan_project(fixture.path())?;
60
+ let pack = PolicyPack {
61
+ name: "mobile".to_owned(),
62
+ path: None,
63
+ rules: vec![
64
+ PolicyRule {
65
+ id: "no-dart-io".to_owned(),
66
+ message: Some("Do not import dart:io from app code".to_owned()),
67
+ severity: None,
68
+ kind: PolicyRuleKind::BannedImport {
69
+ patterns: vec!["dart:io".to_owned()],
70
+ },
71
+ },
72
+ PolicyRule {
73
+ id: "no-process".to_owned(),
74
+ message: None,
75
+ severity: None,
76
+ kind: PolicyRuleKind::BannedCall {
77
+ patterns: vec!["Process.*".to_owned()],
78
+ },
79
+ },
80
+ ],
81
+ };
82
+
83
+ let violations = detect_policy_violations(&project, &[pack])?;
84
+
85
+ assert_eq!(violations.len(), 2);
86
+ assert!(violations.iter().any(|violation| {
87
+ violation.rule_id == "dart-decimate/policy/mobile/no-dart-io"
88
+ && violation.target == "dart:io"
89
+ }));
90
+ assert!(violations.iter().any(|violation| {
91
+ violation.rule_id == "dart-decimate/policy/mobile/no-process"
92
+ && violation.target == "Process.runSync"
93
+ }));
94
+
95
+ Ok(())
96
+ }
97
+
98
+ #[test]
99
+ fn loads_jsonc_policy_pack() -> Result<(), Box<dyn std::error::Error>> {
100
+ let fixture = fixture()?;
101
+ write(
102
+ &fixture,
103
+ "policy.jsonc",
104
+ r#"{
105
+ // policy owner can keep comments
106
+ "name": "mobile",
107
+ "rules": [
108
+ { "id": "no-dart-io", "type": "banned-import", "pattern": "dart:io" }
109
+ ]
110
+ }
111
+ "#,
112
+ )?;
113
+
114
+ let pack = load_policy_pack(fixture.path(), "policy.jsonc")?;
115
+
116
+ assert_eq!(pack.name, "mobile");
117
+ assert_eq!(pack.rules.len(), 1);
118
+
119
+ Ok(())
120
+ }
121
+
122
+ fn fixture() -> Result<TempDir, std::io::Error> {
123
+ let fixture = tempfile::tempdir()?;
124
+ write(&fixture, "pubspec.yaml", "name: app\n")?;
125
+ Ok(fixture)
126
+ }
127
+
128
+ fn write(fixture: &TempDir, path: &str, source: &str) -> Result<(), std::io::Error> {
129
+ let path = fixture.path().join(path);
130
+ if let Some(parent) = path.parent() {
131
+ fs::create_dir_all(parent)?;
132
+ }
133
+ fs::write(path, source)
134
+ }