dart-decimate 0.0.40 → 0.0.41
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 +1 -1
- package/Cargo.toml +1 -1
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/dart_parser.rs +491 -45
- package/src/extract/tests.rs +71 -0
package/Cargo.lock
CHANGED
package/Cargo.toml
CHANGED
package/README.md
CHANGED
|
@@ -671,7 +671,7 @@ This repository forbids `unsafe_code`.
|
|
|
671
671
|
|
|
672
672
|
## Release Flow
|
|
673
673
|
|
|
674
|
-
Current version: `0.0.
|
|
674
|
+
Current version: `0.0.41`.
|
|
675
675
|
|
|
676
676
|
After the first public release, changes should go through pull requests. Every
|
|
677
677
|
PR to `main` must bump both `Cargo.toml` and `package.json` above the base
|
package/package.json
CHANGED
package/src/dart_parser.rs
CHANGED
|
@@ -53,8 +53,24 @@ pub(crate) fn parse_dart_source_strict<'source>(
|
|
|
53
53
|
path: &Path,
|
|
54
54
|
source: &'source str,
|
|
55
55
|
) -> Result<ParsedDart<'source>, DartParseError> {
|
|
56
|
+
if has_malformed_concise_constructor(source) {
|
|
57
|
+
return Err(DartParseError::Syntax {
|
|
58
|
+
path: path.to_path_buf(),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
56
62
|
let original = parse_raw(path, source)?;
|
|
57
63
|
if !original.root_node().has_error() {
|
|
64
|
+
let mut normalized = source.to_owned();
|
|
65
|
+
if normalize_concise_constructors(&mut normalized) {
|
|
66
|
+
let tree = parse_raw(path, &normalized)?;
|
|
67
|
+
if !tree.root_node().has_error() {
|
|
68
|
+
return Ok(ParsedDart {
|
|
69
|
+
tree,
|
|
70
|
+
source: Cow::Owned(normalized),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
58
74
|
return Ok(ParsedDart {
|
|
59
75
|
tree: original,
|
|
60
76
|
source: Cow::Borrowed(source),
|
|
@@ -86,6 +102,16 @@ pub(crate) fn parse_dart_source_lossy<'source>(
|
|
|
86
102
|
) -> Result<ParsedDart<'source>, DartParseError> {
|
|
87
103
|
let original = parse_raw(path, source)?;
|
|
88
104
|
if !original.root_node().has_error() {
|
|
105
|
+
let mut normalized = source.to_owned();
|
|
106
|
+
if normalize_concise_constructors(&mut normalized) {
|
|
107
|
+
let tree = parse_raw(path, &normalized)?;
|
|
108
|
+
if !tree.root_node().has_error() {
|
|
109
|
+
return Ok(ParsedDart {
|
|
110
|
+
tree,
|
|
111
|
+
source: Cow::Owned(normalized),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
89
115
|
return Ok(ParsedDart {
|
|
90
116
|
tree: original,
|
|
91
117
|
source: Cow::Borrowed(source),
|
|
@@ -135,9 +161,9 @@ fn normalize_concise_constructors(source: &mut String) -> bool {
|
|
|
135
161
|
let mut replacements = Vec::new();
|
|
136
162
|
let mut cursor = 0;
|
|
137
163
|
|
|
138
|
-
while let Some((keyword_start, keyword)) =
|
|
164
|
+
while let Some((keyword_start, keyword)) = find_next_class_like_header(source, cursor) {
|
|
139
165
|
let Some((class_name, body_start, body_end)) =
|
|
140
|
-
|
|
166
|
+
class_like_body(source, keyword_start + keyword.len(), keyword)
|
|
141
167
|
else {
|
|
142
168
|
cursor = keyword_start + keyword.len();
|
|
143
169
|
continue;
|
|
@@ -155,7 +181,32 @@ fn normalize_concise_constructors(source: &mut String) -> bool {
|
|
|
155
181
|
apply_text_replacements(source, replacements)
|
|
156
182
|
}
|
|
157
183
|
|
|
158
|
-
fn
|
|
184
|
+
fn has_malformed_concise_constructor(source: &str) -> bool {
|
|
185
|
+
let mut cursor = 0;
|
|
186
|
+
|
|
187
|
+
while let Some((keyword_start, keyword)) = find_next_class_like_header(source, cursor) {
|
|
188
|
+
let Some((_, body_start, body_end)) =
|
|
189
|
+
class_like_body(source, keyword_start + keyword.len(), keyword)
|
|
190
|
+
else {
|
|
191
|
+
cursor = keyword_start + keyword.len();
|
|
192
|
+
continue;
|
|
193
|
+
};
|
|
194
|
+
let mut member_cursor = body_start + 1;
|
|
195
|
+
while let Some((member_start, member_end)) =
|
|
196
|
+
next_class_like_member(source, member_cursor, body_end)
|
|
197
|
+
{
|
|
198
|
+
if malformed_concise_constructor_member(source, member_start, member_end) {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
member_cursor = member_end;
|
|
202
|
+
}
|
|
203
|
+
cursor = body_end + 1;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
false
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
fn class_like_body(
|
|
159
210
|
source: &str,
|
|
160
211
|
cursor: usize,
|
|
161
212
|
keyword: &'static str,
|
|
@@ -174,6 +225,30 @@ fn class_body(
|
|
|
174
225
|
Some((name, body_start, body_end))
|
|
175
226
|
}
|
|
176
227
|
|
|
228
|
+
fn find_next_class_like_header(source: &str, start: usize) -> Option<(usize, &'static str)> {
|
|
229
|
+
let mut cursor = start;
|
|
230
|
+
while cursor < source.len() {
|
|
231
|
+
if let Some(after) = skip_non_code(source, cursor) {
|
|
232
|
+
cursor = after;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if starts_keyword(source, cursor, "class") {
|
|
236
|
+
return Some((cursor, "class"));
|
|
237
|
+
}
|
|
238
|
+
if starts_keyword(source, cursor, "enum") {
|
|
239
|
+
return Some((cursor, "enum"));
|
|
240
|
+
}
|
|
241
|
+
if starts_keyword(source, cursor, "extension") {
|
|
242
|
+
let type_start = skip_whitespace(source, cursor + "extension".len())?;
|
|
243
|
+
if starts_keyword(source, type_start, "type") {
|
|
244
|
+
return Some((type_start, "type"));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
cursor = next_char_boundary(source, cursor)?;
|
|
248
|
+
}
|
|
249
|
+
None
|
|
250
|
+
}
|
|
251
|
+
|
|
177
252
|
fn push_concise_constructor_replacements(
|
|
178
253
|
source: &str,
|
|
179
254
|
start: usize,
|
|
@@ -181,62 +256,244 @@ fn push_concise_constructor_replacements(
|
|
|
181
256
|
class_name: &str,
|
|
182
257
|
replacements: &mut Vec<(usize, usize, String)>,
|
|
183
258
|
) {
|
|
184
|
-
let bytes = source.as_bytes();
|
|
185
259
|
let mut cursor = start;
|
|
260
|
+
while let Some((member_start, member_end)) = next_class_like_member(source, cursor, end) {
|
|
261
|
+
if let Some((replacement_start, replacement_end, replacement)) =
|
|
262
|
+
concise_constructor_replacement(source, member_start, member_end, class_name)
|
|
263
|
+
{
|
|
264
|
+
replacements.push((replacement_start, replacement_end, replacement));
|
|
265
|
+
}
|
|
266
|
+
cursor = member_end;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
fn next_class_like_member(source: &str, start: usize, end: usize) -> Option<(usize, usize)> {
|
|
271
|
+
let member_start = skip_member_trivia(source, start, end);
|
|
272
|
+
if member_start >= end {
|
|
273
|
+
return None;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
let bytes = source.as_bytes();
|
|
277
|
+
let mut cursor = member_start;
|
|
278
|
+
let mut delimiters = Vec::new();
|
|
186
279
|
while cursor < end {
|
|
187
280
|
if let Some(after) = skip_non_code(source, cursor) {
|
|
188
281
|
cursor = after;
|
|
189
282
|
continue;
|
|
190
283
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
284
|
+
match bytes[cursor] {
|
|
285
|
+
b'{' => delimiters.push(b'}'),
|
|
286
|
+
b'(' => delimiters.push(b')'),
|
|
287
|
+
b'[' => delimiters.push(b']'),
|
|
288
|
+
b'}' | b')' | b']' => {
|
|
289
|
+
if delimiters.last().copied() == Some(bytes[cursor]) {
|
|
290
|
+
delimiters.pop();
|
|
291
|
+
if delimiters.is_empty() && bytes[cursor] == b'}' {
|
|
292
|
+
return Some((member_start, cursor + 1));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
200
295
|
}
|
|
296
|
+
b';' if delimiters.is_empty() => return Some((member_start, cursor + 1)),
|
|
297
|
+
_ => {}
|
|
201
298
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
299
|
+
cursor += 1;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
Some((member_start, end))
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
fn concise_constructor_replacement(
|
|
306
|
+
source: &str,
|
|
307
|
+
start: usize,
|
|
308
|
+
end: usize,
|
|
309
|
+
class_name: &str,
|
|
310
|
+
) -> Option<(usize, usize, String)> {
|
|
311
|
+
let mut cursor = skip_member_trivia(source, start, end);
|
|
312
|
+
cursor = skip_member_annotations(source, cursor, end)?;
|
|
313
|
+
|
|
314
|
+
while let Some(after) = skip_member_modifier(source, cursor, end) {
|
|
315
|
+
cursor = after;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
let (keyword_end, factory) = if starts_keyword(source, cursor, "factory") {
|
|
319
|
+
(cursor + "factory".len(), true)
|
|
320
|
+
} else if starts_keyword(source, cursor, "new") {
|
|
321
|
+
(cursor + "new".len(), false)
|
|
322
|
+
} else {
|
|
323
|
+
return None;
|
|
324
|
+
};
|
|
325
|
+
let (suffix, after_suffix) = concise_constructor_suffix(source, keyword_end, end)?;
|
|
326
|
+
let replacement = if factory {
|
|
327
|
+
format!("factory {class_name}{suffix}")
|
|
328
|
+
} else {
|
|
329
|
+
format!("{class_name}{suffix}")
|
|
330
|
+
};
|
|
331
|
+
Some((cursor, after_suffix, replacement))
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
fn skip_member_modifier(source: &str, cursor: usize, end: usize) -> Option<usize> {
|
|
335
|
+
for modifier in ["augment", "const", "external"] {
|
|
336
|
+
if starts_keyword(source, cursor, modifier) {
|
|
337
|
+
let after = cursor + modifier.len();
|
|
338
|
+
return (after <= end).then(|| skip_member_trivia(source, after, end));
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
None
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
fn malformed_concise_constructor_member(source: &str, start: usize, end: usize) -> bool {
|
|
345
|
+
let mut cursor = skip_member_trivia(source, start, end);
|
|
346
|
+
let Some(after_annotations) = skip_member_annotations(source, cursor, end) else {
|
|
347
|
+
return false;
|
|
348
|
+
};
|
|
349
|
+
cursor = after_annotations;
|
|
350
|
+
|
|
351
|
+
while let Some(after) = skip_member_modifier(source, cursor, end) {
|
|
352
|
+
cursor = after;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
let keyword_end = if starts_keyword(source, cursor, "factory") {
|
|
356
|
+
cursor + "factory".len()
|
|
357
|
+
} else if starts_keyword(source, cursor, "new") {
|
|
358
|
+
cursor + "new".len()
|
|
359
|
+
} else {
|
|
360
|
+
return false;
|
|
361
|
+
};
|
|
362
|
+
let suffix_start = skip_member_trivia(source, keyword_end, end);
|
|
363
|
+
if suffix_start >= end || source.as_bytes().get(suffix_start) == Some(&b'(') {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
let Some(suffix_end) = identifier_end(source, suffix_start) else {
|
|
367
|
+
return false;
|
|
368
|
+
};
|
|
369
|
+
let after_suffix = skip_member_trivia(source, suffix_end, end);
|
|
370
|
+
|
|
371
|
+
after_suffix < end
|
|
372
|
+
&& source.as_bytes().get(after_suffix) == Some(&b'(')
|
|
373
|
+
&& is_reserved_dart_word(&source[suffix_start..suffix_end])
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
fn is_reserved_dart_word(word: &str) -> bool {
|
|
377
|
+
matches!(
|
|
378
|
+
word,
|
|
379
|
+
"assert"
|
|
380
|
+
| "break"
|
|
381
|
+
| "case"
|
|
382
|
+
| "catch"
|
|
383
|
+
| "class"
|
|
384
|
+
| "const"
|
|
385
|
+
| "continue"
|
|
386
|
+
| "default"
|
|
387
|
+
| "do"
|
|
388
|
+
| "else"
|
|
389
|
+
| "enum"
|
|
390
|
+
| "extends"
|
|
391
|
+
| "false"
|
|
392
|
+
| "final"
|
|
393
|
+
| "finally"
|
|
394
|
+
| "for"
|
|
395
|
+
| "if"
|
|
396
|
+
| "in"
|
|
397
|
+
| "is"
|
|
398
|
+
| "new"
|
|
399
|
+
| "null"
|
|
400
|
+
| "rethrow"
|
|
401
|
+
| "return"
|
|
402
|
+
| "super"
|
|
403
|
+
| "switch"
|
|
404
|
+
| "this"
|
|
405
|
+
| "throw"
|
|
406
|
+
| "true"
|
|
407
|
+
| "try"
|
|
408
|
+
| "var"
|
|
409
|
+
| "void"
|
|
410
|
+
| "while"
|
|
411
|
+
| "with"
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
fn skip_member_annotations(source: &str, mut cursor: usize, end: usize) -> Option<usize> {
|
|
416
|
+
loop {
|
|
417
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
418
|
+
if source.as_bytes().get(cursor).copied() != Some(b'@') {
|
|
419
|
+
return Some(cursor);
|
|
420
|
+
}
|
|
421
|
+
cursor += 1;
|
|
422
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
423
|
+
cursor = identifier_end(source, cursor)?;
|
|
424
|
+
loop {
|
|
425
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
426
|
+
if source.as_bytes().get(cursor).copied() != Some(b'.') {
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
cursor += 1;
|
|
430
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
431
|
+
cursor = identifier_end(source, cursor)?;
|
|
432
|
+
}
|
|
433
|
+
cursor = skip_member_trivia(source, cursor, end);
|
|
434
|
+
if source.as_bytes().get(cursor).copied() == Some(b'(') {
|
|
435
|
+
cursor = matching_delimiter(source, cursor, b'(', b')')? + 1;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
fn skip_member_trivia(source: &str, mut cursor: usize, end: usize) -> usize {
|
|
441
|
+
while cursor < end {
|
|
442
|
+
if source.as_bytes()[cursor].is_ascii_whitespace() {
|
|
443
|
+
cursor += 1;
|
|
207
444
|
continue;
|
|
208
445
|
}
|
|
209
|
-
if
|
|
210
|
-
|
|
446
|
+
if (source.as_bytes().get(cursor..cursor + 2) == Some(b"//")
|
|
447
|
+
|| source.as_bytes().get(cursor..cursor + 2) == Some(b"/*"))
|
|
448
|
+
&& let Some(after) = skip_non_code(source, cursor)
|
|
211
449
|
{
|
|
212
|
-
|
|
213
|
-
cursor,
|
|
214
|
-
after_suffix,
|
|
215
|
-
format!("factory {class_name}{suffix}"),
|
|
216
|
-
));
|
|
217
|
-
cursor = after_suffix;
|
|
450
|
+
cursor = after;
|
|
218
451
|
continue;
|
|
219
452
|
}
|
|
220
|
-
|
|
453
|
+
break;
|
|
221
454
|
}
|
|
455
|
+
cursor.min(end)
|
|
222
456
|
}
|
|
223
457
|
|
|
224
|
-
fn concise_constructor_suffix(source: &str, cursor: usize) -> Option<(String, usize)> {
|
|
225
|
-
let suffix_start =
|
|
458
|
+
fn concise_constructor_suffix(source: &str, cursor: usize, end: usize) -> Option<(String, usize)> {
|
|
459
|
+
let suffix_start = skip_member_trivia(source, cursor, end);
|
|
460
|
+
if suffix_start >= end {
|
|
461
|
+
return None;
|
|
462
|
+
}
|
|
226
463
|
if source.as_bytes().get(suffix_start).copied() == Some(b'(') {
|
|
227
|
-
return Some((
|
|
464
|
+
return Some((
|
|
465
|
+
preserved_line_breaks(&source[cursor..suffix_start]),
|
|
466
|
+
suffix_start,
|
|
467
|
+
));
|
|
228
468
|
}
|
|
229
469
|
let suffix_end = identifier_end(source, suffix_start)?;
|
|
230
|
-
|
|
470
|
+
if suffix_end > end {
|
|
471
|
+
return None;
|
|
472
|
+
}
|
|
473
|
+
let after_suffix = skip_member_trivia(source, suffix_end, end);
|
|
474
|
+
if after_suffix >= end {
|
|
475
|
+
return None;
|
|
476
|
+
}
|
|
231
477
|
if source.as_bytes().get(after_suffix).copied() != Some(b'(') {
|
|
232
478
|
return None;
|
|
233
479
|
}
|
|
234
480
|
Some((
|
|
235
|
-
format!(
|
|
481
|
+
format!(
|
|
482
|
+
"{}.{}{}",
|
|
483
|
+
preserved_line_breaks(&source[cursor..suffix_start]),
|
|
484
|
+
&source[suffix_start..suffix_end],
|
|
485
|
+
preserved_line_breaks(&source[suffix_end..after_suffix]),
|
|
486
|
+
),
|
|
236
487
|
after_suffix,
|
|
237
488
|
))
|
|
238
489
|
}
|
|
239
490
|
|
|
491
|
+
fn preserved_line_breaks(span: &str) -> String {
|
|
492
|
+
span.chars()
|
|
493
|
+
.filter(|character| matches!(character, '\n' | '\r'))
|
|
494
|
+
.collect()
|
|
495
|
+
}
|
|
496
|
+
|
|
240
497
|
fn normalize_primary_constructors(source: &str) -> Option<String> {
|
|
241
498
|
let mut replacements = Vec::new();
|
|
242
499
|
let mut cursor = 0;
|
|
@@ -790,7 +1047,9 @@ fn is_identifier_char(ch: char) -> bool {
|
|
|
790
1047
|
mod tests {
|
|
791
1048
|
use std::path::Path;
|
|
792
1049
|
|
|
793
|
-
use super::{
|
|
1050
|
+
use super::{
|
|
1051
|
+
DartParseError, normalize_modern_dart_compatibility, parse_dart_source_strict, parse_raw,
|
|
1052
|
+
};
|
|
794
1053
|
|
|
795
1054
|
#[test]
|
|
796
1055
|
fn strict_parse_normalizes_primary_constructor_headers() -> Result<(), DartParseError> {
|
|
@@ -847,30 +1106,97 @@ Widget build(Banner? banner, List<Widget>? extras) {
|
|
|
847
1106
|
#[test]
|
|
848
1107
|
fn strict_parse_normalizes_concise_constructor_forms() -> Result<(), DartParseError> {
|
|
849
1108
|
let source = r"
|
|
850
|
-
class
|
|
1109
|
+
class BodyDefault {
|
|
851
1110
|
new() {}
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
class BodyNamed {
|
|
1114
|
+
new named() {}
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
class ConstDefault {
|
|
1118
|
+
const new();
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
class ConstNamed {
|
|
1122
|
+
const new named();
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
class InitializerDefault {
|
|
1126
|
+
new() : this.other();
|
|
1127
|
+
InitializerDefault.other();
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
class InitializerNamed {
|
|
852
1131
|
new named() : this();
|
|
853
|
-
|
|
1132
|
+
InitializerNamed();
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
class ConstInitializerDefault {
|
|
1136
|
+
const new() : this.other();
|
|
1137
|
+
const ConstInitializerDefault.other();
|
|
854
1138
|
}
|
|
855
1139
|
|
|
856
|
-
class
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
factory redirect() = Child;
|
|
860
|
-
const factory cached() = Child.cached;
|
|
1140
|
+
class ConstInitializerNamed {
|
|
1141
|
+
const new named() : this();
|
|
1142
|
+
const ConstInitializerNamed();
|
|
861
1143
|
}
|
|
862
1144
|
|
|
863
|
-
class
|
|
864
|
-
|
|
865
|
-
|
|
1145
|
+
class FactoryBodyDefault {
|
|
1146
|
+
factory() { return FactoryBodyDefault._(); }
|
|
1147
|
+
FactoryBodyDefault._();
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
class FactoryBodyNamed {
|
|
1151
|
+
factory named() { return FactoryBodyNamed._(); }
|
|
1152
|
+
FactoryBodyNamed._();
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
class FactoryRedirectDefault {
|
|
1156
|
+
factory() = FactoryRedirectDefault._;
|
|
1157
|
+
FactoryRedirectDefault._();
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
class FactoryRedirectNamed {
|
|
1161
|
+
factory named() = FactoryRedirectNamed._;
|
|
1162
|
+
FactoryRedirectNamed._();
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
class ConstFactoryRedirectDefault {
|
|
1166
|
+
const factory() = ConstFactoryRedirectDefault._;
|
|
1167
|
+
const ConstFactoryRedirectDefault._();
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
class ConstFactoryRedirectNamed {
|
|
1171
|
+
const factory named() = ConstFactoryRedirectNamed._;
|
|
1172
|
+
const ConstFactoryRedirectNamed._();
|
|
866
1173
|
}
|
|
867
1174
|
";
|
|
868
1175
|
|
|
869
1176
|
let parsed = parse_dart_source_strict(Path::new("lib/constructors.dart"), source)?;
|
|
870
1177
|
|
|
871
1178
|
assert!(!parsed.tree().root_node().has_error());
|
|
872
|
-
|
|
873
|
-
|
|
1179
|
+
for expected in [
|
|
1180
|
+
"BodyDefault() {}",
|
|
1181
|
+
"BodyNamed.named() {}",
|
|
1182
|
+
"const ConstDefault();",
|
|
1183
|
+
"const ConstNamed.named();",
|
|
1184
|
+
"InitializerDefault() : this.other();",
|
|
1185
|
+
"InitializerNamed.named() : this();",
|
|
1186
|
+
"const ConstInitializerDefault() : this.other();",
|
|
1187
|
+
"const ConstInitializerNamed.named() : this();",
|
|
1188
|
+
"factory FactoryBodyDefault()",
|
|
1189
|
+
"factory FactoryBodyNamed.named()",
|
|
1190
|
+
"factory FactoryRedirectDefault() =",
|
|
1191
|
+
"factory FactoryRedirectNamed.named() =",
|
|
1192
|
+
"const factory ConstFactoryRedirectDefault() =",
|
|
1193
|
+
"const factory ConstFactoryRedirectNamed.named() =",
|
|
1194
|
+
] {
|
|
1195
|
+
assert!(
|
|
1196
|
+
parsed.source().contains(expected),
|
|
1197
|
+
"missing normalized constructor {expected:?}"
|
|
1198
|
+
);
|
|
1199
|
+
}
|
|
874
1200
|
|
|
875
1201
|
Ok(())
|
|
876
1202
|
}
|
|
@@ -898,6 +1224,31 @@ class Example {
|
|
|
898
1224
|
assert!(normalized.contains("Example() {}"));
|
|
899
1225
|
}
|
|
900
1226
|
|
|
1227
|
+
#[test]
|
|
1228
|
+
fn concise_constructor_normalization_accepts_intertoken_comments() {
|
|
1229
|
+
let source = r"
|
|
1230
|
+
class Commented {
|
|
1231
|
+
@deprecated
|
|
1232
|
+
const new /* before name */
|
|
1233
|
+
named // before parameters
|
|
1234
|
+
();
|
|
1235
|
+
factory /* before name */ create
|
|
1236
|
+
/* before parameters */ () => Commented.named();
|
|
1237
|
+
}
|
|
1238
|
+
";
|
|
1239
|
+
let Some(normalized) = normalize_modern_dart_compatibility(source) else {
|
|
1240
|
+
panic!("commented constructors were not normalized");
|
|
1241
|
+
};
|
|
1242
|
+
let Ok(tree) = parse_raw(Path::new("commented.dart"), &normalized) else {
|
|
1243
|
+
panic!("normalized commented constructors did not parse");
|
|
1244
|
+
};
|
|
1245
|
+
|
|
1246
|
+
assert!(!tree.root_node().has_error());
|
|
1247
|
+
assert!(normalized.contains("const Commented\n.named\n();"));
|
|
1248
|
+
assert!(normalized.contains("factory Commented.create\n()"));
|
|
1249
|
+
assert_eq!(normalized.lines().count(), source.lines().count());
|
|
1250
|
+
}
|
|
1251
|
+
|
|
901
1252
|
#[test]
|
|
902
1253
|
fn strict_parse_keeps_unrecoverable_syntax_errors() {
|
|
903
1254
|
let error = parse_dart_source_strict(Path::new("lib/bad.dart"), "class {")
|
|
@@ -909,4 +1260,99 @@ class Example {
|
|
|
909
1260
|
Some("Dart syntax errors found in lib/bad.dart")
|
|
910
1261
|
);
|
|
911
1262
|
}
|
|
1263
|
+
|
|
1264
|
+
#[test]
|
|
1265
|
+
fn strict_parse_normalizes_concise_constructors_even_when_raw_parse_is_clean()
|
|
1266
|
+
-> Result<(), DartParseError> {
|
|
1267
|
+
let source = "class Clean { new() {} }";
|
|
1268
|
+
let raw = parse_raw(Path::new("clean.dart"), source)?;
|
|
1269
|
+
assert!(!raw.root_node().has_error());
|
|
1270
|
+
|
|
1271
|
+
let strict = parse_dart_source_strict(Path::new("clean.dart"), source)?;
|
|
1272
|
+
assert!(!strict.tree().root_node().has_error());
|
|
1273
|
+
assert!(strict.source().contains("Clean()"));
|
|
1274
|
+
|
|
1275
|
+
let lossy = super::parse_dart_source_lossy(Path::new("clean.dart"), source)?;
|
|
1276
|
+
assert!(!lossy.tree().root_node().has_error());
|
|
1277
|
+
assert!(lossy.source().contains("Clean()"));
|
|
1278
|
+
Ok(())
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
#[test]
|
|
1282
|
+
fn concise_constructor_normalization_is_limited_to_top_level_members() {
|
|
1283
|
+
let source = r"
|
|
1284
|
+
class Owner {
|
|
1285
|
+
new() {}
|
|
1286
|
+
new named() {}
|
|
1287
|
+
const new cached();
|
|
1288
|
+
factory() {}
|
|
1289
|
+
factory namedFactory() {}
|
|
1290
|
+
const factory constFactory() = Target.cached;
|
|
1291
|
+
|
|
1292
|
+
final field = new Target();
|
|
1293
|
+
static final qualified = Target.new();
|
|
1294
|
+
void method() {
|
|
1295
|
+
final local = new Target();
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
mixin class MixinOwner {
|
|
1300
|
+
new() {}
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
enum Tone {
|
|
1304
|
+
quiet;
|
|
1305
|
+
new() {}
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
extension type UserId(int value) {
|
|
1309
|
+
new() : this(0);
|
|
1310
|
+
}
|
|
1311
|
+
";
|
|
1312
|
+
let Some(normalized) = normalize_modern_dart_compatibility(source) else {
|
|
1313
|
+
panic!("constructor source was not normalized");
|
|
1314
|
+
};
|
|
1315
|
+
let Ok(tree) = parse_raw(Path::new("normalized.dart"), &normalized) else {
|
|
1316
|
+
panic!("normalized source did not parse");
|
|
1317
|
+
};
|
|
1318
|
+
assert!(!tree.root_node().has_error());
|
|
1319
|
+
|
|
1320
|
+
for expected in [
|
|
1321
|
+
"Owner() {}",
|
|
1322
|
+
"Owner.named() {}",
|
|
1323
|
+
"const Owner.cached();",
|
|
1324
|
+
"factory Owner() {}",
|
|
1325
|
+
"factory Owner.namedFactory() {}",
|
|
1326
|
+
"const factory Owner.constFactory() = Target.cached;",
|
|
1327
|
+
"MixinOwner() {}",
|
|
1328
|
+
"Tone() {}",
|
|
1329
|
+
"UserId() : this(0);",
|
|
1330
|
+
] {
|
|
1331
|
+
assert!(
|
|
1332
|
+
normalized.contains(expected),
|
|
1333
|
+
"missing normalized {expected:?}"
|
|
1334
|
+
);
|
|
1335
|
+
}
|
|
1336
|
+
for untouched in [
|
|
1337
|
+
"final field = new Target();",
|
|
1338
|
+
"static final qualified = Target.new();",
|
|
1339
|
+
"final local = new Target();",
|
|
1340
|
+
] {
|
|
1341
|
+
assert!(normalized.contains(untouched), "changed {untouched:?}");
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
#[test]
|
|
1346
|
+
fn strict_parse_rejects_reserved_concise_constructor_names() {
|
|
1347
|
+
for source in [
|
|
1348
|
+
"class Invalid { new new() {} }",
|
|
1349
|
+
"class Invalid { factory class() = Invalid._; Invalid._(); }",
|
|
1350
|
+
"class Invalid { const new void(); }",
|
|
1351
|
+
] {
|
|
1352
|
+
assert!(matches!(
|
|
1353
|
+
parse_dart_source_strict(Path::new("lib/invalid.dart"), source),
|
|
1354
|
+
Err(DartParseError::Syntax { .. })
|
|
1355
|
+
));
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
912
1358
|
}
|
package/src/extract/tests.rs
CHANGED
|
@@ -524,6 +524,77 @@ extension type UserId(String value) {
|
|
|
524
524
|
Ok(())
|
|
525
525
|
}
|
|
526
526
|
|
|
527
|
+
#[test]
|
|
528
|
+
fn extracts_dart_313_abbreviated_constructors_with_exact_member_names() -> Result<(), ExtractError>
|
|
529
|
+
{
|
|
530
|
+
let source = r"
|
|
531
|
+
class Box {
|
|
532
|
+
new() {}
|
|
533
|
+
new named() {}
|
|
534
|
+
const new cached();
|
|
535
|
+
final untouched = new Other();
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
class FactoryBox {
|
|
539
|
+
factory() {}
|
|
540
|
+
factory namedFactory() {}
|
|
541
|
+
const factory constFactory() = FactoryBox.cached;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
mixin class MixinBox {
|
|
545
|
+
new() {}
|
|
546
|
+
new named() {}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
enum Tone {
|
|
550
|
+
quiet;
|
|
551
|
+
const new();
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
extension type UserId(int value) {
|
|
555
|
+
new() : this(0);
|
|
556
|
+
}
|
|
557
|
+
";
|
|
558
|
+
|
|
559
|
+
let extracted = extract_dart_source("lib/constructors.dart", source)?;
|
|
560
|
+
let members = extracted
|
|
561
|
+
.members
|
|
562
|
+
.iter()
|
|
563
|
+
.map(|member| (member.owner.as_str(), member.kind, member.name.as_str()))
|
|
564
|
+
.collect::<Vec<_>>();
|
|
565
|
+
|
|
566
|
+
for expected in [
|
|
567
|
+
("Box", MemberKind::Constructor, "Box"),
|
|
568
|
+
("Box", MemberKind::Constructor, "named"),
|
|
569
|
+
("Box", MemberKind::Constructor, "cached"),
|
|
570
|
+
("Box", MemberKind::Field, "untouched"),
|
|
571
|
+
("FactoryBox", MemberKind::Constructor, "FactoryBox"),
|
|
572
|
+
("FactoryBox", MemberKind::Constructor, "namedFactory"),
|
|
573
|
+
("FactoryBox", MemberKind::Constructor, "constFactory"),
|
|
574
|
+
("MixinBox", MemberKind::Constructor, "MixinBox"),
|
|
575
|
+
("MixinBox", MemberKind::Constructor, "named"),
|
|
576
|
+
("Tone", MemberKind::Constructor, "Tone"),
|
|
577
|
+
("UserId", MemberKind::Constructor, "UserId"),
|
|
578
|
+
] {
|
|
579
|
+
assert!(members.contains(&expected), "missing member {expected:?}");
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
assert_eq!(
|
|
583
|
+
members
|
|
584
|
+
.iter()
|
|
585
|
+
.filter(|(owner, kind, _)| {
|
|
586
|
+
*kind == MemberKind::Constructor
|
|
587
|
+
&& matches!(
|
|
588
|
+
*owner,
|
|
589
|
+
"Box" | "FactoryBox" | "MixinBox" | "Tone" | "UserId"
|
|
590
|
+
)
|
|
591
|
+
})
|
|
592
|
+
.count(),
|
|
593
|
+
10
|
|
594
|
+
);
|
|
595
|
+
Ok(())
|
|
596
|
+
}
|
|
597
|
+
|
|
527
598
|
#[test]
|
|
528
599
|
fn extracts_identifier_references_without_directive_metadata_or_declaration_names()
|
|
529
600
|
-> Result<(), ExtractError> {
|