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.
- package/Cargo.lock +572 -0
- package/Cargo.toml +41 -0
- package/LICENSE +21 -0
- package/README.md +617 -0
- package/assets/dart-decimate-banner.png +0 -0
- package/npm/bin/dart-decimate-mcp.js +48 -0
- package/npm/bin/dart-decimate.js +48 -0
- package/npm/scripts/postinstall.js +46 -0
- package/npm/scripts/test-npx-local.js +54 -0
- package/npm/scripts/test-npx-mcp-local.js +56 -0
- package/package.json +45 -0
- package/src/baseline.rs +643 -0
- package/src/bin/dart-decimate-mcp.rs +11 -0
- package/src/boundaries/tests.rs +60 -0
- package/src/boundaries.rs +317 -0
- package/src/changed_scope.rs +117 -0
- package/src/ci_template.rs +399 -0
- package/src/cli/analyze.rs +462 -0
- package/src/cli/analyzer_options.rs +82 -0
- package/src/cli/audit_run.rs +327 -0
- package/src/cli/boundary_args.rs +44 -0
- package/src/cli/ci_template_run.rs +196 -0
- package/src/cli/command_name.rs +20 -0
- package/src/cli/common_args.rs +167 -0
- package/src/cli/config_run.rs +78 -0
- package/src/cli/coverage_run.rs +393 -0
- package/src/cli/decision_surface_run.rs +99 -0
- package/src/cli/default_command.rs +69 -0
- package/src/cli/dupes_args.rs +150 -0
- package/src/cli/entry_points.rs +121 -0
- package/src/cli/error.rs +149 -0
- package/src/cli/error_output.rs +68 -0
- package/src/cli/explain_run.rs +72 -0
- package/src/cli/fix_run.rs +162 -0
- package/src/cli/flags_args.rs +23 -0
- package/src/cli/health_args.rs +200 -0
- package/src/cli/hooks_run.rs +191 -0
- package/src/cli/impact_run.rs +95 -0
- package/src/cli/init_run.rs +49 -0
- package/src/cli/inspect_args.rs +20 -0
- package/src/cli/inspect_run.rs +66 -0
- package/src/cli/issue_filter_args.rs +240 -0
- package/src/cli/list_run.rs +311 -0
- package/src/cli/mode_args.rs +72 -0
- package/src/cli/output_format.rs +62 -0
- package/src/cli/regression_args.rs +156 -0
- package/src/cli/request_args.rs +175 -0
- package/src/cli/schema_commands.rs +72 -0
- package/src/cli/scope_args.rs +174 -0
- package/src/cli/security_args.rs +175 -0
- package/src/cli/security_gate_run.rs +66 -0
- package/src/cli/security_summary_run.rs +43 -0
- package/src/cli/summary_args.rs +12 -0
- package/src/cli/tests.rs +516 -0
- package/src/cli/trace_args.rs +67 -0
- package/src/cli/trace_run.rs +131 -0
- package/src/cli/trace_tests.rs +235 -0
- package/src/cli/unsupported_run.rs +116 -0
- package/src/cli/watch_run.rs +153 -0
- package/src/cli.rs +603 -0
- package/src/config/boundary_config.rs +235 -0
- package/src/config/cache.rs +25 -0
- package/src/config/dependencies.rs +114 -0
- package/src/config/health.rs +237 -0
- package/src/config/jsonc.rs +66 -0
- package/src/config/policy.rs +91 -0
- package/src/config/rule_aliases.rs +487 -0
- package/src/config/rules.rs +339 -0
- package/src/config.rs +635 -0
- package/src/coverage.rs +469 -0
- package/src/dart_parser.rs +673 -0
- package/src/decision_surface.rs +405 -0
- package/src/dependencies/analyze.rs +218 -0
- package/src/dependencies/codegen.rs +118 -0
- package/src/dependencies/discovery.rs +60 -0
- package/src/dependencies/overrides.rs +76 -0
- package/src/dependencies/private_src_imports.rs +32 -0
- package/src/dependencies/pubspec_document.rs +86 -0
- package/src/dependencies/pubspec_entry.rs +96 -0
- package/src/dependencies/tests.rs +544 -0
- package/src/dependencies/usage.rs +33 -0
- package/src/dependencies.rs +553 -0
- package/src/dependency_scripts.rs +91 -0
- package/src/dupes/lex.rs +236 -0
- package/src/dupes/tests.rs +119 -0
- package/src/dupes.rs +625 -0
- package/src/explain.rs +692 -0
- package/src/extract/directives.rs +252 -0
- package/src/extract/error_tests.rs +25 -0
- package/src/extract/members.rs +210 -0
- package/src/extract/primary_constructors.rs +421 -0
- package/src/extract/references.rs +151 -0
- package/src/extract/routes.rs +691 -0
- package/src/extract/signatures.rs +266 -0
- package/src/extract/string_tests.rs +18 -0
- package/src/extract/strings.rs +107 -0
- package/src/extract/tests.rs +697 -0
- package/src/extract.rs +680 -0
- package/src/feature_flags/tests.rs +76 -0
- package/src/feature_flags.rs +453 -0
- package/src/finding_identity.rs +60 -0
- package/src/fix.rs +551 -0
- package/src/generated.rs +53 -0
- package/src/graph/augment_tests.rs +212 -0
- package/src/graph/conditionals.rs +74 -0
- package/src/graph/package_config_tests.rs +100 -0
- package/src/graph/parts.rs +248 -0
- package/src/graph/pubspec_tests.rs +119 -0
- package/src/graph/tests.rs +837 -0
- package/src/graph/uri_tests.rs +102 -0
- package/src/graph.rs +644 -0
- package/src/health/coverage.rs +309 -0
- package/src/health/ownership.rs +175 -0
- package/src/health/runtime_coverage.rs +616 -0
- package/src/health/runtime_intelligence.rs +511 -0
- package/src/health/scores.rs +296 -0
- package/src/health/tests.rs +240 -0
- package/src/health/threshold_types.rs +163 -0
- package/src/health/thresholds.rs +229 -0
- package/src/health/types.rs +636 -0
- package/src/health.rs +597 -0
- package/src/hooks/agent_settings.rs +175 -0
- package/src/hooks/templates.rs +51 -0
- package/src/hooks.rs +569 -0
- package/src/impact.rs +287 -0
- package/src/init.rs +237 -0
- package/src/inspect.rs +142 -0
- package/src/intelligence/tests.rs +232 -0
- package/src/intelligence.rs +305 -0
- package/src/lib.rs +198 -0
- package/src/main.rs +5 -0
- package/src/manifest/mcp.rs +565 -0
- package/src/manifest.rs +552 -0
- package/src/mcp/cli_args/value_args.rs +175 -0
- package/src/mcp/cli_args.rs +658 -0
- package/src/mcp/cli_args_tests.rs +522 -0
- package/src/mcp/code_execute.rs +671 -0
- package/src/mcp/tools.rs +660 -0
- package/src/mcp.rs +325 -0
- package/src/output/audit_risk.rs +135 -0
- package/src/output/dependency_findings.rs +327 -0
- package/src/output/duplication_findings.rs +114 -0
- package/src/output/feature_flag_findings.rs +122 -0
- package/src/output/format.rs +31 -0
- package/src/output/graph_findings.rs +461 -0
- package/src/output/health_findings.rs +612 -0
- package/src/output/human.rs +27 -0
- package/src/output/next_steps.rs +92 -0
- package/src/output/route_findings.rs +76 -0
- package/src/output/runtime_coverage.rs +515 -0
- package/src/output/scope.rs +183 -0
- package/src/output/security_findings.rs +390 -0
- package/src/output/security_sarif.rs +150 -0
- package/src/output/suppressions.rs +323 -0
- package/src/output/symbol_findings.rs +257 -0
- package/src/output/tests.rs +114 -0
- package/src/output/types.rs +604 -0
- package/src/output/widget_findings.rs +213 -0
- package/src/output.rs +682 -0
- package/src/package_map.rs +495 -0
- package/src/policy/packs.rs +232 -0
- package/src/policy/tests.rs +134 -0
- package/src/policy.rs +640 -0
- package/src/project_list.rs +376 -0
- package/src/report_schema/inventory.rs +418 -0
- package/src/report_schema.rs +488 -0
- package/src/routes.rs +354 -0
- package/src/scan/tests.rs +250 -0
- package/src/scan.rs +275 -0
- package/src/security/detect.rs +639 -0
- package/src/security/tests.rs +178 -0
- package/src/security.rs +373 -0
- package/src/security_gate.rs +555 -0
- package/src/symbols/path_filters.rs +27 -0
- package/src/symbols/private_type_leaks.rs +125 -0
- package/src/symbols/riverpod.rs +113 -0
- package/src/symbols/tests.rs +692 -0
- package/src/symbols.rs +693 -0
- package/src/trace/tests.rs +225 -0
- package/src/trace.rs +690 -0
- package/src/unsupported.rs +47 -0
- package/src/widgets/lifecycle.rs +484 -0
- package/src/widgets/params.rs +236 -0
- package/src/widgets/tests.rs +430 -0
- package/src/widgets/top_level.rs +110 -0
- package/src/widgets/unrendered.rs +374 -0
- package/src/widgets.rs +590 -0
- package/src/workspace_scope.rs +358 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
use serde::{Deserialize, Serialize};
|
|
2
|
+
use tree_sitter::Node;
|
|
3
|
+
|
|
4
|
+
use super::{DeclarationKind, Location};
|
|
5
|
+
|
|
6
|
+
/// A type name referenced from a top-level declaration signature.
|
|
7
|
+
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
8
|
+
pub struct SignatureReference {
|
|
9
|
+
/// Top-level declaration containing the signature reference.
|
|
10
|
+
pub declaration: String,
|
|
11
|
+
/// Kind of the containing declaration.
|
|
12
|
+
pub declaration_kind: DeclarationKind,
|
|
13
|
+
/// Referenced type name.
|
|
14
|
+
pub name: String,
|
|
15
|
+
/// Location of the referenced type token.
|
|
16
|
+
pub location: Location,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
pub(super) fn extract_signature_references(
|
|
20
|
+
root: Node<'_>,
|
|
21
|
+
source: &str,
|
|
22
|
+
) -> Vec<SignatureReference> {
|
|
23
|
+
let mut references = Vec::new();
|
|
24
|
+
let mut cursor = root.walk();
|
|
25
|
+
for child in root.named_children(&mut cursor) {
|
|
26
|
+
collect_top_level_signature_references(child, source, &mut references);
|
|
27
|
+
}
|
|
28
|
+
references
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
fn collect_top_level_signature_references(
|
|
32
|
+
node: Node<'_>,
|
|
33
|
+
source: &str,
|
|
34
|
+
references: &mut Vec<SignatureReference>,
|
|
35
|
+
) {
|
|
36
|
+
let identities = declaration_identities(node, source);
|
|
37
|
+
if identities.is_empty() {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
let Some(signature) = signature_node(node) else {
|
|
41
|
+
return;
|
|
42
|
+
};
|
|
43
|
+
let mut names = Vec::new();
|
|
44
|
+
collect_type_names(signature, source, &mut names);
|
|
45
|
+
for (declaration, declaration_kind) in identities {
|
|
46
|
+
references.extend(names.iter().filter_map(|node| {
|
|
47
|
+
let name = node.utf8_text(source.as_bytes()).ok()?.to_owned();
|
|
48
|
+
if name == declaration {
|
|
49
|
+
return None;
|
|
50
|
+
}
|
|
51
|
+
Some(SignatureReference {
|
|
52
|
+
declaration: declaration.clone(),
|
|
53
|
+
declaration_kind,
|
|
54
|
+
name,
|
|
55
|
+
location: node.start_position().into(),
|
|
56
|
+
})
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
fn declaration_identities(node: Node<'_>, source: &str) -> Vec<(String, DeclarationKind)> {
|
|
62
|
+
match node.kind() {
|
|
63
|
+
"class_declaration" => optional_identity(
|
|
64
|
+
field_text(node, "name", source).or_else(|| mixin_application_class_name(node, source)),
|
|
65
|
+
DeclarationKind::Class,
|
|
66
|
+
),
|
|
67
|
+
"mixin_declaration" => {
|
|
68
|
+
optional_identity(field_text(node, "name", source), DeclarationKind::Mixin)
|
|
69
|
+
}
|
|
70
|
+
"extension_declaration" => optional_identity(
|
|
71
|
+
Some(field_text(node, "name", source).unwrap_or_else(|| "<extension>".to_owned())),
|
|
72
|
+
DeclarationKind::Extension,
|
|
73
|
+
),
|
|
74
|
+
"extension_type_declaration" => optional_identity(
|
|
75
|
+
node.child_by_field_name("name")
|
|
76
|
+
.and_then(|child| first_identifier_text(child, source))
|
|
77
|
+
.or_else(|| field_text(node, "name", source)),
|
|
78
|
+
DeclarationKind::ExtensionType,
|
|
79
|
+
),
|
|
80
|
+
"enum_declaration" => {
|
|
81
|
+
optional_identity(field_text(node, "name", source), DeclarationKind::Enum)
|
|
82
|
+
}
|
|
83
|
+
"type_alias" => {
|
|
84
|
+
optional_identity(type_alias_name(node, source), DeclarationKind::TypeAlias)
|
|
85
|
+
}
|
|
86
|
+
"top_level_variable_declaration" | "external_variable_declaration" => {
|
|
87
|
+
variable_names(node, source)
|
|
88
|
+
.into_iter()
|
|
89
|
+
.map(|name| (name, DeclarationKind::Variable))
|
|
90
|
+
.collect()
|
|
91
|
+
}
|
|
92
|
+
"function_declaration"
|
|
93
|
+
| "external_function_declaration"
|
|
94
|
+
| "getter_declaration"
|
|
95
|
+
| "external_getter_declaration"
|
|
96
|
+
| "setter_declaration"
|
|
97
|
+
| "external_setter_declaration" => optional_identity(
|
|
98
|
+
node.child_by_field_name("signature")
|
|
99
|
+
.and_then(|signature| field_text(signature, "name", source)),
|
|
100
|
+
DeclarationKind::Function,
|
|
101
|
+
),
|
|
102
|
+
_ => Vec::new(),
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
fn optional_identity(
|
|
107
|
+
name: Option<String>,
|
|
108
|
+
kind: DeclarationKind,
|
|
109
|
+
) -> Vec<(String, DeclarationKind)> {
|
|
110
|
+
name.map_or_else(Vec::new, |name| vec![(name, kind)])
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
fn signature_node(node: Node<'_>) -> Option<Node<'_>> {
|
|
114
|
+
match node.kind() {
|
|
115
|
+
"class_declaration"
|
|
116
|
+
| "mixin_declaration"
|
|
117
|
+
| "enum_declaration"
|
|
118
|
+
| "extension_declaration"
|
|
119
|
+
| "extension_type_declaration"
|
|
120
|
+
| "type_alias" => Some(node),
|
|
121
|
+
"top_level_variable_declaration" | "external_variable_declaration" => {
|
|
122
|
+
node.child_by_field_name("type")
|
|
123
|
+
}
|
|
124
|
+
"function_declaration"
|
|
125
|
+
| "external_function_declaration"
|
|
126
|
+
| "getter_declaration"
|
|
127
|
+
| "external_getter_declaration"
|
|
128
|
+
| "setter_declaration"
|
|
129
|
+
| "external_setter_declaration" => node.child_by_field_name("signature"),
|
|
130
|
+
_ => None,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fn collect_type_names<'tree>(node: Node<'tree>, source: &str, names: &mut Vec<Node<'tree>>) {
|
|
135
|
+
if matches!(
|
|
136
|
+
node.kind(),
|
|
137
|
+
"class_body" | "enum_body" | "extension_body" | "extension_type_body" | "mixin_body"
|
|
138
|
+
) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if node.kind() == "type_identifier"
|
|
142
|
+
&& node
|
|
143
|
+
.utf8_text(source.as_bytes())
|
|
144
|
+
.is_ok_and(should_keep_type_name)
|
|
145
|
+
{
|
|
146
|
+
names.push(node);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let mut cursor = node.walk();
|
|
151
|
+
for child in node.named_children(&mut cursor) {
|
|
152
|
+
collect_type_names(child, source, names);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
fn should_keep_type_name(name: &str) -> bool {
|
|
157
|
+
!matches!(
|
|
158
|
+
name,
|
|
159
|
+
"void"
|
|
160
|
+
| "dynamic"
|
|
161
|
+
| "Never"
|
|
162
|
+
| "Null"
|
|
163
|
+
| "Object"
|
|
164
|
+
| "String"
|
|
165
|
+
| "int"
|
|
166
|
+
| "double"
|
|
167
|
+
| "num"
|
|
168
|
+
| "bool"
|
|
169
|
+
| "List"
|
|
170
|
+
| "Map"
|
|
171
|
+
| "Set"
|
|
172
|
+
| "Future"
|
|
173
|
+
| "Stream"
|
|
174
|
+
| "Iterable"
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn type_alias_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
179
|
+
let mut cursor = node.walk();
|
|
180
|
+
node.named_children(&mut cursor)
|
|
181
|
+
.find(|child| matches!(child.kind(), "identifier" | "type_identifier"))
|
|
182
|
+
.and_then(|child| child.utf8_text(source.as_bytes()).ok())
|
|
183
|
+
.map(str::to_owned)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
fn variable_names(node: Node<'_>, source: &str) -> Vec<String> {
|
|
187
|
+
let mut names = Vec::new();
|
|
188
|
+
collect_named_fields(
|
|
189
|
+
node,
|
|
190
|
+
source,
|
|
191
|
+
&["static_final_declaration", "initialized_identifier"],
|
|
192
|
+
&mut names,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
if names.is_empty()
|
|
196
|
+
&& let Some(identifier_list) = find_first_named_descendant(node, "identifier_list")
|
|
197
|
+
{
|
|
198
|
+
collect_direct_identifier_children(identifier_list, source, &mut names);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
names
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
fn mixin_application_class_name(node: Node<'_>, source: &str) -> Option<String> {
|
|
205
|
+
find_first_named_descendant(node, "mixin_application_class")
|
|
206
|
+
.and_then(|child| first_identifier_text(child, source))
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
fn field_text(node: Node<'_>, field_name: &str, source: &str) -> Option<String> {
|
|
210
|
+
node.child_by_field_name(field_name)
|
|
211
|
+
.and_then(|child| child.utf8_text(source.as_bytes()).ok())
|
|
212
|
+
.map(str::to_owned)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
fn collect_named_fields(
|
|
216
|
+
node: Node<'_>,
|
|
217
|
+
source: &str,
|
|
218
|
+
owner_kinds: &[&str],
|
|
219
|
+
names: &mut Vec<String>,
|
|
220
|
+
) {
|
|
221
|
+
if owner_kinds.contains(&node.kind())
|
|
222
|
+
&& let Some(name) = field_text(node, "name", source)
|
|
223
|
+
{
|
|
224
|
+
names.push(name);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let mut cursor = node.walk();
|
|
228
|
+
for child in node.named_children(&mut cursor) {
|
|
229
|
+
collect_named_fields(child, source, owner_kinds, names);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
fn collect_direct_identifier_children(node: Node<'_>, source: &str, names: &mut Vec<String>) {
|
|
234
|
+
let mut cursor = node.walk();
|
|
235
|
+
names.extend(
|
|
236
|
+
node.named_children(&mut cursor)
|
|
237
|
+
.filter(|child| child.kind() == "identifier")
|
|
238
|
+
.filter_map(|child| child.utf8_text(source.as_bytes()).ok())
|
|
239
|
+
.map(str::to_owned),
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
fn first_identifier_text(node: Node<'_>, source: &str) -> Option<String> {
|
|
244
|
+
if matches!(node.kind(), "identifier" | "type_identifier") {
|
|
245
|
+
return node.utf8_text(source.as_bytes()).ok().map(str::to_owned);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let mut cursor = node.walk();
|
|
249
|
+
node.named_children(&mut cursor)
|
|
250
|
+
.find_map(|child| first_identifier_text(child, source))
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
fn find_first_named_descendant<'tree>(node: Node<'tree>, kind: &str) -> Option<Node<'tree>> {
|
|
254
|
+
if node.kind() == kind {
|
|
255
|
+
return Some(node);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
let mut cursor = node.walk();
|
|
259
|
+
for child in node.named_children(&mut cursor) {
|
|
260
|
+
if let Some(found) = find_first_named_descendant(child, kind) {
|
|
261
|
+
return Some(found);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
None
|
|
266
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
use super::*;
|
|
2
|
+
|
|
3
|
+
#[test]
|
|
4
|
+
fn decodes_non_raw_dart_string_escapes_in_directive_uris() -> Result<(), ExtractError> {
|
|
5
|
+
let source = "\
|
|
6
|
+
import 'src/\\u0061pi.dart';
|
|
7
|
+
export \"src/\\u{66}eature.dart\";
|
|
8
|
+
part 'src/model\\u{20}part.dart';
|
|
9
|
+
";
|
|
10
|
+
|
|
11
|
+
let extracted = extract_dart_source("lib/main.dart", source)?;
|
|
12
|
+
|
|
13
|
+
assert_eq!(extracted.imports[0].uri, "src/api.dart");
|
|
14
|
+
assert_eq!(extracted.exports[0].uri, "src/feature.dart");
|
|
15
|
+
assert_eq!(extracted.parts[0].uri, "src/model part.dart");
|
|
16
|
+
|
|
17
|
+
Ok(())
|
|
18
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
pub(super) fn unquote_dart_string(raw: &str) -> Option<String> {
|
|
2
|
+
let trimmed = raw.trim();
|
|
3
|
+
let (quoted, raw_literal) = quoted_dart_string_body(trimmed)?;
|
|
4
|
+
|
|
5
|
+
if raw_literal {
|
|
6
|
+
Some(quoted.to_owned())
|
|
7
|
+
} else {
|
|
8
|
+
decode_dart_string_body(quoted)
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
fn quoted_dart_string_body(trimmed: &str) -> Option<(&str, bool)> {
|
|
13
|
+
if let Some(raw) = trimmed
|
|
14
|
+
.strip_prefix('r')
|
|
15
|
+
.or_else(|| trimmed.strip_prefix('R'))
|
|
16
|
+
&& let Some(quoted) = quoted_dart_string_body_without_prefix(raw)
|
|
17
|
+
{
|
|
18
|
+
return Some((quoted, true));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
quoted_dart_string_body_without_prefix(trimmed).map(|quoted| (quoted, false))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fn quoted_dart_string_body_without_prefix(value: &str) -> Option<&str> {
|
|
25
|
+
value
|
|
26
|
+
.strip_prefix("'''")
|
|
27
|
+
.and_then(|inner| inner.strip_suffix("'''"))
|
|
28
|
+
.or_else(|| {
|
|
29
|
+
value
|
|
30
|
+
.strip_prefix("\"\"\"")
|
|
31
|
+
.and_then(|inner| inner.strip_suffix("\"\"\""))
|
|
32
|
+
})
|
|
33
|
+
.or_else(|| {
|
|
34
|
+
value
|
|
35
|
+
.strip_prefix('\'')
|
|
36
|
+
.and_then(|inner| inner.strip_suffix('\''))
|
|
37
|
+
})
|
|
38
|
+
.or_else(|| {
|
|
39
|
+
value
|
|
40
|
+
.strip_prefix('"')
|
|
41
|
+
.and_then(|inner| inner.strip_suffix('"'))
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
fn decode_dart_string_body(value: &str) -> Option<String> {
|
|
46
|
+
let mut decoded = String::with_capacity(value.len());
|
|
47
|
+
let mut chars = value.chars();
|
|
48
|
+
|
|
49
|
+
while let Some(ch) = chars.next() {
|
|
50
|
+
if ch != '\\' {
|
|
51
|
+
decoded.push(ch);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let escaped = chars.next()?;
|
|
56
|
+
match escaped {
|
|
57
|
+
'n' => decoded.push('\n'),
|
|
58
|
+
'r' => decoded.push('\r'),
|
|
59
|
+
'f' => decoded.push('\u{000C}'),
|
|
60
|
+
'b' => decoded.push('\u{0008}'),
|
|
61
|
+
't' => decoded.push('\t'),
|
|
62
|
+
'v' => decoded.push('\u{000B}'),
|
|
63
|
+
'x' => {
|
|
64
|
+
let byte = u8::try_from(hex_escape_value(&mut chars, 2)?).ok()?;
|
|
65
|
+
decoded.push(char::from(byte));
|
|
66
|
+
}
|
|
67
|
+
'u' => decoded.push(unicode_escape_value(&mut chars)?),
|
|
68
|
+
other => {
|
|
69
|
+
decoded.push('\\');
|
|
70
|
+
decoded.push(other);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
Some(decoded)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn unicode_escape_value(chars: &mut std::str::Chars<'_>) -> Option<char> {
|
|
79
|
+
if chars.clone().next() != Some('{') {
|
|
80
|
+
return char::from_u32(hex_escape_value(chars, 4)?);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
chars.next();
|
|
84
|
+
let mut value = String::new();
|
|
85
|
+
for ch in chars.by_ref() {
|
|
86
|
+
if ch == '}' {
|
|
87
|
+
return char::from_u32(u32::from_str_radix(&value, 16).ok()?);
|
|
88
|
+
}
|
|
89
|
+
if !ch.is_ascii_hexdigit() {
|
|
90
|
+
return None;
|
|
91
|
+
}
|
|
92
|
+
value.push(ch);
|
|
93
|
+
}
|
|
94
|
+
None
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fn hex_escape_value(chars: &mut std::str::Chars<'_>, digits: usize) -> Option<u32> {
|
|
98
|
+
let mut value = String::with_capacity(digits);
|
|
99
|
+
for _ in 0..digits {
|
|
100
|
+
let ch = chars.next()?;
|
|
101
|
+
if !ch.is_ascii_hexdigit() {
|
|
102
|
+
return None;
|
|
103
|
+
}
|
|
104
|
+
value.push(ch);
|
|
105
|
+
}
|
|
106
|
+
u32::from_str_radix(&value, 16).ok()
|
|
107
|
+
}
|